Skip to content

Commit 3940af7

Browse files
authored
Merge pull request #12932 from owncloud/chore/backport-oc-select-fix
chore: backport oc-select fix
2 parents 1c7c1a4 + 8a50759 commit 3940af7

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

packages/design-system/src/components/OcSelect/OcSelect.spec.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ describe('OcSelect', () => {
5858
options[0].customLabel
5959
)
6060
})
61+
it('displays with a custom label function', () => {
62+
const options = [{ customLabel: 'label1' }, { customLabel: 'label2' }]
63+
const wrapper = getWrapper<(typeof options)[0]>({
64+
options,
65+
modelValue: options[0],
66+
getOptionLabel: (o) => o.customLabel
67+
})
68+
expect(wrapper.findAll(selectors.selectedOptions).at(0).text()).toEqual(
69+
options[0].customLabel
70+
)
71+
})
6172
it('can be cleared if multi-select is allowed', () => {
6273
const options = [{ label: 'label1' }, { label: 'label2' }]
6374
const wrapper = getWrapper({ options, modelValue: options[0], multiple: true })
@@ -87,9 +98,13 @@ describe('OcSelect', () => {
8798
})
8899
})
89100

90-
function getWrapper(
101+
function getWrapper<T>(
91102
props: Partial<
92-
PartialComponentProps<typeof OcSelect> & { options: unknown[]; modelValue: unknown }
103+
Omit<PartialComponentProps<typeof OcSelect>, 'getOptionLabel'> & {
104+
options: T[]
105+
getOptionLabel: (o: T) => string
106+
modelValue: T
107+
}
93108
> = {}
94109
) {
95110
return mount(OcSelect, {

packages/design-system/src/components/OcSelect/OcSelect.vue

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ interface Props {
166166
labelHidden?: boolean
167167
contextualHelper?: ContextualHelper | null
168168
optionLabel?: string
169+
getOptionLabel?: (option: unknown) => string
169170
searchable?: boolean
170171
clearable?: boolean
171172
loading?: boolean
@@ -218,6 +219,7 @@ const {
218219
labelHidden = false,
219220
contextualHelper = null,
220221
optionLabel = 'label',
222+
getOptionLabel: getOptionLabelProp = null,
221223
searchable = true,
222224
clearable = false,
223225
loading = false,
@@ -234,21 +236,7 @@ const emit = defineEmits<Emits>()
234236
const { $gettext } = useGettext()
235237
const select: VNodeRef = ref()
236238
const attrs = useAttrs()
237-
const getOptionLabel = (option: string | Record<string, unknown>): string => {
238-
if (typeof option === 'object') {
239-
const key = optionLabel || label
240-
if (!Object.hasOwn(option, key)) {
241-
console.warn(
242-
`[vue-select warn]: Label key "option.${key}" does not` +
243-
` exist in options object ${JSON.stringify(option)}.\n` +
244-
'https://vue-select.org/api/html#getoptionlabel'
245-
)
246-
return ''
247-
}
248-
return option[key] as string
249-
}
250-
return option
251-
}
239+
252240
const setComboBoxAriaLabel = () => {
253241
const comboBoxElement = (unref(select) as ComponentPublicInstance).$el.querySelector(
254242
'div:first-child'
@@ -368,10 +356,32 @@ const setDropdownPosition = () => {
368356
const dropdownOpen = computed(() => {
369357
return unref(select)?.dropdownOpen
370358
})
359+
360+
const getOptionLabel = computed(() => {
361+
return (
362+
getOptionLabelProp ||
363+
((option: string | Record<string, unknown>): string => {
364+
if (typeof option === 'object') {
365+
const key = optionLabel || label
366+
if (!Object.hasOwn(option, key)) {
367+
console.warn(
368+
`[vue-select warn]: Label key "option.${key}" does not` +
369+
` exist in options object ${JSON.stringify(option)}.\n` +
370+
'https://vue-select.org/api/html#getoptionlabel'
371+
)
372+
return ''
373+
}
374+
return option[key] as string
375+
}
376+
return option
377+
})
378+
)
379+
})
380+
371381
const additionalAttributes = computed(() => {
372382
const additionalAttrs: Record<string, unknown> = {}
373383
additionalAttrs['input-id'] = id
374-
additionalAttrs['getOptionLabel'] = getOptionLabel
384+
additionalAttrs['getOptionLabel'] = unref(getOptionLabel)
375385
additionalAttrs['label'] = optionLabel
376386
377387
return { ...attrs, ...additionalAttrs }

0 commit comments

Comments
 (0)