Skip to content

Commit

Permalink
fix(comp:select): value key of select is 'value' by default (#892)
Browse files Browse the repository at this point in the history
  • Loading branch information
threedayAAAAA authored May 9, 2022
1 parent 84e8e17 commit 582cd4a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
13 changes: 13 additions & 0 deletions packages/components/select/__tests__/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ describe('Select', () => {
expect(optionsComps.length).toBe(3)
})

test('config labelKey as "value"', async () => {
const dataSource = [
{ key: 1, value: 'Tom' },
{ key: 2, value: 'Jerry' },
]

const wrapper = SelectMount({
props: { value: 2, open: true, dataSource, getKey: 'key', labelKey: 'value' },
})

expect(wrapper.find('.ix-selector-item').text()).toBe('Jerry')
})

test('clearable work', async () => {
const onUpdateValue = vi.fn()
const wrapper = SelectMount({ props: { clearable: true, 'onUpdate:value': onUpdateValue } })
Expand Down
8 changes: 6 additions & 2 deletions packages/components/select/src/composables/useGetOptionKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ export function useGetOptionKey(props: SelectProps, config: SelectConfig): Compu
if (isString(getKey)) {
return (record: unknown) => {
// value 是为了兼容之前的版本
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const key = (record as any)['value'] ?? (record as any)[getKey]
/* eslint-disable @typescript-eslint/no-explicit-any */
let key = (record as any)[getKey]
if (props.labelKey !== 'value' && (record as any)['value']) {
key = (record as any)['value']
}
/* eslint-enable @typescript-eslint/no-explicit-any */
if (__DEV__ && key === undefined) {
Logger.warn('components/select', 'Each record in dataSource should have a unique `key` prop.')
}
Expand Down

0 comments on commit 582cd4a

Please sign in to comment.