mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-02-02 13:17:57 +01:00
docs: improve select options from types (#758)
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
This commit is contained in:
committed by
Benjamin Canac
parent
49a753f80f
commit
0c807db005
@@ -56,7 +56,6 @@ import { transformContent } from '@nuxt/content/transformers'
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import { useShikiHighlighter } from '@nuxtjs/mdc/runtime'
|
import { useShikiHighlighter } from '@nuxtjs/mdc/runtime'
|
||||||
import { upperFirst, camelCase, kebabCase } from 'scule'
|
import { upperFirst, camelCase, kebabCase } from 'scule'
|
||||||
import * as config from '#ui/ui.config'
|
|
||||||
|
|
||||||
// eslint-disable-next-line vue/no-dupe-keys
|
// eslint-disable-next-line vue/no-dupe-keys
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
@@ -92,8 +91,8 @@ const props = defineProps({
|
|||||||
type: Array,
|
type: Array,
|
||||||
default: () => []
|
default: () => []
|
||||||
},
|
},
|
||||||
extraColors: {
|
options: {
|
||||||
type: Array,
|
type: Array as PropType<{ name: string; values: string[]; restriction: 'expected' | 'included' | 'excluded' | 'only' }[]>,
|
||||||
default: () => []
|
default: () => []
|
||||||
},
|
},
|
||||||
backgroundClass: {
|
backgroundClass: {
|
||||||
@@ -125,8 +124,6 @@ const meta = await fetchComponentMeta(name)
|
|||||||
|
|
||||||
// Computed
|
// Computed
|
||||||
|
|
||||||
const ui = computed(() => ({ ...config[camelName], ...props.ui }))
|
|
||||||
|
|
||||||
const fullProps = computed(() => ({ ...baseProps, ...componentProps }))
|
const fullProps = computed(() => ({ ...baseProps, ...componentProps }))
|
||||||
const vModel = computed({
|
const vModel = computed({
|
||||||
get: () => baseProps.modelValue,
|
get: () => baseProps.modelValue,
|
||||||
@@ -135,19 +132,47 @@ const vModel = computed({
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const generateOptions = (key: string, schema: { kind: string, schema: [], type: string }) => {
|
||||||
|
let options = []
|
||||||
|
const optionItem = props?.options?.find(item => item?.name === key) || null
|
||||||
|
const types = schema?.type?.split('|')?.map(item => item.trim()?.replaceAll('"', '')) || []
|
||||||
|
const hasIgnoredTypes = types?.every(item => ['string', 'number', 'boolean', 'array', 'object', 'Function'].includes(item))
|
||||||
|
|
||||||
|
if (key.toLowerCase().endsWith('color')) {
|
||||||
|
options = [...appConfig.ui.colors]
|
||||||
|
}
|
||||||
|
|
||||||
|
if (schema?.schema?.length > 0 && schema?.kind === 'enum' && !hasIgnoredTypes && optionItem?.restriction !== 'only') {
|
||||||
|
options = schema.schema.filter(option => typeof option === 'string').map((option: string) => option.replaceAll('"', ''))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (optionItem?.restriction === 'only') {
|
||||||
|
options = optionItem.values
|
||||||
|
}
|
||||||
|
|
||||||
|
if (optionItem?.restriction === 'expected') {
|
||||||
|
options = options.filter(item => optionItem.values.includes(item))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (optionItem?.restriction === 'included') {
|
||||||
|
options = [...options, ...optionItem.values]
|
||||||
|
}
|
||||||
|
|
||||||
|
if (optionItem?.restriction === 'excluded') {
|
||||||
|
options = options.filter(item => !optionItem.values.includes(item))
|
||||||
|
}
|
||||||
|
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|
||||||
const propsToSelect = computed(() => Object.keys(componentProps).map((key) => {
|
const propsToSelect = computed(() => Object.keys(componentProps).map((key) => {
|
||||||
if (props.excludedProps.includes(key)) {
|
if (props.excludedProps.includes(key)) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
const prop = meta?.meta?.props?.find((prop: any) => prop.name === key)
|
const prop = meta?.meta?.props?.find((prop: any) => prop.name === key)
|
||||||
const dottedKey = kebabCase(key).replaceAll('-', '.')
|
const schema = prop?.schema || {}
|
||||||
const keys = ui.value[dottedKey] ?? {}
|
const options = generateOptions(key, schema)
|
||||||
let options = typeof keys === 'object' && Object.keys(keys)
|
|
||||||
if (key.toLowerCase().endsWith('color')) {
|
|
||||||
// @ts-ignore
|
|
||||||
options = [...appConfig.ui.colors, ...props.extraColors]
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type: prop?.type || 'string',
|
type: prop?.type || 'string',
|
||||||
|
|||||||
@@ -65,20 +65,24 @@ props:
|
|||||||
color: 'primary'
|
color: 'primary'
|
||||||
variant: 'soft'
|
variant: 'soft'
|
||||||
size: 'sm'
|
size: 'sm'
|
||||||
ui:
|
options:
|
||||||
variant:
|
- name: variant
|
||||||
solid: 1
|
restriction: included
|
||||||
outline: 1
|
values:
|
||||||
ghost: 1
|
- solid
|
||||||
soft: 1
|
- outline
|
||||||
link: 1
|
- ghost
|
||||||
size:
|
- soft
|
||||||
2xs: ''
|
- link
|
||||||
xs: ''
|
- name: size
|
||||||
sm: ''
|
restriction: included
|
||||||
md: ''
|
values:
|
||||||
lg: ''
|
- 2xs
|
||||||
xl: ''
|
- xs
|
||||||
|
- sm
|
||||||
|
- md
|
||||||
|
- lg
|
||||||
|
- xl
|
||||||
---
|
---
|
||||||
::
|
::
|
||||||
|
|
||||||
|
|||||||
@@ -79,8 +79,11 @@ props:
|
|||||||
icon: 'i-heroicons-command-line'
|
icon: 'i-heroicons-command-line'
|
||||||
color: 'primary'
|
color: 'primary'
|
||||||
variant: 'solid'
|
variant: 'solid'
|
||||||
extraColors:
|
options:
|
||||||
- white
|
- name: color
|
||||||
|
restriction: included
|
||||||
|
values:
|
||||||
|
- white
|
||||||
excludedProps:
|
excludedProps:
|
||||||
- icon
|
- icon
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -41,8 +41,11 @@ props:
|
|||||||
chipText: ''
|
chipText: ''
|
||||||
chipPosition: 'top-right'
|
chipPosition: 'top-right'
|
||||||
size : 'sm'
|
size : 'sm'
|
||||||
extraColors:
|
options:
|
||||||
- gray
|
- name: 'chipColor'
|
||||||
|
restriction: 'included'
|
||||||
|
values:
|
||||||
|
- 'gray'
|
||||||
baseProps:
|
baseProps:
|
||||||
src: 'https://avatars.githubusercontent.com/u/739984?v=4'
|
src: 'https://avatars.githubusercontent.com/u/739984?v=4'
|
||||||
alt: 'Avatar'
|
alt: 'Avatar'
|
||||||
@@ -92,17 +95,6 @@ To stack avatars as a group, use the `AvatarGroup` component.
|
|||||||
props:
|
props:
|
||||||
size: 'sm'
|
size: 'sm'
|
||||||
max: 2
|
max: 2
|
||||||
ui:
|
|
||||||
size:
|
|
||||||
'3xs': ''
|
|
||||||
'2xs': ''
|
|
||||||
xs: ''
|
|
||||||
sm: ''
|
|
||||||
md: ''
|
|
||||||
lg: ''
|
|
||||||
xl: ''
|
|
||||||
'2xl': ''
|
|
||||||
'3xl': ''
|
|
||||||
code: |
|
code: |
|
||||||
<UAvatar src="https://avatars.githubusercontent.com/u/739984?v=4" alt="benjamincanac" />
|
<UAvatar src="https://avatars.githubusercontent.com/u/739984?v=4" alt="benjamincanac" />
|
||||||
<UAvatar src="https://avatars.githubusercontent.com/u/904724?v=4" alt="Atinux" />
|
<UAvatar src="https://avatars.githubusercontent.com/u/904724?v=4" alt="Atinux" />
|
||||||
|
|||||||
@@ -52,9 +52,11 @@ Besides all the colors from the `ui.colors` object, you can also use the `white`
|
|||||||
props:
|
props:
|
||||||
color: 'white'
|
color: 'white'
|
||||||
variant: 'solid'
|
variant: 'solid'
|
||||||
ui:
|
options:
|
||||||
variant:
|
- name: variant
|
||||||
solid: 1
|
restriction: expected
|
||||||
|
values:
|
||||||
|
- solid
|
||||||
excludedProps:
|
excludedProps:
|
||||||
- color
|
- color
|
||||||
---
|
---
|
||||||
@@ -69,9 +71,11 @@ Badge
|
|||||||
props:
|
props:
|
||||||
color: 'gray'
|
color: 'gray'
|
||||||
variant: 'solid'
|
variant: 'solid'
|
||||||
ui:
|
options:
|
||||||
variant:
|
- name: variant
|
||||||
solid: 1
|
restriction: expected
|
||||||
|
values:
|
||||||
|
- solid
|
||||||
excludedProps:
|
excludedProps:
|
||||||
- color
|
- color
|
||||||
---
|
---
|
||||||
@@ -86,10 +90,11 @@ Badge
|
|||||||
props:
|
props:
|
||||||
color: 'black'
|
color: 'black'
|
||||||
variant: 'solid'
|
variant: 'solid'
|
||||||
ui:
|
options:
|
||||||
variant:
|
- name: variant
|
||||||
solid: 1
|
restriction: only
|
||||||
link: 1
|
values:
|
||||||
|
- solid
|
||||||
excludedProps:
|
excludedProps:
|
||||||
- color
|
- color
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -51,10 +51,12 @@ backgroundClass: 'bg-gray-50 dark:bg-gray-800'
|
|||||||
props:
|
props:
|
||||||
color: 'white'
|
color: 'white'
|
||||||
variant: 'solid'
|
variant: 'solid'
|
||||||
ui:
|
options:
|
||||||
variant:
|
- name: variant
|
||||||
solid: 1
|
restriction: expected
|
||||||
ghost: 1
|
values:
|
||||||
|
- solid
|
||||||
|
- ghost
|
||||||
excludedProps:
|
excludedProps:
|
||||||
- color
|
- color
|
||||||
---
|
---
|
||||||
@@ -69,11 +71,13 @@ Button
|
|||||||
props:
|
props:
|
||||||
color: 'gray'
|
color: 'gray'
|
||||||
variant: 'solid'
|
variant: 'solid'
|
||||||
ui:
|
options:
|
||||||
variant:
|
- name: variant
|
||||||
solid: 1
|
restriction: expected
|
||||||
ghost: 1
|
values:
|
||||||
link: 1
|
- solid
|
||||||
|
- ghost
|
||||||
|
- link
|
||||||
excludedProps:
|
excludedProps:
|
||||||
- color
|
- color
|
||||||
---
|
---
|
||||||
@@ -88,10 +92,12 @@ Button
|
|||||||
props:
|
props:
|
||||||
color: 'black'
|
color: 'black'
|
||||||
variant: 'solid'
|
variant: 'solid'
|
||||||
ui:
|
options:
|
||||||
variant:
|
- name: variant
|
||||||
solid: 1
|
restriction: expected
|
||||||
link: 1
|
values:
|
||||||
|
- solid
|
||||||
|
- link
|
||||||
excludedProps:
|
excludedProps:
|
||||||
- color
|
- color
|
||||||
---
|
---
|
||||||
@@ -284,17 +290,6 @@ To stack buttons as a group, use the `ButtonGroup` component.
|
|||||||
props:
|
props:
|
||||||
size: 'sm'
|
size: 'sm'
|
||||||
orientation: 'horizontal'
|
orientation: 'horizontal'
|
||||||
ui:
|
|
||||||
size:
|
|
||||||
2xs: ''
|
|
||||||
xs: ''
|
|
||||||
sm: ''
|
|
||||||
md: ''
|
|
||||||
lg: ''
|
|
||||||
xl: ''
|
|
||||||
orientation:
|
|
||||||
horizontal: ''
|
|
||||||
vertical: ''
|
|
||||||
code: |
|
code: |
|
||||||
<UButton label="Action" color="white" />
|
<UButton label="Action" color="white" />
|
||||||
<UButton icon="i-heroicons-chevron-down-20-solid" color="gray" />
|
<UButton icon="i-heroicons-chevron-down-20-solid" color="gray" />
|
||||||
|
|||||||
@@ -115,9 +115,12 @@ props:
|
|||||||
size: 'sm'
|
size: 'sm'
|
||||||
color: 'white'
|
color: 'white'
|
||||||
trailing: false
|
trailing: false
|
||||||
extraColors:
|
options:
|
||||||
- white
|
- name: color
|
||||||
- gray
|
restriction: included
|
||||||
|
values:
|
||||||
|
- white
|
||||||
|
- gray
|
||||||
excludedProps:
|
excludedProps:
|
||||||
- icon
|
- icon
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -171,9 +171,12 @@ props:
|
|||||||
icon: 'i-heroicons-magnifying-glass-20-solid'
|
icon: 'i-heroicons-magnifying-glass-20-solid'
|
||||||
color: 'white'
|
color: 'white'
|
||||||
size: 'sm'
|
size: 'sm'
|
||||||
extraColors:
|
options:
|
||||||
- white
|
- name: color
|
||||||
- gray
|
restriction: included
|
||||||
|
values:
|
||||||
|
- white
|
||||||
|
- gray
|
||||||
excludedProps:
|
excludedProps:
|
||||||
- icon
|
- icon
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -56,14 +56,6 @@ baseProps:
|
|||||||
total: 100
|
total: 100
|
||||||
props:
|
props:
|
||||||
size: 'sm'
|
size: 'sm'
|
||||||
ui:
|
|
||||||
size:
|
|
||||||
2xs: true
|
|
||||||
xs: true
|
|
||||||
sm: true
|
|
||||||
md: true
|
|
||||||
lg: true
|
|
||||||
xl: true
|
|
||||||
---
|
---
|
||||||
::
|
::
|
||||||
|
|
||||||
|
|||||||
@@ -175,8 +175,11 @@ baseProps:
|
|||||||
props:
|
props:
|
||||||
icon: 'i-heroicons-check-badge'
|
icon: 'i-heroicons-check-badge'
|
||||||
color: 'primary'
|
color: 'primary'
|
||||||
extraColors:
|
options:
|
||||||
- gray
|
- name: color
|
||||||
|
restriction: included
|
||||||
|
values:
|
||||||
|
- gray
|
||||||
excludedProps:
|
excludedProps:
|
||||||
- icon
|
- icon
|
||||||
---
|
---
|
||||||
|
|||||||
Reference in New Issue
Block a user