Skip to content

Commit

Permalink
fix(Inputs): reactive validated prop
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0dr1y authored and mtorromeo committed Sep 27, 2024
1 parent 258ddfd commit 92dddea
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/components/TextInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</template>

<script lang="ts" setup>
import { type Ref, ref, computed, type InputHTMLAttributes, getCurrentInstance } from 'vue';
import { type Ref, ref, computed, toRefs, type InputHTMLAttributes, getCurrentInstance } from 'vue';
import { useChildrenTracker } from '../use';
import styles from '@patternfly/react-styles/css/components/FormControl/form-control';
import { useInputValidation, type InputValidateState } from '../input';
Expand Down Expand Up @@ -116,6 +116,7 @@ const ouiaProps = useOUIAProps({id: props.ouiaId, safe: props.ouiaSafe});
const input: Ref<HTMLInputElement | undefined> = ref();
const hasStatusIcon = computed(() => !props.noStatusIcon && ['success', 'error', 'warning'].includes(effectiveValidated.value));
const { validated } = toRefs(props);
const {
value,
effectiveValidated,
Expand All @@ -128,7 +129,7 @@ const {
} = useInputValidation({
inputElement: input,
autoValidate: props.autoValidate,
validated: props.validated,
validated: validated,
});
useChildrenTracker(FormInputsKey, getCurrentInstance()?.proxy);
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/components/Textarea.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<script lang="ts" setup>
import styles from '@patternfly/react-styles/css/components/FormControl/form-control';
import { computed, ref, onMounted, type TextareaHTMLAttributes, type Ref, getCurrentInstance } from 'vue';
import { computed, ref, onMounted, toRefs, type TextareaHTMLAttributes, type Ref, getCurrentInstance } from 'vue';
import { useInputValidation } from '../input';
import { useChildrenTracker } from '../use';
import { FormGroupInputsKey, FormInputsKey } from './Form/common';
Expand Down Expand Up @@ -114,6 +114,7 @@ const ouiaProps = useOUIAProps({id: props.ouiaId, safe: props.ouiaSafe});
const input: Ref<HTMLTextAreaElement | undefined> = ref();
const hasStatusIcon = computed(() => ['success', 'error', 'warning'].includes(effectiveValidated.value));
const { validated } = toRefs(props);
const {
value,
effectiveValidated,
Expand All @@ -126,7 +127,7 @@ const {
} = useInputValidation({
inputElement: input,
autoValidate: props.autoValidate,
validated: props.validated,
validated: validated,
customCheckValidity: checkValidity,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ export function useInputValidation({
customCheckValidity,
}: {
autoValidate: '' | 'blur' | 'input' | 'change' | 'enter' | boolean;
validated?: InputValidateState | null;
validated?: Ref<InputValidateState | undefined>;
inputElement?: MaybeRef<InputElement | undefined>;
customCheckValidity?: () => boolean;
}) {
const instance = getCurrentInstance()?.proxy;

const innerValidated: Ref<InputValidateState> = ref('default');
const effectiveValidated = computed(() => validated ?? innerValidated.value);
const effectiveValidated = computed(() => validated?.value ?? innerValidated.value);
watch(effectiveValidated, () => instance?.$emit('update:validated', effectiveValidated.value));

const value = useManagedProp('modelValue', '');
Expand Down

0 comments on commit 92dddea

Please sign in to comment.