@mui/lab#DatePicker TypeScript Examples

The following examples show how to use @mui/lab#DatePicker. 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: TransactionBilledAt.tsx    From abrechnung with GNU Affero General Public License v3.0 5 votes vote down vote up
export default function TransactionBilledAt({ group, transaction }) {
    const [error, setError] = useState(null);
    const setLocalTransactionDetails = useSetRecoilState(pendingTransactionDetailChanges(transaction.id));

    const save = (billedAt) => {
        if (billedAt == null || billedAt.invalid) {
            setError("Invalid date format");
            return;
        }
        setError(null);
        if (transaction.is_wip && billedAt !== transaction.billed_at) {
            setLocalTransactionDetails((currState) => {
                return {
                    ...currState,
                    billed_at: billedAt,
                };
            });
        }
    };
    if (!transaction.is_wip) {
        return (
            <DisabledTextField
                label="Billed At"
                variant="standard"
                fullWidth
                value={transaction.billed_at}
                disabled={true}
            />
        );
    }

    return (
        <DatePicker
            label="Billed At"
            inputFormat="yyyy-MM-dd"
            value={transaction.billed_at}
            onChange={save}
            renderInput={(params) => (
                <TextField variant="standard" fullWidth {...params} helperText={error} error={error !== null} />
            )}
        />
    );
}
Example #2
Source File: DatePickerElement.tsx    From react-hook-form-mui with MIT License 4 votes vote down vote up
export default function DatePickerElement({
  isDate,
  parseError,
  name,
  required,
  parseDate,
  validation = {},
  inputProps,
  control,
  ...rest
}: DatePickerElementProps): JSX.Element {

  if (required) {
    validation.required = 'This field is required'
  }

  return (
    <Controller
      name={name}
      rules={validation}
      control={control}
      render={({ field: { onChange, value }, fieldState: { error, invalid } }) =>
        <DatePicker
          {...rest}
          value={value || ''}
          onChange={(date, selectionState) => {
            let parsedDate = ''
            if (selectionState) {
              if (typeof parseDate === 'function') {
                parsedDate = parseDate(selectionState)
              }
            } else {
              parsedDate = date?.toISOString().substr(0, 10)
              if (typeof parseDate === 'function') {
                parsedDate = parseDate(date)
              }
            }
            onChange(parsedDate)
            if (typeof rest.onChange === 'function') {
              rest.onChange(parsedDate)
            }
          }}
          renderInput={
            (params) =>
              <TextField
                {...params}
                {...inputProps}
                required={!!required}
                error={invalid}
                helperText={
                  error
                    ? (typeof parseError === 'function' ? parseError(error) : error.message)
                    : inputProps?.helperText || rest.helperText
                }
              />
          }
        />}
    />
  )
}