mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-27 10:20:42 +01:00
docs(ComponentCode): handle model prop for cases like v-model:page
This commit is contained in:
@@ -16,6 +16,8 @@ const props = defineProps<{
|
|||||||
hide?: string[]
|
hide?: string[]
|
||||||
/** List of props to externalize in script setup */
|
/** List of props to externalize in script setup */
|
||||||
external?: string[]
|
external?: string[]
|
||||||
|
/** List of props to use with `v-model` */
|
||||||
|
model?: string[]
|
||||||
/** List of items for each prop */
|
/** List of items for each prop */
|
||||||
items?: { [key: string]: string[] }
|
items?: { [key: string]: string[] }
|
||||||
props?: { [key: string]: any }
|
props?: { [key: string]: any }
|
||||||
@@ -39,6 +41,10 @@ const camelName = camelCase(props.slug ?? route.params.slug?.[route.params.slug.
|
|||||||
const name = `U${upperFirst(camelName)}`
|
const name = `U${upperFirst(camelName)}`
|
||||||
|
|
||||||
const componentProps = reactive({ ...(props.props || {}) })
|
const componentProps = reactive({ ...(props.props || {}) })
|
||||||
|
const componentEvents = reactive({
|
||||||
|
...Object.fromEntries((props.model || []).map(key => [`onUpdate:${key}`, (e: any) => setComponentProp(key, e)])),
|
||||||
|
...(componentProps.modelValue ? { [`onUpdate:modelValue`]: (e: any) => setComponentProp('modelValue', e) } : {})
|
||||||
|
})
|
||||||
|
|
||||||
function getComponentProp(name: string) {
|
function getComponentProp(name: string) {
|
||||||
return get(componentProps, name) || undefined
|
return get(componentProps, name) || undefined
|
||||||
@@ -122,6 +128,11 @@ const code = computed(() => {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (props.model?.includes(key)) {
|
||||||
|
code += ` v-model:${key}="${key}"`
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if (value === undefined || value === null || value === '' || props.hide?.includes(key)) {
|
if (value === undefined || value === null || value === '' || props.hide?.includes(key)) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -253,7 +264,7 @@ const { data: ast } = await useAsyncData(`component-code-${name}-${hash({ props:
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="flex justify-center border border-b-0 border-gray-200 dark:border-gray-700 relative p-4 z-[1]" :class="[!options.length && 'rounded-t-md', props.class]">
|
<div class="flex justify-center border border-b-0 border-gray-200 dark:border-gray-700 relative p-4 z-[1]" :class="[!options.length && 'rounded-t-md', props.class]">
|
||||||
<component :is="name" v-bind="componentProps" @update:model-value="!!componentProps.modelValue && setComponentProp('modelValue', $event)">
|
<component :is="name" v-bind="{ ...componentProps, ...componentEvents }">
|
||||||
<template v-for="slot in Object.keys(slots || {})" :key="slot" #[slot]>
|
<template v-for="slot in Object.keys(slots || {})" :key="slot" #[slot]>
|
||||||
<ContentSlot :name="slot" unwrap="p">
|
<ContentSlot :name="slot" unwrap="p">
|
||||||
{{ slots?.[slot] }}
|
{{ slots?.[slot] }}
|
||||||
|
|||||||
Reference in New Issue
Block a user