autoComplete.stories.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. import React, { Component, useState } from 'react';
  2. import CustomTrigger from './CustomTrigger';
  3. import AutoComplete from '../index';
  4. import { IconSearch } from '@douyinfe/semi-icons';
  5. export default {
  6. title: 'AutoComplete',
  7. parameters: {
  8. chromatic: { disableSnapshot: true },
  9. },
  10. }
  11. const props = {
  12. onBlur: (v, e) => {
  13. console.log('onBlur');
  14. console.log(v, e);
  15. },
  16. onFocus: (v, e) => {
  17. console.log('onFocus');
  18. console.log(v, e);
  19. },
  20. };
  21. class Demo extends React.Component {
  22. constructor() {
  23. super();
  24. this.state = {
  25. data: [],
  26. data2: ['mike', 'tony', 'steve'],
  27. };
  28. this.acref = React.createRef();
  29. }
  30. handleSearch(value) {
  31. // let data = !value ? [] : [value, value + value, value + value + value];
  32. let result; // if (!value || value.indexOf('@') >= 0) {
  33. // result = [];
  34. // } else {
  35. if (value) {
  36. result = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
  37. } else {
  38. result = [];
  39. } // }
  40. this.setState({
  41. data: result,
  42. });
  43. }
  44. handleSearch2(value) {
  45. // let data2 = !value ? [] : [value, value + value, value + value + value];
  46. let result;
  47. if (!value || value.indexOf('@') >= 0) {
  48. result = [];
  49. } else {
  50. result = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
  51. }
  52. this.setState({
  53. data2: result,
  54. });
  55. }
  56. render() {
  57. const { data, data2 } = this.state;
  58. return (
  59. <div>
  60. <AutoComplete
  61. placeholder="fe"
  62. className="test-ac"
  63. prefix={<IconSearch />}
  64. showClear
  65. data={data}
  66. style={{
  67. width: 300,
  68. }}
  69. onSearch={this.handleSearch.bind(this)}
  70. onSelect={v => console.log(v)}
  71. {...props}
  72. ref={this.acref}
  73. />
  74. </div>
  75. );
  76. }
  77. }
  78. export const BasicUsage = () => <Demo />;
  79. class CustomOptionDemo extends Component {
  80. constructor(props) {
  81. super(props);
  82. this.state = {
  83. data: [],
  84. data2: [],
  85. };
  86. }
  87. search = value => {
  88. let result;
  89. if (!value) {
  90. result = [];
  91. } else {
  92. result = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
  93. }
  94. this.setState({
  95. data: result,
  96. });
  97. };
  98. renderOption = item => {
  99. return (
  100. <>
  101. <span
  102. style={{
  103. color: 'pink',
  104. }}
  105. >
  106. 邮箱
  107. </span>
  108. : <span>{item}</span>
  109. </>
  110. );
  111. };
  112. search2 = value => {
  113. let result;
  114. if (!value) {
  115. result = [];
  116. } else {
  117. result = ['gmail.com', '163.com', 'qq.com'].map(domain => {
  118. return {
  119. email: `${value}@${domain}`,
  120. time: new Date().valueOf(),
  121. value: `${value}@${domain}`,
  122. };
  123. });
  124. }
  125. this.setState({
  126. data2: result,
  127. });
  128. };
  129. renderObjectOption = item => {
  130. return (
  131. <div>
  132. <span
  133. style={{
  134. color: 'pink',
  135. }}
  136. >
  137. 邮箱
  138. </span>
  139. : <span>{item.email}</span>
  140. <span>time</span>: <span>{item.time}</span>
  141. </div>
  142. );
  143. };
  144. render() {
  145. return (
  146. <>
  147. <AutoComplete
  148. showClear
  149. data={this.state.data}
  150. renderItem={this.renderOption}
  151. style={{
  152. width: '250px',
  153. }}
  154. onSearch={this.search}
  155. ></AutoComplete>
  156. <br />
  157. <br />
  158. <AutoComplete
  159. onChangeWithObject
  160. style={{
  161. width: '250px',
  162. }}
  163. renderItem={this.renderObjectOption}
  164. renderSelectedItem={node => node.email}
  165. data={this.state.data2}
  166. onSearch={this.search2}
  167. />
  168. </>
  169. );
  170. }
  171. }
  172. export const RenderItem = () => <CustomOptionDemo />;
  173. class WithDefaultValue extends React.Component {
  174. constructor() {
  175. super();
  176. this.state = {
  177. data: ['[email protected]', '[email protected]', '[email protected]'],
  178. };
  179. this.onSearch = this.onSearch.bind(this);
  180. }
  181. onSearch(value) {
  182. let result;
  183. if (!value) {
  184. result = [];
  185. } else {
  186. result = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
  187. }
  188. this.setState({
  189. data: result,
  190. });
  191. }
  192. render() {
  193. let { data } = this.state;
  194. return (
  195. <>
  196. {/* <AutoComplete
  197. defaultValue='[email protected]'
  198. data={data}
  199. onSearch={this.onSearch}
  200. /> */}
  201. <AutoComplete defaultValue="semi" data={data} onSearch={this.onSearch} />
  202. </>
  203. );
  204. }
  205. }
  206. export const DefaultValue = () => <WithDefaultValue />;
  207. class ControlledMode extends React.Component {
  208. constructor() {
  209. super();
  210. this.state = {
  211. data: [],
  212. dataObject: [],
  213. value: '',
  214. };
  215. this.onSearch = this.onSearch.bind(this);
  216. this.onChange = this.onChange.bind(this);
  217. }
  218. onSearch(value) {
  219. let result, resultObject;
  220. if (!value) {
  221. result = [];
  222. resultObject = [];
  223. } else {
  224. result = ['gmail.com', '163.com', 'qq.com'].map(domain => `${value}@${domain}`);
  225. resultObject = ['gmail.com', '163.com', 'qq.com'].map(domain => ({
  226. label: `${value}@${domain}`,
  227. value: `${value}@${domain}`,
  228. }));
  229. }
  230. this.setState({
  231. data: result,
  232. dataObject: resultObject,
  233. });
  234. }
  235. onChange(value) {
  236. this.setState({
  237. value: value,
  238. });
  239. }
  240. render() {
  241. let { data, value, dataObject } = this.state;
  242. return (
  243. <>
  244. <AutoComplete
  245. showClear
  246. value={value}
  247. data={data}
  248. onChange={this.onChange}
  249. onSearch={this.onSearch}
  250. style={{
  251. width: 200,
  252. }}
  253. />
  254. <br />
  255. <AutoComplete
  256. showClear
  257. value={value}
  258. data={dataObject}
  259. onChange={this.onChange}
  260. onSearch={this.onSearch}
  261. style={{
  262. width: 200,
  263. }}
  264. />
  265. <br />
  266. <AutoComplete
  267. defaultValue={'hello semi'}
  268. showClear
  269. value={value}
  270. data={dataObject}
  271. onChange={this.onChange}
  272. onSearch={this.onSearch}
  273. style={{
  274. width: 200,
  275. }}
  276. />
  277. </>
  278. );
  279. }
  280. }
  281. export const EmptyContent = () => {
  282. let [data, setData] = useState([]);
  283. const [loading, setLoading] = useState(false);
  284. const fetchData = v => {
  285. setLoading(true);
  286. setTimeout(() => {
  287. if (!v) {
  288. setData([]);
  289. setLoading(false);
  290. return;
  291. }
  292. setData(() => {
  293. const res = Array.from(Array(5)).map(c => Math.random());
  294. return res;
  295. });
  296. setLoading(false);
  297. }, 1000);
  298. };
  299. return (
  300. <AutoComplete loading={loading} data={data} emptyContent={'空数据'} onSearch={fetchData} />
  301. );
  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 />;