hookDemo.jsx 6.4 KB

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