hookDemo.jsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import React, { useState, useLayoutEffect, Component } from 'react';
  2. import { storiesOf } from '@storybook/react';
  3. import { Button, Modal, TreeSelect, Row, Col, Avatar } from '../../../index';
  4. import { Select as BasicSelect } from '../../../index';
  5. import {
  6. Form,
  7. useFormState,
  8. useFormApi,
  9. useFieldApi,
  10. useFieldState,
  11. withFormState,
  12. withFormApi,
  13. withField,
  14. ArrayField,
  15. AutoComplete,
  16. Collapse,
  17. Icon,
  18. } from '../../../index';
  19. const { Input, Select, DatePicker, Switch, Slider, CheckboxGroup, Checkbox, RadioGroup, Radio, TimePicker, InputNumber, InputGroup } = Form;
  20. const UseFormApiDemo = () => {
  21. return (
  22. <Form>
  23. <Form.Input field="name" initValue="mike"></Form.Input>
  24. <ArrayField field="arrays" initValue={[]}>
  25. {({ add, arrayFields }) => (
  26. <React.Fragment>
  27. <Button onClick={add}>Add</Button>
  28. {arrayFields.map(({ field, key, remove }, i) => (
  29. <div key={key} style={{ width: 1000, display: 'flex' }}>
  30. <Form.Input
  31. field={`${field}[name]`}
  32. label={`name:(${field}.name)`}
  33. style={{ width: 200, marginRight: 16 }}
  34. ></Form.Input>
  35. <Form.Input
  36. field={`${field}[type]`}
  37. label={`type:(${field}.type`}
  38. style={{ width: 200, marginRight: 16 }}
  39. ></Form.Input>
  40. {/* <Form.Select
  41. field={`${field}[type]`}
  42. label={`素材类型:(${field}.type)`}
  43. style={{width: 90}}
  44. >
  45. <Form.Select.Option value='2D'>2D</Form.Select.Option>
  46. <Form.Select.Option value='3D'>3D</Form.Select.Option>
  47. </Form.Select> */}
  48. <Button type="danger" onClick={remove} style={{ margin: 16 }}>
  49. remove
  50. </Button>
  51. </div>
  52. ))}
  53. </React.Fragment>
  54. )}
  55. </ArrayField>
  56. <ComponentUsingFormApi />
  57. <ComponentUsingFormState />
  58. </Form>
  59. );
  60. };
  61. const CustomStringify = values => {
  62. return JSON.stringify(values, (k, v) => (v === undefined ? '__undefined' : v)).replace(
  63. '"__undefined"',
  64. 'undefined'
  65. );
  66. };
  67. const ComponentUsingFormApi = () => {
  68. const formApi = useFormApi();
  69. const [count, setCount] = useState(0);
  70. const change = () => {
  71. let value0 = {
  72. name: 'mikeyyaya',
  73. cc: 'adele',
  74. arrays: [
  75. { name: 1, type: '1' },
  76. { name: 2, type: '2' },
  77. ],
  78. };
  79. formApi.setValues(value0, { isOverride: true });
  80. };
  81. const change2 = () => {
  82. let value2 = {
  83. // name: '2',
  84. // cc: 'mike',
  85. arrays: [],
  86. };
  87. formApi.setValues(value2, { isOverride: true });
  88. };
  89. return (
  90. <>
  91. <Button onClick={change}>ChangeName By【formApi】</Button>
  92. <Button onClick={change2}>Change 2</Button>
  93. </>
  94. );
  95. };
  96. /** */
  97. const ComponentUsingFormState = () => {
  98. const formState = useFormState();
  99. return (
  100. <pre>
  101. <code style={{wordBreak:'break-all', width: 600, whiteSpace: 'normal'}}>{JSON.stringify(formState)}</code>
  102. {/* <code style={{wordBreak:'break-all', width: 600, whiteSpace: 'normal'}}>{CustomStringify(formState)}</code> */}
  103. </pre>
  104. );
  105. };
  106. const UseFormStateDemo = () => {
  107. return (
  108. <Form>
  109. <Input field="name" initValue="nike" />
  110. <h5>FormState read by 【useFormState】:</h5>
  111. <ComponentUsingFormState />
  112. </Form>
  113. );
  114. };
  115. const ComponentUsingFieldApi = () => {
  116. const nameFieldApi = useFieldApi('name');
  117. const change = () => {
  118. nameFieldApi.setValue(Math.random());
  119. };
  120. return <Button onClick={change}>changeNameBy【fieldApi】</Button>;
  121. };
  122. const UseFieldApiDemo = () => {
  123. return (
  124. <Form>
  125. <Form.Input field="name"></Form.Input>
  126. <ComponentUsingFieldApi />
  127. </Form>
  128. );
  129. };
  130. const ComponentUsingFieldState = props => {
  131. const fieldState = useFieldState(props.field);
  132. return (
  133. <pre>
  134. <code>{CustomStringify(fieldState)}</code>
  135. </pre>
  136. );
  137. };
  138. const UseFieldStateDemo = () => {
  139. return (
  140. <Form>
  141. <Form.Input field="name" initValue="mike"></Form.Input>
  142. <h5> 【name】FieldState read by 【useFieldState】:</h5>
  143. <ComponentUsingFieldState field="name" />
  144. <Form.Input field="country" initValue="china"></Form.Input>
  145. <h5> 【country】FieldState read by 【useFieldState】:</h5>
  146. <ComponentUsingFieldState field="country" />
  147. </Form>
  148. );
  149. };
  150. const WithFormStateDemo = () => {
  151. const SomeComponent = props => <code>{CustomStringify(props.formState)}</code>;
  152. const ComponentWithFormState = withFormState(SomeComponent);
  153. return (
  154. <Form>
  155. <Form.Input field="name" initValue="steve"></Form.Input>
  156. <Form.Input field="familyName" initValue="jobs"></Form.Input>
  157. <Button htmlType="submit">submit</Button>
  158. <ComponentWithFormState />
  159. </Form>
  160. );
  161. };
  162. const WithFormApiDemo = () => {
  163. const SomeComponet = props => (
  164. <Button
  165. onClick={() => {
  166. props.formApi.setValue('name', Math.random());
  167. }}
  168. >
  169. ChangeName By【formApi】
  170. </Button>
  171. );
  172. const ComponentWithFormApi = withFormApi(SomeComponet);
  173. return (
  174. <Form>
  175. <Input field="name" initValue="mike"></Input>
  176. <Button htmlType="submit">submit</Button>
  177. <ComponentWithFormApi />
  178. </Form>
  179. );
  180. };
  181. export { UseFormApiDemo, UseFormStateDemo, UseFieldApiDemo, UseFieldStateDemo, WithFormStateDemo, WithFormApiDemo, ComponentUsingFormState, CustomStringify }