layoutDemo.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import React, { useState, useLayoutEffect, Component } from 'react';
  2. import { storiesOf } from '@storybook/react';
  3. import { Button, Modal, TreeSelect, Row, Col, Avatar, Icon, 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 } from '../../../index';
  15. import { ComponentUsingFormState } from '../Hook/hookDemo';
  16. const { Input, Select, DatePicker, Switch, Slider, CheckboxGroup, Checkbox, RadioGroup, Radio, TimePicker, InputNumber, InputGroup } = Form;
  17. const plainOptions = ['Apple', 'Pear', 'Orange'];
  18. const { Option } = Select;
  19. const { Label } = Form;
  20. const { Section } = Form;
  21. // wrapperCol, labelCol
  22. class LayoutDemo extends React.Component {
  23. constructor() {
  24. super();
  25. this.state = {
  26. labelPosition: 'left',
  27. labelAlign: 'left',
  28. // labelWidth: '180px'
  29. };
  30. this.changeLabelPos = this.changeLabelPos.bind(this);
  31. this.changeLabelAlign = this.changeLabelAlign.bind(this);
  32. }
  33. changeLabelPos(labelPosition) {
  34. let labelWidth;
  35. // labelPosition === 'left' ? labelWidth = '180px' : labelWidth = 'auto';
  36. this.setState({ labelPosition, labelWidth });
  37. }
  38. changeLabelAlign(labelAlign) {
  39. this.setState({ labelAlign });
  40. }
  41. setValues() {
  42. this.formApi.setValues({
  43. });
  44. }
  45. render() {
  46. const { labelPosition, labelAlign, labelWidth } = this.state;
  47. return (
  48. <>
  49. <div>
  50. <Label style={{ marginLeft: 10 }}>lablPosition:</Label>
  51. <BasicSelect onChange={this.changeLabelPos} value={labelPosition}>
  52. <Select.Option value="top">top</Select.Option>
  53. <Select.Option value="left">left</Select.Option>
  54. </BasicSelect>
  55. <Label style={{ marginLeft: 10 }}>lablAlign:</Label>
  56. <BasicSelect onChange={this.changeLabelAlign} value={labelAlign}>
  57. <Select.Option value="left">left</Select.Option>
  58. <Select.Option value="right">right</Select.Option>
  59. </BasicSelect>
  60. </div>
  61. <hr />
  62. <Form
  63. labelPosition={labelPosition}
  64. labelWidth={labelWidth}
  65. labelAlign={labelAlign}
  66. labelCol={{ span: 6 }}
  67. wrapperCol={{ span: 18 }}
  68. onChange={formState => console.log(formState)}
  69. onValueChange={formState => console.log(formState)}
  70. style={{ padding: '10px', width: 600 }}>
  71. <Form.Input
  72. field="input"
  73. label="我是label阿"
  74. trigger="blur"
  75. rules={[
  76. { required: true, message: 'required error' },
  77. { type: 'string', message: 'type error' },
  78. { validator: (rule, value) => value === 'muji', message: 'not muji' }
  79. ]}
  80. />
  81. <Form.Switch field="agree" />
  82. <Form.InputNumber field="price" />
  83. <Form.Select field="name" style={{ width: 300 }}>
  84. <Option value="mike">mike</Option>
  85. <Option value="jane">jane</Option>
  86. <Option value="kate">kate</Option>
  87. </Form.Select>
  88. <Form.CheckboxGroup field="role">
  89. <Form.Checkbox value="admin">管理员admin</Form.Checkbox>
  90. <Form.Checkbox value="user">用户user</Form.Checkbox>
  91. <Form.Checkbox value="guest">访客guest</Form.Checkbox>
  92. <Form.Checkbox value="root">根用户root</Form.Checkbox>
  93. </Form.CheckboxGroup>
  94. <Form.Checkbox useOutSideGroup field="root">root</Form.Checkbox>
  95. <Form.RadioGroup field="sex" name="apple">
  96. <Form.Radio value="1">man</Form.Radio>
  97. <Form.Radio value="2">woman</Form.Radio>
  98. </Form.RadioGroup>
  99. <Form.Slot label={{ text: 'texxt', required: true }}>
  100. <div>slot</div>
  101. </Form.Slot>
  102. <Form.InputGroup label='group text' style={{ width: 600 }}>
  103. <Form.Input field='inGroupName' style={{ width: 200 }}></Form.Input>
  104. <Form.Input field='inGroupType' style={{ width: 390 }}></Form.Input>
  105. </Form.InputGroup>
  106. </Form>
  107. </>
  108. );
  109. }
  110. }
  111. class InsetLabelDemo extends React.Component {
  112. constructor(props) {
  113. super();
  114. this.state = {
  115. labelPosition: 'left',
  116. labelAlign: 'left',
  117. // labelWidth: '180px'
  118. };
  119. this.changeLabelPos = this.changeLabelPos.bind(this);
  120. this.changeLabelAlign = this.changeLabelAlign.bind(this);
  121. }
  122. changeLabelPos(labelPosition) {
  123. let labelWidth;
  124. // labelPosition === 'left' ? labelWidth = '180px' : labelWidth = 'auto';
  125. this.setState({ labelPosition, labelWidth });
  126. }
  127. changeLabelAlign(labelAlign) {
  128. this.setState({ labelAlign });
  129. }
  130. render() {
  131. const { labelPosition, labelAlign } = this.state;
  132. return (
  133. <Form
  134. labelPosition={'inset'}
  135. labelCol={{ span: 6 }}
  136. wrapperCol={{ span: 18 }}
  137. onChange={formState => console.log(formState)}
  138. onValueChange={formState => console.log(formState)}
  139. style={{ padding: '10px', width: 600 }}>
  140. <Form.Input
  141. field="input"
  142. />
  143. <Form.InputNumber field="price" />
  144. <Form.DatePicker field="date" placeholder="请选择日期时间范围" />
  145. <Form.TimePicker field="time" />
  146. <Form.Slot label={{ text: 'texxt', required: true }}>
  147. <div>slot</div>
  148. </Form.Slot>
  149. </Form>
  150. );
  151. }
  152. }
  153. class LayoutForm extends React.Component {
  154. constructor() {
  155. super();
  156. this.state = {
  157. filed: {},
  158. layout: 'vertical',
  159. labelPosition: 'top',
  160. // horizontal
  161. };
  162. this.getFormApi = this.getFormApi.bind(this);
  163. this.changeLayout = this.changeLayout.bind(this);
  164. this.changeLabelPos = this.changeLabelPos.bind(this);
  165. }
  166. changeLayout(layout) {
  167. this.setState({ layout });
  168. }
  169. changeLabelPos(labelPosition) {
  170. this.setState({ labelPosition });
  171. }
  172. changeInput() {
  173. this.formApi.setValue('input', Math.random());
  174. }
  175. getFormApi(formApi) {
  176. this.formApi = formApi;
  177. }
  178. render() {
  179. const { field } = this.state;
  180. const { layout, labelPosition } = this.state;
  181. return (
  182. <>
  183. <div>
  184. <BasicSelect onChange={this.changeLayout} value={layout}>
  185. <BasicSelect.Option value="vertical">vertical</BasicSelect.Option>
  186. <BasicSelect.Option value="horizontal">horizontal</BasicSelect.Option>
  187. </BasicSelect>
  188. <BasicSelect onChange={this.changeLabelPos} value={labelPosition}>
  189. <BasicSelect.Option value="top">top</BasicSelect.Option>
  190. <BasicSelect.Option value="left">left</BasicSelect.Option>
  191. </BasicSelect>
  192. </div>
  193. <Form
  194. layout={layout}
  195. labelPosition={labelPosition}
  196. labelWidth={'auto'}
  197. getFormApi={this.getFormApi}
  198. style={{ padding: '10px' }}>
  199. <Row>
  200. <Col span={6}>
  201. <Input
  202. field="input"
  203. label="我是label阿"
  204. style={{ width: '250px' }}
  205. trigger="blur"
  206. rules={[
  207. { required: true, message: 'required error' },
  208. { type: 'string', message: 'type error' },
  209. { validator: (rule, value) => value === 'muji', message: 'not muji' }
  210. ]}
  211. />
  212. </Col>
  213. <Col span={6}>
  214. <Switch field="switch" />
  215. </Col>
  216. <Col span={6}>
  217. <InputNumber field="number" />
  218. </Col>
  219. <Col span={6}>
  220. <Select field="select">
  221. <Option value="mike">mike</Option>
  222. <Option value="jane">jane</Option>
  223. <Option value="kate">kate</Option>
  224. </Select>
  225. </Col>
  226. </Row>
  227. <Row>
  228. <Col span={6}>
  229. <Select
  230. field="peoples"
  231. multiple
  232. label="Select [multiple]"
  233. rules={[
  234. { type: 'array', min: 1, max: 3, messag: 'peoples errors' }
  235. ]}
  236. >
  237. <Option value="a">a</Option>
  238. <Option value="b">b</Option>
  239. <Option value="c">c</Option>
  240. <Option value="d">d</Option>
  241. <Option value="e">e</Option>
  242. </Select>
  243. </Col>
  244. <Col span={6}>
  245. <CheckboxGroup options={plainOptions} field="checkbox" />
  246. </Col>
  247. <Col span={6}>
  248. <CheckboxGroup
  249. field="role"
  250. rules={[
  251. { required: true }
  252. ]}
  253. >
  254. <Checkbox value="admin">admin</Checkbox>
  255. <Checkbox value="user">user</Checkbox>
  256. <Checkbox value="guest">guest</Checkbox>
  257. <Checkbox value="root">root</Checkbox>
  258. </CheckboxGroup>
  259. </Col>
  260. </Row>
  261. <Row>
  262. <Col span={12}>
  263. <Button type="primary" htmlType="submit">
  264. Submit
  265. </Button>
  266. <Button htmlType="reset">reset</Button>
  267. <Button onClick={this.changeInput.bind(this)}>change Input</Button>
  268. <ComponentUsingFormState />
  269. </Col>
  270. </Row>
  271. </Form>
  272. </>
  273. );
  274. }
  275. }
  276. export { LayoutDemo, InsetLabelDemo };