Skip to content

Commit

Permalink
refactor(demo): update form stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuagraber committed Jun 26, 2024
1 parent 86d0be8 commit 34b20a0
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 21 deletions.
17 changes: 13 additions & 4 deletions src/components/Form/PdapForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
: ''
"
:value="values[field.name]"
@input="updateForm(field.name, $event)"
@input="updateForm(field, $event)"
/>
<slot />
</form>
Expand All @@ -36,7 +36,7 @@ import { createRule } from '../../utils/vuelidate';
// Types
import { PdapFormProps } from './types';
import { PdapInputTypes } from '../Input/types';
import { PdapInputProps, PdapInputTypes } from '../Input/types';
// Props
const props = withDefaults(defineProps<PdapFormProps>(), {
Expand Down Expand Up @@ -92,10 +92,19 @@ const v$ = useVuelidate(rules, values, { $autoDirty: false, $lazy: true });
const errorMessage = ref(props.error);
// Handlers
function updateForm(fieldName: string, event: Event) {
function updateForm(field: PdapInputProps, event: Event) {
const target = event.target as HTMLInputElement;
values.value[fieldName] = target.value;
const update = (() => {
switch (field.type) {
case PdapInputTypes.CHECKBOX:
return target.checked ? 'true' : 'false';
default:
return target.value;
}
})();
values.value[field.name] = update;
emit('change', values.value);
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/Form/form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ describe('Form component', () => {
await inputTextTwo.setValue('bar');
await inputEmail.setValue('hello@hello.com');
await inputPassword.setValue('P@ssword1!');
await inputCheckboxDefaultChecked.trigger('change');
await inputCheckboxDefaultUnchecked.trigger('change');
await inputCheckboxDefaultChecked.trigger('input');
await inputCheckboxDefaultUnchecked.trigger('input');
await nextTick();

// Assert new values
Expand Down
1 change: 0 additions & 1 deletion src/components/Header/PdapHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const navLogoLinkIsPath = props.logoImageAnchorPath.startsWith('/');
// Lifecycle methods
onMounted(() => {
console.debug('on mounted');
getHeightAndSetToCSSVar();
window.addEventListener('resize', getHeightAndSetToCSSVar);
});
Expand Down
8 changes: 4 additions & 4 deletions src/components/Input/Checkbox/InputCheckbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class="pdap-input-checkbox"
type="checkbox"
v-bind="$attrs"
@change="change"
@input="input"
/>
</template>

Expand All @@ -18,10 +18,10 @@ defineProps<PdapInputCheckboxProps>();
// Emits
const emit = defineEmits<{
(event: 'change', val: Event): void;
(event: 'input', val: Event): void;
}>();
const change = (event: Event) => {
emit('change', event);
const input = (event: Event) => {
emit('input', event);
};
</script>
4 changes: 2 additions & 2 deletions src/components/Input/PdapInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const errorMessageId = computed(() => `pdap-${props.name}-input-error`);
@layer components {
/* General input styles */
.pdap-input {
@apply h-[max-content] gap-4 leading-normal mb-3 w-full flex flex-col;
@apply h-[max-content] gap-1 leading-normal mb-3 w-full flex flex-col;
}
.pdap-input input {
Expand Down Expand Up @@ -78,7 +78,7 @@ const errorMessageId = computed(() => `pdap-${props.name}-input-error`);
}
.pdap-input-error label {
@apply justify-start text-sm;
@apply justify-start;
}
.pdap-input-error input {
Expand Down
6 changes: 0 additions & 6 deletions src/components/Input/Text/InputText.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
:placeholder="placeholder"
:value="value"
v-bind="$attrs"
@change="change"
@input="input"
/>
</template>
Expand All @@ -18,14 +17,9 @@ defineProps<PdapInputTextProps>();
// Emits
const emit = defineEmits<{
(event: 'change', val: Event): void;
(event: 'input', val: Event): void;
}>();
const change = (event: Event) => {
emit('change', event);
};
const input = (event: Event) => {
emit('input', event);
};
Expand Down
4 changes: 2 additions & 2 deletions src/demo/pages/SignupFormDemo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const SCHEMA = [
value: '',
validators: {
password: {
message: 'Please provide your password',
message: 'Please provide a valid password',
value: true,
},
},
Expand All @@ -64,7 +64,7 @@ const SCHEMA = [
value: '',
validators: {
password: {
message: 'Please confirm your password',
message: 'Please confirm your valid password',
value: true,
},
},
Expand Down

0 comments on commit 34b20a0

Please sign in to comment.