react-hook-form#FormContext TypeScript Examples

The following examples show how to use react-hook-form#FormContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LabelCreate.tsx    From knboard with MIT License 6 votes vote down vote up
LabelCreate = ({ setCreating }: Props) => {
  const dispatch = useDispatch();
  const boardId = useSelector((state: RootState) => state.board.detail?.id);
  const labels = useSelector(selectAllLabels);
  const methods = useForm<DialogFormData>({
    defaultValues: { name: "", color: getRandomHexColor() },
    mode: "onChange",
  });

  const onSubmit = methods.handleSubmit(({ name, color }) => {
    if (labels.map((label) => label.name).includes(name)) {
      methods.setError("name", "Label already exists");
      return;
    }
    if (boardId) {
      dispatch(createLabel({ name, color, board: boardId }));
      setCreating(false);
    }
  });

  return (
    <FormContext {...methods}>
      <Container>
        <LabelFields
          fieldsId="create"
          onSubmit={onSubmit}
          setActive={setCreating}
        />
      </Container>
    </FormContext>
  );
}
Example #2
Source File: LabelRow.tsx    From knboard with MIT License 5 votes vote down vote up
LabelRow = ({ label }: RowProps) => {
  const dispatch = useDispatch();
  const [editing, setEditing] = useState(false);
  const detail = useSelector((state: RootState) => state.board.detail);
  const methods = useForm<DialogFormData>({
    defaultValues: { name: label.name, color: label.color },
    mode: "onChange",
  });

  const onSubmit = methods.handleSubmit(({ name, color }) => {
    if (detail) {
      dispatch(
        patchLabel({ id: label.id, fields: { name, color, board: detail.id } })
      );
      setEditing(false);
    }
  });

  const handleDelete = () => {
    if (
      window.confirm(
        "Are you sure? Deleting a label will remove it from all tasks."
      )
    ) {
      dispatch(deleteLabel(label.id));
    }
  };

  return (
    <RowDiv data-testid={`row-${label.id}`}>
      <Flex
        css={css`
          ${editing && "margin-bottom: 1rem;"}
          transition: all 0.1s ease-in-out;
        `}
      >
        <LabelChip label={label} />
        <Flex>
          {!editing && (
            <Button
              size="small"
              onClick={() => setEditing(true)}
              css={css`
                margin-left: 0.5rem;
                font-size: 0.675rem;
              `}
            >
              Edit
            </Button>
          )}
          <Button
            size="small"
            onClick={handleDelete}
            css={css`
              margin-left: 0.5rem;
              font-size: 0.675rem;
            `}
          >
            Delete
          </Button>
        </Flex>
      </Flex>
      <FormContext {...methods}>
        {editing && (
          <LabelFields
            fieldsId={label.id.toString()}
            onSubmit={onSubmit}
            setActive={setEditing}
          />
        )}
      </FormContext>
    </RowDiv>
  );
}