diff --git a/packages/form/src/form-item.vue b/packages/form/src/form-item.vue index c07df6e2e..2a2e7a5a7 100644 --- a/packages/form/src/form-item.vue +++ b/packages/form/src/form-item.vue @@ -149,10 +149,6 @@ if (!model || !this.prop) { return; } let path = this.prop; - if (path.indexOf(':') !== -1) { - path = path.replace(/:/, '.'); - } - return getPropByPath(model, path, true).v; }, isRequired() { @@ -234,10 +230,6 @@ let model = this.form.model; let value = this.fieldValue; let path = this.prop; - if (path.indexOf(':') !== -1) { - path = path.replace(/:/, '.'); - } - let prop = getPropByPath(model, path, true); this.validateDisabled = true; diff --git a/src/utils/util.js b/src/utils/util.js index eab95814a..583abe004 100644 --- a/src/utils/util.js +++ b/src/utils/util.js @@ -46,10 +46,15 @@ export const getValueByPath = function(object, prop) { export function getPropByPath(obj, path, strict) { let tempObj = obj; - path = path.replace(/\[(\w+)\]/g, '.$1'); - path = path.replace(/^\./, ''); + let matcher = /(?:\[['"])(.+)(?=['"]\])|(\w+)(?!['"]\])/g; + let match; + let keyArr = []; + + do { + match = matcher.exec(path); + if (match) { keyArr.push(match[1] || match[0]); } + } while (match); - let keyArr = path.split('.'); let i = 0; for (let len = keyArr.length; i < len - 1; ++i) { if (!tempObj && !strict) break; diff --git a/test/unit/specs/util.spec.js b/test/unit/specs/util.spec.js new file mode 100644 index 000000000..5329a1add --- /dev/null +++ b/test/unit/specs/util.spec.js @@ -0,0 +1,28 @@ +import { getPropByPath } from 'element-ui/src/utils/util'; + +describe('utils.getPropByPath', () => { + it('getPropByPath', () => { + + const target = { + mountains: { + 'Mt. Fiji': 1, + 'Mountain: Mt. Blanca': 2, + 'mt fiji': 3, + 'fiji': 4, + 'A Very \'Strange\' ["key"]': 5 + } + }; + + expect(getPropByPath(target, 'mountains[\'Mt. Fiji\']').v).to.equal(1); + expect(getPropByPath(target, 'mountains[\'Mountain: Mt. Blanca\']').v).to.equal(2); + expect(getPropByPath(target, 'mountains[\'mt fiji\']').v).to.equal(3); + expect(getPropByPath(target, 'mountains["Mt. Fiji"]').v).to.equal(1); + expect(getPropByPath(target, 'mountains["Mountain: Mt. Blanca"]').v).to.equal(2); + expect(getPropByPath(target, 'mountains["mt fiji"]').v).to.equal(3); + expect(getPropByPath(target, 'mountains[fiji]').v).to.equal(4); + expect(getPropByPath(target, 'mountains.fiji').v).to.equal(4); + expect(getPropByPath(target, 'mountains["A Very \'Strange\' ["key"]"]').v).to.equal(5); + + }); + +});