layoutDemo.jsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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">
  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>
  103. </>
  104. );
  105. }
  106. }
  107. class InsetLabelDemo extends React.Component {
  108. constructor(props) {
  109. super();
  110. this.state = {
  111. labelPosition: 'left',
  112. labelAlign: 'left',
  113. // labelWidth: '180px'
  114. };
  115. this.changeLabelPos = this.changeLabelPos.bind(this);
  116. this.changeLabelAlign = this.changeLabelAlign.bind(this);
  117. }
  118. changeLabelPos(labelPosition) {
  119. let labelWidth;
  120. // labelPosition === 'left' ? labelWidth = '180px' : labelWidth = 'auto';
  121. this.setState({ labelPosition, labelWidth });
  122. }
  123. changeLabelAlign(labelAlign) {
  124. this.setState({ labelAlign });
  125. }
  126. render() {
  127. const { labelPosition, labelAlign } = this.state;
  128. return (
  129. <Form
  130. labelPosition={'inset'}
  131. labelCol={{ span: 6 }}
  132. wrapperCol={{ span: 18 }}
  133. onChange={formState => console.log(formState)}
  134. onValueChange={formState => console.log(formState)}
  135. style={{ padding: '10px', width: 600 }}>
  136. <Form.Input
  137. field="input"
  138. />
  139. <Form.InputNumber field="price" />
  140. <Form.DatePicker field="date" placeholder="请选择日期时间范围" />
  141. <Form.TimePicker field="time" />
  142. <Form.Slot label={{ text: 'texxt', required: true }}>
  143. <div>slot</div>
  144. </Form.Slot>
  145. </Form>
  146. );
  147. }
  148. }
  149. class GroupFormDemo extends React.Component {
  150. constructor() {
  151. super();
  152. this.saveFormApi = this.saveFormApi.bind(this);
  153. this.manualSubmit = this.manualSubmit.bind(this);
  154. }
  155. saveFormApi(formApi) {
  156. this.formApi = formApi;
  157. }
  158. manualSubmit() {
  159. this.formApi.submitForm();
  160. }
  161. render() {
  162. const selectProps = {
  163. style: { width: '100px' },
  164. placeholder: '国家',
  165. field: 'country',
  166. rules: [
  167. { required: true }
  168. ]
  169. };
  170. return (
  171. <>
  172. <Form onSubmit={values => console.log(values)} labelPosition="left" getFormApi={this.saveFormApi}>
  173. <InputGroup label={{ text: 'Movie', required: true }} labelPosition="left">
  174. <Select {...selectProps}>
  175. <Select.Option value="crime">+86</Select.Option>
  176. <Select.Option value="comedy">+1</Select.Option>
  177. <Select.Option value="tragedy">+83</Select.Option>
  178. </Select>
  179. <Input placeholder="手机号码" style={{ width: 100 }} field="phone" noLabel rules={[{ required: true }]} />
  180. <InputNumber placeholder="评分" style={{ width: 140 }} field="MovieScore" noLabel />
  181. </InputGroup>
  182. <Input field="name" trigger="blur" rules={[{ required: true }]} />
  183. <Input field="familyName[0].before" trigger="blur" />
  184. <Input field="familyName[0].after" trigger="blur" />
  185. <Select field="Sex">
  186. <Option value="female">female</Option>
  187. <Option value="male">male</Option>
  188. </Select>
  189. <Button htmlType="submit">提交</Button>
  190. <Button onClick={this.manualSubmit}>手动提交</Button>
  191. </Form>
  192. </>
  193. );
  194. }
  195. }
  196. class LayoutForm extends React.Component {
  197. constructor() {
  198. super();
  199. this.state = {
  200. filed: {},
  201. layout: 'vertical',
  202. labelPosition: 'top',
  203. // horizontal
  204. };
  205. this.getFormApi = this.getFormApi.bind(this);
  206. this.changeLayout = this.changeLayout.bind(this);
  207. this.changeLabelPos = this.changeLabelPos.bind(this);
  208. }
  209. changeLayout(layout) {
  210. this.setState({ layout });
  211. }
  212. changeLabelPos(labelPosition) {
  213. this.setState({ labelPosition });
  214. }
  215. changeInput() {
  216. this.formApi.setValue('input', Math.random());
  217. }
  218. getFormApi(formApi) {
  219. this.formApi = formApi;
  220. }
  221. render() {
  222. const { field } = this.state;
  223. const { layout, labelPosition } = this.state;
  224. return (
  225. <>
  226. <div>
  227. <BasicSelect onChange={this.changeLayout} value={layout}>
  228. <BasicSelect.Option value="vertical">vertical</BasicSelect.Option>
  229. <BasicSelect.Option value="horizontal">horizontal</BasicSelect.Option>
  230. </BasicSelect>
  231. <BasicSelect onChange={this.changeLabelPos} value={labelPosition}>
  232. <BasicSelect.Option value="top">top</BasicSelect.Option>
  233. <BasicSelect.Option value="left">left</BasicSelect.Option>
  234. </BasicSelect>
  235. </div>
  236. <Form
  237. layout={layout}
  238. labelPosition={labelPosition}
  239. labelWidth={'auto'}
  240. getFormApi={this.getFormApi}
  241. style={{ padding: '10px' }}>
  242. <Row>
  243. <Col span={6}>
  244. <Input
  245. field="input"
  246. label="我是label阿"
  247. style={{ width: '250px' }}
  248. trigger="blur"
  249. rules={[
  250. { required: true, message: 'required error' },
  251. { type: 'string', message: 'type error' },
  252. { validator: (rule, value) => value === 'muji', message: 'not muji' }
  253. ]}
  254. />
  255. </Col>
  256. <Col span={6}>
  257. <Switch field="switch" />
  258. </Col>
  259. <Col span={6}>
  260. <InputNumber field="number" />
  261. </Col>
  262. <Col span={6}>
  263. <Select field="select">
  264. <Option value="mike">mike</Option>
  265. <Option value="jane">jane</Option>
  266. <Option value="kate">kate</Option>
  267. </Select>
  268. </Col>
  269. </Row>
  270. <Row>
  271. <Col span={6}>
  272. <Select
  273. field="peoples"
  274. multiple
  275. label="Select [multiple]"
  276. rules={[
  277. { type: 'array', min: 1, max: 3, messag: 'peoples errors' }
  278. ]}
  279. >
  280. <Option value="a">a</Option>
  281. <Option value="b">b</Option>
  282. <Option value="c">c</Option>
  283. <Option value="d">d</Option>
  284. <Option value="e">e</Option>
  285. </Select>
  286. </Col>
  287. <Col span={6}>
  288. <CheckboxGroup options={plainOptions} field="checkbox" />
  289. </Col>
  290. <Col span={6}>
  291. <CheckboxGroup
  292. field="role"
  293. rules={[
  294. { required: true }
  295. ]}
  296. >
  297. <Checkbox value="admin">admin</Checkbox>
  298. <Checkbox value="user">user</Checkbox>
  299. <Checkbox value="guest">guest</Checkbox>
  300. <Checkbox value="root">root</Checkbox>
  301. </CheckboxGroup>
  302. </Col>
  303. </Row>
  304. <Row>
  305. <Col span={12}>
  306. <Button type="primary" htmlType="submit">
  307. Submit
  308. </Button>
  309. <Button htmlType="reset">reset</Button>
  310. <Button onClick={this.changeInput.bind(this)}>change Input</Button>
  311. <ComponentUsingFormState />
  312. </Col>
  313. </Row>
  314. </Form>
  315. </>
  316. );
  317. }
  318. }
  319. export { LayoutDemo, InsetLabelDemo, GroupFormDemo };