Skip to content

Commit

Permalink
feat(components): expose setValues handler from Form
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuagraber committed Aug 20, 2024
1 parent de32d27 commit e2d6a29
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
41 changes: 25 additions & 16 deletions src/components/Form/PdapForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ const props = withDefaults(defineProps<PdapFormProps>(), {
// Emits
const emit = defineEmits(['submit', 'change']);
// Expose
defineExpose({
setValues,
});
// State
const data = computed(() =>
props.schema.map((input) => {
Expand Down Expand Up @@ -97,6 +102,22 @@ const v$ = useVuelidate(rules, values, { $autoDirty: false, $lazy: true });
// Vars
const errorMessage = ref(props.error);
// Effects
// Effect - Updates form error state based on input error state and/or props
watchEffect(() => {
if (props.error) errorMessage.value = props.error;
else if (errorMessage.value && v$.value.$errors.length === 0)
errorMessage.value = null;
else if (!errorMessage.value && v$.value.$errors.length > 0)
errorMessage.value = 'Please update this form to correct the errors';
});
watchEffect(() => {
if (props.resetOn && props.resetOn !== 'submit') {
resetForm();
}
});
// Handlers
function updateForm(field: PdapInputProps, event: Event) {
const target = event.target as HTMLInputElement;
Expand All @@ -114,22 +135,6 @@ function updateForm(field: PdapInputProps, event: Event) {
emit('change', values.value, event);
}
// Effects
// Effect - Updates form error state based on input error state and/or props
watchEffect(() => {
if (props.error) errorMessage.value = props.error;
else if (errorMessage.value && v$.value.$errors.length === 0)
errorMessage.value = null;
else if (!errorMessage.value && v$.value.$errors.length > 0)
errorMessage.value = 'Please update this form to correct the errors';
});
watchEffect(() => {
if (props.resetOn && props.resetOn !== 'submit') {
resetForm();
}
});
/**
* Reset vuelidate and wipe values state
*/
Expand All @@ -140,6 +145,10 @@ function resetForm() {
}, {});
}
function setValues<T extends typeof values.value>(update: T) {
values.value = update;
}
async function submit(e: Event) {
// Check form submission
const isValidSubmission = await v$.value.$validate();
Expand Down
23 changes: 5 additions & 18 deletions src/demo/pages/ComponentDemo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
<h2>Form</h2>
<Form
id="test"
ref="formRef"
name="test"
:schema="mockFormSchema"
@change="change"
Expand Down Expand Up @@ -266,24 +267,6 @@ const mockFormSchema = [
},
},
},
{
id: 'password',
name: 'password',
label: 'Password',
type: PdapInputTypes.PASSWORD,
placeholder: 'Password',
value: '',
validators: {
password: {
message: 'Please enter a valid password',
value: true,
},
required: {
message: 'Please enter a valid password',
value: true,
},
},
},
{
id: 'likes-ice-cream',
defaultChecked: true,
Expand All @@ -294,6 +277,7 @@ const mockFormSchema = [
},
];
const formRef = ref();
const dropDownPressIsOpen = ref(false);
const dropDownHoverIsOpen = ref(false);
const loadingText = ref('customizable, with optional text...');
Expand Down Expand Up @@ -344,6 +328,9 @@ function change(
values: Record<'firstName' | 'lastName' | 'iceCream', string>,
event: Event
) {
if (formRef.value) {
console.debug({ ref: formRef.value });
}
console.debug('onChange', { values, event });
}
Expand Down

0 comments on commit e2d6a29

Please sign in to comment.