|
| 1 | +import { |
| 2 | + Show, |
| 3 | + For, |
| 4 | + mergeProps, |
| 5 | + type Component, |
| 6 | + splitProps, |
| 7 | + ComponentProps, |
| 8 | +} from "solid-js"; |
| 9 | +import { createFormControl, IFormGroup, type IFormControl } from "solid-forms"; |
| 10 | +import { TextField } from "../material"; |
| 11 | + |
| 12 | +const FormTextField = ( |
| 13 | + props: { |
| 14 | + control: IFormControl<string>; |
| 15 | + } & ComponentProps<typeof TextField> |
| 16 | +) => { |
| 17 | + const [local, remote] = splitProps(props, ["control"]); |
| 18 | + |
| 19 | + return ( |
| 20 | + <> |
| 21 | + <TextField |
| 22 | + {...remote} |
| 23 | + value={local.control.value} |
| 24 | + oninput={(e) => { |
| 25 | + local.control.setValue(e.currentTarget.value); |
| 26 | + }} |
| 27 | + onblur={() => local.control.markTouched(true)} |
| 28 | + required={local.control.isRequired} |
| 29 | + disabled={local.control.isDisabled} |
| 30 | + /> |
| 31 | + |
| 32 | + <Show when={local.control.isTouched && !local.control.isValid}> |
| 33 | + <For each={Object.keys(local.control.errors!)}> |
| 34 | + {(errorMsg: string) => <small>{errorMsg}</small>} |
| 35 | + </For> |
| 36 | + </Show> |
| 37 | + </> |
| 38 | + ); |
| 39 | +}; |
| 40 | + |
| 41 | +const SampleTextInput: Component<{ |
| 42 | + control?: IFormControl<string>; |
| 43 | + name?: string; |
| 44 | + type?: string; |
| 45 | +}> = (props) => { |
| 46 | + // here we provide a default form control in case the user doesn't supply one |
| 47 | + let localProps = mergeProps( |
| 48 | + { control: createFormControl(""), type: "text" }, |
| 49 | + props |
| 50 | + ); |
| 51 | + |
| 52 | + return ( |
| 53 | + <div |
| 54 | + classList={{ |
| 55 | + "is-invalid": !!localProps.control.errors, |
| 56 | + "is-touched": localProps.control.isTouched, |
| 57 | + "is-required": localProps.control.isRequired, |
| 58 | + "is-disabled": localProps.control.isDisabled, |
| 59 | + }} |
| 60 | + > |
| 61 | + <input |
| 62 | + name={localProps.name} |
| 63 | + type={localProps.type} |
| 64 | + value={localProps.control.value} |
| 65 | + oninput={(e) => { |
| 66 | + localProps.control.setValue(e.currentTarget.value); |
| 67 | + }} |
| 68 | + onblur={() => localProps.control.markTouched(true)} |
| 69 | + required={localProps.control.isRequired} |
| 70 | + disabled={localProps.control.isDisabled} |
| 71 | + /> |
| 72 | + |
| 73 | + <Show when={localProps.control.isTouched && !localProps.control.isValid}> |
| 74 | + <For each={Object.values(localProps.control.errors!)}> |
| 75 | + {(errorMsg: string) => <small>{errorMsg}</small>} |
| 76 | + </For> |
| 77 | + </Show> |
| 78 | + </div> |
| 79 | + ); |
| 80 | +}; |
| 81 | + |
| 82 | +function submitHandler(group: IFormGroup, handler: () => Promise<void>) { |
| 83 | + return async (e: Event) => { |
| 84 | + e.preventDefault(); |
| 85 | + |
| 86 | + for (const control of Object.values(group.controls)) { |
| 87 | + control.markTouched(true); |
| 88 | + } |
| 89 | + |
| 90 | + if (group.isSubmitted || !group.isValid) return; |
| 91 | + |
| 92 | + group.markSubmitted(true); |
| 93 | + }; |
| 94 | +} |
| 95 | + |
| 96 | +export const Form2 = { |
| 97 | + TextField: FormTextField, |
| 98 | + submitHandler, |
| 99 | +}; |
0 commit comments