benjamincanac / ui3

A UI Library for Modern Web Apps, powered by Vue & Tailwind CSS.
https://ui.nuxt.com
MIT License
53 stars 7 forks source link

[Form] Input events & Validation #100

Closed romhml closed 1 month ago

romhml commented 3 months ago

The goal is to clarify when form validation is triggered for each inputs and determine which events are emitted by input components.

Here's a comparison between events for native, radix-vue and @nuxt/ui@3 elements:

Input & Textarea

native @nuxt/ui
input x x
change x x
blur x x

Checkbox & Switch

native radix-vue @nuxt/ui
input x x x
change x
blur

RadioGroup

native radix-vue @nuxt/ui
input x x x
change x
blur

Slider

native radix-vue @nuxt/ui
input x x x
change x x @valueCommit x
blur

Select

native radix-vue @nuxt/ui
input x x x
change x
blur x x

SelectMenu

radix-vue @nuxt/ui
input x x
change
blur x x

InputMenu

radix-vue @nuxt/ui
input x x
change
blur x x

There's a difference in how the change event is handled in regular HTML elements versus certain components in radix-vue like Checkbox, Select, and RadioGroup. Since both input and change events act the same for those elements, we can just use the radix-vue way and ditch the change event. Or, we could add these events to @nuxt/ui to match how native components work.

Form validation

Regarding validation with the Form component:

Approach 1

We choose to emit an input event on top of the change for all components. This seems to be the expected behaviour, but requires to de-duplicate validation events since a lot of components will emit both change and input events at the same time.

input change blur
Input x x x
Textarea x x x
Checkbox x x
Toggle x x
Select x x x
SelectMenu x x x
InputMenu x x x
Slider x x
RadioGroup x x

Approach 2

The input event is emitted only when there's a notable difference with the change event. This does not require to de-duplicate input events, the downside is that using :validate-on="['input']" won't trigger validation for a lot of components.

input change blur
Input x x x
Textarea x x x
Checkbox x
Switch x
Select x x
SelectMenu x x
InputMenu x x x
Slider x x
RadioGroup x