0

我目前正在努力使用“自动生成链接”按钮。单击后,“链接”字段应填写数据“ http://predefinedlink.com

由于这里使用了 Formik + Material UI 的组合,我很难做到这一点。

const useStyles = makeStyles((theme) => ({[...]}));

const EventStream = ({ onSubmit, eventStream }) => {
  const classes = useStyles();

  return (
    <Formik
      initialValues={{
        link: maybe(() => eventStream.link, "") || "",
      }}
      onSubmit={onSubmit}
      validateOnBlur={false}
      validateOnChange={false}
      enableReinitialize
    >
      {({ handleSubmit, isSubmitting, dirty }) => (
        <FormikForm autoComplete="off" noValidate>
          <Card>
            <CardContent>
              <Grid container spacing={3}>
                <Grid xs={12} item>
                  <Field name="link" label="Link" component={TextField} />
                  <button
                    onClick={console.log("Fill TextField 'link' with new data")}
                  >
                    Auto generate link &raquo;
                  </button>
                </Grid>
              </Grid>
            </CardContent>
            <CardActions className={classes.cardActions}>
              <Button
                variant="contained"
                color="primary"
                classes={{
                  root: clsx(classes.saveButton, {
                    [classes.saveButtonDirty]: !dirty,
                  }),
                  disabled: isSubmitting && classes.saveButtonDisabled,
                }}
                disabled={isSubmitting || !dirty}
                onClick={handleSubmit}
              >
                Save
                {isSubmitting && (
                  <CircularProgress
                    size={20}
                    thickness={5}
                    color="inherit"
                    className={classes.saveButtonProgress}
                  />
                )}
              </Button>
            </CardActions>
          </Card>
        </FormikForm>
      )}
    </Formik>
  );
};

export default EventStream;
4

1 回答 1

0

您可能需要使用setFieldValue

{({ handleSubmit, isSubmitting, dirty }) => (

{({ handleSubmit, isSubmitting, dirty, setFieldValue  }) => (

按钮单击处理程序

             <button
                onClick={()=> {

                 setFieldValue ('link',  'http://predefinedlink.com')
                 }
               }
              >
                Auto generate link &raquo;
              </button>
于 2020-04-14T15:23:52.397 回答