autoComplete.stories.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. import React, { Component, useState } from 'react';
  2. import CustomTrigger from './CustomTrigger';
  3. import AutoComplete from '../index';
  4. import { Form, Avatar } from '../../index';
  5. import { IconSearch } from '@douyinfe/semi-icons';
  6. import { debounce } from 'lodash';
  7. export default {
  8. title: 'AutoComplete',
  9. parameters: {
  10. chromatic: { disableSnapshot: true },
  11. },
  12. };
  13. const props = {
  14. onBlur: (v, e) => {
  15. console.log('onBlur');
  16. console.log(v, e);
  17. },
  18. onFocus: (v, e) => {
  19. console.log('onFocus');
  20. console.log(v, e);
  21. },
  22. };
  23. class Demo extends React.Component {
  24. constructor() {
  25. super();
  26. this.state = {
  27. data: [],
  28. data2: ['mike', 'tony', 'steve'],
  29. };
  30. this.acref = React.createRef();
  31. }
  32. handleSearch(value) {
  33. // let data = !value ? [] : [value, value + value, value + value + value];
  34. let result; // if (!value || value.indexOf('@') >= 0) {
  35. // result = [];
  36. // } else {
  37. if (value) {
  38. result = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
  39. } else {
  40. result = [];
  41. } // }
  42. this.setState({
  43. data: result,
  44. });
  45. }
  46. handleSearch2(value) {
  47. // let data2 = !value ? [] : [value, value + value, value + value + value];
  48. let result;
  49. if (!value || value.indexOf('@') >= 0) {
  50. result = [];
  51. } else {
  52. result = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
  53. }
  54. this.setState({
  55. data2: result,
  56. });
  57. }
  58. render() {
  59. const { data, data2 } = this.state;
  60. return (
  61. <div>
  62. <AutoComplete
  63. placeholder="fe"
  64. className="test-ac"
  65. prefix={<IconSearch />}
  66. showClear
  67. data={data}
  68. style={{
  69. width: 300,
  70. }}
  71. onSearch={this.handleSearch.bind(this)}
  72. onSelect={v => console.log(v)}
  73. {...props}
  74. ref={this.acref}
  75. />
  76. </div>
  77. );
  78. }
  79. }
  80. export const BasicUsage = () => <Demo />;
  81. class CustomOptionDemo extends Component {
  82. constructor(props) {
  83. super(props);
  84. this.state = {
  85. data: [],
  86. data2: [],
  87. };
  88. }
  89. search = value => {
  90. let result;
  91. if (!value) {
  92. result = [];
  93. } else {
  94. result = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
  95. }
  96. this.setState({
  97. data: result,
  98. });
  99. };
  100. renderOption = item => {
  101. return (
  102. <>
  103. <span
  104. style={{
  105. color: 'pink',
  106. }}
  107. >
  108. 邮箱
  109. </span>
  110. : <span>{item}</span>
  111. </>
  112. );
  113. };
  114. search2 = value => {
  115. let result;
  116. if (!value) {
  117. result = [];
  118. } else {
  119. result = ['gmail.com', '163.com', 'qq.com'].map(domain => {
  120. return {
  121. email: `${value}@${domain}`,
  122. time: new Date().valueOf(),
  123. value: `${value}@${domain}`,
  124. };
  125. });
  126. }
  127. this.setState({
  128. data2: result,
  129. });
  130. };
  131. renderObjectOption = item => {
  132. return (
  133. <div>
  134. <span
  135. style={{
  136. color: 'pink',
  137. }}
  138. >
  139. 邮箱
  140. </span>
  141. : <span>{item.email}</span>
  142. <span>time</span>: <span>{item.time}</span>
  143. </div>
  144. );
  145. };
  146. render() {
  147. return (
  148. <>
  149. <AutoComplete
  150. showClear
  151. data={this.state.data}
  152. renderItem={this.renderOption}
  153. style={{
  154. width: '250px',
  155. }}
  156. onSearch={this.search}
  157. ></AutoComplete>
  158. <br />
  159. <br />
  160. <AutoComplete
  161. onChangeWithObject
  162. style={{
  163. width: '250px',
  164. }}
  165. renderItem={this.renderObjectOption}
  166. renderSelectedItem={node => node.email}
  167. data={this.state.data2}
  168. onSearch={this.search2}
  169. />
  170. </>
  171. );
  172. }
  173. }
  174. export const RenderItem = () => <CustomOptionDemo />;
  175. class WithDefaultValue extends React.Component {
  176. constructor() {
  177. super();
  178. this.state = {
  179. data: ['[email protected]', '[email protected]', '[email protected]'],
  180. };
  181. this.onSearch = this.onSearch.bind(this);
  182. }
  183. onSearch(value) {
  184. let result;
  185. if (!value) {
  186. result = [];
  187. } else {
  188. result = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
  189. }
  190. this.setState({
  191. data: result,
  192. });
  193. }
  194. render() {
  195. let { data } = this.state;
  196. return (
  197. <>
  198. {/* <AutoComplete
  199. defaultValue='[email protected]'
  200. data={data}
  201. onSearch={this.onSearch}
  202. /> */}
  203. <AutoComplete defaultValue="semi" data={data} onSearch={this.onSearch} />
  204. </>
  205. );
  206. }
  207. }
  208. export const DefaultValue = () => <WithDefaultValue />;
  209. class ControlledMode extends React.Component {
  210. constructor() {
  211. super();
  212. this.state = {
  213. data: [],
  214. dataObject: [],
  215. value: '',
  216. };
  217. this.onSearch = this.onSearch.bind(this);
  218. this.onChange = this.onChange.bind(this);
  219. }
  220. onSearch(value) {
  221. let result, resultObject;
  222. if (!value) {
  223. result = [];
  224. resultObject = [];
  225. } else {
  226. result = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
  227. resultObject = ['gmail.com', '163.com', 'qq.com'].map(domain => ({
  228. label: `${value}@${domain}`,
  229. value: `${value}@${domain}`,
  230. }));
  231. }
  232. this.setState({
  233. data: result,
  234. dataObject: resultObject,
  235. });
  236. }
  237. onChange(value) {
  238. this.setState({
  239. value: value,
  240. });
  241. }
  242. render() {
  243. let { data, value, dataObject } = this.state;
  244. return (
  245. <>
  246. <AutoComplete
  247. showClear
  248. value={value}
  249. data={data}
  250. onChange={this.onChange}
  251. onSearch={this.onSearch}
  252. style={{
  253. width: 200,
  254. }}
  255. />
  256. <br />
  257. <AutoComplete
  258. showClear
  259. value={value}
  260. data={dataObject}
  261. onChange={this.onChange}
  262. onSearch={this.onSearch}
  263. style={{
  264. width: 200,
  265. }}
  266. />
  267. <br />
  268. <AutoComplete
  269. defaultValue={'hello semi'}
  270. showClear
  271. value={value}
  272. data={dataObject}
  273. onChange={this.onChange}
  274. onSearch={this.onSearch}
  275. style={{
  276. width: 200,
  277. }}
  278. />
  279. </>
  280. );
  281. }
  282. }
  283. export const EmptyContent = () => {
  284. let [data, setData] = useState([]);
  285. const [loading, setLoading] = useState(false);
  286. const fetchData = v => {
  287. setLoading(true);
  288. setTimeout(() => {
  289. if (!v) {
  290. setData([]);
  291. setLoading(false);
  292. return;
  293. }
  294. setData(() => {
  295. const res = Array.from(Array(5)).map(c => Math.random());
  296. return res;
  297. });
  298. setLoading(false);
  299. }, 1000);
  300. };
  301. return <AutoComplete loading={loading} data={data} emptyContent={'空数据'} onSearch={fetchData} />;
  302. };
  303. export const AutoFocus = () => {
  304. return <AutoComplete autoFocus />;
  305. };
  306. export const ControlledValue = () => <ControlledMode />;
  307. export const CustomTriggerDemo = () => <CustomTrigger />;
  308. export const Disabled = () => <AutoComplete disabled />;
  309. export const KeyDown = () => (
  310. <AutoComplete
  311. onKeyDown={e => {
  312. console.log('onKeyDown', e.keyCode);
  313. }}
  314. />
  315. );
  316. export const ControlledOnSelectWithObjectDemo = () => {
  317. const [v, setV] = useState();
  318. return (
  319. <Form>
  320. <AutoComplete
  321. onSelectWithObject
  322. data={[]}
  323. showClear
  324. value={v}
  325. placeholder='controlled autocomplete'
  326. onChange={val => setV(val)}
  327. style={{ width: 200 }}
  328. />
  329. <Form.AutoComplete field='test' placeholder='form autocomplete' onSelectWithObject data={[]} showClear style={{ width: 200 }} />
  330. </Form>
  331. );
  332. };
  333. export const AutoScrollToKeyboardUpDown = () => {
  334. const [data, setData] = useState([]);
  335. const getData = (v) => {
  336. let newData = Array.from({ length: 20 }, (v, i) => ({ label: `option-${i}`, value: i, 'data-cy': `option-${i}` }));
  337. setData(newData);
  338. };
  339. const renderOption = (item) => {
  340. let optionStyle = {
  341. display: 'flex',
  342. };
  343. return (
  344. <>
  345. <Avatar color={'cyan'} size="small">
  346. Semi
  347. </Avatar>
  348. <div style={{ marginLeft: 4 }}>
  349. <div style={{ fontSize: 14, marginLeft: 4 }}>{item.label}</div>
  350. <div style={{ marginLeft: 4 }}>{item.value}</div>
  351. </div>
  352. </>
  353. );
  354. }
  355. return <>
  356. <AutoComplete
  357. data={data}
  358. onSearch={(v) => getData(v)}
  359. style={{ width: 320 }}
  360. >
  361. </AutoComplete>
  362. <AutoComplete
  363. placeholder='with render'
  364. data={data}
  365. renderItem={renderOption}
  366. onSearch={(v) => getData(v)}
  367. style={{ width: 320 }}
  368. >
  369. </AutoComplete>
  370. </>
  371. }
  372. export const Fix2951 = () => {
  373. let initList = [
  374. '1.60','1.62','1.63','1.64'
  375. ];
  376. const [loading, setLoading] = useState(false);
  377. const [list, setList] = useState([]);
  378. const handleSearch = (inputValue) => {
  379. setLoading(true);
  380. let newList = [];
  381. if (inputValue) {
  382. newList = initList.filter(item => item.includes(inputValue));
  383. }
  384. setTimeout(() => {
  385. setList(newList);
  386. setLoading(false);
  387. }, 1000);
  388. };
  389. const search = debounce(handleSearch, 200);
  390. return (
  391. <AutoComplete
  392. data={list}
  393. style={{ width: 250 }}
  394. prefix={<IconSearch />}
  395. onSearch={search}
  396. loading={loading}
  397. defaultActiveFirstOption={true}
  398. ></AutoComplete>
  399. );
  400. }