withFieldDemo.jsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* eslint-disable no-unused-vars, no-debugger */
  2. import React, { useState, useLayoutEffect, Component } from 'react';
  3. import { storiesOf } from '@storybook/react';
  4. import {
  5. Button,
  6. Modal,
  7. TreeSelect,
  8. Row,
  9. Col,
  10. Avatar,
  11. Select as BasicSelect,
  12. Form,
  13. useFormState,
  14. useFormApi,
  15. useFieldApi,
  16. useFieldState,
  17. withFormState,
  18. withFormApi,
  19. withField,
  20. ArrayField,
  21. AutoComplete
  22. } from '../../../index';
  23. import { BASE_CLASS_PREFIX } from '@douyinfe/semi-foundation/base/constants';
  24. import { ComponentUsingFormState, CustomStringify } from '../Hook/hookDemo';
  25. const {
  26. Input,
  27. Select,
  28. DatePicker,
  29. Switch,
  30. Slider,
  31. CheckboxGroup,
  32. Checkbox,
  33. RadioGroup,
  34. Radio,
  35. TimePicker,
  36. InputNumber,
  37. InputGroup,
  38. } = Form;
  39. // 这里将html原生的input封装
  40. const htmlInput = props => {
  41. let { validateStatus, ...rest } = props;
  42. let value = props.value || '';
  43. return <input {...rest} value={value} />;
  44. };
  45. const CustomInput = withField(htmlInput, {
  46. valueKey: 'value',
  47. onKeyChangeFnName: 'onChange',
  48. valuePath: 'target.value',
  49. });
  50. class CustomFieldDemo extends React.PureComponent {
  51. render() {
  52. // 观察formState,看input的数据流是否已被form接管
  53. const ComponentUsingFormState = () => {
  54. const formState = useFormState();
  55. return (
  56. <pre>
  57. <code>{CustomStringify(formState)}</code>
  58. </pre>
  59. );
  60. };
  61. return (
  62. <Form>
  63. <CustomInput field="name" />
  64. <div>test</div>
  65. <ComponentUsingFormState />
  66. </Form>
  67. );
  68. }
  69. }
  70. const StateFulSelect = ({ value, onChange, onBlur, validateStatus }) => {
  71. const [song, setSong] = useState([]);
  72. const onSearch = inputValue => {
  73. if (inputValue === '') {
  74. setSong([]);
  75. } else {
  76. let songs = ['@XiamiMusic', '@TmeMusic', '@NeteaseMusic'].map(company => ({
  77. value: String(inputValue) + company,
  78. label: String(inputValue) + company,
  79. }));
  80. setSong(songs);
  81. }
  82. };
  83. return (
  84. <div>
  85. <BasicSelect
  86. value={value}
  87. style={{ width: 300 }}
  88. onChange={onChange}
  89. onSearch={onSearch}
  90. optionList={song}
  91. filter
  92. placeholder="type someting to search"
  93. />
  94. </div>
  95. );
  96. };
  97. class StatefulSelectClass extends React.Component {
  98. constructor(props) {
  99. super(props);
  100. this.state = {
  101. songs: [],
  102. };
  103. }
  104. onSearch = inputValue => {
  105. if (inputValue === '') {
  106. this.setState({ songs: [] });
  107. } else {
  108. let songs = ['@XiamiMusic', '@TmeMusic', '@NeteaseMusic'].map(company => ({
  109. value: String(inputValue) + company,
  110. label: String(inputValue) + company,
  111. }));
  112. this.setState({ song: songs });
  113. }
  114. };
  115. render() {
  116. let { value, onChange } = this.props;
  117. let { song } = this.state;
  118. return (
  119. <BasicSelect
  120. value={value}
  121. style={{ width: 300 }}
  122. onChange={onChange}
  123. onSearch={this.onSearch}
  124. optionList={song}
  125. filter
  126. multiple
  127. placeholder="type someting to search"
  128. />
  129. );
  130. }
  131. }
  132. let SelectField = withField(StateFulSelect);
  133. let CSelectField = withField(StatefulSelectClass);
  134. class WithFieldDemo extends React.Component {
  135. constructor(props) {
  136. super(props);
  137. this.state = {
  138. visible: true,
  139. };
  140. this.formApi = null;
  141. this.validate = this.validate.bind(this);
  142. this.saveFormApi = this.saveFormApi.bind(this);
  143. }
  144. saveFormApi(formApi) {
  145. this.formApi = formApi;
  146. }
  147. validate() {
  148. this.formApi.validate().then(() => {
  149. debugger;
  150. });
  151. }
  152. render() {
  153. return (
  154. <Form getFormApi={this.saveFormApi}>
  155. <SelectField field="name" />
  156. <CSelectField field="name2" />
  157. <ComponentUsingFormState />
  158. <Button onClick={() => this.validate()}>validate</Button>
  159. </Form>
  160. );
  161. }
  162. }
  163. class PureTest extends React.PureComponent {
  164. render() {
  165. console.log('render: test');
  166. return <div>test</div>;
  167. }
  168. }
  169. const PureTextWithField = withField(PureTest);
  170. let NumberRange = props => {
  171. const { onChange = () => {} } = props || {};
  172. let value = props && props.value ? props.value : [null, null];
  173. let label = props && props.labelText ? props.labelText : null;
  174. let style = props && props.style ? props.style : null;
  175. console.log(props);
  176. const rangeChange = v => {
  177. onChange(v);
  178. };
  179. const oc1 = v => {
  180. rangeChange([v, value[1]]);
  181. };
  182. const oc2 = v => {
  183. rangeChange([value[0], v]);
  184. };
  185. return (
  186. <div className="number-range" style={{ width: 280 }}>
  187. {label ? <span className={`${BASE_CLASS_PREFIX}-select-inline-label`}>{label}</span> : null}
  188. <InputNumber style={{ width: 90 }} value={value[0]} onChange={oc1} min={0} />
  189. <span style={{ margin: '0 5px' }}>~</span>
  190. <InputNumber style={{ width: 90 }} value={value[1]} onChange={oc2} min={0} />
  191. </div>
  192. );
  193. };
  194. NumberRange = withField(NumberRange);
  195. export { CustomFieldDemo, WithFieldDemo, NumberRange };