mirror of
https://github.com/element-plus/element-plus.git
synced 2026-03-13 07:51:17 +08:00
fix(utils): undefined when omiting prop default (#5868)
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
/* eslint-disable @typescript-eslint/ban-types */
|
||||
|
||||
import { defineComponent } from 'vue'
|
||||
import { mount } from '@vue/test-utils'
|
||||
import { expectTypeOf } from 'expect-type'
|
||||
import { buildProp, definePropType, mutable, keyOf, buildProps } from '..'
|
||||
import type { propKey } from '../vue/prop'
|
||||
@@ -444,3 +446,38 @@ describe('buildProps', () => {
|
||||
}>()
|
||||
})
|
||||
})
|
||||
|
||||
describe('runtime', () => {
|
||||
it('default value', () => {
|
||||
const warnHandler = jest.fn()
|
||||
|
||||
const Foo = defineComponent({
|
||||
props: buildProps({
|
||||
bar: { type: Boolean },
|
||||
baz: { values: ['a', 'b', 'c'] },
|
||||
qux: { values: ['a', 'b', 'c'], required: true },
|
||||
qux2: { values: ['a', 'b', 'c'], required: true },
|
||||
} as const),
|
||||
template: `{{ $props }}`,
|
||||
})
|
||||
const props = mount(Foo as any, {
|
||||
props: {
|
||||
baz: undefined,
|
||||
qux2: undefined,
|
||||
},
|
||||
global: {
|
||||
config: {
|
||||
warnHandler,
|
||||
},
|
||||
},
|
||||
}).props()
|
||||
|
||||
expect(props.bar).toBe(false)
|
||||
expect(props.baz).toBe(undefined)
|
||||
|
||||
expect(warnHandler.mock.calls[0][0]).toBe('Missing required prop: "qux"')
|
||||
expect(warnHandler.mock.calls[1][0]).toBe(
|
||||
'Invalid prop: validation failed for prop "qux2". Expected one of ["a", "b", "c"], got value undefined.'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { warn } from 'vue'
|
||||
import { fromPairs } from 'lodash-unified'
|
||||
import { isObject } from '../types'
|
||||
import { hasOwn } from '../objects'
|
||||
import type { ExtractPropTypes, PropType } from 'vue'
|
||||
|
||||
const wrapperKey = Symbol()
|
||||
@@ -117,7 +118,10 @@ export function buildProp<
|
||||
let allowedValues: unknown[] = []
|
||||
|
||||
if (values) {
|
||||
allowedValues = [...values, defaultValue]
|
||||
allowedValues = Array.from(values)
|
||||
if (hasOwn(option, 'default')) {
|
||||
allowedValues.push(defaultValue)
|
||||
}
|
||||
valid ||= allowedValues.includes(val)
|
||||
}
|
||||
if (validator) valid ||= validator(val)
|
||||
@@ -138,17 +142,18 @@ export function buildProp<
|
||||
}
|
||||
: undefined
|
||||
|
||||
return {
|
||||
const prop: any = {
|
||||
type:
|
||||
typeof type === 'object' &&
|
||||
Object.getOwnPropertySymbols(type).includes(wrapperKey)
|
||||
isObject(type) && Object.getOwnPropertySymbols(type).includes(wrapperKey)
|
||||
? type[wrapperKey]
|
||||
: type,
|
||||
required: !!required,
|
||||
default: defaultValue,
|
||||
validator: _validator,
|
||||
[propKey]: true,
|
||||
} as unknown as BuildPropReturn<T, D, R, V, C>
|
||||
}
|
||||
if (hasOwn(option, 'default')) prop.default = defaultValue
|
||||
|
||||
return prop as BuildPropReturn<T, D, R, V, C>
|
||||
}
|
||||
|
||||
type NativePropType = [
|
||||
|
||||
Reference in New Issue
Block a user