123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- import {
- AutoComplete,
- Button,
- Cascader,
- Checkbox,
- Col,
- Form,
- Input,
- InputNumber,
- Row,
- Select,
- Card,
- } from 'antd';
- import React, { useState } from 'react';
-
- const { Option } = Select;
- const residences = [
- {
- value: 'zhejiang',
- label: 'Zhejiang',
- children: [
- {
- value: 'hangzhou',
- label: 'Hangzhou',
- children: [
- {
- value: 'xihu',
- label: 'West Lake',
- },
- ],
- },
- ],
- },
- {
- value: 'jiangsu',
- label: 'Jiangsu',
- children: [
- {
- value: 'nanjing',
- label: 'Nanjing',
- children: [
- {
- value: 'zhonghuamen',
- label: 'Zhong Hua Men',
- },
- ],
- },
- ],
- },
- ];
- const formItemLayout = {
- labelCol: {
- xs: {
- span: 24,
- },
- sm: {
- span: 8,
- },
- },
- wrapperCol: {
- xs: {
- span: 24,
- },
- sm: {
- span: 16,
- },
- },
- };
- const tailFormItemLayout = {
- wrapperCol: {
- xs: {
- span: 24,
- offset: 0,
- },
- sm: {
- span: 16,
- offset: 8,
- },
- },
- };
-
-
- const BasicForm = () => {
- const [form] = Form.useForm();
-
- const onFinish = (values) => {
- console.log('Received values of form: ', values);
- };
-
- const prefixSelector = (
- <Form.Item name="prefix" noStyle>
- <Select
- style={{
- width: 70,
- }}
- >
- <Option value="86">+86</Option>
- <Option value="87">+87</Option>
- </Select>
- </Form.Item>
- );
- const suffixSelector = (
- <Form.Item name="suffix" noStyle>
- <Select
- style={{
- width: 70,
- }}
- >
- <Option value="USD">$</Option>
- <Option value="CNY">¥</Option>
- </Select>
- </Form.Item>
- );
- const [autoCompleteResult, setAutoCompleteResult] = useState([]);
-
- const onWebsiteChange = (value) => {
- if (!value) {
- setAutoCompleteResult([]);
- } else {
- setAutoCompleteResult(['.com', '.org', '.net'].map((domain) => `${value}${domain}`));
- }
- };
-
- const websiteOptions = autoCompleteResult.map((website) => ({
- label: website,
- value: website,
- }));
- return (
- <Form
- {...formItemLayout}
- form={form}
- name="register"
- onFinish={onFinish}
- initialValues={{
- residence: ['zhejiang', 'hangzhou', 'xihu'],
- prefix: '86',
- }}
- scrollToFirstError
- style={{ background: '#fff', boxSizing: 'border-box', padding: '24px' }}
- >
- <Form.Item
- name="email"
- label="E-mail"
- rules={[
- {
- type: 'email',
- message: 'The input is not valid E-mail!',
- },
- {
- required: true,
- message: 'Please input your E-mail!',
- },
- ]}
- >
- <Input />
- </Form.Item>
-
- <Form.Item
- name="password"
- label="Password"
- rules={[
- {
- required: true,
- message: 'Please input your password!',
- },
- ]}
- hasFeedback
- >
- <Input.Password />
- </Form.Item>
-
- <Form.Item
- name="confirm"
- label="Confirm Password"
- dependencies={['password']}
- hasFeedback
- rules={[
- {
- required: true,
- message: 'Please confirm your password!',
- },
- ({ getFieldValue }) => ({
- validator(_, value) {
- if (!value || getFieldValue('password') === value) {
- return Promise.resolve();
- }
-
- return Promise.reject(new Error('The two passwords that you entered do not match!'));
- },
- }),
- ]}
- >
- <Input.Password />
- </Form.Item>
-
- <Form.Item
- name="nickname"
- label="Nickname"
- tooltip="What do you want others to call you?"
- rules={[
- {
- required: true,
- message: 'Please input your nickname!',
- whitespace: true,
- },
- ]}
- >
- <Input />
- </Form.Item>
-
- <Form.Item
- name="residence"
- label="Habitual Residence"
- rules={[
- {
- type: 'array',
- required: true,
- message: 'Please select your habitual residence!',
- },
- ]}
- >
- <Cascader options={residences} />
- </Form.Item>
-
- <Form.Item
- name="phone"
- label="Phone Number"
- rules={[
- {
- required: true,
- message: 'Please input your phone number!',
- },
- ]}
- >
- <Input
- addonBefore={prefixSelector}
- style={{
- width: '100%',
- }}
- />
- </Form.Item>
-
- <Form.Item
- name="donation"
- label="Donation"
- rules={[
- {
- required: true,
- message: 'Please input donation amount!',
- },
- ]}
- >
- <InputNumber
- addonAfter={suffixSelector}
- style={{
- width: '100%',
- }}
- />
- </Form.Item>
-
- <Form.Item
- name="website"
- label="Website"
- rules={[
- {
- required: true,
- message: 'Please input website!',
- },
- ]}
- >
- <AutoComplete options={websiteOptions} onChange={onWebsiteChange} placeholder="website">
- <Input />
- </AutoComplete>
- </Form.Item>
-
- <Form.Item
- name="intro"
- label="Intro"
- rules={[
- {
- required: true,
- message: 'Please input Intro',
- },
- ]}
- >
- <Input.TextArea showCount maxLength={100} />
- </Form.Item>
-
- <Form.Item
- name="gender"
- label="Gender"
- rules={[
- {
- required: true,
- message: 'Please select gender!',
- },
- ]}
- >
- <Select placeholder="select your gender">
- <Option value="male">Male</Option>
- <Option value="female">Female</Option>
- <Option value="other">Other</Option>
- </Select>
- </Form.Item>
-
- <Form.Item label="Captcha" extra="We must make sure that your are a human.">
- <Row gutter={8}>
- <Col span={12}>
- <Form.Item
- name="captcha"
- noStyle
- rules={[
- {
- required: true,
- message: 'Please input the captcha you got!',
- },
- ]}
- >
- <Input />
- </Form.Item>
- </Col>
- <Col span={12}>
- <Button>Get captcha</Button>
- </Col>
- </Row>
- </Form.Item>
-
- <Form.Item
- name="agreement"
- valuePropName="checked"
- rules={[
- {
- validator: (_, value) =>
- value ? Promise.resolve() : Promise.reject(new Error('Should accept agreement')),
- },
- ]}
- {...tailFormItemLayout}
- >
- <Checkbox>
- I have read the <a href="">agreement</a>
- </Checkbox>
- </Form.Item>
- <Form.Item {...tailFormItemLayout}>
- <Button type="primary" htmlType="submit">
- Register
- </Button>
- </Form.Item>
- </Form>
- );
- };
-
- export default () => <Card><BasicForm /></Card>;
|