Skip to content

Commit

Permalink
feat(InputMenu/SelectMenu): add support for dot notation in by pr…
Browse files Browse the repository at this point in the history
…op (#2607)
  • Loading branch information
rdjanuar authored Nov 13, 2024
1 parent 0d1a76e commit 53df9d9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
28 changes: 23 additions & 5 deletions src/runtime/components/forms/InputMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,24 @@ export default defineComponent({
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 query = computed({
get() {
Expand All @@ -305,9 +323,7 @@ export default defineComponent({
})
const label = computed(() => {
if (!props.modelValue) {
return
}
if (!props.modelValue) return null
function getValue(value: any) {
if (props.valueAttribute) {
Expand All @@ -318,7 +334,7 @@ export default defineComponent({
}
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, value2)
Expand Down Expand Up @@ -507,7 +523,9 @@ export default defineComponent({
query,
accessor,
onUpdate,
onQueryChange
onQueryChange,
// eslint-disable-next-line vue/no-dupe-keys
by
}
}
})
Expand Down
34 changes: 25 additions & 9 deletions src/runtime/components/forms/SelectMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,24 @@ export default defineComponent({
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 { emitFormBlur, emitFormChange, inputId, color, size: sizeFormGroup, name } = useFormGroup(props, config)
Expand All @@ -366,8 +384,8 @@ export default defineComponent({
const selected = computed(() => {
function compareValues(value1: any, value2: any) {
if (props.by && typeof value1 === 'object' && typeof value2 === 'object') {
return isEqual(value1[props.by], value2[props.by])
if (by.value && typeof by.value !== 'function' && typeof value1 === 'object' && typeof value2 === 'object') {
return isEqual(value1[by.value], value2[by.value])
}
return isEqual(value1, value2)
}
Expand Down Expand Up @@ -399,16 +417,12 @@ export default defineComponent({
})
const label = computed(() => {
if (!selected.value) return null
if (props.valueAttribute) {
return accessor(selected.value as Record<string, any>, props.optionAttribute)
}
if (!props.modelValue) return null
if (Array.isArray(props.modelValue) && props.modelValue.length) {
return `${props.modelValue.length} selected`
} 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)
Expand Down Expand Up @@ -612,7 +626,9 @@ export default defineComponent({
// eslint-disable-next-line vue/no-dupe-keys
query,
onUpdate,
onQueryChange
onQueryChange,
// eslint-disable-next-line vue/no-dupe-keys
by
}
}
})
Expand Down

0 comments on commit 53df9d9

Please sign in to comment.