| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- import React, { useState, useLayoutEffect, Component } from 'react';
- import { storiesOf } from '@storybook/react';
- import { Button, Modal, TreeSelect, Row, Col, Avatar, Icon, Select as BasicSelect,
- Form,
- useFormState,
- useFormApi,
- useFieldApi,
- useFieldState,
- withFormState,
- withFormApi,
- withField,
- ArrayField,
- AutoComplete,
- Collapse } from '../../../index';
- import { ComponentUsingFormState } from '../Hook/hookDemo';
- const { Input, Select, DatePicker, Switch, Slider, CheckboxGroup, Checkbox, RadioGroup, Radio, TimePicker, InputNumber, InputGroup } = Form;
- const plainOptions = ['Apple', 'Pear', 'Orange'];
- const { Option } = Select;
- const { Label } = Form;
- const { Section } = Form;
- // wrapperCol, labelCol
- class LayoutDemo extends React.Component {
- constructor() {
- super();
- this.state = {
- labelPosition: 'left',
- labelAlign: 'left',
- // labelWidth: '180px'
- };
- this.changeLabelPos = this.changeLabelPos.bind(this);
- this.changeLabelAlign = this.changeLabelAlign.bind(this);
- }
- changeLabelPos(labelPosition) {
- let labelWidth;
- // labelPosition === 'left' ? labelWidth = '180px' : labelWidth = 'auto';
- this.setState({ labelPosition, labelWidth });
- }
- changeLabelAlign(labelAlign) {
- this.setState({ labelAlign });
- }
- setValues() {
- this.formApi.setValues({
- });
- }
- render() {
- const { labelPosition, labelAlign, labelWidth } = this.state;
- return (
- <>
- <div>
- <Label style={{ marginLeft: 10 }}>lablPosition:</Label>
- <BasicSelect onChange={this.changeLabelPos} value={labelPosition}>
- <Select.Option value="top">top</Select.Option>
- <Select.Option value="left">left</Select.Option>
- </BasicSelect>
- <Label style={{ marginLeft: 10 }}>lablAlign:</Label>
- <BasicSelect onChange={this.changeLabelAlign} value={labelAlign}>
- <Select.Option value="left">left</Select.Option>
- <Select.Option value="right">right</Select.Option>
- </BasicSelect>
- </div>
- <hr />
- <Form
- labelPosition={labelPosition}
- labelWidth={labelWidth}
- labelAlign={labelAlign}
- labelCol={{ span: 6 }}
- wrapperCol={{ span: 18 }}
- onChange={formState => console.log(formState)}
- onValueChange={formState => console.log(formState)}
- style={{ padding: '10px', width: 600 }}>
- <Form.Input
- field="input"
- label="我是label阿"
- trigger="blur"
- rules={[
- { required: true, message: 'required error' },
- { type: 'string', message: 'type error' },
- { validator: (rule, value) => value === 'muji', message: 'not muji' }
- ]}
- />
- <Form.Switch field="agree" />
- <Form.InputNumber field="price" />
- <Form.Select field="name">
- <Option value="mike">mike</Option>
- <Option value="jane">jane</Option>
- <Option value="kate">kate</Option>
- </Form.Select>
- <Form.CheckboxGroup field="role">
- <Form.Checkbox value="admin">管理员admin</Form.Checkbox>
- <Form.Checkbox value="user">用户user</Form.Checkbox>
- <Form.Checkbox value="guest">访客guest</Form.Checkbox>
- <Form.Checkbox value="root">根用户root</Form.Checkbox>
- </Form.CheckboxGroup>
- <Form.Checkbox useOutSideGroup field="root">root</Form.Checkbox>
- <Form.RadioGroup field="sex" name="apple">
- <Form.Radio value="1">man</Form.Radio>
- <Form.Radio value="2">woman</Form.Radio>
- </Form.RadioGroup>
- <Form.Slot label={{ text: 'texxt', required: true }}>
- <div>slot</div>
- </Form.Slot>
- </Form>
- </>
- );
- }
- }
- class InsetLabelDemo extends React.Component {
- constructor(props) {
- super();
- this.state = {
- labelPosition: 'left',
- labelAlign: 'left',
- // labelWidth: '180px'
- };
- this.changeLabelPos = this.changeLabelPos.bind(this);
- this.changeLabelAlign = this.changeLabelAlign.bind(this);
- }
- changeLabelPos(labelPosition) {
- let labelWidth;
- // labelPosition === 'left' ? labelWidth = '180px' : labelWidth = 'auto';
- this.setState({ labelPosition, labelWidth });
- }
- changeLabelAlign(labelAlign) {
- this.setState({ labelAlign });
- }
- render() {
- const { labelPosition, labelAlign } = this.state;
- return (
- <Form
- labelPosition={'inset'}
- labelCol={{ span: 6 }}
- wrapperCol={{ span: 18 }}
- onChange={formState => console.log(formState)}
- onValueChange={formState => console.log(formState)}
- style={{ padding: '10px', width: 600 }}>
- <Form.Input
- field="input"
- />
- <Form.InputNumber field="price" />
- <Form.DatePicker field="date" placeholder="请选择日期时间范围" />
- <Form.TimePicker field="time" />
- <Form.Slot label={{ text: 'texxt', required: true }}>
- <div>slot</div>
- </Form.Slot>
- </Form>
- );
- }
- }
- class GroupFormDemo extends React.Component {
- constructor() {
- super();
- this.saveFormApi = this.saveFormApi.bind(this);
- this.manualSubmit = this.manualSubmit.bind(this);
- }
- saveFormApi(formApi) {
- this.formApi = formApi;
- }
- manualSubmit() {
- this.formApi.submitForm();
- }
- render() {
- const selectProps = {
- style: { width: '100px' },
- placeholder: '国家',
- field: 'country',
- rules: [
- { required: true }
- ]
- };
- return (
- <>
- <Form onSubmit={values => console.log(values)} labelPosition="left" getFormApi={this.saveFormApi}>
- <InputGroup label={{ text: 'Movie', required: true }} labelPosition="left">
- <Select {...selectProps}>
- <Select.Option value="crime">+86</Select.Option>
- <Select.Option value="comedy">+1</Select.Option>
- <Select.Option value="tragedy">+83</Select.Option>
- </Select>
- <Input placeholder="手机号码" style={{ width: 100 }} field="phone" noLabel rules={[{ required: true }]} />
- <InputNumber placeholder="评分" style={{ width: 140 }} field="MovieScore" noLabel />
- </InputGroup>
- <Input field="name" trigger="blur" rules={[{ required: true }]} />
- <Input field="familyName[0].before" trigger="blur" />
- <Input field="familyName[0].after" trigger="blur" />
- <Select field="Sex">
- <Option value="female">female</Option>
- <Option value="male">male</Option>
- </Select>
- <Button htmlType="submit">提交</Button>
- <Button onClick={this.manualSubmit}>手动提交</Button>
- </Form>
- </>
- );
- }
- }
- class LayoutForm extends React.Component {
- constructor() {
- super();
- this.state = {
- filed: {},
- layout: 'vertical',
- labelPosition: 'top',
- // horizontal
- };
- this.getFormApi = this.getFormApi.bind(this);
- this.changeLayout = this.changeLayout.bind(this);
- this.changeLabelPos = this.changeLabelPos.bind(this);
- }
- changeLayout(layout) {
- this.setState({ layout });
- }
- changeLabelPos(labelPosition) {
- this.setState({ labelPosition });
- }
- changeInput() {
- this.formApi.setValue('input', Math.random());
- }
- getFormApi(formApi) {
- this.formApi = formApi;
- }
- render() {
- const { field } = this.state;
- const { layout, labelPosition } = this.state;
- return (
- <>
- <div>
- <BasicSelect onChange={this.changeLayout} value={layout}>
- <BasicSelect.Option value="vertical">vertical</BasicSelect.Option>
- <BasicSelect.Option value="horizontal">horizontal</BasicSelect.Option>
- </BasicSelect>
- <BasicSelect onChange={this.changeLabelPos} value={labelPosition}>
- <BasicSelect.Option value="top">top</BasicSelect.Option>
- <BasicSelect.Option value="left">left</BasicSelect.Option>
- </BasicSelect>
- </div>
- <Form
- layout={layout}
- labelPosition={labelPosition}
- labelWidth={'auto'}
- getFormApi={this.getFormApi}
- style={{ padding: '10px' }}>
- <Row>
- <Col span={6}>
- <Input
- field="input"
- label="我是label阿"
- style={{ width: '250px' }}
- trigger="blur"
- rules={[
- { required: true, message: 'required error' },
- { type: 'string', message: 'type error' },
- { validator: (rule, value) => value === 'muji', message: 'not muji' }
- ]}
- />
- </Col>
- <Col span={6}>
- <Switch field="switch" />
- </Col>
- <Col span={6}>
- <InputNumber field="number" />
- </Col>
- <Col span={6}>
- <Select field="select">
- <Option value="mike">mike</Option>
- <Option value="jane">jane</Option>
- <Option value="kate">kate</Option>
- </Select>
- </Col>
- </Row>
- <Row>
- <Col span={6}>
- <Select
- field="peoples"
- multiple
- label="Select [multiple]"
- rules={[
- { type: 'array', min: 1, max: 3, messag: 'peoples errors' }
- ]}
- >
- <Option value="a">a</Option>
- <Option value="b">b</Option>
- <Option value="c">c</Option>
- <Option value="d">d</Option>
- <Option value="e">e</Option>
- </Select>
- </Col>
- <Col span={6}>
- <CheckboxGroup options={plainOptions} field="checkbox" />
- </Col>
- <Col span={6}>
- <CheckboxGroup
- field="role"
- rules={[
- { required: true }
- ]}
- >
- <Checkbox value="admin">admin</Checkbox>
- <Checkbox value="user">user</Checkbox>
- <Checkbox value="guest">guest</Checkbox>
- <Checkbox value="root">root</Checkbox>
- </CheckboxGroup>
- </Col>
- </Row>
- <Row>
- <Col span={12}>
- <Button type="primary" htmlType="submit">
- Submit
- </Button>
- <Button htmlType="reset">reset</Button>
- <Button onClick={this.changeInput.bind(this)}>change Input</Button>
- <ComponentUsingFormState />
- </Col>
- </Row>
- </Form>
- </>
- );
- }
- }
- export { LayoutDemo, InsetLabelDemo, GroupFormDemo };
|