setValuesDemo.jsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import React, { useState, useLayoutEffect } from 'react';
  2. import { storiesOf } from '@storybook/react';
  3. import { Button, Modal, TreeSelect, Row, Col, Avatar, TextArea, Select as BasicSelect,
  4. Form,
  5. useFormState,
  6. useFormApi,
  7. useFieldApi,
  8. useFieldState,
  9. withFormState,
  10. withFormApi,
  11. withField,
  12. ArrayField,
  13. AutoComplete,
  14. Collapse,
  15. Icon,
  16. } from '../../../index';
  17. import { ComponentUsingFormState, CustomStringify } from '../Hook/hookDemo';
  18. const { Input, Select, DatePicker, Switch, Slider, CheckboxGroup, Checkbox, RadioGroup, Radio, TimePicker, InputNumber, InputGroup } = Form;
  19. class SetValuesDemo extends React.Component {
  20. constructor() {
  21. super();
  22. this.formApi = null;
  23. this.state = {
  24. flag: false,
  25. };
  26. this.getFormApi = this.getFormApi.bind(this);
  27. this.change = this.change.bind(this);
  28. }
  29. change() {
  30. let a = this.formApi.getFieldExist('ffff');
  31. let b = this.formApi.getFieldExist('name');
  32. this.formApi.setValues({ name: 'nike', notExist: '12' }, { isOverride: true });
  33. this.setState({ flag: true });
  34. }
  35. getFormApi(formApi) {
  36. this.formApi = formApi;
  37. }
  38. render() {
  39. const { flag } = this.state;
  40. return (
  41. <Form getFormApi={this.getFormApi} onChange={v => console.log(v)} onSubmit={v => console.log(v)}>
  42. {({ formState }) => (
  43. <>
  44. <Input field="name" initValue=""></Input>
  45. <Select field="familyName" multiple style={{ width: 200 }}>
  46. <Select.Option value="mike">Mike</Select.Option>
  47. <Select.Option value="jane">Jane</Select.Option>
  48. <Select.Option value="kate">Kate</Select.Option>
  49. </Select>
  50. {/* { flag ? <Input field='notExist' /> : null} */}
  51. <Checkbox value="false" field="agree" noLabel={true}>
  52. 我已阅读并清楚相关规定(Checkbox)
  53. </Checkbox>
  54. <Button onClick={this.change}>change</Button>
  55. <Button htmlType="submit">Submit</Button>
  56. <code>{CustomStringify(formState)}</code>
  57. </>
  58. )}
  59. </Form>
  60. );
  61. }
  62. }
  63. class SetValuesWithArrayField extends React.Component {
  64. constructor() {
  65. super();
  66. this.formApi = null;
  67. this.state = {
  68. flag: false,
  69. };
  70. this.getFormApi = this.getFormApi.bind(this);
  71. this.change = this.change.bind(this);
  72. }
  73. change() {
  74. let length = Math.floor(Math.random() * 100);
  75. let newEffect = Array.from({ length }, (v, i) => {
  76. return { name: Math.random(), type: Math.random() };
  77. });
  78. this.formApi.setValues({ name: 'nike', notExist: '12', effects: newEffect }, { isOverride: true });
  79. }
  80. getFormApi(formApi) {
  81. this.formApi = formApi;
  82. }
  83. render() {
  84. const { flag } = this.state;
  85. return (
  86. <Form getFormApi={this.getFormApi} onValueChange={v => console.log(v)} onSubmit={v => console.log(v)}>
  87. {({ formState }) => (
  88. <>
  89. <ArrayField field='effects' initValue={[]}>
  90. {({ add, arrayFields }) => (
  91. <React.Fragment>
  92. <Button onClick={add}>Add</Button>
  93. {
  94. arrayFields.map(({ field, key, remove }, i) => (
  95. <div key={key} style={{ width: 1000, display: 'flex' }}>
  96. <Form.Input
  97. field={`${field}[name]`}
  98. label={`名称:(${field}.name)`}
  99. style={{ width: 200, marginRight: 16 }}
  100. >
  101. </Form.Input>
  102. <Form.Input
  103. field={`${field}[type]`}
  104. label={`类型:(${field}.type)`}
  105. style={{ width: 90 }}
  106. >
  107. </Form.Input>
  108. <Button type='danger' onClick={remove} style={{ margin: 16 }}>remove</Button>
  109. </div>
  110. ))
  111. }
  112. </React.Fragment>
  113. )}
  114. </ArrayField>
  115. <Button onClick={this.change}>change</Button>
  116. <Button htmlType="submit">Submit</Button>
  117. <TextArea value={CustomStringify(formState)}></TextArea>
  118. </>
  119. )}
  120. </Form>
  121. );
  122. }
  123. }
  124. export { SetValuesDemo, SetValuesWithArrayField };