【发布时间】:2019-02-25 13:57:14
【问题描述】:
我已经使用 antd 创建了一个示例 Formik 表单。现在我正在使用 POST_MUTATION 添加突变。如何通过 formik 提交表单值。这里我在表单中调用了handleSubmit。但是它没有被调用?
import React from 'react'
import { Formik, Field, Form } from 'formik';
import * as AntD from "antd";
import TextField from "./shared/TextField"
import { Mutation, graphql } from 'react-apollo'
import gql from 'graphql-tag'
import data from './shared/data'
const POST_MUTATION = gql`
mutation PostMutation($username: String!, $email: String!, $password: String!){
post(username: $username, email: $email, password: $password) {
username
email
password
}
}
`
class FormikApollo extends React.Component {
state = {
username: '',
email: '',
password: '',
data: {}
}
handleChange= (e) => {
this.setState({
[e.target.name] : e.target.value,
data
})
}
handleSubmit = () => {
alert("called")
}
我使用这种方式添加我的formik表单。现在我想提交这些未提交的表单值。如何在formik中提交表单值?
form = props => {
const { username, email, password } = this.state;
return (
<div align="center">
<h3 align="center">Registration Form</h3>
<Mutation mutation={POST_MUTATION} variables={{ username, email, password }}>
{ postMutation => (
<Form onSubmit={(formikValues) => postMutation({ variables: formikValues })}>
<Row gutter={4}>
<Col span={12} push={5}>
<Field
name="username"
label="Name"
placeholder="Enter a Name"
component={TextField}
formitemlayout={formItemLayout}
value={this.state.username}
onChange={this.handleChange}
/>
<Field
name="email"
label="Email"
placeholder="Enter an Email"
component={TextField}
formitemlayout={formItemLayout}
value={this.state.email}
onChange={this.handleChange}
/>
<Field
name="password"
label="Password"
type="password"
placeholder="Enter a Password"
component={TextField}
formitemlayout={formItemLayout}
value={this.state.password}
onChange={this.handleChange}
/>
<Button type="submit" onClick={JSON.stringify(postMutation)}>Submit</Button>
</Col>
</Row>
</Form>
)}
</Mutation>
</div>
)
}
render() {
return (
<div align="center">
<Formik
initialValues = {{
username: '',
email:'',
password:''
}}
render={this.form}
/>
</div>
)
}
}
export default FormikApollo
【问题讨论】:
标签: reactjs gatsby antd apollo-client formik