Browse Source

docs: update demo for form

pull/3904/head
Benjy Cui 8 years ago
parent
commit
f250bdcc98
  1. 70
      components/form/demo/customized-form-controls.md
  2. 1
      components/form/demo/register.md

70
components/form/demo/customized-form-controls.md

@ -19,44 +19,70 @@ Customized or third-party form controls can be used in Form, too. Controls must
````jsx ````jsx
import { Form, Input, Button } from 'antd'; import { Form, Input, Select, Button } from 'antd';
const FormItem = Form.Item; const FormItem = Form.Item;
const Option = Select.Option;
const CustomizedInputNumber = React.createClass({ const PriceInput = React.createClass({
getInitialState() { getInitialState() {
const value = this.props.value || {};
return { return {
value: this.props.value || 0, number: value.number || 0,
currency: value.currency || 'rmb',
}; };
}, },
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
// Should be a controlled component. // Should be a controlled component.
if ('value' in nextProps) { if ('value' in nextProps) {
const value = nextProps.value; const value = nextProps.value;
this.setState({ value }); this.setState(value);
} }
}, },
handleChange(e) { handleNumberChange(e) {
const number = parseFloat(e.target.value || 0); const number = parseInt(e.target.value || 0, 10);
if (isNaN(number)) { if (isNaN(number)) {
return; return;
} }
if (!('value' in this.props)) { if (!('value' in this.props)) {
this.setState({ value: number }); this.setState({ number });
} }
this.triggerChange({ number });
},
handleCurrencyChange(currency) {
if (!('value' in this.props)) {
this.setState({ currency });
}
this.triggerChange({ currency });
},
triggerChange(changedValue) {
// Should provide an event to pass value to Form. // Should provide an event to pass value to Form.
const onChange = this.props.onChange; const onChange = this.props.onChange;
if (onChange) { if (onChange) {
onChange(number); onChange(Object.assign({}, this.state, changedValue));
} }
}, },
render() { render() {
const { size } = this.props;
const state = this.state;
return ( return (
<Input <span>
type="text" <Input
size={this.props.size} type="text"
value={this.state.value} size={size}
onChange={this.handleChange} value={state.number}
/> onChange={this.handleNumberChange}
style={{ width: '65%', marginRight: '3%' }}
/>
<Select
value={state.currency}
size={size}
style={{ width: '32%' }}
onChange={this.handleCurrencyChange}
>
<Option value="rmb">RMB</Option>
<Option value="dollar">Dollar</Option>
</Select>
</span>
); );
}, },
}); });
@ -70,14 +96,22 @@ const Demo = Form.create()(React.createClass({
} }
}); });
}, },
checkPrice(rule, value, callback) {
if (value.number > 0) {
callback();
return;
}
callback('Price must greater than zero!');
},
render() { render() {
const { getFieldDecorator } = this.props.form; const { getFieldDecorator } = this.props.form;
return ( return (
<Form inline onSubmit={this.handleSubmit}> <Form inline onSubmit={this.handleSubmit}>
<FormItem label="Number Only"> <FormItem label="Price">
{getFieldDecorator('number', { {getFieldDecorator('price', {
rules: [{ type: 'number' }], initialValue: { number: 0, currency: 'rmb' },
})(<CustomizedInputNumber />)} rules: [{ validator: this.checkPrice }],
})(<PriceInput />)}
</FormItem> </FormItem>
<FormItem> <FormItem>
<Button type="primary" htmlType="submit">Submit</Button> <Button type="primary" htmlType="submit">Submit</Button>

1
components/form/demo/register.md

@ -183,6 +183,7 @@ const RegistrationForm = Form.create()(React.createClass({
<FormItem <FormItem
{...formItemLayout} {...formItemLayout}
label="Captcha" label="Captcha"
extra="We must make sure that your are a human."
> >
<Row gutter={8}> <Row gutter={8}>
<Col span={12}> <Col span={12}>

Loading…
Cancel
Save