form.stories.jsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. import React, { useState, useLayoutEffect, useEffect, useRef } from 'react';
  2. import {
  3. Button,
  4. } from '../../index';
  5. import {
  6. Form,
  7. } from '../../index';
  8. import { BasicDemoWithInit, LinkFieldForm, DifferentDeclareUsage } from './demo';
  9. const {
  10. Input,
  11. Select,
  12. DatePicker,
  13. Switch,
  14. Slider,
  15. CheckboxGroup,
  16. Checkbox,
  17. RadioGroup,
  18. Radio,
  19. TimePicker,
  20. } = Form;
  21. import {
  22. UseFormApiDemo,
  23. UseFormStateDemo,
  24. UseFieldApiDemo,
  25. UseFieldStateDemo,
  26. WithFormStateDemo,
  27. WithFormApiDemo,
  28. ComponentUsingFormState,
  29. CustomStringify,
  30. } from './Hook/hookDemo';
  31. // layout
  32. import { LayoutDemo, InsetLabelDemo } from './Layout/layoutDemo';
  33. import { AssistComponent } from './Layout/slotDemo';
  34. import { ModalFormDemo } from './Layout/modalFormDemo';
  35. import { WithFieldDemo, CustomFieldDemo, NumberRange } from './HOC/withFieldDemo';
  36. import {
  37. CustomValidateDemo,
  38. ValidateFieldsDemo,
  39. PartValidAndResetDemo,
  40. RulesValidateDemo,
  41. SetBugDemo,
  42. UnmountedLeafDemo,
  43. RulesExample,
  44. RaceAsyncDemo,
  45. } from './Validate/validateDemo';
  46. import { TriggerDemo } from './Validate/TriggerAndStopValidateWithError';
  47. // field props
  48. import { ConvertDemo } from './FieldProps/convert';
  49. import { NameDemo } from './FieldProps/name';
  50. import { HelpAndExtra, ExtraPositionDemo } from './FieldProps/helpAndExtra';
  51. import { BigNumberFieldDemo } from './FieldProps/bigNumberFieldPath';
  52. import { UpdateDemo, RuleupdateDemo } from './FieldProps/rulesUpdateDemo';
  53. import { FieldRefDemo } from './FieldProps/fieldRef';
  54. import { LableOptionalDemo } from './FieldProps/labelOptional';
  55. // form inputGroup
  56. import { InputGroupDemo } from './InputGroup/groupProps';
  57. // arrayField
  58. import {
  59. ArrayFieldCollapseDemo,
  60. ArrayFieldDemo,
  61. ArrayFieldWithFormInitValues,
  62. ArrayFieldWithInitValue,
  63. ArrayFieldSetValues,
  64. } from './DynamicField/arrayFieldDemo';
  65. import { NestArrayField } from './DynamicField/nestArrayField';
  66. import { ArrayDemo } from './FormApi/arrayDemo';
  67. // performance
  68. import {
  69. ManyFieldDemo,
  70. EffectDemo,
  71. ModalFormSelectWithObject,
  72. } from './Performance/performanceDemo';
  73. import { SetValuesDemo, SetValuesWithArrayField } from './FormApi/setValuesDemo';
  74. import { SetValueUsingParentPath } from './FormApi/formApiDemo';
  75. import { FieldPathWithArrayDemo } from './Debug/bugDemo';
  76. import ChildDidMount from './Debug/childDidMount';
  77. export { default as FormSubmit } from './FormSubmit';
  78. export { default as TabelForm } from './TableDemo';
  79. export default {
  80. title: 'Form'
  81. }
  82. const Option = Select.Option;
  83. export const FormDeclareUsage = () => <DifferentDeclareUsage />;
  84. FormDeclareUsage.story = {
  85. name: 'Form declare usage',
  86. };
  87. export const BasicDemo = () => <BasicDemoWithInit />;
  88. BasicDemo.story = {
  89. name: 'BasicDemo',
  90. };
  91. export const LayoutFormWrapperColLabelCol = () => <LayoutDemo />;
  92. LayoutFormWrapperColLabelCol.story = {
  93. name: 'Layout-Form wrapperCol/labelCol',
  94. };
  95. export const LayoutInsetLabel = () => <InsetLabelDemo />;
  96. LayoutInsetLabel.story = {
  97. name: 'Layout-insetLabel',
  98. };
  99. export const LableOptional = () => <LableOptionalDemo></LableOptionalDemo>;
  100. LableOptional.story = {
  101. name: 'Layout-label show optional',
  102. };
  103. export const LayoutSlotErrorMessageLabel = () => <AssistComponent />;
  104. LayoutSlotErrorMessageLabel.story = {
  105. name: 'Layout-Slot/ErrorMessage/Label',
  106. };
  107. export const LayoutModalDemo = () => <ModalFormDemo />;
  108. LayoutModalDemo.story = {
  109. name: 'Layout- ModalDemo',
  110. };
  111. export const FormApiSetValuesOverride = () => <SetValuesDemo />;
  112. FormApiSetValuesOverride.story = {
  113. name: 'formApi-setValues(override)',
  114. };
  115. export const FormApiValidate = () => <PartValidAndResetDemo />;
  116. FormApiValidate.story = {
  117. name: 'formApi-validate',
  118. };
  119. export const FormApiSetValueUsingFieldParentPath = () => <SetValueUsingParentPath />;
  120. FormApiSetValueUsingFieldParentPath.story = {
  121. name: 'formApi-setValue using field parent path',
  122. };
  123. export const UseFormApiSetValueUpdateArray = () => <ArrayDemo />;
  124. UseFormApiSetValueUpdateArray.story = {
  125. name: 'formApi-setValue set array',
  126. };
  127. export const DynamicAddRemoveField = () => (
  128. <Form>
  129. {({ formState }) => (
  130. <React.Fragment>
  131. <Input field="name" label="First name:" />
  132. <RadioGroup field="married" label="Are you married?">
  133. <Radio value="yes">yes</Radio>
  134. <Radio value="no">no</Radio>
  135. </RadioGroup>
  136. {formState.values.married === 'yes' ? <Input field="spouse" label="Spouse name:" /> : null}
  137. <Button htmlType="submit">Submit</Button>
  138. <ComponentUsingFormState />
  139. </React.Fragment>
  140. )}
  141. </Form>
  142. );
  143. DynamicAddRemoveField.story = {
  144. name: 'Dynamic Add / Remove Field',
  145. };
  146. export const ArrayFieldBasicUsage = () => <ArrayFieldDemo />;
  147. ArrayFieldBasicUsage.story = {
  148. name: 'ArrayField-basic usage',
  149. };
  150. export const _ArrayFieldWithFormInitValues = () => <ArrayFieldWithFormInitValues />;
  151. _ArrayFieldWithFormInitValues.story = {
  152. name: 'ArrayField-with form initValues',
  153. };
  154. export const ArrayFieldWithArrayFieldInitValue = () => <ArrayFieldWithInitValue />;
  155. ArrayFieldWithArrayFieldInitValue.story = {
  156. name: 'ArrayField-with arrayField initValue',
  157. };
  158. export const ArrayFieldSetValuesDemo = () => <ArrayFieldSetValues />;
  159. ArrayFieldSetValuesDemo.story ={
  160. name: 'ArrayField-setValues didMount'
  161. }
  162. export const ArrayFieldNestUsage = () => <NestArrayField />;
  163. ArrayFieldNestUsage.story = {
  164. name: 'ArrayField-Nest Usage',
  165. };
  166. export const _ArrayFieldCollapseDemo = () => <ArrayFieldCollapseDemo />;
  167. _ArrayFieldCollapseDemo.story = {
  168. name: 'ArrayField-CollapseDemo',
  169. };
  170. export const LinkField = () => <LinkFieldForm />;
  171. LinkField.story = {
  172. name: 'LinkField',
  173. };
  174. export const ValidateFormLevel = () => <ValidateFieldsDemo />;
  175. ValidateFormLevel.story = {
  176. name: 'Validate-FormLevel',
  177. };
  178. export const ValidateFieldValel = () => <CustomValidateDemo />;
  179. ValidateFieldValel.story = {
  180. name: 'Validate-FieldValel',
  181. };
  182. export const ValidateUseRules = () => <RulesValidateDemo />;
  183. ValidateUseRules.story = {
  184. name: 'Validate-use rules',
  185. };
  186. export const RaceAsync = () => <RaceAsyncDemo />;
  187. RaceAsyncDemo.story = {
  188. name: 'Validate - race async'
  189. }
  190. export const Trigger = () => <TriggerDemo></TriggerDemo>;
  191. export const HooksUseFormApi = () => <UseFormApiDemo />;
  192. HooksUseFormApi.story = {
  193. name: 'Hooks-useFormApi',
  194. };
  195. export const HooksUseFormState = () => <UseFormStateDemo />;
  196. HooksUseFormState.story = {
  197. name: 'Hooks-useFormState',
  198. };
  199. export const HooksUseFieldApi = () => <UseFieldApiDemo />;
  200. HooksUseFieldApi.story = {
  201. name: 'Hooks-useFieldApi',
  202. };
  203. export const HooksUseFieldState = () => <UseFieldStateDemo />;
  204. HooksUseFieldState.story = {
  205. name: 'Hooks-useFieldState',
  206. };
  207. export const HocWithFormApi = () => <WithFormApiDemo />;
  208. HocWithFormApi.story = {
  209. name: 'Hoc-withFormApi',
  210. };
  211. export const HocWithFormState = () => <WithFormStateDemo />;
  212. HocWithFormState.story = {
  213. name: 'Hoc-withFormState',
  214. };
  215. export const WithField = () => <CustomFieldDemo />;
  216. WithField.story = {
  217. name: 'withField',
  218. };
  219. export const WithFieldWithStatefulComponent = () => <WithFieldDemo />;
  220. WithFieldWithStatefulComponent.story = {
  221. name: 'withField- with stateful component',
  222. };
  223. export const WithFieldNumberRange = () => (
  224. <Form onChange={v => console.log(v)}>
  225. <NumberRange field="number" initValue={[1, 2]} noLabel={true}></NumberRange>
  226. </Form>
  227. );
  228. WithFieldNumberRange.story = {
  229. name: 'withField - NumberRange',
  230. };
  231. export const PerformanceManyField = () => <ManyFieldDemo />;
  232. PerformanceManyField.story = {
  233. name: 'Performance-ManyField',
  234. };
  235. export const PerformanceModalFormSelectWithObject = () => <ModalFormSelectWithObject />;
  236. PerformanceModalFormSelectWithObject.story = {
  237. name: 'Performance-ModalFormSelectWithObject',
  238. };
  239. export const FiledPropConvert = () => <ConvertDemo />;
  240. FiledPropConvert.story = {
  241. name: 'Filed Prop-convert',
  242. };
  243. export const FieldPropsName = () => <NameDemo />;
  244. FieldPropsName.story = {
  245. name: 'Filed Prop-name',
  246. };
  247. export const FiledPropHelpTextExtraTextExtraTextPosition = () => (
  248. <>
  249. <HelpAndExtra />
  250. <ExtraPositionDemo />
  251. </>
  252. );
  253. FiledPropHelpTextExtraTextExtraTextPosition.story = {
  254. name: 'Filed Prop-helpText / extraText / extraTextPosition',
  255. };
  256. export const FieldPropsDynamicUpdateRulesRequired = () => <RuleupdateDemo />;
  257. FieldPropsDynamicUpdateRulesRequired.story = {
  258. name: 'Field Props-dynamic update rules.required',
  259. };
  260. export const FieldPropInitValue = () => <InitEmptyStringDemo />;
  261. FieldPropInitValue.story = {
  262. name: 'Field Prop-initValue=""',
  263. };
  264. export const FieldPropBigNumberFieldSubmit = () => <BigNumberFieldDemo />;
  265. FieldPropBigNumberFieldSubmit.story = {
  266. name: 'Field prop-big number field submit',
  267. };
  268. export const FieldPropPure = () => (
  269. <Form>
  270. <Form.Select
  271. field="name"
  272. pure
  273. className="fefefe"
  274. fieldClassName="feichang"
  275. style={{ width: 400 }}
  276. />
  277. </Form>
  278. );
  279. FieldPropPure.story = {
  280. name: 'Field Prop-pure',
  281. };
  282. export const FieldPropRef = () => <FieldRefDemo />;
  283. FieldPropRef.story = {
  284. name: 'Field Prop-ref',
  285. };
  286. export const GroupProp = () => <InputGroupDemo />
  287. GroupProp.story = {
  288. name: 'InputGroup Prop - basic'
  289. };
  290. const InitEmptyStringDemo = () => {
  291. return (
  292. <Form allowEmpty>
  293. <Form.Input field="name" initValue="" />
  294. <ComponentUsingFormState />
  295. </Form>
  296. );
  297. };
  298. export const DebugSetBugDemo = () => (
  299. <>
  300. <SetBugDemo />
  301. <UnmountedLeafDemo />
  302. </>
  303. );
  304. DebugSetBugDemo.story = {
  305. name: 'Debug-SetBugDemo',
  306. };
  307. export const DebugSetValuesWithArrayField = () => (
  308. <>
  309. <SetValuesWithArrayField />
  310. </>
  311. );
  312. DebugSetValuesWithArrayField.story = {
  313. name: 'Debug-SetValuesWithArrayField',
  314. };
  315. import { SetValuesArray, DoubleRerender } from './Debug/bugDemo';
  316. export const DebugArrayFieldPath = () => <FieldPathWithArrayDemo />;
  317. DebugArrayFieldPath.story = {
  318. name: 'Debug-数组类fieldPath',
  319. };
  320. export const DebugSetValuesDemo = () => (
  321. <>
  322. <SetValuesArray />
  323. </>
  324. );
  325. DebugSetValuesDemo.story = {
  326. name: 'Debug-SetValuesDemo',
  327. };
  328. export const DebugRerenderTwice = () => (
  329. <>
  330. <DoubleRerender />
  331. </>
  332. );
  333. DebugRerenderTwice.story = {
  334. name: 'Debug-RerenderTwice',
  335. };
  336. export const _ChildDidMount = () => <ChildDidMount />;
  337. _ChildDidMount.story = {
  338. name: 'child did mount',
  339. };