Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to avoid wrapping every fieldComponent with AppField #1313

Open
MistyKuu opened this issue Mar 19, 2025 · 0 comments
Open

Allow to avoid wrapping every fieldComponent with AppField #1313

MistyKuu opened this issue Mar 19, 2025 · 0 comments

Comments

@MistyKuu
Copy link

I'd like to do this:

<Form.TextInput fieldName={"name"} labelText="Name" />

instead of

<form.AppField name={"name"}>
        {(field) => <field.TextInput labelText="Name" />}
      </form.AppField>

I was able to implement it with proper typing but avoiding fieldComponents.

export const useAppForm = <
  TFormData,
  TOnMount extends undefined | FormValidateOrFn<TFormData>,
  TOnChange extends undefined | FormValidateOrFn<TFormData>,
  TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
  TOnBlur extends undefined | FormValidateOrFn<TFormData>,
  TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
  TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
  TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
  TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
  TSubmitMeta = never,
>(
  options: FormOptions<
    TFormData,
    TOnMount,
    TOnChange,
    TOnChangeAsync,
    TOnBlur,
    TOnBlurAsync,
    TOnSubmit,
    TOnSubmitAsync,
    TOnServer,
    TSubmitMeta
  >
) => {
  const form = useAppFormTanStack(options);

  const id = useId();

  useEffect(() => {
    form.baseStore.setState((s) => ({ ...s, id }));
  }, []);

  const FieldWrapper =
    <TProps extends {}>(Component: React.ComponentType<TProps>) =>
    ({
      fieldName,
      mode,
      ...rest
    }: TProps & {
      fieldName: DeepKeys<TFormData>;
      mode?: "array";
    }) => {
      return (
        <form.AppField name={fieldName} mode={mode}>
          {() => <Component {...(rest as any)} />}
        </form.AppField>
      );
    };

  const Fields = {
    TextInput: useMemo(() => FieldWrapper(TextInputField), []),
    SelectInput: useMemo(() => FieldWrapper(SelectField), []),
    MultiSelect: useMemo(() => FieldWrapper(MultiSelectField), []),
    CheckBox: useMemo(() => FieldWrapper(CheckBoxField), []),
    RadioButtonGroup: useMemo(() => FieldWrapper(RadioButtonGroupField), []),
    NumberInput: useMemo(() => FieldWrapper(NumberInputField), []),
    TextArea: useMemo(() => FieldWrapper(TextAreaField), []),
    DatePicker: useMemo(() => FieldWrapper(DatePickerField), [])
  };

  return { form, Fields };
};

Can we support it natively with fieldComponents?

usage:

<Fields.TextInput fieldName={"name"} labelText="Name" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant