demo.jsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /* eslint-disable */
  2. import React, { Component } from 'react';
  3. import { Button, Row, Col, InputGroup as BasicInputGroup, AutoComplete } from '../../index';
  4. import { Form, useFormState, ArrayField, Label, withField } from '../index';
  5. import BasicSelect from '../../select/index';
  6. import BasicInput from '../../input/index';
  7. import BasicInputNumber from '../../inputNumber/index';
  8. import LocaleProvider from '../../locale/localeProvider';
  9. import zh_CN from '@douyinfe/semi-ui/locale/source/zh_CN';
  10. import en_GB from '@douyinfe/semi-ui/locale/source/en_GB';
  11. import en_US from '@douyinfe/semi-ui/locale/source/en_US';
  12. import ko_KR from '@douyinfe/semi-ui/locale/source/ko_KR';
  13. import ja_JP from '@douyinfe/semi-ui/locale/source/ja_JP';
  14. import {
  15. UseFormApiDemo,
  16. UseFormStateDemo,
  17. UseFieldApiDemo,
  18. UseFieldStateDemo,
  19. WithFormStateDemo,
  20. WithFormApiDemo,
  21. ComponentUsingFormState,
  22. CustomStringify,
  23. } from './Hook/hookDemo';
  24. const { Input, Select, DatePicker, Switch, Slider, CheckboxGroup, Checkbox, RadioGroup, Radio, TimePicker, InputNumber, InputGroup } = Form;
  25. const plainOptions = ['Apple', 'Pear', 'Orange'];
  26. const treeData = [
  27. {
  28. label: '亚洲',
  29. value: 'Asia',
  30. key: '0',
  31. children: [
  32. {
  33. label: '中国',
  34. value: 'China',
  35. key: '0-0',
  36. children: [
  37. {
  38. label: '北京',
  39. value: 'Beijing',
  40. key: '0-0-0',
  41. },
  42. {
  43. label: '上海',
  44. value: 'Shanghai',
  45. key: '0-0-1',
  46. },
  47. ],
  48. },
  49. ],
  50. },
  51. {
  52. label: '北美洲',
  53. value: 'North America',
  54. key: '1',
  55. }
  56. ];
  57. const DifferentDeclareUsage = () => (
  58. <React.Fragment>
  59. <h2>一般写法</h2>
  60. <Form layout="horizontal">
  61. <Input field="name" />
  62. <Select field="familyName">
  63. <Option value="mike">Mike</Option>
  64. <Option value="jane">Jane</Option>
  65. <Option value="kate">Kate</Option>
  66. </Select>
  67. </Form>
  68. <h2 style={{ marginTop: 20 }}>以下的几种写法可以直接在Form结构内部获取到formState值</h2>
  69. <h2>通过Render Props</h2>
  70. <Form
  71. render={({ formState }) => (
  72. <>
  73. <Input field="name" />
  74. <Input field="hello[0]" />
  75. <Input field="hello[1]" />
  76. <code>{CustomStringify(formState)}</code>
  77. </>
  78. )}
  79. layout="horizontal"
  80. ></Form>
  81. <h2 style={{ marginTop: 20 }}>通过child render function</h2>
  82. <Form layout="horizontal">
  83. {({ formState }) => (
  84. <>
  85. <Input field="name" />
  86. <Input field="hello[0]" />
  87. <Input field="hello[1]" />
  88. <code>{CustomStringify(formState)}</code>
  89. </>
  90. )}
  91. </Form>
  92. <h2 style={{ marginTop: 20 }}>通过props.component</h2>
  93. {/* <Form component={FormContent} layout='horizontal'></Form> */}
  94. <h2 style={{ marginTop: 20 }}>通过withFormState Hoc(示例见Hoc部分)</h2>
  95. <h2 style={{ marginTop: 20 }}>通过useFormState Hooks(示例见Hooks部分)</h2>
  96. </React.Fragment>
  97. )
  98. class BasicDemoWithInit extends Component {
  99. constructor() {
  100. super();
  101. this.state = {
  102. filed: {},
  103. layout: 'vertical',
  104. // layout: 'horizontal',
  105. labelPosition: 'left',
  106. // labelPosition: 'top',
  107. labelAlign: 'left'
  108. // horizontal
  109. };
  110. this.getFormApi = this.getFormApi.bind(this);
  111. this.changeLayout = this.changeLayout.bind(this);
  112. this.changeLabelPos = this.changeLabelPos.bind(this);
  113. this.changeLabelAlign = this.changeLabelAlign.bind(this);
  114. this.handleReset = this.handleReset.bind(this);
  115. this.validate = this.validate.bind(this);
  116. }
  117. changeLayout(layout) {
  118. this.setState({ layout });
  119. }
  120. changeLabelPos(labelPosition) {
  121. this.setState({ labelPosition });
  122. }
  123. changeInput() {
  124. this.formApi.setValue('input', Math.random());
  125. }
  126. changeLabelAlign(labelAlign) {
  127. this.setState({ labelAlign });
  128. }
  129. changeSelect() {
  130. this.formApi.setValue('select', 'jane');
  131. }
  132. getFormApi(formApi) {
  133. this.formApi = formApi;
  134. }
  135. handleReset() {
  136. console.log('reset')
  137. }
  138. validate() {
  139. this.formApi.validate()
  140. .then(value => {
  141. debugger
  142. })
  143. .catch(error => {
  144. debugger
  145. })
  146. }
  147. render() {
  148. const { layout, labelPosition, labelAlign } = this.state;
  149. const formInitValue = {
  150. // name: 'semi',
  151. business: ['hotsoon'],
  152. role: 'ued',
  153. tree: 'Beijing'
  154. };
  155. const plainOptions = ['A', 'B', 'C'];
  156. const style = { width: '90%' };
  157. return (
  158. <>
  159. <div>
  160. <BasicSelect onChange={this.changeLayout} onChange={this.changeLayout} value={layout}>
  161. <BasicSelect.Option value='vertical'>vertical</BasicSelect.Option>
  162. <BasicSelect.Option value='horizontal'>horizontal</BasicSelect.Option>
  163. </BasicSelect>
  164. <BasicSelect onChange={this.changeLabelPos} value={labelPosition}>
  165. <BasicSelect.Option value='top'>top</BasicSelect.Option>
  166. <BasicSelect.Option value='left'>left</BasicSelect.Option>
  167. <BasicSelect.Option value='inset'>inset</BasicSelect.Option>
  168. </BasicSelect>
  169. <BasicSelect onChange={this.changeLabelAlign} value={labelAlign}>
  170. <BasicSelect.Option value='left'>left</BasicSelect.Option>
  171. <BasicSelect.Option value='right'>right</BasicSelect.Option>
  172. </BasicSelect>
  173. </div>
  174. <Form
  175. layout={layout}
  176. labelPosition={labelPosition}
  177. labelAlign={labelAlign}
  178. getFormApi={this.getFormApi}
  179. initValues={formInitValue}
  180. onReset={this.handleReset}
  181. onValueChange={(v)=>console.log(v)}
  182. style={{ padding: '10px', width: 900 }}
  183. autoScrollToError
  184. aria-label='Demo Form'
  185. id='demo-form-id'
  186. >
  187. <Form.Section text={'基本信息'}>
  188. <Row>
  189. <Col span={12}>
  190. <Form.Input
  191. field='name'
  192. label="名称"
  193. // initValue={'mikeya'}
  194. style={style}
  195. trigger='blur'
  196. rules={[
  197. { required: true, message: 'required error' },
  198. { type: 'string', message: 'type error' },
  199. { validator: (rule, value) => value === 'muji', message: 'not muji' }
  200. ]}
  201. />
  202. </Col>
  203. <Col span={12}>
  204. <Form.DatePicker field="date" label='日期' style={style} placeholder='请选择生效日期' />
  205. </Col>
  206. </Row>
  207. <Row>
  208. <Col span={12}>
  209. <Form.Select field="role" style={style} label='角色' placeholder='请选择你的角色'>
  210. <Form.Select.Option value="operate">运营</Form.Select.Option>
  211. <Form.Select.Option value="rd">开发</Form.Select.Option>
  212. <Form.Select.Option value="pm">产品</Form.Select.Option>
  213. <Form.Select.Option value="ued">设计</Form.Select.Option>
  214. </Form.Select>
  215. </Col>
  216. <Col span={12}>
  217. <Form.Select
  218. field="business"
  219. multiple
  220. style={style}
  221. placeholder='请选择业务线'
  222. label="业务线(多选)"
  223. >
  224. <Form.Select.Option value="dy">抖音</Form.Select.Option>
  225. <Form.Select.Option value="hootsoon">火山小视频</Form.Select.Option>
  226. <Form.Select.Option value="toutiao">今日头条</Form.Select.Option>
  227. </Form.Select>
  228. </Col>
  229. </Row>
  230. <Row>
  231. <Col span={12}>
  232. <Form.Cascader
  233. placeholder="请选择所在地区"
  234. treeData={treeData}
  235. field='area'
  236. label='地区(级联选择)'
  237. >
  238. </Form.Cascader>
  239. </Col>
  240. <Col span={12}>
  241. <Form.TreeSelect
  242. field="tree"
  243. style={style}
  244. label='节点(树选择)'
  245. placeholder='请选择服务节点'
  246. treeData={treeData}
  247. filterTreeNode
  248. >
  249. </Form.TreeSelect>
  250. </Col>
  251. </Row>
  252. <Row>
  253. <Col span={12}>
  254. <Form.TimePicker
  255. field='time'
  256. helpText='原则上应当在 9:00 - 18:00 之间'
  257. label='时间选择'
  258. >
  259. </Form.TimePicker>
  260. </Col>
  261. <Col span={12}>
  262. <Form.AutoComplete
  263. field='typeData'
  264. label='类型选择'
  265. data={['1', '2' , '3']}
  266. >
  267. </Form.AutoComplete>
  268. </Col>
  269. </Row>
  270. </Form.Section>
  271. <Form.Section text='资源详情'>
  272. <Row>
  273. <Col span={12}>
  274. <Form.TextArea
  275. style={style}
  276. field='description'
  277. label='申请理由'
  278. placeholder='请填写申请资源理由'
  279. />
  280. </Col>
  281. <Col span={12}>
  282. <Form.CheckboxGroup
  283. field="type"
  284. label='申请类型'
  285. initValue={['user', 'admin']}
  286. rules={[
  287. { required: true }
  288. ]}
  289. >
  290. <Form.Checkbox value="admin">管理员admin</Form.Checkbox>
  291. <Form.Checkbox value="user">用户user</Form.Checkbox>
  292. <Form.Checkbox value="guest">访客guest</Form.Checkbox>
  293. <Form.Checkbox value="root">根用户root</Form.Checkbox>
  294. </Form.CheckboxGroup>
  295. </Col>
  296. </Row>
  297. <Row>
  298. <Col span={12}>
  299. <Form.RadioGroup field="isMonopolize" label='是否独占资源' rules={[
  300. { type: 'boolean' },
  301. { required: true, message: '必须选择是否独占 ' }
  302. ]}>
  303. <Form.Radio value={true}>是</Form.Radio>
  304. <Form.Radio value={false}>否</Form.Radio>
  305. </Form.RadioGroup>
  306. </Col>
  307. <Col span={12}>
  308. <Form.InputNumber field='number' label='申请数量' initValue={20} style={style}/>
  309. </Col>
  310. </Row>
  311. <Row>
  312. <Col span={12}>
  313. <Form.Slider field="range" label='资源使用报警阈值(%)' initValue={10} style={{ width: '90%' }}/>
  314. </Col>
  315. <Col span={12}>
  316. <Form.CheckboxGroup options={plainOptions} field="checkbox" label='Type' direction='horizontal'/>
  317. </Col>
  318. </Row>
  319. <Row>
  320. <Col span={12}>
  321. <Form.Rating field="rating" label='满意度(Rating)' initValue={2} style={{ width: '90%' }}/>
  322. </Col>
  323. <Col span={12}>
  324. <Form.Switch field='switch' label='开关(Switch)'/>
  325. </Col>
  326. </Row>
  327. <Form.CheckboxGroup field="cardCheckbox" label='卡片选择' style={{ width: '90%' }} type='card' initValue={['1', '3']} direction={'horizontal'} aria-label="CheckboxGroup 示例">
  328. <Form.Checkbox value={'1'} disabled extra='Semi Design 是由互娱社区前端团队与 UED 团队共同设计开发并维护的设计系统' style={{width:280}}>
  329. 单选框标题
  330. </Form.Checkbox>
  331. <Form.Checkbox value={'2'} disabled extra='Semi Design 是由互娱社区前端团队与 UED 团队共同设计开发并维护的设计系统' style={{width:280}}>
  332. 单选框标题
  333. </Form.Checkbox>
  334. </Form.CheckboxGroup>
  335. <Row>
  336. <Col span={12}>
  337. <Form.RadioGroup field='buttonRadio' type='button' buttonSize='middle' defaultValue={1} aria-label="单选组合示例">
  338. <Radio value={1}>即时推送</Radio>
  339. <Radio value={2}>定时推送</Radio>
  340. <Radio value={3}>动态推送</Radio>
  341. </Form.RadioGroup>
  342. </Col>
  343. </Row>
  344. </Form.Section>
  345. <Form.Checkbox value="false" field="agree" useOutSideGroup={true} noLabel={true}>
  346. 我已阅读并清楚相关规定
  347. </Form.Checkbox>
  348. <div style={{marginTop: 28}}>
  349. <Button type="primary" htmlType="submit">提交(submit)</Button>
  350. <Button htmlType="reset">重置(reset)</Button>
  351. <Button onClick={this.validate}>校验(validate)</Button>
  352. </div>
  353. </Form>
  354. </>
  355. );
  356. }
  357. }
  358. class LinkFieldForm extends Component {
  359. constructor() {
  360. super();
  361. this.state = {
  362. filed: {},
  363. };
  364. this.getFormApi = this.getFormApi.bind(this);
  365. this.handleSelectChange = this.handleSelectChange.bind(this);
  366. }
  367. handleSelectChange(value) {
  368. let text = value === 'male' ? 'Hi male' : 'Hi female!';
  369. // this.formApi.setValue('Note', text);
  370. }
  371. getFormApi(formApi) {
  372. this.formApi = formApi;
  373. }
  374. render() {
  375. return (
  376. <Form getFormApi={this.getFormApi}>
  377. <Row>
  378. <Col span={8}>
  379. <span>Note will change after Sex select</span>
  380. <Input
  381. field="Note"
  382. />
  383. <Select field="Sex" onChange={this.handleSelectChange} style={{width: 500}}>
  384. <Option value="female">female</Option>
  385. <Option value="male">male</Option>
  386. </Select>
  387. </Col>
  388. </Row>
  389. <Row>
  390. <Button type="primary" htmlType="submit">
  391. Submit
  392. </Button>
  393. <Button htmlType="reset">reset</Button>
  394. </Row>
  395. </Form>
  396. );
  397. }
  398. }
  399. export { BasicDemoWithInit, LinkFieldForm, DifferentDeclareUsage };