123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484 |
- /* eslint-disable */
- import React, { useState, useLayoutEffect, Component } from 'react';
- import { Button, Modal, TreeSelect, Row, Col, Avatar, Select as BasicSelect,
- Form,
- useFormState,
- useFormApi,
- useFieldApi,
- useFieldState,
- withFormState,
- withFormApi,
- withField,
- ArrayField,
- AutoComplete,
- Collapse,
- Space,
- TextArea,
- Icon } from '../../../index';
- import { IconPlusCircle, IconMinusCircle, } from '@douyinfe/semi-icons';
- import { ComponentUsingFormState } from '../Hook/hookDemo';
- const { Input, Select, DatePicker, Switch, Slider, CheckboxGroup, Checkbox, RadioGroup, Radio, TimePicker, InputNumber, InputGroup } = Form;
- class ArrayFieldCollapseDemo extends React.Component {
- constructor() {
- super();
- this.state = {
- actions: [],
- };
- }
- renderItems(arrayFields, values) {
- console.log(values);
- return (
- <Collapse keepDOM>
- {JSON.stringify(values)}
- {arrayFields.map(({ field, key, remove }, i) => (
- <Collapse.Panel header="触发条件" itemKey={i.toString()} key={key}>
- <Form.Input
- field={`${field}[name]`}
- label={`特效类型:(${field}.name)`}
- style={{ width: 200, marginRight: 16 }}
- />
- <Button type="danger" onClick={remove} style={{ margin: 16 }}>
- remove
- </Button>
- </Collapse.Panel>
- ))}
- {/* {arrayFields.length && values.actions[0].name ? '2' : '1'} */}
- </Collapse>
- );
- }
- render() {
- let { actions } = this.state;
- return (
- <Form style={{ width: 500 }} labelPosition="left" labelWidth="220px" allowEmpty>
- {({ values }) => (
- <ArrayField field="actions" initValue={actions}>
- {({ add, arrayFields }) => (
- <React.Fragment>
- <Button onClick={add}>Add</Button>
- {this.renderItems(arrayFields, values)}
- </React.Fragment>
- )}
- </ArrayField>
- )}
- </Form>
- );
- }
- }
- // 使用Form Props设置 ArrayField的initValues
- class ArrayFieldWithFormInitValues extends React.Component {
- constructor() {
- super();
- this.state = {
- data: {
- c: 'c',
- effects: [
- { name: 'form-level-init-sugar', time: 'form-level-init-3min', otherKey: 1 },
- { name: 'form-level-init-bacon', time: 'form-level-init-6min', otherKey: 2, key: 343 },
- ],
- },
- flag: true
- };
- this.getFormApi = this.getFormApi.bind(this);
- this.change = this.change.bind(this);
- }
- change = () => {
- let number = this.formApi.getValue('number');
- let newData = Array.from({ length: number }, (v, i) => ({
- name: `${i}-name`,
- time: `${i}-time`
- }));
- console.log(newData);
- this.formApi.setValue('effects', newData);
- }
- getFormApi(formApi) {
- this.formApi = formApi;
- }
- render() {
- return (
- <Form
- onChange={(...v) => console.log('onchange:' + v)}
- onValueChange={(...v) => console.log('onValueChange:' + v)}
- getFormApi={this.getFormApi}
- initValues={this.state.data}
- >
- {/* <Button onClick={this.setValues}>setValues</Button> */}
- <Row>
- <Col span={12}>
- <ArrayField field="effects">
- {({ add, arrayFields }) => (
- <React.Fragment>
- <Button onClick={add} type="primary">Add</Button>
- {
- arrayFields.map(({ field, key, remove }, i) => (
- <div key={key}>
- <Input
- field={`${field}[name]`}
- label={`${field}.name`}
- // initValue="field-level-init-2D"
- />
- <Input
- field={`${field}[time]`}
- label={`${field}.time`}
- />
- <Button type="danger" onClick={remove}>remove</Button>
- </div>
- ))
- }
- </React.Fragment>
- )}
- </ArrayField>
- <Button htmlType="submit">submit</Button>
- <Button htmlType='reset'>Reset</Button>
- <Form.InputNumber field="number" label="期望个数" />
- <Button onClick={this.change}>改变</Button>
- </Col>
- <Col span={12}>
- <ComponentUsingFormState />
- </Col>
- </Row>
- </Form>
- );
- }
- }
- // 使用 ArrayField props initValue来设置初始值
- class ArrayFieldWithInitValue extends React.Component {
- constructor() {
- super();
- this.state = {
- data: {
- effects: [
- { name: 'arrayField-props.initValue-0-name', time: 'arrayField-props.initValue-0-time', otherKey: 1 },
- { name: 'arrayField-props.initValue-1-name', time: 'arrayField-props.initValue-1-time', otherKey: 453 },
- ],
- },
- flag: true
- };
- this.getFormApi = this.getFormApi.bind(this);
- this.change = this.change.bind(this);
- }
- componentDidMount() {
- this.formApi.setValues({
- effects: [
- { name: "脸部贴纸", type: "2D" },
- { name: "前景贴纸", type: "3D" },
- ],
- test: 'fff'
- })
- }
- change = () => {
- let number = this.formApi.getValue('number');
- let newData = Array.from({ length: 2 }, (v, i) => ({
- name: `${i}-name`,
- time: `${i}-time`
- }));
- console.log(newData);
- this.formApi.setValue('effects', newData);
- }
- getFormApi(formApi) {
- this.formApi = formApi;
- }
- render() {
- const { effects } = this.state.data;
- return (
- <Form
- onChange={(...v) => console.log('onchange:' + v)}
- onValueChange={(...v) => console.log('onValueChange:' + v)}
- getFormApi={this.getFormApi}
- >
- {/* <Button onClick={this.setValues}>setValues</Button> */}
- <Row>
- <Col span={12}>
- <ArrayField field="effects">
- {({ add, arrayFields }) => (
- <React.Fragment>
- <Button onClick={add} type="primary">Add</Button>
- {
- arrayFields.map(({ field, key, remove }, i) => (
- <div key={key}>
- <Input
- field={`${field}[name]`}
- label={`${field}.name`}
- // initValue="field-level-init-2D"
- />
- <Input
- field={`${field}[time]`}
- label={`${field}.time`}
- />
- <Button type="danger" onClick={remove}>remove</Button>
- </div>
- ))
- }
- </React.Fragment>
- )}
- </ArrayField>
- <Button htmlType="submit">submit</Button>
- <Button htmlType='reset'>Reset</Button>
- {/* <Form.InputNumber field="number" label="期望个数" /> */}
- {/* <Button onClick={this.change}>改变</Button> */}
- </Col>
- <Col span={12}>
- <ComponentUsingFormState />
- </Col>
- </Row>
- </Form>
- );
- }
- }
- /* basic usage */
- class ArrayFieldDemo extends React.Component {
- constructor() {
- super();
- this.state = {
- data: {
- c: 'c',
- test: [
- // {},
- // {},
- { name: 'sugar', time: '3min' },
- // { name: 'bacon', time: '6min', key: 'c2' },
- { name: 'bacon', time: '6min' },
- ],
- },
- flag: true
- };
- this.getFormApi = this.getFormApi.bind(this);
- }
- change = () => {
- let number = this.formApi.getValue('number');
- let newData = Array.from({ length: number }, (v, i) => ({
- name: `${i}-name`,
- time: `${i}-time`
- }));
- this.formApi.setValue('effects', newData);
- }
- clear = () => {
- // this.formApi.setValues({ number: 3 });
- // this.formApi.setValues({}, { isOverride: true });
- this.formApi.setValues({ number: 3 }, { isOverride: true });
- }
- getFormApi(formApi) {
- this.formApi = formApi;
- }
- render() {
- return (
- <Form
- // onChange={(...v) => console.log(v)}
- onValueChange={(...v) => console.log(v)}
- getFormApi={this.getFormApi}>
- <Row>
- <Col span={12}>
- <ArrayField field="effects" initValue={this.state.data.test}>
- {({ add, arrayFields }) => (
- <React.Fragment>
- <Button onClick={add} type="primary">Add</Button>
- <Button onClick={this.clear} type="primary">Clear by setValues empty Object</Button>
- {
- arrayFields.map(({ field, key, remove }, i) => (
- <div key={key}>
- <Input
- field={`${field}[name]`}
- label={`${field}.name`}
- // initValue='test'
- />
- <Input
- field={`${field}[time]`}
- label={`${field}.time`}
- />
- <Button type="danger" onClick={remove}>remove</Button>
- </div>
- ))
- }
- </React.Fragment>
- )}
- </ArrayField>
- <Form.InputNumber field="number" label="期望个数" />
- <Space>
- <Button onClick={this.change}>改变</Button>
- <Button htmlType="submit">submit</Button>
- <Button htmlType='reset'>Reset</Button>
- </Space>
- </Col>
- <Col span={12}>
- <ComponentUsingFormState />
- </Col>
- </Row>
- </Form>
- );
- }
- }
- // Fix 1409
- class ArrayFieldSetValues extends React.Component {
- constructor() {
- super();
- this.state = {
- data: {
- },
- };
- this.getFormApi = this.getFormApi.bind(this);
- }
- componentDidMount() {
- this.formApi.setValues({
- effects: [
- { name: "Semi D2C", role: "Engineer" },
- { name: "Semi DSM", role: "Designer" },
- ],
- test: 'fff'
- })
- }
- getFormApi(formApi) {
- this.formApi = formApi;
- }
- render() {
- return (
- <Form
- // onChange={(...v) => console.log(v)}
- onValueChange={(...v) => console.log(v)}
- getFormApi={this.getFormApi}>
- <Row>
- <Col span={12}>
- <ArrayField field="effects">
- {({ add, arrayFields }) => (
- <React.Fragment>
- <Button onClick={add} type="primary">Add</Button>
- {
- arrayFields.map(({ field, key, remove }, i) => (
- <div key={key}>
- <Input
- field={`${field}[name]`}
- label={`${field}.name`}
- // initValue='test'
- />
- <Input
- field={`${field}[role]`}
- label={`${field}.role`}
- />
- <Button type="danger" onClick={remove}>remove</Button>
- </div>
- ))
- }
- </React.Fragment>
- )}
- </ArrayField>
- <Form.Input field="test" label="Test" />
- <Space>
- <Button htmlType='reset'>Reset</Button>
- </Space>
- </Col>
- <Col span={12}>
- <ComponentUsingFormState />
- </Col>
- </Row>
- </Form>
- );
- }
- }
- class AsyncSetArrayField extends React.Component {
- constructor() {
- super();
- this.state = {
- data: [
- { name: 'Semi D2C', role: 'Engineer' },
- { name: 'Semi C2D', role: 'Designer' },
- ]
- };
- }
- getFormApi = (formApi) => {
- this.formApi = formApi;
- }
- change = () => {
- let rules = this.formApi.getValue('rules');
- if (!rules) {
- efferulests = [];
- }
- rules.push({ name: `Semi ${new Date().valueOf()}`, role: 'Designer', key: rules.length + 1 });
- setTimeout(() => {
- this.formApi.setValue('rules', rules);
- }, 300);
- }
- render() {
- let { data } = this.state;
- const ComponentUsingFormState = () => {
- const formState = useFormState();
- return (
- <TextArea style={{ marginTop: 10 }} value={JSON.stringify(formState)} />
- );
- };
- return (
- <Form style={{ width: 800 }} labelPosition='left' labelWidth='100px' allowEmpty getFormApi={this.getFormApi}>
- <ArrayField field='rules' initValue={data}>
- {({ add, arrayFields, addWithInitValue }) => (
- <React.Fragment>
- <Button onClick={this.change} theme='light'>change</Button>
- <Button onClick={add} icon={<IconPlusCircle />} theme='light'>Add new line</Button>
- <Button icon={<IconPlusCircle />} onClick={() => {addWithInitValue({ name: `Semi DSM ${arrayFields.length + 1}`, role: 'Designer' });}} style={{ marginLeft: 8 }}>Add new line with init value</Button>
- {
- arrayFields.map(({ field, key, remove }, i) => (
- <div key={key} style={{ width: 1000, display: 'flex' }}>
- <Form.Input
- field={`${field}[name]`}
- label={`${field}.name`}
- style={{ width: 200, marginRight: 16 }}
- >
- </Form.Input>
- <Form.Select
- field={`${field}[role]`}
- label={`${field}.role`}
- style={{ width: 120 }}
- optionList={[
- { label: 'Engineer', value: 'Engineer' },
- { label: 'Designer', value: 'Designer' },
- ]}
- >
- </Form.Select>
- <Button
- type='danger'
- theme='borderless'
- icon={<IconMinusCircle />}
- onClick={remove}
- style={{ margin: 12 }}
- />
- </div>
- ))
- }
- </React.Fragment>
- )}
- </ArrayField>
- <ComponentUsingFormState />
- </Form>
- );
- }
- }
- export { ArrayFieldCollapseDemo, ArrayFieldDemo, ArrayFieldWithFormInitValues, ArrayFieldWithInitValue, ArrayFieldSetValues, AsyncSetArrayField };
|