setValuesDemo.jsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import React, { useState, useLayoutEffect } from 'react';
  2. import { storiesOf } from '@storybook/react';
  3. import { Button, Modal, TreeSelect, Row, Col, Avatar, TextArea, 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,
  15. Icon,
  16. } from '../../../index';
  17. import { ComponentUsingFormState, CustomStringify } from '../Hook/hookDemo';
  18. const { Input, Select, DatePicker, Switch, Slider, CheckboxGroup, Checkbox, RadioGroup, Radio, TimePicker, InputNumber, InputGroup } = Form;
  19. class SetValuesDemo extends React.Component {
  20. constructor() {
  21. super();
  22. this.formApi = null;
  23. this.state = {
  24. flag: false,
  25. };
  26. this.getFormApi = this.getFormApi.bind(this);
  27. this.change = this.change.bind(this);
  28. }
  29. change() {
  30. let a = this.formApi.getFieldExist('ffff');
  31. let b = this.formApi.getFieldExist('name');
  32. this.formApi.setValues({ name: 'nike', notExist: '12' }, { isOverride: true });
  33. this.setState({ flag: true });
  34. }
  35. getFormApi(formApi) {
  36. this.formApi = formApi;
  37. }
  38. render() {
  39. const { flag } = this.state;
  40. return (
  41. <Form getFormApi={this.getFormApi} onChange={v => console.log('onChange', v)} onSubmit={v => console.log('onSubmit', v)}>
  42. {({ formState }) => (
  43. <>
  44. <Input field="name" initValue=""></Input>
  45. <Select field="familyName" multiple style={{ width: 200 }}>
  46. <Select.Option value="mike">Mike</Select.Option>
  47. <Select.Option value="jane">Jane</Select.Option>
  48. <Select.Option value="kate">Kate</Select.Option>
  49. </Select>
  50. {/* { flag ? <Input field='notExist' /> : null} */}
  51. <Checkbox value="false" field="agree" noLabel={true}>
  52. 我已阅读并清楚相关规定(Checkbox)
  53. </Checkbox>
  54. <Button onClick={this.change}>change</Button>
  55. <Button htmlType="submit">Submit</Button>
  56. <code>{CustomStringify(formState)}</code>
  57. </>
  58. )}
  59. </Form>
  60. );
  61. }
  62. }
  63. class SetValuesWithArrayField extends React.Component {
  64. constructor() {
  65. super();
  66. this.formApi = null;
  67. this.state = {
  68. flag: false,
  69. };
  70. this.getFormApi = this.getFormApi.bind(this);
  71. this.change = this.change.bind(this);
  72. }
  73. change() {
  74. let length = Math.floor(Math.random() * 100);
  75. let newEffect = Array.from({ length }, (v, i) => {
  76. return { name: Math.random(), type: Math.random() };
  77. });
  78. this.formApi.setValues({ name: 'nike', notExist: '12', effects: newEffect }, { isOverride: true });
  79. }
  80. getFormApi(formApi) {
  81. this.formApi = formApi;
  82. }
  83. render() {
  84. const { flag } = this.state;
  85. return (
  86. <Form
  87. getFormApi={this.getFormApi}
  88. onValueChange={(values, changedValue) => console.log('onValueChange', { values, changedValue })}
  89. onSubmit={v => console.log(v)}
  90. >
  91. {({ formState }) => (
  92. <>
  93. <ArrayField field='effects' initValue={[]}>
  94. {({ add, arrayFields }) => (
  95. <React.Fragment>
  96. <Button onClick={add}>Add</Button>
  97. {
  98. arrayFields.map(({ field, key, remove }, i) => (
  99. <div key={key} style={{ width: 1000, display: 'flex' }}>
  100. <Form.Input
  101. field={`${field}[name]`}
  102. label={`名称:(${field}.name)`}
  103. style={{ width: 200, marginRight: 16 }}
  104. >
  105. </Form.Input>
  106. <Form.Input
  107. field={`${field}[type]`}
  108. label={`类型:(${field}.type)`}
  109. style={{ width: 90 }}
  110. >
  111. </Form.Input>
  112. <Button type='danger' onClick={remove} style={{ margin: 16 }}>remove</Button>
  113. </div>
  114. ))
  115. }
  116. </React.Fragment>
  117. )}
  118. </ArrayField>
  119. <Button onClick={this.change}>change</Button>
  120. <Button htmlType="submit">Submit</Button>
  121. <TextArea value={CustomStringify(formState)}></TextArea>
  122. </>
  123. )}
  124. </Form>
  125. );
  126. }
  127. }
  128. export { SetValuesDemo, SetValuesWithArrayField };