react-hook-form#UseFormReset TypeScript Examples

The following examples show how to use react-hook-form#UseFormReset. 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: AddProjectDialog.tsx    From backstage with Apache License 2.0 5 votes vote down vote up
AddProjectDialog = ({
  catalogEntities,
  open,
  handleClose,
  fetchBazaarProjects,
  fetchCatalogEntities,
}: Props) => {
  const bazaarApi = useApi(bazaarApiRef);
  const [selectedEntity, setSelectedEntity] = useState<Entity | null>(null);

  const defaultValues = {
    name: '',
    title: 'Add project',
    community: '',
    description: '',
    status: 'proposed' as Status,
    size: 'medium' as Size,
    responsible: '',
    startDate: null,
    endDate: null,
  };

  const handleEntityClick = (entity: Entity) => {
    setSelectedEntity(entity);
  };

  const handleSubmit: (
    getValues: UseFormGetValues<FormValues>,
    reset: UseFormReset<FormValues>,
  ) => Promise<void> = async (
    getValues: UseFormGetValues<FormValues>,
    reset: UseFormReset<FormValues>,
  ) => {
    const formValues = getValues();
    const response = await bazaarApi.addProject({
      ...formValues,
      entityRef: selectedEntity ? stringifyEntityRef(selectedEntity) : null,
      startDate: formValues.startDate ?? null,
      endDate: formValues.endDate ?? null,
    } as BazaarProject);

    if (response.status === 'ok') {
      fetchBazaarProjects();
      fetchCatalogEntities();
    }

    handleClose();
    reset(defaultValues);
  };

  return (
    <ProjectDialog
      handleSave={handleSubmit}
      title="Add project"
      isAddForm
      defaultValues={defaultValues}
      open={open}
      projectSelector={
        <ProjectSelector
          onChange={handleEntityClick}
          catalogEntities={catalogEntities || []}
          disableClearable={false}
          defaultValue={null}
          label="Select a project"
        />
      }
      handleClose={handleClose}
    />
  );
}