Skip to content

Commit

Permalink
fix: respect type order when boolean casting multi-typed props
Browse files Browse the repository at this point in the history
fix #7485
  • Loading branch information
yyx990803 committed Mar 9, 2018
1 parent a2cd412 commit 81e1e47
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/core/util/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ export function validateProp (
const prop = propOptions[key]
const absent = !hasOwn(propsData, key)
let value = propsData[key]
// handle boolean props
if (isType(Boolean, prop.type)) {
// boolean casting
const booleanIndex = getTypeIndex(Boolean, prop.type)
if (booleanIndex > -1) {
if (absent && !hasOwn(prop, 'default')) {
value = false
} else if (!isType(String, prop.type) && (value === '' || value === hyphenate(key))) {
value = true
} else if (value === '' || value === hyphenate(key)) {
// only cast empty string / same name to boolean if
// boolean has higher priority
const stringIndex = getTypeIndex(String, prop.type)
if (stringIndex < 0 || booleanIndex < stringIndex) {
value = true
}
}
}
// check default value
Expand Down Expand Up @@ -179,15 +185,18 @@ function getType (fn) {
return match ? match[1] : ''
}

function isType (type, fn) {
if (!Array.isArray(fn)) {
return getType(fn) === getType(type)
function isSameType (a, b) {
return getType(a) === getType(b)
}

function getTypeIndex (type, expectedTypes): number {
if (!Array.isArray(expectedTypes)) {
return isSameType(expectedTypes, type) ? 0 : -1
}
for (let i = 0, len = fn.length; i < len; i++) {
if (getType(fn[i]) === getType(type)) {
return true
for (let i = 0, len = expectedTypes.length; i < len; i++) {
if (isSameType(expectedTypes[i], type)) {
return i
}
}
/* istanbul ignore next */
return false
return -1
}
17 changes: 17 additions & 0 deletions test/unit/features/options/props.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,21 @@ describe('Options props', () => {
expect(`"${attr}" is a reserved attribute`).toHaveBeenWarned()
})
})

it('should consider order when casting [Boolean, String] multi-type props', () => {
const vm = new Vue({
template: '<test ref="test" booleanOrString stringOrBoolean />',
components: {
test: {
template: '<div></div>',
props: {
booleanOrString: [Boolean, String],
stringOrBoolean: [String, Boolean]
}
}
}
}).$mount()
expect(vm.$refs.test.$props.booleanOrString).toBe(true)
expect(vm.$refs.test.$props.stringOrBoolean).toBe('')
})
})

0 comments on commit 81e1e47

Please sign in to comment.