feat(InputMenu/SelectMenu): add support for dot notation in by prop (#2607)

This commit is contained in:
kyyy
2024-11-13 18:25:31 +07:00
committed by GitHub
parent 0d1a76e3c6
commit 53df9d9a8c
2 changed files with 48 additions and 14 deletions

View File

@@ -293,6 +293,24 @@ export default defineComponent({
const size = computed(() => sizeButtonGroup.value ?? sizeFormGroup.value) const size = computed(() => sizeButtonGroup.value ?? sizeFormGroup.value)
const by = computed(() => {
if (!props.by) return undefined
if (typeof props.by === 'function') {
return props.by
}
const key = props.by
const hasDot = key.indexOf('.')
if (hasDot > 0) {
return (a: any, z: any) => {
return accessor(a, key) === accessor(z, key)
}
}
return key
})
const internalQuery = ref('') const internalQuery = ref('')
const query = computed({ const query = computed({
get() { get() {
@@ -305,9 +323,7 @@ export default defineComponent({
}) })
const label = computed(() => { const label = computed(() => {
if (!props.modelValue) { if (!props.modelValue) return null
return
}
function getValue(value: any) { function getValue(value: any) {
if (props.valueAttribute) { if (props.valueAttribute) {
@@ -318,7 +334,7 @@ export default defineComponent({
} }
function compareValues(value1: any, value2: any) { function compareValues(value1: any, value2: any) {
if (props.by && typeof value1 === 'object' && typeof value2 === 'object') { if (by.value && typeof by.value !== 'function' && typeof value1 === 'object' && typeof value2 === 'object') {
return isEqual(value1[props.by], value2[props.by]) return isEqual(value1[props.by], value2[props.by])
} }
return isEqual(value1, value2) return isEqual(value1, value2)
@@ -507,7 +523,9 @@ export default defineComponent({
query, query,
accessor, accessor,
onUpdate, onUpdate,
onQueryChange onQueryChange,
// eslint-disable-next-line vue/no-dupe-keys
by
} }
} }
}) })

View File

@@ -348,6 +348,24 @@ export default defineComponent({
const [trigger, container] = usePopper(popper.value) const [trigger, container] = usePopper(popper.value)
const by = computed(() => {
if (!props.by) return undefined
if (typeof props.by === 'function') {
return props.by
}
const key = props.by
const hasDot = key.indexOf('.')
if (hasDot > 0) {
return (a: any, z: any) => {
return accessor(a, key) === accessor(z, key)
}
}
return key
})
const { size: sizeButtonGroup, rounded } = useInjectButtonGroup({ ui, props }) const { size: sizeButtonGroup, rounded } = useInjectButtonGroup({ ui, props })
const { emitFormBlur, emitFormChange, inputId, color, size: sizeFormGroup, name } = useFormGroup(props, config) const { emitFormBlur, emitFormChange, inputId, color, size: sizeFormGroup, name } = useFormGroup(props, config)
@@ -366,8 +384,8 @@ export default defineComponent({
const selected = computed(() => { const selected = computed(() => {
function compareValues(value1: any, value2: any) { function compareValues(value1: any, value2: any) {
if (props.by && typeof value1 === 'object' && typeof value2 === 'object') { if (by.value && typeof by.value !== 'function' && typeof value1 === 'object' && typeof value2 === 'object') {
return isEqual(value1[props.by], value2[props.by]) return isEqual(value1[by.value], value2[by.value])
} }
return isEqual(value1, value2) return isEqual(value1, value2)
} }
@@ -399,16 +417,12 @@ export default defineComponent({
}) })
const label = computed(() => { const label = computed(() => {
if (!selected.value) return null if (!props.modelValue) return null
if (props.valueAttribute) {
return accessor(selected.value as Record<string, any>, props.optionAttribute)
}
if (Array.isArray(props.modelValue) && props.modelValue.length) { if (Array.isArray(props.modelValue) && props.modelValue.length) {
return `${props.modelValue.length} selected` return `${props.modelValue.length} selected`
} else if (['string', 'number'].includes(typeof props.modelValue)) { } else if (['string', 'number'].includes(typeof props.modelValue)) {
return props.modelValue return props.valueAttribute ? accessor(selected.value, props.optionAttribute) : props.modelValue
} }
return accessor(props.modelValue as Record<string, any>, props.optionAttribute) return accessor(props.modelValue as Record<string, any>, props.optionAttribute)
@@ -612,7 +626,9 @@ export default defineComponent({
// eslint-disable-next-line vue/no-dupe-keys // eslint-disable-next-line vue/no-dupe-keys
query, query,
onUpdate, onUpdate,
onQueryChange onQueryChange,
// eslint-disable-next-line vue/no-dupe-keys
by
} }
} }
}) })