'Moment keep coming up on validation form

I am working on a date picker and as simple as it seems, the validation keeps return the moment object regardless. I'll jump straight to the code:

const DATE_VALIDATION_RULE = {
  validator(_, value) {

    console.log(value, "Here"); // This keeps returning the moment object

    if (!value) {
      return Promise.resolve();
    }
    if (!isDefined(parseDate(value))) {
      return Promise.reject(new Error('Must be valid date of form DD/MM/YYYY'));
    }
    return Promise.resolve();
  },
};

export interface DateInputFormItemProps {
  value?: (string | undefined);
  label?: string;
  name: string | (string | number)[];
  onChange?: (value: (string)) => void;
  rules?: Rule[];
  dependencies?: (string | (string | number)[])[];
  disabled?: boolean;
}

export const DateInputFormItem = ({
  value,
  label,
  name,
  onChange,
  rules = [],
  dependencies,
  disabled,
}: DateInputFormItemProps) => {
  const [date, setDate] = useState<string | undefined>(value);

  const dateFormat = 'DD/MM/YYYY';

  useEffect(() => {
    setDate(value);
  }, [value]);

  const handleOnChange = (value, dateString) => {
    setDate(dateString);
    console.log('Formatted Selected Time: ', dateString, date);
    onChange?.(dateString);

  }

  return (
    <Form.Item
      label={label}
      name={name}
      rules={[DATE_VALIDATION_RULE, ...rules]}
      dependencies={dependencies}
    >
      <DatePicker
        format={dateFormat}
        onChange={handleOnChange}
        disabled={disabled}
      />
    </Form.Item>
  );
};

I recently did a range picker, which has exactly the same code, but in the range case, the validation gets the dates as expected, but this one keeps returning the moment object. Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source