From bad2e49de92d33f656517c2f7b1dfb86f0f6443e Mon Sep 17 00:00:00 2001 From: Romain Hamel Date: Mon, 1 Jul 2024 20:37:57 +0200 Subject: [PATCH] refactor(Form): input events (#99) Co-authored-by: Benjamin Canac --- playground/app/pages/form.vue | 41 ++- src/runtime/components/Checkbox.vue | 14 +- src/runtime/components/Input.vue | 3 +- src/runtime/components/InputMenu.vue | 38 ++- src/runtime/components/RadioGroup.vue | 17 +- src/runtime/components/Select.vue | 26 +- src/runtime/components/SelectMenu.vue | 31 ++- src/runtime/components/Slider.vue | 15 +- src/runtime/components/Switch.vue | 14 +- src/runtime/components/Textarea.vue | 3 +- src/runtime/composables/useFormField.ts | 4 +- test/components/Checkbox.spec.ts | 71 ++++- test/components/Form.spec.ts | 352 +----------------------- test/components/Input.spec.ts | 81 ++++++ test/components/InputMenu.spec.ts | 85 +++++- test/components/RadioGroup.spec.ts | 77 +++++- test/components/Select.spec.ts | 94 ++++++- test/components/SelectMenu.spec.ts | 84 +++++- test/components/Slider.spec.ts | 76 ++++- test/components/Switch.spec.ts | 70 ++++- test/components/Textarea.spec.ts | 86 +++++- test/utils/form.ts | 53 ++++ 22 files changed, 947 insertions(+), 388 deletions(-) create mode 100644 test/utils/form.ts diff --git a/playground/app/pages/form.vue b/playground/app/pages/form.vue index 4cf0987c..e4e20c04 100644 --- a/playground/app/pages/form.vue +++ b/playground/app/pages/form.vue @@ -72,14 +72,43 @@ function onSubmit(event: FormSubmitEvent) { - - -
- -
-
+ + + +
+
+

+ Validate on input +

+ +
+
+

+ Validate on change +

+ +
+
+

+ Validate on blur +

+ +
+
+

+ Default +

+ +
+
+

+ Disabled +

+ +
+
diff --git a/src/runtime/components/Checkbox.vue b/src/runtime/components/Checkbox.vue index 717fef75..597ae4e4 100644 --- a/src/runtime/components/Checkbox.vue +++ b/src/runtime/components/Checkbox.vue @@ -41,6 +41,7 @@ export interface CheckboxSlots { export interface CheckboxEmits { (e: 'update:modelValue', payload: boolean): void + (e: 'change', payload: Event): void } @@ -52,14 +53,14 @@ import { useId, useAppConfig, useFormField } from '#imports' const props = defineProps() const slots = defineSlots() -defineEmits() +const emits = defineEmits() const modelValue = defineModel({ default: undefined }) const rootProps = useForwardProps(reactivePick(props, 'required', 'value')) const appConfig = useAppConfig() -const { id: _id, emitFormChange, size, color, name, disabled } = useFormField(props) +const { id: _id, emitFormChange, emitFormInput, size, color, name, disabled } = useFormField(props) const id = _id.value ?? useId() const indeterminate = computed(() => (modelValue.value === undefined && props.indeterminate)) @@ -80,6 +81,13 @@ const ui = computed(() => tv({ extend: checkbox, slots: props.ui })({ disabled: disabled.value, checked: (modelValue.value ?? props.defaultValue) || indeterminate.value })) + +function onUpdate(value: any) { + const event = new Event('change', { target: { value } }) + emits('change', event) + emitFormChange() + emitFormInput() +}