fix: add disabled support for boolean

This commit is contained in:
Damien Roche 2024-12-17 09:19:21 +01:00
parent 86d265dee4
commit 0c77d1943f
4 changed files with 38 additions and 4 deletions

View File

@ -5,7 +5,7 @@ import { FormControl, FormDescription, FormField, FormItem, FormMessage } from '
import { Switch } from '@/lib/registry/default/ui/switch'
import { computed } from 'vue'
import AutoFormLabel from './AutoFormLabel.vue'
import { beautifyObjectName } from './utils'
import { beautifyObjectName, maybeBooleanishToBoolean } from './utils'
const props = defineProps<FieldProps>()
@ -21,7 +21,7 @@ const booleanComponent = computed(() => props.config?.component === 'switch' ? S
<component
:is="booleanComponent"
v-bind="{ ...slotProps.componentField }"
:disabled="disabled"
:disabled="maybeBooleanishToBoolean(config?.inputProps?.disabled) ?? disabled"
:checked="slotProps.componentField.modelValue"
@update:checked="slotProps.componentField['onUpdate:modelValue']"
/>

View File

@ -169,3 +169,20 @@ export function getFromPath<TValue = unknown, TFallback = TValue>(
return resolvedValue as TValue | undefined
}
type Booleanish = boolean | 'true' | 'false'
export function booleanishToBoolean(value: Booleanish) {
switch (value) {
case 'true':
case true:
return true
case 'false':
case false:
return false
}
}
export function maybeBooleanishToBoolean(value?: Booleanish) {
return value ? booleanishToBoolean(value) : undefined
}

View File

@ -5,7 +5,7 @@ import { FormControl, FormDescription, FormField, FormItem, FormMessage } from '
import { Switch } from '@/lib/registry/new-york/ui/switch'
import { computed } from 'vue'
import AutoFormLabel from './AutoFormLabel.vue'
import { beautifyObjectName } from './utils'
import { beautifyObjectName, maybeBooleanishToBoolean } from './utils'
const props = defineProps<FieldProps>()
@ -21,7 +21,7 @@ const booleanComponent = computed(() => props.config?.component === 'switch' ? S
<component
:is="booleanComponent"
v-bind="{ ...slotProps.componentField }"
:disabled="disabled"
:disabled="maybeBooleanishToBoolean(config?.inputProps?.disabled) ?? disabled"
:checked="slotProps.componentField.modelValue"
@update:checked="slotProps.componentField['onUpdate:modelValue']"
/>

View File

@ -169,3 +169,20 @@ export function getFromPath<TValue = unknown, TFallback = TValue>(
return resolvedValue as TValue | undefined
}
type Booleanish = boolean | 'true' | 'false'
export function booleanishToBoolean(value: Booleanish) {
switch (value) {
case 'true':
case true:
return true
case 'false':
case false:
return false
}
}
export function maybeBooleanishToBoolean(value?: Booleanish) {
return value ? booleanishToBoolean(value) : undefined
}