modular-forms icon indicating copy to clipboard operation
modular-forms copied to clipboard

Qwik "special form" example doesn't work with number select box

Open ffMathy opened this issue 1 year ago • 14 comments

See example here, where I have (under "special") created a new combobox called "Select number".

Note that selecting the number does not give the value out of the form, whereas selecting the string does.

https://stackblitz.com/edit/modular-forms-qwik-gfsy2g?file=src%2Froutes%2Fspecial%2Findex.tsx,src%2Fcomponents%2FSelect.tsx

ffMathy avatar Jul 09 '23 10:07 ffMathy

This is because <select /> cannot return numbers as values. Modular Forms builds on the functionality of the native HTML form elements here.

fabian-hiller avatar Jul 09 '23 10:07 fabian-hiller

That's why I wrapped the calls in JSON.stringify. Shouldn't this still work?

ffMathy avatar Jul 09 '23 10:07 ffMathy

This has no effect on it. Modular Forms can only capture a data type if it is supported by the HTML element being used and <select /> can only return strings. To maintain type safety, Modular Forms simply does not capture a value in this case. Here is the implementation: https://github.com/fabian-hiller/modular-forms/blob/main/packages/qwik/src/utils/getElementInput.ts

fabian-hiller avatar Jul 09 '23 10:07 fabian-hiller

Hmmm okay. Is there another way to build (for instance) a date picker around Modular Forms, if the input element is of type Date for instance? Or maybe just a number field.

ffMathy avatar Jul 09 '23 11:07 ffMathy

Yes, I think this is possible. For custom input elements, setValue can also be used to capture inputs.

fabian-hiller avatar Jul 09 '23 12:07 fabian-hiller

I see. In that case, would I have a component which takes in a Form as a prop for this to work?

ffMathy avatar Jul 09 '23 12:07 ffMathy

My dilemma is how I preserve the strong-typing inside the component.

ffMathy avatar Jul 09 '23 12:07 ffMathy

You can also simply provide an onInput$ prop where you then call setValue directly in your form. This way you won't have any typing issues and your component will remain independent of Modular Forms.

fabian-hiller avatar Jul 09 '23 13:07 fabian-hiller

Alright.

What I ended up with now is a component that looks like this:

<Select
    form={form}
    fieldPath="durationInMinutes"
    options={getDurationsInMinutes().map(durationInMinutes => ({
      label: getLabelFromDurationInMinutes(durationInMinutes),
      value: durationInMinutes
    }))}
    label="Duration"
  />

The fieldPath and form is strong-typed. However, when I call setValue(form, fieldPath, value), no value is set (confirmed by retrieving it again using getValue(form, fieldPath)).

Any ideas what may be causing this?

ffMathy avatar Jul 09 '23 15:07 ffMathy

Is your component enclosed by the Field component? Is it perhaps related to the active state? Feel free to ask me any questions you may have. Otherwise I need a code example to investigate the problem.

fabian-hiller avatar Jul 09 '23 21:07 fabian-hiller

Ah no. It's not. I keep forgetting that. Would be awesome if that was a runtime error.

To be honest, I don't think that adds a lot to the bundle size. People today don't care much about a 30 byte difference in libraries. It has neglectable performance difference compared to the things we install also.

ffMathy avatar Jul 10 '23 07:07 ffMathy

Thanks for your feedback. I will consider it as soon as I have some more time for Modular Forms.

I recommend to always enclose a field with the Field component. This makes it easier to control your form field and display error messages.

fabian-hiller avatar Jul 10 '23 10:07 fabian-hiller

Great stuff. I love your work.

I've been using getValue a lot lore by the way. I've not had one single case where I didn't need to set shouldActive to false. Sorry to bring that up again, just thought the new info would be helpful.

Not sure how I can fix that with this component too. Is it because I'm missing some other calls?

ffMathy avatar Jul 10 '23 11:07 ffMathy

Thanks a lot! A new open source project will follow soon, which can be used together with Modular Forms.

If you send me a simple code example, I can take a look at it.

fabian-hiller avatar Jul 10 '23 17:07 fabian-hiller