From b7df7f06146bbe4652e483a8cc03a5ef794d0e29 Mon Sep 17 00:00:00 2001 From: Kael Date: Wed, 27 Nov 2024 23:47:48 +1100 Subject: [PATCH] fix(inputs): normalise inheriting readonly and disabled from form fixes #20730 --- .../VAutocomplete/VAutocomplete.tsx | 8 +++---- .../src/components/VCombobox/VCombobox.tsx | 8 +++---- .../src/components/VSelect/VSelect.tsx | 6 ++--- packages/vuetify/src/composables/form.ts | 9 ++++++-- .../vuetify/src/composables/validation.ts | 22 +++++++++---------- .../src/labs/VNumberInput/VNumberInput.tsx | 6 ++--- 6 files changed, 31 insertions(+), 28 deletions(-) diff --git a/packages/vuetify/src/components/VAutocomplete/VAutocomplete.tsx b/packages/vuetify/src/components/VAutocomplete/VAutocomplete.tsx index d9118990211..841821ba2a1 100644 --- a/packages/vuetify/src/components/VAutocomplete/VAutocomplete.tsx +++ b/packages/vuetify/src/components/VAutocomplete/VAutocomplete.tsx @@ -166,7 +166,7 @@ export const VAutocomplete = genericComponent isPristine.value ? '' : search.value) const displayItems = computed(() => { @@ -192,7 +192,7 @@ export const VAutocomplete = genericComponent ( (props.hideNoData && !displayItems.value.length) || - props.readonly || form?.isReadonly.value + form.isReadonly.value || form.isDisabled.value )) const listRef = ref() @@ -224,7 +224,7 @@ export const VAutocomplete = genericComponent !!(props.chips || slots.chip)) const hasSelectionSlot = computed(() => hasChips.value || !!slots.selection) @@ -243,7 +243,7 @@ export const VCombobox = genericComponent ( (props.hideNoData && !displayItems.value.length) || - props.readonly || form?.isReadonly.value + form.isReadonly.value || form.isDisabled.value )) const listRef = ref() @@ -276,7 +276,7 @@ export const VCombobox = genericComponent model.value.map(selection => selection.value)) const isFocused = shallowRef(false) const label = computed(() => menu.value ? props.closeText : props.openText) @@ -184,7 +184,7 @@ export const VSelect = genericComponent ( (props.hideNoData && !displayItems.value.length) || - props.readonly || form?.isReadonly.value + form.isReadonly.value || form.isDisabled.value )) const computedMenuProps = computed(() => { @@ -215,7 +215,7 @@ export const VSelect = genericComponent !!(props?.readonly ?? form?.isReadonly.value)), + isDisabled: computed(() => !!(props?.disabled ?? form?.isDisabled.value)), + } } diff --git a/packages/vuetify/src/composables/validation.ts b/packages/vuetify/src/composables/validation.ts index 5567e71f351..01a1868e4fa 100644 --- a/packages/vuetify/src/composables/validation.ts +++ b/packages/vuetify/src/composables/validation.ts @@ -83,22 +83,20 @@ export function useValidation ( ) { const model = useProxiedModel(props, 'modelValue') const validationModel = computed(() => props.validationValue === undefined ? model.value : props.validationValue) - const form = useForm() + const form = useForm(props) const internalErrorMessages = ref([]) const isPristine = shallowRef(true) const isDirty = computed(() => !!( wrapInArray(model.value === '' ? null : model.value).length || wrapInArray(validationModel.value === '' ? null : validationModel.value).length )) - const isDisabled = computed(() => !!(props.disabled ?? form?.isDisabled.value)) - const isReadonly = computed(() => !!(props.readonly ?? form?.isReadonly.value)) const errorMessages = computed(() => { return props.errorMessages?.length ? wrapInArray(props.errorMessages).concat(internalErrorMessages.value).slice(0, Math.max(0, +props.maxErrors)) : internalErrorMessages.value }) const validateOn = computed(() => { - let value = (props.validateOn ?? form?.validateOn.value) || 'input' + let value = (props.validateOn ?? form.validateOn?.value) || 'input' if (value === 'lazy') value = 'input lazy' if (value === 'eager') value = 'input eager' const set = new Set(value?.split(' ') ?? []) @@ -125,8 +123,8 @@ export function useValidation ( return { [`${name}--error`]: isValid.value === false, [`${name}--dirty`]: isDirty.value, - [`${name}--disabled`]: isDisabled.value, - [`${name}--readonly`]: isReadonly.value, + [`${name}--disabled`]: form.isDisabled.value, + [`${name}--readonly`]: form.isReadonly.value, } }) @@ -134,7 +132,7 @@ export function useValidation ( const uid = computed(() => props.name ?? unref(id)) onBeforeMount(() => { - form?.register({ + form.register?.({ id: uid.value, vm, validate, @@ -144,14 +142,14 @@ export function useValidation ( }) onBeforeUnmount(() => { - form?.unregister(uid.value) + form.unregister?.(uid.value) }) onMounted(async () => { if (!validateOn.value.lazy) { await validate(!validateOn.value.eager) } - form?.update(uid.value, isValid.value, errorMessages.value) + form.update?.(uid.value, isValid.value, errorMessages.value) }) useToggleScope(() => validateOn.value.input || (validateOn.value.invalidInput && isValid.value === false), () => { @@ -175,7 +173,7 @@ export function useValidation ( }) watch([isValid, errorMessages], () => { - form?.update(uid.value, isValid.value, errorMessages.value) + form.update?.(uid.value, isValid.value, errorMessages.value) }) async function reset () { @@ -228,8 +226,8 @@ export function useValidation ( return { errorMessages, isDirty, - isDisabled, - isReadonly, + isDisabled: form.isDisabled, + isReadonly: form.isReadonly, isPristine, isValid, isValidating, diff --git a/packages/vuetify/src/labs/VNumberInput/VNumberInput.tsx b/packages/vuetify/src/labs/VNumberInput/VNumberInput.tsx index bbf59a65c74..5e0a2835bbe 100644 --- a/packages/vuetify/src/labs/VNumberInput/VNumberInput.tsx +++ b/packages/vuetify/src/labs/VNumberInput/VNumberInput.tsx @@ -91,9 +91,9 @@ export const VNumberInput = genericComponent()({ const stepDecimals = computed(() => getDecimals(props.step)) const modelDecimals = computed(() => typeof model.value === 'number' ? getDecimals(model.value) : 0) - const form = useForm() + const form = useForm(props) const controlsDisabled = computed(() => ( - props.disabled || props.readonly || form?.isReadonly.value + form.isDisabled.value || form.isReadonly.value )) const canIncrease = computed(() => { @@ -119,7 +119,7 @@ export const VNumberInput = genericComponent()({ const decrementSlotProps = computed(() => ({ click: onClickDown })) onMounted(() => { - if (!props.readonly && !props.disabled) { + if (!controlsDisabled.value) { clampModel() } })