@@ -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>()
234236const { $gettext } = useGettext ()
235237const select: VNodeRef = ref ()
236238const 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+
252240const setComboBoxAriaLabel = () => {
253241 const comboBoxElement = (unref (select ) as ComponentPublicInstance ).$el .querySelector (
254242 ' div:first-child'
@@ -368,10 +356,32 @@ const setDropdownPosition = () => {
368356const 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+
371381const 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