Formik的handleChange在DateInput的onChange

时间:2019-01-27 18:43:07

标签: javascript reactjs typescript semantic-ui-react formik

当我仅将来自Formik的handleChange添加到“ semantic-ui-calendar-react” DateInput组件时,选择日期时出现以下错误。

https://imgur.com/a/BOEDEXc“控制台输出”

AddWishlistFormDate.tsx

 import { FormikErrors, FormikProps, withFormik } from 'formik';
 import * as React from 'react';
 import { DateInput } from 'semantic-ui-calendar-react';
 import { Button, Form } from 'semantic-ui-react';

 export interface FormValues {
  date: string;
}

 interface Props {
  submit: (values: FormValues) => Promise<FormikErrors<FormValues> | null>;
}

 class C extends React.PureComponent<FormikProps<FormValues> & Props> {
  public render() {
    const {
      values: { date },
      handleChange,
      handleSubmit,
    } = this.props;
    return (
      <Form onSubmit={handleSubmit}>
        <Form.Field>
          <label>Date</label>
          <DateInput
            name="date"
            value={date}
            iconPosition="left"
            onChange={handleChange}
          />
        </Form.Field>
        <Button type="submit">Add</Button>
      </Form>
    );
  }
}

 export const AddWishlistFormDate = withFormik<Props, FormValues>({
  mapPropsToValues: () => ({ date: '' }),
  handleSubmit: async (values, { props, setErrors }) => {
    const errors = await props.submit(values);
    if (errors) {
      setErrors(errors);
    }
  },
})(C);

使用以下命令运行

 const handleSubmit = async (values: FormValues) => {
    console.log(values);
    return null;
  };
  return <AddWishlistFormDate submit={handleSubmit} />;

1 个答案:

答案 0 :(得分:2)

您会这样做

          <DateInput
            name="date"
            value={date}
            iconPosition="left"
            onChange={(_, {value}) => setFieldValue('date', value)}
          />