mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-29 11:20:36 +01:00
feat(RadioGroup): handle value-key prop
This commit is contained in:
@@ -24,6 +24,11 @@ export interface RadioGroupProps<T> extends Pick<RadioGroupRootProps, 'defaultVa
|
|||||||
*/
|
*/
|
||||||
as?: any
|
as?: any
|
||||||
legend?: string
|
legend?: string
|
||||||
|
/**
|
||||||
|
* When `items` is an array of objects, select the field to use as the value.
|
||||||
|
* @defaultValue 'value'
|
||||||
|
*/
|
||||||
|
valueKey?: string
|
||||||
items?: T[]
|
items?: T[]
|
||||||
size?: RadioGroupVariants['size']
|
size?: RadioGroupVariants['size']
|
||||||
color?: RadioGroupVariants['color']
|
color?: RadioGroupVariants['color']
|
||||||
@@ -57,6 +62,7 @@ import { useId } from '#imports'
|
|||||||
import { useFormField } from '../composables/useFormField'
|
import { useFormField } from '../composables/useFormField'
|
||||||
|
|
||||||
const props = withDefaults(defineProps<RadioGroupProps<T>>(), {
|
const props = withDefaults(defineProps<RadioGroupProps<T>>(), {
|
||||||
|
valueKey: 'value' as any,
|
||||||
orientation: 'vertical'
|
orientation: 'vertical'
|
||||||
})
|
})
|
||||||
const emits = defineEmits<RadioGroupEmits>()
|
const emits = defineEmits<RadioGroupEmits>()
|
||||||
@@ -84,14 +90,20 @@ function normalizeItem(item: any) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const value = item[props.valueKey]
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...item,
|
...item,
|
||||||
id: `${id}:${item.value}`
|
value,
|
||||||
|
id: `${id}:${value}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const normalizedItems = computed(() => {
|
const normalizedItems = computed(() => {
|
||||||
if (!props.items) return []
|
if (!props.items) {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
|
||||||
return props.items.map(normalizeItem)
|
return props.items.map(normalizeItem)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user