Skip to content

Support fieldProps alone without the need to define validators or onSubmit #17

Open
@sandrotaje

Description

@sandrotaje

The use case is when you just need fieldProps but you don't need validation or submit functionality. For example when you don't have a standard form and you just want an easy way to have name, value, onChange, onBlur defined.

So my proposal is to have something like this code below.
This is thought to be used with the useState so you can pass to fieldProps directly what useState returns.

  const fieldProps = <A, B extends keyof A>(
    field: B,
    model: [A, (val: A) => void]
  ): {
    name: B;
    value: A[B];
    onChange: (val: A[B]) => void;
    onBlur: () => void;
  } => ({
    name: field,
    value: model[0][field],
    onChange: (val) => {
      model[1]({
        ...model[0],
        [field]: val,
      });
    },
    onBlur: () => {},
  });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions