iusehooks / usetheform

React library for composing declarative forms, manage their state, handling their validation and much more.
https://iusehooks.github.io/usetheform/
MIT License
86 stars 33 forks source link

innerRef - setValue for type='custom' #27

Closed iusehooks closed 3 years ago

iusehooks commented 3 years ago

Improvement:

Example:

const CustomField = ({ name }) => {
  const { value, setValue } = useField({ type: "custom", name, value: "5" });
  const onSetValue = () => setValue(prev => ++prev)

  return (
    <pre>
      <code data-testid="output">{JSON.stringify(value)}</code>
      <button type="button" onClick={onSetValue}>Set Value</button>
    </pre>
  );
};

function App() {
  const formRef = useRef();
  const inputRef = useRef();

  return (
    <Form innerRef={formRef} >
      <Input  innerRef={inputRef}  type="text" name="user" value="BeBo" />
    </Form>
  );
}