Skip to content

Commit 53df9d9

Browse files
authoredNov 13, 2024··
feat(InputMenu/SelectMenu): add support for dot notation in by prop (#2607)
1 parent 0d1a76e commit 53df9d9

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed
 

‎src/runtime/components/forms/InputMenu.vue

+23-5
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,24 @@ export default defineComponent({
293293
294294
const size = computed(() => sizeButtonGroup.value ?? sizeFormGroup.value)
295295
296+
const by = computed(() => {
297+
if (!props.by) return undefined
298+
299+
if (typeof props.by === 'function') {
300+
return props.by
301+
}
302+
303+
const key = props.by
304+
const hasDot = key.indexOf('.')
305+
if (hasDot > 0) {
306+
return (a: any, z: any) => {
307+
return accessor(a, key) === accessor(z, key)
308+
}
309+
}
310+
311+
return key
312+
})
313+
296314
const internalQuery = ref('')
297315
const query = computed({
298316
get() {
@@ -305,9 +323,7 @@ export default defineComponent({
305323
})
306324
307325
const label = computed(() => {
308-
if (!props.modelValue) {
309-
return
310-
}
326+
if (!props.modelValue) return null
311327
312328
function getValue(value: any) {
313329
if (props.valueAttribute) {
@@ -318,7 +334,7 @@ export default defineComponent({
318334
}
319335
320336
function compareValues(value1: any, value2: any) {
321-
if (props.by && typeof value1 === 'object' && typeof value2 === 'object') {
337+
if (by.value && typeof by.value !== 'function' && typeof value1 === 'object' && typeof value2 === 'object') {
322338
return isEqual(value1[props.by], value2[props.by])
323339
}
324340
return isEqual(value1, value2)
@@ -507,7 +523,9 @@ export default defineComponent({
507523
query,
508524
accessor,
509525
onUpdate,
510-
onQueryChange
526+
onQueryChange,
527+
// eslint-disable-next-line vue/no-dupe-keys
528+
by
511529
}
512530
}
513531
})

‎src/runtime/components/forms/SelectMenu.vue

+25-9
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,24 @@ export default defineComponent({
348348
349349
const [trigger, container] = usePopper(popper.value)
350350
351+
const by = computed(() => {
352+
if (!props.by) return undefined
353+
354+
if (typeof props.by === 'function') {
355+
return props.by
356+
}
357+
358+
const key = props.by
359+
const hasDot = key.indexOf('.')
360+
if (hasDot > 0) {
361+
return (a: any, z: any) => {
362+
return accessor(a, key) === accessor(z, key)
363+
}
364+
}
365+
366+
return key
367+
})
368+
351369
const { size: sizeButtonGroup, rounded } = useInjectButtonGroup({ ui, props })
352370
const { emitFormBlur, emitFormChange, inputId, color, size: sizeFormGroup, name } = useFormGroup(props, config)
353371
@@ -366,8 +384,8 @@ export default defineComponent({
366384
367385
const selected = computed(() => {
368386
function compareValues(value1: any, value2: any) {
369-
if (props.by && typeof value1 === 'object' && typeof value2 === 'object') {
370-
return isEqual(value1[props.by], value2[props.by])
387+
if (by.value && typeof by.value !== 'function' && typeof value1 === 'object' && typeof value2 === 'object') {
388+
return isEqual(value1[by.value], value2[by.value])
371389
}
372390
return isEqual(value1, value2)
373391
}
@@ -399,16 +417,12 @@ export default defineComponent({
399417
})
400418
401419
const label = computed(() => {
402-
if (!selected.value) return null
403-
404-
if (props.valueAttribute) {
405-
return accessor(selected.value as Record<string, any>, props.optionAttribute)
406-
}
420+
if (!props.modelValue) return null
407421
408422
if (Array.isArray(props.modelValue) && props.modelValue.length) {
409423
return `${props.modelValue.length} selected`
410424
} else if (['string', 'number'].includes(typeof props.modelValue)) {
411-
return props.modelValue
425+
return props.valueAttribute ? accessor(selected.value, props.optionAttribute) : props.modelValue
412426
}
413427
414428
return accessor(props.modelValue as Record<string, any>, props.optionAttribute)
@@ -612,7 +626,9 @@ export default defineComponent({
612626
// eslint-disable-next-line vue/no-dupe-keys
613627
query,
614628
onUpdate,
615-
onQueryChange
629+
onQueryChange,
630+
// eslint-disable-next-line vue/no-dupe-keys
631+
by
616632
}
617633
}
618634
})

0 commit comments

Comments
 (0)
Please sign in to comment.