form.stories.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import React, { FunctionComponent } from 'react';
  2. import { storiesOf } from '@storybook/react';
  3. import { Form, useFormState, useFormApi, withField, Input, Button, Upload } from '../../index';
  4. import { values } from 'lodash';
  5. const stories = storiesOf('Form', module);
  6. import { FormApi, FormFCChild } from '../interface';
  7. const treeData = [
  8. {
  9. label: '浙江省',
  10. value: 'zhejiang',
  11. key: '0',
  12. children: [
  13. {
  14. label: '杭州市',
  15. value: 'hangzhou',
  16. key: '0-0',
  17. children: [
  18. {
  19. label: '西湖区',
  20. value: 'xihu',
  21. key: '0-0-0',
  22. },
  23. {
  24. label: '萧山区',
  25. value: 'xiaoshan',
  26. key: '0-0-1',
  27. },
  28. {
  29. label: '临安区',
  30. value: 'linan',
  31. key: '0-0-2',
  32. },
  33. ],
  34. },
  35. {
  36. label: '宁波市',
  37. value: 'ningbo',
  38. key: '0-1',
  39. children: [
  40. {
  41. label: '海曙区',
  42. value: 'haishu',
  43. key: '0-1-0',
  44. },
  45. {
  46. label: '江北区',
  47. value: 'jiangbei',
  48. key: '0-1-1',
  49. },
  50. ],
  51. },
  52. ],
  53. },
  54. ];
  55. const htmlInput = (props: any) => {
  56. let value = props.value || '';
  57. delete props.validateStatus; // prevent props being transparently transmitted to DOM
  58. return <input {...props} value={value} />;
  59. };
  60. const FieldB = withField(Input);
  61. const FieldA = withField(htmlInput, { valueKey: 'value', onKeyChangeFnName: 'onChange', valuePath: 'target.value' });
  62. const Fields: FunctionComponent<FormFCChild> = ({ formState, values, formApi }) => {
  63. const ref = React.useRef();
  64. return (
  65. <>
  66. <Form.Rating field='test' className='fe' count={2} ref={ref} />
  67. <Form.Input field='test' ref={ref} />
  68. <Input size='default' showClear insetLabel />
  69. <FieldB insetLabel placeholder='fe' fieldClassName='fefe' field='custom' />
  70. <Button onClick={() => formApi.setValue('fieldA', 'fe')}>set</Button>
  71. <Form.Select field='test' ref={ref}>
  72. <Form.Select.Option value="f1"></Form.Select.Option>
  73. <Form.Select.Option value="f2"></Form.Select.Option>
  74. </Form.Select>
  75. <Form.Input field="UserName" label="用户名" ref={ref} />
  76. <Form.TextArea field="textarea" onKeyDown={(v: any) => console.log(v)} ref={ref} />
  77. <Form.Input field="Password" label="密码" />
  78. <Form.InputNumber field="number" ref={ref} />
  79. <Form.Rating field="rating" />
  80. <Form.Switch field="switch" checkedText="on" uncheckedText="off" ref={ref} />
  81. <Form.Cascader
  82. placeholder="请选择所在地区"
  83. field="area"
  84. ref={ref}
  85. label={{ text: '123', required: true, extra: 123 }}
  86. treeData={treeData}
  87. ></Form.Cascader>
  88. <Form.TimePicker field="time" minuteStep={2} ref={ref} />
  89. <Form.AutoComplete field="fe" />
  90. <Form.TreeSelect field="treeSelect" treeData={treeData} ref={ref} />
  91. <Form.Slider field="slider" ref={ref} />
  92. <Form.DatePicker field="datepicker" ref={ref} />
  93. <Form.CheckboxGroup
  94. field="type"
  95. ref={ref}
  96. label="申请类型(CheckboxGroup)"
  97. initValue={['user', 'admin']}
  98. rules={[{ required: true }]}
  99. >
  100. <Form.Checkbox value="admin">admin</Form.Checkbox>
  101. <Form.Checkbox value="user">user</Form.Checkbox>
  102. <Form.Checkbox value="guest">guest</Form.Checkbox>
  103. <Form.Checkbox value="root">root</Form.Checkbox>
  104. </Form.CheckboxGroup>
  105. <Form.RadioGroup
  106. field="radio"
  107. ref={ref}
  108. label="是否独占资源(Radio)"
  109. rules={[{ type: 'boolean' }, { required: true, message: '必须选择是否独占 ' }]}
  110. >
  111. <Form.Radio value={1}>是</Form.Radio>
  112. <Form.Radio value={0}>否</Form.Radio>
  113. </Form.RadioGroup>
  114. <Form.Checkbox field='abc' noLabel>
  115. 我已阅读并清楚相关规定(Checkbox)
  116. </Form.Checkbox>
  117. <Form.Label text="fe" required />
  118. <Form.ErrorMessage error="errorText" />
  119. <FieldB field="custom" />
  120. <FieldA field="cuB" />
  121. <Form.TagInput field='tagInput' ref={ref} ></Form.TagInput>
  122. <Form.Upload ref={ref} fileName='semi' action='https://test.com' field='file' />
  123. <Form.Slot />
  124. <code style={{ marginTop: 30 }}>{JSON.stringify(formState)}</code>
  125. </>
  126. )
  127. };
  128. stories.add('Form', () => <Form>{Fields}</Form>);
  129. interface IProps {
  130. [x:string]: any;
  131. }
  132. interface IState {
  133. visible: boolean;
  134. }
  135. interface FData {
  136. test: boolean;
  137. test2: boolean;
  138. test3: string;
  139. // [x: string]: unknown;
  140. }
  141. class Demo extends React.Component<IProps, IState> {
  142. constructor(props:any) {
  143. super(props);
  144. this.state = { visible: false};
  145. }
  146. getFormApi(formApi: FormApi<FData>) {
  147. formApi.getValue()
  148. }
  149. render() {
  150. const { visible } = this.state;
  151. return (
  152. <>
  153. <Form getFormApi={this.getFormApi}>
  154. </Form>
  155. </>
  156. );
  157. }
  158. }
  159. stories.add('Form render', () => <Form render={({values, formApi, formState}) => <div></div>}></Form>);
  160. stories.add('Form children', () => <Form>
  161. {({ formState, formApi, values }) => (
  162. <>
  163. <Form.Input field='fe'>
  164. </Form.Input>
  165. <Form.DatePicker field='role'/>
  166. </>
  167. )
  168. }
  169. </Form>);