bugDemo.jsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* eslint-disable */
  2. import React, { useState, useLayoutEffect, Component, useEffect } from 'react';
  3. import { storiesOf } from '@storybook/react';
  4. import { Button, Modal, TreeSelect, Row, Col, Avatar, TextArea, Input as BasicInput, Select as BasicSelect,
  5. Form,
  6. useFormState,
  7. useFormApi,
  8. useFieldApi,
  9. useFieldState,
  10. withFormState,
  11. withFormApi,
  12. withField,
  13. ArrayField,
  14. AutoComplete,
  15. Collapse,
  16. Icon } from '../../../index';
  17. import { ComponentUsingFormState } from '../Hook/hookDemo';
  18. const { Input, Select, DatePicker, Switch, Slider, CheckboxGroup, Checkbox, RadioGroup, Radio, TimePicker, InputNumber, InputGroup } = Form;
  19. class Test extends React.Component {
  20. constructor() {
  21. super();
  22. console.log(this.props);
  23. debugger;
  24. }
  25. componentWillReceiveProps(prevProps) {
  26. console.log(this.props);
  27. debugger;
  28. }
  29. componentDidMount() {
  30. console.log(this.props);
  31. debugger;
  32. }
  33. componentWillUnmount() {
  34. console.log(this.props);
  35. debugger;
  36. }
  37. render() {
  38. return this.props.value;
  39. }
  40. }
  41. const FCC = (() => () => {
  42. useEffect(() => {
  43. // effect
  44. debugger;
  45. return () => {
  46. debugger;
  47. };
  48. }, []);
  49. return <div>t</div>;
  50. })();
  51. class SetValuesArray extends React.Component {
  52. constructor() {
  53. super();
  54. this.state = {
  55. initValues: {
  56. effects: [
  57. { name: 'A', key: '12312' },
  58. { name: 'B', key: '234234' },
  59. { name: 'C', key: '345435' },
  60. ]
  61. }
  62. };
  63. this.id = 3;
  64. this.getFormApi = this.getFormApi.bind(this);
  65. this.add = this.add.bind(this);
  66. this.remove = this.remove.bind(this);
  67. this.renderItems = this.renderItems.bind(this);
  68. const iii = Form.Input;
  69. }
  70. getFormApi(formApi) {
  71. this.formApi = formApi;
  72. }
  73. add(obj) {
  74. let effects = this.formApi.getValue('effects');
  75. if (!effects) {
  76. effects = [];
  77. }
  78. effects.push({ name: '', type: '', key: Math.random() });
  79. // effects.push({ name: '', type: '', key: this.id++ });
  80. // this.formApi.setValue('effects', effects);
  81. this.formApi.setValues({
  82. ...this.formApi.getValue(),
  83. effects
  84. }, { isOverride: true });
  85. }
  86. remove(key) {
  87. let effects = this.formApi.getValue('effects');
  88. effects = effects.filter((effect, index) => key !== effect.key);
  89. if (!effects.length) {
  90. effects = undefined;
  91. }
  92. // this.formApi.setValue('effects', effects);
  93. this.formApi.setValues({
  94. ...this.formApi.getValue(),
  95. effects
  96. }, { isOverride: true });
  97. }
  98. renderItems(formState, values) {
  99. console.log(values);
  100. return values.effects && values.effects.map((effect, i) => (
  101. // <div style={{ width: 1000, display: 'flex' }}>
  102. <div style={{ width: 1000, display: 'flex' }} key={effect.key}>
  103. {/* <Test value={`effects[${i}].name`} /> */}
  104. {/* <FCC value={`effects[${i}].name`} /> */}
  105. {/* <BasicInput props={`effects[${i}].name`} /> */}
  106. {/* <Button type='danger' onClick={() => this.remove(effect.key)} style={{ margin: 12 }}>Remove</Button> */}
  107. <Form.Input field={`effects[${i}].name`} style={{ width: 200, marginRight: 12 }} key={`effects[${i}].name`} />
  108. {/* <Form.Input field={`effects[${i}].name`} style={{width: 200, marginRight: 12}} key={effect.key}></Form.Input> */}
  109. <Button type="danger" onClick={() => this.remove(effect.key)} style={{ margin: 12 }}>Remove</Button>
  110. <span>{effect.key}</span>
  111. </div>
  112. ));
  113. }
  114. render() {
  115. let { initValues } = this.state;
  116. return (
  117. <>
  118. <dd />
  119. <Form
  120. getFormApi={this.getFormApi}
  121. initValues={initValues}
  122. style={{ width: 500 }}
  123. labelPosition="left"
  124. labelWidth="180px"
  125. >
  126. {({ formState, values }) => (
  127. <>
  128. <Button onClick={this.add}>add</Button>
  129. {this.renderItems(formState, values)}
  130. <TextArea style={{ marginTop: 10 }} value={JSON.stringify(formState.values)} />
  131. </>
  132. )}
  133. </Form>
  134. <div className="test">will be render by react</div>
  135. </>
  136. );
  137. }
  138. }
  139. class FieldPathWithArrayDemo extends React.Component {
  140. constructor(props) {
  141. super();
  142. this.state = {
  143. data: {
  144. value: [
  145. { name: 'kkk' },
  146. { name: 'ccc' },
  147. { name: 'ddd' },
  148. ]
  149. },
  150. regionLoad: 0
  151. };
  152. this.change = this.change.bind(this);
  153. }
  154. change() {
  155. let { regionLoad } = this.state;
  156. if (regionLoad !== 0) {
  157. this.setState({ regionLoad: 0 });
  158. } else {
  159. this.setState({ regionLoad: 1 });
  160. }
  161. }
  162. render() {
  163. const { data, regionLoad } = this.state;
  164. return (
  165. <Form
  166. initValues={data}
  167. onChange={formState => console.log(formState)}
  168. onValueChange={formState => console.log(formState)}
  169. style={{ padding: '10px', width: 600 }}>
  170. {
  171. regionLoad === 0 ? (
  172. <Form.Input
  173. field="value[0].name"
  174. />
  175. ) : null
  176. }
  177. <Button onClick={this.change}>change</Button>
  178. </Form>
  179. );
  180. }
  181. }
  182. const Twice = () => {
  183. // 或者把useFormState去掉
  184. // const { values } = useFormState();
  185. console.log('render');
  186. return (
  187. <Form.Input
  188. field="test"
  189. // 把rules去掉就不会render两次
  190. rules={[{ required: true, message: '版本号不能为空' }]}
  191. />
  192. );
  193. };
  194. class DoubleRerender extends React.Component {
  195. render() {
  196. return (
  197. <Form>
  198. <Twice />
  199. </Form>
  200. );
  201. }
  202. }
  203. export { SetValuesArray, FieldPathWithArrayDemo, DoubleRerender };