select.stories.js 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906
  1. import React, { useState, useRef, useEffect } from 'react';
  2. import './select.scss';
  3. import { Input, Select, Button, Icon, Avatar, Checkbox, Form, withField, Space } from '../../index';
  4. import CustomTrigger from './CustomTrigger';
  5. import classNames from 'classnames';
  6. import { getHighLightTextHTML } from '../../_utils/index';
  7. const Option = Select.Option;
  8. import { IconSearch, IconGift } from '@douyinfe/semi-icons';
  9. export default {
  10. title: 'Select',
  11. parameters: {
  12. chromatic: { disableSnapshot: true },
  13. },
  14. }
  15. let Test = () => {
  16. let [options, setOptions] = useState([1, 2, 3, 4]);
  17. function add() {
  18. let newOptions = Array.from(
  19. {
  20. length: Math.floor(Math.random() * 10),
  21. },
  22. (v, i) => i + 1
  23. );
  24. setOptions(newOptions);
  25. }
  26. let style = {
  27. width: 150,
  28. margin: 20,
  29. };
  30. let slotStyle = {
  31. backgroundColor: 'whitesmoke',
  32. height: '40px',
  33. display: 'flex',
  34. justifyContent: 'center',
  35. alignItems: 'center',
  36. borderRadius: '0 0 6px 6px',
  37. };
  38. let outSlotStyle = {
  39. backgroundColor: 'whitesmoke',
  40. height: '29px',
  41. display: 'flex',
  42. justifyContent: 'center',
  43. alignItems: 'center',
  44. cursor: 'pointer',
  45. };
  46. const click = e => {};
  47. let outSlotNode = (
  48. <div onClick={e => click(e)}>
  49. <Checkbox>sendLarkNotification</Checkbox>
  50. <div>
  51. <Button theme="solid">confirm</Button>
  52. </div>
  53. </div>
  54. );
  55. return (
  56. <>
  57. <Select
  58. style={style}
  59. dropdownClassName="test-dropdown"
  60. dropdownStyle={{
  61. width: 150,
  62. }}
  63. filter
  64. placeholder="fefe"
  65. position="rightTop"
  66. innerBottomSlot={
  67. <div style={slotStyle}>
  68. <Button
  69. size="small"
  70. style={{
  71. margin: 0,
  72. }}
  73. >
  74. 申请其他地区权限
  75. </Button>
  76. </div>
  77. }
  78. >
  79. {options.map(option => (
  80. <Option value={option} key={option} className="fefe">
  81. {option}
  82. </Option>
  83. ))}
  84. </Select>
  85. <Select
  86. style={{
  87. marginTop: 20,
  88. width: 200,
  89. }}
  90. dropdownClassName="test-dropdown"
  91. dropdownStyle={{
  92. width: 150,
  93. }}
  94. filter
  95. placeholder="fefe"
  96. position="rightTop"
  97. outerBottomSlot={outSlotNode}
  98. >
  99. {options.map(option => (
  100. <Option value={option} key={option} className="fefe">
  101. {option}
  102. </Option>
  103. ))}
  104. </Select>
  105. </>
  106. );
  107. };
  108. const AutoFocusDemo = () => {
  109. return (
  110. <>
  111. <Select
  112. autoFocus
  113. style={{
  114. width: 200,
  115. }}
  116. onFocus={() => console.log('onFocus')}
  117. onBlur={() => console.log('onBlur')}
  118. >
  119. <Option value="abc">抖音</Option>
  120. <Option value="hotsoon">火山</Option>
  121. <Option value="pipixia">皮皮虾</Option>
  122. <Option value="duoshan">多闪</Option>
  123. <Option value="xigua">西瓜视频</Option>
  124. </Select>
  125. <div className="test-div">test-div</div>
  126. </>
  127. );
  128. };
  129. export const AutoFocus = () => <AutoFocusDemo />;
  130. AutoFocus.story = {
  131. name: 'autoFocus',
  132. };
  133. export const InnerBottomSlotOuterBottomSlot = () => <Test />;
  134. InnerBottomSlotOuterBottomSlot.story = {
  135. name: 'innerBottomSlot / outerBottomSlot',
  136. };
  137. export const InnerTopSlotOuterTopSlot = () => {
  138. const slot = <div>未找到应用?</div>;
  139. return (
  140. <div>
  141. innerTopSlot
  142. <div>
  143. <Select
  144. innerTopSlot={slot}
  145. style={{
  146. width: 250,
  147. }}
  148. maxHeight={150}
  149. >
  150. <Option value="abc">抖音</Option>
  151. <Option value="hotsoon">火山</Option>
  152. <Option value="pipixia">皮皮虾</Option>
  153. <Option value="duoshan">多闪</Option>
  154. <Option value="xigua">西瓜视频</Option>
  155. </Select>
  156. </div>
  157. outerTopSlot
  158. <div>
  159. <Select
  160. outerTopSlot={slot}
  161. style={{
  162. width: 250,
  163. }}
  164. maxHeight={150}
  165. >
  166. <Option value="abc">抖音</Option>
  167. <Option value="hotsoon">火山</Option>
  168. <Option value="pipixia">皮皮虾</Option>
  169. <Option value="duoshan">多闪</Option>
  170. <Option value="xigua">西瓜视频</Option>
  171. </Select>
  172. </div>
  173. </div>
  174. );
  175. };
  176. InnerTopSlotOuterTopSlot.story = {
  177. name: 'innerTopSlot / outerTopSlot',
  178. };
  179. export const OneOptionJsxWithOtherOptionArray = () => (
  180. <Select
  181. defaultValue={'all'}
  182. style={{
  183. width: 250,
  184. }}
  185. >
  186. <Option value="all" key="all">
  187. all
  188. </Option>
  189. {[1, 2, 3].map(item => (
  190. <Option value={`type${item}`} key={item}>{`type${item}`}</Option>
  191. ))}
  192. </Select>
  193. );
  194. OneOptionJsxWithOtherOptionArray.story = {
  195. name: 'one option jsx with other option array',
  196. };
  197. let options = [
  198. {
  199. value: 'all',
  200. label: '全部',
  201. otherKey: 'all semi',
  202. },
  203. {
  204. value: 'abc',
  205. label: '抖音',
  206. otherKey: 'abc semi',
  207. },
  208. {
  209. value: 'hotsoon',
  210. label: '火山小视频',
  211. otherKey: 'hostsoom semi',
  212. },
  213. {
  214. value: 'pipixia',
  215. label: '皮皮虾',
  216. otherKey: 'pif',
  217. },
  218. {
  219. value: 'toutiao',
  220. label: '今日头条',
  221. otherKey: 'toutiao semi',
  222. },
  223. {
  224. value: 'rd',
  225. label: 'rd',
  226. otherKey: 'semi rd',
  227. },
  228. {
  229. value: 'ued',
  230. label: 'ued',
  231. otherKey: 'semi ued',
  232. },
  233. {
  234. value: 'ued',
  235. label: 'japan',
  236. otherKey: 'semi ued',
  237. },
  238. {
  239. value: '+86',
  240. label: '+86',
  241. otherKey: 'semi',
  242. },
  243. ];
  244. let longOptions = options.concat({
  245. value: 'long',
  246. label: 'Semi Design 是一个设计系统,它定义了一套中后台设计与前端基础组',
  247. });
  248. export const SelectSize = () => (
  249. <div
  250. style={{
  251. margin: 20,
  252. }}
  253. >
  254. <h4>
  255. 使用方不设width时,下拉菜单根据内容自动适配宽度(不推荐这样使用,select的宽度会动态变化)
  256. </h4>
  257. <Select
  258. defaultValue={'all'}
  259. optionList={options}
  260. style={{
  261. margin: 10,
  262. }}
  263. ></Select>
  264. <Select
  265. defaultValue={'long'}
  266. optionList={longOptions}
  267. style={{
  268. margin: 10,
  269. }}
  270. ></Select>
  271. <Select
  272. defaultValue={'abc'}
  273. size="large"
  274. optionList={options}
  275. style={{
  276. margin: 10,
  277. }}
  278. ></Select>
  279. {/* <Select defaultValue={'+86'} size="large" optionList={options} style={{margin: 10}}>
  280. </Select> */}
  281. <h4>通过style设width的</h4>
  282. 90px:{' '}
  283. <Select
  284. defaultValue={'all'}
  285. style={{
  286. width: 90,
  287. marign: 10,
  288. }}
  289. optionList={options}
  290. ></Select>
  291. 120px:{' '}
  292. <Select
  293. defaultValue={'all'}
  294. style={{
  295. width: 120,
  296. margin: 10,
  297. }}
  298. optionList={options}
  299. ></Select>
  300. 400px:{' '}
  301. <Select
  302. defaultValue={'all'}
  303. style={{
  304. width: 400,
  305. margin: 10,
  306. }}
  307. optionList={options}
  308. ></Select>
  309. <br />
  310. 100%:{' '}
  311. <Select
  312. defaultValue={'all'}
  313. style={{
  314. width: '100%',
  315. margin: 10,
  316. }}
  317. optionList={options}
  318. ></Select>
  319. <br />
  320. <h4>通过css设width的</h4>
  321. <Select defaultValue={'all'} className="test-width" optionList={options}></Select>
  322. <br />
  323. <h4>dropdownMatchSelectWidth</h4>
  324. <p>当该项设为true时,下拉菜单最小宽度会等于Select宽度(默认为true)</p>
  325. <div
  326. style={{
  327. margin: 10,
  328. }}
  329. >
  330. style方式指定90px:
  331. <Select
  332. defaultValue={'all'}
  333. optionList={options}
  334. dropdownMatchSelectWidth={true}
  335. style={{
  336. width: 90,
  337. }}
  338. />
  339. </div>
  340. <div
  341. style={{
  342. margin: 10,
  343. }}
  344. >
  345. css方式声明130px:
  346. <Select
  347. defaultValue={'all'}
  348. className="test-width"
  349. optionList={options}
  350. dropdownMatchSelectWidth={true}
  351. ></Select>
  352. </div>
  353. <div>
  354. <h4>需要强制下拉菜单与select同宽的时候</h4>
  355. <p>通过dropdownStyle覆盖min-width,将其设成与select的width同样的值</p>
  356. <Select
  357. defaultValue={'all'}
  358. style={{
  359. width: 300,
  360. margin: 10,
  361. }}
  362. dropdownStyle={{
  363. width: 300,
  364. }}
  365. optionList={options}
  366. />
  367. </div>
  368. </div>
  369. );
  370. SelectSize.story = {
  371. name: 'select size',
  372. };
  373. export const WithPrefixSuffixInsetLabelShowClearShowArrow = () => (
  374. <>
  375. <h4>prefix & suffix</h4>
  376. <Select
  377. style={{
  378. width: '250px',
  379. }}
  380. optionList={options}
  381. prefix={<IconSearch />}
  382. suffix={<IconGift></IconGift>}
  383. ></Select>
  384. <h4>insetLabel</h4>
  385. <Select
  386. style={{
  387. width: '250px',
  388. }}
  389. optionList={options}
  390. insetLabel={'业务线'}
  391. ></Select>
  392. <h4>showClear</h4>
  393. <Select
  394. style={{
  395. width: '250px',
  396. }}
  397. optionList={options}
  398. showClear
  399. ></Select>
  400. <h4>showArrow = false</h4>
  401. <Select
  402. style={{
  403. width: '250px',
  404. }}
  405. optionList={options}
  406. showArrow={false}
  407. ></Select>
  408. <h4>defaultValue是不存在的值</h4>
  409. <Select
  410. style={{
  411. width: '250px',
  412. }}
  413. optionList={options}
  414. defaultValue="+85"
  415. ></Select>
  416. </>
  417. );
  418. WithPrefixSuffixInsetLabelShowClearShowArrow.story = {
  419. name: 'with prefix / suffix / insetLabel, showClear, showArrow',
  420. };
  421. WithPrefixSuffixInsetLabelShowClearShowArrow.parameters = {
  422. chromatic: { disableSnapshot: false },
  423. };
  424. export const WithDefaultSelected = () => (
  425. <Select
  426. style={{
  427. width: '250px',
  428. }}
  429. defaultValue={1}
  430. >
  431. <Option value={1}>opt1</Option>
  432. <Option value={2}>opt2</Option>
  433. <Option value={3}>opt3</Option>
  434. <Option value="4">opt4</Option>
  435. </Select>
  436. );
  437. WithDefaultSelected.story = {
  438. name: 'with default selected',
  439. };
  440. export const WithScrollbar = () => (
  441. <Select
  442. style={{
  443. width: '250px',
  444. }}
  445. defaultValue={1}
  446. >
  447. <Option value={1}>opt1</Option>
  448. <Option value={2}>opt2</Option>
  449. <Option value={3}>opt3</Option>
  450. <Option value="4">opt4</Option>
  451. <Option value={5}>opt5</Option>
  452. <Option value={6}>opt6</Option>
  453. <Option value={7}>opt7</Option>
  454. <Option value="8">opt8</Option>
  455. <Option value={9}>opt9</Option>
  456. <Option value={10}>opt10dfsdfsdfdsfdsfsdf</Option>
  457. <Option value={11}>opt11</Option>
  458. <Option value="12">opt12jfldsjflsdjlfldjslfjhifsdfdsfdsffdsodsjlfhjl</Option>
  459. </Select>
  460. );
  461. WithScrollbar.story = {
  462. name: 'with scrollbar',
  463. };
  464. class Link extends React.Component {
  465. get provinces() {
  466. return ['Sichuan', 'Guangdong'];
  467. }
  468. get maps() {
  469. return {
  470. Sichuan: ['Chengdu', 'Dujiangyan'],
  471. Guangdong: ['Guangzhou', 'Shenzhen', 'Dongguan'],
  472. };
  473. }
  474. constructor() {
  475. super();
  476. this.state = {
  477. provinces: this.provinces,
  478. maps: this.maps,
  479. citys: this.maps[this.provinces[0]],
  480. city: this.maps[this.provinces[0]][0],
  481. };
  482. this.provinceChange = this.provinceChange.bind(this);
  483. this.cityChange = this.cityChange.bind(this);
  484. }
  485. provinceChange(newProvince) {
  486. const { maps } = this.state;
  487. this.setState({
  488. citys: maps[newProvince],
  489. city: maps[newProvince][0],
  490. });
  491. }
  492. cityChange(city) {
  493. this.setState({
  494. city,
  495. });
  496. }
  497. render() {
  498. const { provinces, citys, city } = this.state;
  499. return (
  500. <React.Fragment>
  501. <Select
  502. style={{
  503. width: '150px',
  504. margin: '10px',
  505. }}
  506. onChange={this.provinceChange}
  507. defaultValue={provinces[0]}
  508. >
  509. {provinces.map(pro => (
  510. <Option value={pro} key={pro}>
  511. {pro}
  512. </Option>
  513. ))}
  514. </Select>
  515. <Select
  516. style={{
  517. width: '150px',
  518. margin: '10px',
  519. }}
  520. value={city}
  521. onChange={this.cityChange}
  522. >
  523. {citys.map(c => (
  524. <Option value={c} key={c}>
  525. {c}
  526. </Option>
  527. ))}
  528. </Select>
  529. </React.Fragment>
  530. );
  531. }
  532. }
  533. export const TwoSelectChangeAtTheSameTime = () => <Link />;
  534. TwoSelectChangeAtTheSameTime.story = {
  535. name: 'two select change at the same time',
  536. };
  537. export const SelectMultiple = () => (
  538. <>
  539. <Select
  540. multiple={true}
  541. max={10}
  542. style={{
  543. width: '180px',
  544. }}
  545. placeholder="fefe"
  546. >
  547. <Option value={1}>opt1</Option>
  548. <Option value={2}>opt2</Option>
  549. <Option value={3}>opt3</Option>
  550. <Option value="4">opt4</Option>
  551. <Option value={5}>opt5</Option>
  552. <Option value={6}>opt6</Option>
  553. <Option value={7}>opt7</Option>
  554. <Option value={8}>opt8</Option>
  555. </Select>
  556. <br />
  557. <br />
  558. <Select
  559. multiple={true}
  560. style={{
  561. width: '300px',
  562. }}
  563. defaultValue={[1, 2, 3]}
  564. placeholder="fefe"
  565. >
  566. <Option value={1}>opt1</Option>
  567. <Option value={2}>opt2</Option>
  568. <Option value={3}>opt3</Option>
  569. <Option value="4">opt4</Option>
  570. <Option value={5}>opt5</Option>
  571. <Option value={6}>opt6</Option>
  572. <Option value={7}>opt7</Option>
  573. <Option value={8}>opt8</Option>
  574. </Select>
  575. <br />
  576. <br />
  577. <Select
  578. multiple={true}
  579. style={{
  580. width: '300px',
  581. }}
  582. defaultValue={[1, 2, 3]}
  583. placeholder="fefe"
  584. disabled
  585. onSelect={(...res) => console.log(res)}
  586. onDeselect={(...res) => console.log(res)}
  587. >
  588. <Option value={1}>opt1</Option>
  589. <Option value={2}>opt2</Option>
  590. <Option value={3}>opt3</Option>
  591. <Option value="4">opt4</Option>
  592. <Option value={5}>opt5</Option>
  593. <Option value={6}>opt6</Option>
  594. <Option value={7}>opt7</Option>
  595. <Option value={8}>opt8</Option>
  596. </Select>
  597. <br />
  598. <br />
  599. maxTagCount = 3
  600. <Select
  601. multiple={true}
  602. maxTagCount={3}
  603. style={{
  604. width: '350px',
  605. }}
  606. defaultValue={[1, 2, 3]}
  607. placeholder="fefe"
  608. insetLabel="标签"
  609. onSelect={(...res) => console.log(res)}
  610. onDeselect={(...res) => console.log(res)}
  611. >
  612. <Option value={1}>opt1</Option>
  613. <Option value={2}>opt2</Option>
  614. <Option value={3}>opt3</Option>
  615. <Option value="4">opt4</Option>
  616. <Option value={5}>opt5</Option>
  617. <Option value={6}>opt6</Option>
  618. <Option value={7}>opt7</Option>
  619. <Option value={8}>opt8</Option>
  620. </Select>
  621. <br />
  622. <br />
  623. maxTagCount = 3, max=5
  624. <Select
  625. multiple={true}
  626. maxTagCount={3}
  627. max={5}
  628. style={{
  629. width: '350px',
  630. }}
  631. defaultValue={[1, 2, 3]}
  632. placeholder="fefe"
  633. insetLabel="标签"
  634. onSelect={(...res) => console.log(res)}
  635. onDeselect={(...res) => console.log(res)}
  636. >
  637. <Option value={1}>opt1</Option>
  638. <Option value={2}>opt2</Option>
  639. <Option value={3}>opt3</Option>
  640. <Option value="4">opt4</Option>
  641. <Option value={5}>opt5</Option>
  642. <Option value={6}>opt6</Option>
  643. <Option value={7}>opt7</Option>
  644. <Option value={8}>opt8</Option>
  645. </Select>
  646. </>
  647. );
  648. SelectMultiple.story = {
  649. name: 'select multiple',
  650. };
  651. SelectMultiple.parameters = {
  652. chromatic: { disableSnapshot: false },
  653. };
  654. export const SelectDisabled = () => (
  655. <Select
  656. disabled
  657. multiple={true}
  658. max={10}
  659. style={{
  660. width: '250px',
  661. }}
  662. >
  663. <Option value={1}>opt1</Option>
  664. <Option value={2} disabled>
  665. opt2
  666. </Option>
  667. <Option value={3}>opt3</Option>
  668. <Option value="4">opt4</Option>
  669. </Select>
  670. );
  671. SelectDisabled.story = {
  672. name: 'select disabled',
  673. };
  674. function filter(input, option) {
  675. console.log(option);
  676. return option.label.includes(input);
  677. }
  678. const spanStyle = {
  679. display: 'inline-block',
  680. marginRight: '8px',
  681. width: '16px',
  682. height: '16px',
  683. borderRadius: '50%',
  684. border: '1px solid var(--semi-color-bg-1)',
  685. };
  686. const colorOptions = [
  687. {
  688. value: 'grey-1',
  689. spanStyle: { ...spanStyle, backgroundColor: 'rgb(107, 116, 117)' },
  690. },
  691. {
  692. value: 'purple-5',
  693. spanStyle: { ...spanStyle, backgroundColor: 'rgb(158, 40, 179)' },
  694. },
  695. {
  696. value: 'pink-2',
  697. spanStyle: { ...spanStyle, backgroundColor: 'rgb(233, 30, 99)' },
  698. },
  699. {
  700. value: 'blue-3',
  701. spanStyle: { ...spanStyle, backgroundColor: 'rgb(0, 119, 250)' },
  702. },
  703. ];
  704. const alignStyle = {
  705. display: 'flex',
  706. alignItems: 'center',
  707. };
  708. const customFilter = (input, option) => {
  709. return option.value.includes(input);
  710. };
  711. export const SelectFilterSingle = () => (
  712. <div>
  713. <h5>默认筛选</h5>
  714. <Select
  715. filter
  716. style={{
  717. width: '250px',
  718. margin: 10,
  719. }}
  720. autoFocus
  721. onFocus={() => console.log('onFocus')}
  722. onBlur={() => console.log('onBlur')}
  723. >
  724. <Option value={1}>opt1</Option>
  725. <Option value={2} disabled>
  726. disabled
  727. </Option>
  728. <Option value={3}>Lucy</Option>
  729. <Option value="4">bay</Option>
  730. <Option value="5">sert</Option>
  731. <Option value="6">wym</Option>
  732. <Option value="7" disabled>
  733. meno
  734. </Option>
  735. <Option value="8">opts</Option>
  736. </Select>
  737. <h5>自定义筛选函数</h5>
  738. <Select
  739. style={{
  740. width: '250px',
  741. margin: 10,
  742. }}
  743. filter={filter}
  744. onBlur={() => console.log('onBlur')}
  745. onFocus={() => console.log('onFocus')}
  746. >
  747. <Option value={1}>opt1(value:1)</Option>
  748. <Option value={2}>mike(value:2)</Option>
  749. <Option value={3}>Lucy(value:3)</Option>
  750. <Option value={4}>bay(value:4)</Option>
  751. </Select>
  752. <h5>filter为true,但option label为node时</h5>
  753. <Select
  754. style={{
  755. width: '250px',
  756. margin: 10,
  757. }}
  758. filter={customFilter}
  759. onChange={v => console.log(v)}
  760. insetLabel="insetLabel"
  761. onFocus={() => console.log('onFocus')}
  762. onBlur={() => console.log('onBlur')}
  763. >
  764. {colorOptions.map(option => (
  765. <Option value={option.value} key={option.value}>
  766. <div style={alignStyle}>
  767. <span style={option.spanStyle}></span>
  768. {option.value}
  769. </div>
  770. </Option>
  771. ))}
  772. </Select>
  773. </div>
  774. );
  775. SelectFilterSingle.story = {
  776. name: 'select filter single',
  777. };
  778. export const SelectFilterMultiple = () => (
  779. <>
  780. <Select
  781. filter
  782. multiple={true}
  783. style={{
  784. width: '250px',
  785. }}
  786. placeholder="fefe"
  787. >
  788. <Option value={1}>opt1</Option>
  789. <Option value={2}>opt2</Option>
  790. <Option value={3}>opt22</Option>
  791. <Option value={3}>opt3</Option>
  792. <Option value={4}>opt4</Option>
  793. <Option value={5}>opt5</Option>
  794. <Option value={6}>opt6</Option>
  795. <Option value={7}>opt7</Option>
  796. <Option value={8}>opt8</Option>
  797. </Select>
  798. <Select
  799. filter
  800. multiple={true}
  801. maxTagCount={3}
  802. style={{
  803. width: '270px',
  804. }}
  805. placeholder="fefe"
  806. >
  807. <Option value={1}>opt1</Option>
  808. <Option value={2}>opt2</Option>
  809. <Option value={3}>opt22</Option>
  810. <Option value={3}>opt3</Option>
  811. <Option value={4}>opt4</Option>
  812. <Option value={5}>opt5</Option>
  813. <Option value={6}>opt6</Option>
  814. <Option value={7}>opt7</Option>
  815. <Option value={8}>opt8</Option>
  816. </Select>
  817. </>
  818. );
  819. SelectFilterMultiple.story = {
  820. name: 'select filter multiple',
  821. };
  822. const OptionLabelProp = () => {
  823. const [value, setValue] = useState(1);
  824. return (
  825. <>
  826. 设置optionLabelProp属性(默认为'children')为'value'时,回填到选择框中的文本会是Option.value
  827. <br></br>
  828. <Select
  829. style={{
  830. width: '250px',
  831. }}
  832. defaultValue={1}
  833. optionLabelProp="value"
  834. >
  835. <Option value={1}>opt1</Option>
  836. <Option value={2}>opt2</Option>
  837. <Option value={3}>
  838. <span
  839. style={{
  840. color: 'pink',
  841. }}
  842. >
  843. opt3 Node
  844. </span>
  845. </Option>
  846. <Option value="4">
  847. <span
  848. style={{
  849. color: 'red',
  850. }}
  851. >
  852. testNode
  853. </span>
  854. </Option>
  855. </Select>
  856. <br />
  857. <br />
  858. <Select
  859. style={{
  860. width: '250px',
  861. }}
  862. value={value}
  863. optionLabelProp="value"
  864. onChange={setValue}
  865. >
  866. <Option value={1}>opt1</Option>
  867. <Option value={2}>opt2</Option>
  868. <Option value={3}>
  869. <span
  870. style={{
  871. color: 'pink',
  872. }}
  873. >
  874. opt3 Node
  875. </span>
  876. </Option>
  877. <Option value="4">
  878. <span
  879. style={{
  880. color: 'red',
  881. }}
  882. >
  883. testNode
  884. </span>
  885. </Option>
  886. </Select>
  887. <br />
  888. <br />
  889. <Select
  890. style={{
  891. width: '250px',
  892. }}
  893. defaultValue={1}
  894. >
  895. <Option value={1}>children Label Text 1</Option>
  896. <Option value={2}>opt2</Option>
  897. <Option value={3}>opt3</Option>
  898. <Option value="4">
  899. <span
  900. style={{
  901. color: 'red',
  902. }}
  903. >
  904. testNode
  905. </span>
  906. </Option>
  907. </Select>
  908. <Select
  909. style={{
  910. width: '250px',
  911. }}
  912. defaultValue={1}
  913. filter
  914. optionLabelProp="value"
  915. >
  916. <Option value={1}>children Label Text 1</Option>
  917. <Option value={2}>opt2</Option>
  918. <Option value={3}>opt3</Option>
  919. <Option value="4">
  920. <span
  921. style={{
  922. color: 'red',
  923. }}
  924. >
  925. testNode
  926. </span>
  927. </Option>
  928. </Select>
  929. <br />
  930. <br />
  931. 多选
  932. <Select
  933. style={{
  934. width: '250px',
  935. }}
  936. multiple
  937. filter
  938. optionLabelProp="value"
  939. >
  940. <Option value={1}>children Label Text 1</Option>
  941. <Option value={2}>opt2</Option>
  942. <Option value={3}>opt3</Option>
  943. <Option value="4">
  944. <span
  945. style={{
  946. color: 'red',
  947. }}
  948. >
  949. testNode
  950. </span>
  951. </Option>
  952. </Select>
  953. </>
  954. );
  955. };
  956. class CustomRender extends React.Component {
  957. constructor() {
  958. super();
  959. this.state = {
  960. list: [
  961. {
  962. name: '夏可漫',
  963. email: '[email protected]',
  964. abbr: 'XK',
  965. color: 'amber',
  966. },
  967. {
  968. name: '申悦',
  969. email: '[email protected]',
  970. abbr: 'SY',
  971. color: 'indigo',
  972. },
  973. {
  974. name: '曲晨一',
  975. email: '[email protected]',
  976. abbr: 'CY',
  977. color: 'blue',
  978. },
  979. {
  980. name: '文嘉茂',
  981. email: '[email protected]',
  982. abbr: 'JM',
  983. color: 'cyan',
  984. },
  985. ],
  986. };
  987. }
  988. renderCustomOption(item, index) {
  989. const optionStyle = {
  990. display: 'flex',
  991. };
  992. return (
  993. <Option key={index} value={item.name} style={optionStyle} showTick={false} {...item}>
  994. <Avatar color={item.color} size="small">
  995. {item.abbr}
  996. </Avatar>
  997. <div
  998. style={{
  999. marginLeft: 4,
  1000. }}
  1001. >
  1002. <p
  1003. style={{
  1004. fontSize: 14,
  1005. margin: 4,
  1006. }}
  1007. >
  1008. {item.name}
  1009. </p>
  1010. <p
  1011. style={{
  1012. margin: 4,
  1013. }}
  1014. >
  1015. {item.email}
  1016. </p>
  1017. </div>
  1018. </Option>
  1019. );
  1020. }
  1021. renderSelectedItem(optionNode) {
  1022. return (
  1023. <div>
  1024. <Avatar color={optionNode.color} size="small">
  1025. {optionNode.abbr}
  1026. </Avatar>
  1027. <span
  1028. style={{
  1029. margin: 8,
  1030. }}
  1031. >
  1032. {optionNode.email}
  1033. </span>
  1034. </div>
  1035. );
  1036. }
  1037. renderMultipleSelectedItem(optionNode) {
  1038. let content = (
  1039. <div>
  1040. <Avatar color={optionNode.color} size="small">
  1041. {optionNode.abbr}
  1042. </Avatar>
  1043. </div>
  1044. );
  1045. return {
  1046. isRenderInTag: true,
  1047. content,
  1048. };
  1049. }
  1050. renderMultipleWithoutTag(optionNode) {
  1051. let content = (
  1052. <div>
  1053. <Avatar color={optionNode.color} size="small">
  1054. {optionNode.abbr}
  1055. </Avatar>
  1056. </div>
  1057. );
  1058. return {
  1059. isRenderInTag: false,
  1060. content,
  1061. };
  1062. }
  1063. render() {
  1064. const { list } = this.state;
  1065. return (
  1066. <React.Fragment>
  1067. <Select
  1068. style={{
  1069. width: 300,
  1070. height: 40,
  1071. }}
  1072. onChange={this.provinceChange}
  1073. defaultValue={'夏可漫'}
  1074. renderSelectedItem={this.renderSelectedItem}
  1075. >
  1076. {list.map((item, index) => this.renderCustomOption(item, index))}
  1077. </Select>
  1078. <Select
  1079. style={{
  1080. width: 360,
  1081. height: 60,
  1082. marginTop: 20,
  1083. }}
  1084. onChange={this.provinceChange}
  1085. defaultValue={['夏可漫', '申悦']}
  1086. multiple
  1087. renderSelectedItem={this.renderMultipleSelectedItem}
  1088. >
  1089. {list.map((item, index) => this.renderCustomOption(item, index))}
  1090. </Select>
  1091. <Select
  1092. style={{
  1093. width: 360,
  1094. height: 60,
  1095. marginTop: 20,
  1096. }}
  1097. onChange={this.provinceChange}
  1098. defaultValue={['夏可漫', '申悦']}
  1099. multiple
  1100. renderSelectedItem={this.renderMultipleWithoutTag}
  1101. >
  1102. {list.map((item, index) => this.renderCustomOption(item, index))}
  1103. </Select>
  1104. </React.Fragment>
  1105. );
  1106. }
  1107. }
  1108. export const RenderSelectedItem = () => (
  1109. <>
  1110. renderSelectedItem
  1111. <CustomRender />
  1112. <br />
  1113. <br />
  1114. OptionLabelProp
  1115. <OptionLabelProp />
  1116. </>
  1117. );
  1118. RenderSelectedItem.story = {
  1119. name: 'renderSelectedItem',
  1120. };
  1121. RenderSelectedItem.parameters = {
  1122. chromatic: { disableSnapshot: false },
  1123. };
  1124. const ControledSelect = () => {
  1125. const [value, setValue] = useState('nick');
  1126. const [value2, setValue2] = useState('jerry');
  1127. const [value3, setValue3] = useState();
  1128. const [value4, setValue4] = useState(['nick']);
  1129. const [value5, setValue5] = useState();
  1130. return (
  1131. <>
  1132. <span>value + onChange</span>
  1133. <Select
  1134. value={value}
  1135. onChange={setValue}
  1136. style={{
  1137. width: 200,
  1138. }}
  1139. >
  1140. <Option value="nick">nick</Option>
  1141. <Option value="jerry">jerry</Option>
  1142. <Option value="mark">mark</Option>
  1143. </Select>
  1144. <br />
  1145. <br />
  1146. <span>只传value,不传onChange</span>
  1147. <Select
  1148. value={value2}
  1149. style={{
  1150. width: 200,
  1151. }}
  1152. >
  1153. <Option value="nick">nick</Option>
  1154. <Option value="jerry">jerry</Option>
  1155. <Option value="mark">mark</Option>
  1156. </Select>
  1157. <br />
  1158. <br />
  1159. <span>value + onChange , 多选</span>
  1160. <Select
  1161. value={value3}
  1162. onChange={setValue3}
  1163. multiple
  1164. style={{
  1165. width: 200,
  1166. }}
  1167. >
  1168. <Option value="nick">nick</Option>
  1169. <Option value="jerry">jerry</Option>
  1170. <Option value="mark">mark</Option>
  1171. <Option value="nick2">nick2</Option>
  1172. <Option value="jerry2">jerry2</Option>
  1173. <Option value="mark2">mark2</Option>
  1174. </Select>
  1175. <br />
  1176. <br />
  1177. <span>value, 多选</span>
  1178. <Select
  1179. value={value4}
  1180. multiple
  1181. style={{
  1182. width: 200,
  1183. }}
  1184. >
  1185. <Option value="nick">nick</Option>
  1186. <Option value="jerry">jerry</Option>
  1187. <Option value="mark">mark</Option>
  1188. </Select>
  1189. <br />
  1190. <h5>filter为true,但option label为node时</h5>
  1191. <Select
  1192. style={{
  1193. width: '250px',
  1194. }}
  1195. filter={customFilter}
  1196. onChange={v => console.log(v)}
  1197. insetLabel="insetLabel"
  1198. value={value5}
  1199. onChange={setValue5}
  1200. >
  1201. {colorOptions.map(option => (
  1202. <Option value={option.value} key={option.value}>
  1203. <div style={alignStyle}>
  1204. <span style={option.spanStyle}></span>
  1205. {option.value}
  1206. </div>
  1207. </Option>
  1208. ))}
  1209. </Select>
  1210. </>
  1211. );
  1212. };
  1213. export const Controlled = () => <ControledSelect></ControledSelect>;
  1214. Controlled.story = {
  1215. name: 'controlled',
  1216. };
  1217. const UnControledSelect = () => {
  1218. const onChange = value => {
  1219. console.log(value);
  1220. };
  1221. return (
  1222. <>
  1223. <h5>defaultValue在list中不存在</h5>
  1224. <Select
  1225. defaultValue={90}
  1226. onChange={onChange}
  1227. style={{
  1228. width: 200,
  1229. }}
  1230. >
  1231. <Option value={20}>nick</Option>
  1232. <Option value={10}>jerry</Option>
  1233. <Option value={5}>mark</Option>
  1234. </Select>
  1235. <h5>defaultValue在list中存在</h5>
  1236. <Select
  1237. defaultValue={10}
  1238. onChange={onChange}
  1239. style={{
  1240. width: 200,
  1241. }}
  1242. >
  1243. <Option value={20}>nick</Option>
  1244. <Option value={10}>jerry</Option>
  1245. <Option value={5}>mark</Option>
  1246. </Select>
  1247. </>
  1248. );
  1249. };
  1250. export { UnControledSelect };
  1251. UnControledSelect.story = {
  1252. name: '非受控组件'
  1253. };
  1254. export const TestScroll = () => (
  1255. <div
  1256. style={{
  1257. marginTop: '600px',
  1258. marginBottom: '50px',
  1259. }}
  1260. >
  1261. <Select
  1262. style={{
  1263. width: '150px',
  1264. }}
  1265. >
  1266. <Option value="tony">IronMan</Option>
  1267. <Option value="Thor" disabled>
  1268. Thor
  1269. </Option>
  1270. <Option value="steve">Caption</Option>
  1271. <Option value="peter">SpiderBoy</Option>
  1272. </Select>
  1273. </div>
  1274. );
  1275. TestScroll.story = {
  1276. name: 'test scroll',
  1277. };
  1278. let optionList = [
  1279. {
  1280. value: 'tony',
  1281. label: 'Ironman',
  1282. },
  1283. {
  1284. value: 'Thor',
  1285. label: 'Thor',
  1286. },
  1287. {
  1288. value: 'steve',
  1289. label: 'Caption',
  1290. },
  1291. {
  1292. value: 'peter',
  1293. label: 'SpiderBoy',
  1294. },
  1295. ];
  1296. export const OptionList = () => (
  1297. <Select
  1298. style={{
  1299. width: '100px',
  1300. }}
  1301. optionList={optionList}
  1302. ></Select>
  1303. );
  1304. OptionList.story = {
  1305. name: 'optionList',
  1306. };
  1307. export const InsetLabel = () => (
  1308. <>
  1309. <Select
  1310. style={{
  1311. width: 300,
  1312. }}
  1313. insetLabel="主播类型"
  1314. placeholder="请选择"
  1315. optionList={optionList}
  1316. ></Select>
  1317. <Select
  1318. style={{
  1319. width: 300,
  1320. }}
  1321. multiple
  1322. insetLabel="主播类型"
  1323. optionList={optionList}
  1324. ></Select>
  1325. <Select
  1326. style={{
  1327. width: 300,
  1328. }}
  1329. filter
  1330. insetLabel="主播类型"
  1331. optionList={optionList}
  1332. ></Select>
  1333. <Select
  1334. style={{
  1335. width: 300,
  1336. }}
  1337. filter
  1338. multiple
  1339. insetLabel="主播类型"
  1340. optionList={optionList}
  1341. ></Select>
  1342. </>
  1343. );
  1344. InsetLabel.story = {
  1345. name: 'insetLabel',
  1346. };
  1347. export const ChangeOptionDynamic = () => {
  1348. function App() {
  1349. let [options, setOptions] = useState([]);
  1350. let [index, setIndex] = useState(0);
  1351. const addOption = () => {
  1352. const randomItem = optionList[index];
  1353. index = index + 1;
  1354. setIndex(index);
  1355. options = [...options, { ...randomItem }];
  1356. setOptions(options);
  1357. };
  1358. const reset = () => {
  1359. setOptions([]);
  1360. setIndex(0);
  1361. };
  1362. return (
  1363. <div>
  1364. <Select
  1365. style={{
  1366. width: '150px',
  1367. }}
  1368. defaultValue="tony"
  1369. >
  1370. {options.map((option, idx) => (
  1371. <Select.Option key={option.key || idx} value={option.value}>
  1372. {option.label}
  1373. </Select.Option>
  1374. ))}
  1375. </Select>
  1376. <h4>多选</h4>
  1377. <Select
  1378. style={{
  1379. width: '150px',
  1380. }}
  1381. multiple
  1382. defaultValue={['tony']}
  1383. >
  1384. {options.map((option, idx) => (
  1385. <Select.Option key={option.key || idx} value={option.value}>
  1386. {option.label}
  1387. </Select.Option>
  1388. ))}
  1389. </Select>
  1390. <button onClick={addOption}>add option</button>
  1391. <button onClick={reset}>reset</button>
  1392. </div>
  1393. );
  1394. }
  1395. return <App />;
  1396. };
  1397. ChangeOptionDynamic.story = {
  1398. name: 'change option dynamic',
  1399. };
  1400. let list = [
  1401. {
  1402. value: 'tony',
  1403. label: 'Ironman',
  1404. otherKey: {
  1405. role: 1,
  1406. },
  1407. },
  1408. {
  1409. value: 'Thor',
  1410. label: 'Thor',
  1411. otherKey: {
  1412. role: 2,
  1413. },
  1414. },
  1415. {
  1416. value: 'steve',
  1417. label: 'Caption',
  1418. otherKey: {
  1419. role: 3,
  1420. },
  1421. },
  1422. {
  1423. value: 'peter',
  1424. label: 'SpiderBoy',
  1425. otherKey: {
  1426. role: 4,
  1427. },
  1428. },
  1429. ];
  1430. const SearchDemo1 = () => {
  1431. const [optionList, setOptionList] = useState(list);
  1432. const [loading, setLoading] = useState(false);
  1433. const handleSearch = value => {
  1434. setLoading(true);
  1435. let length = Math.ceil(Math.random() * 10);
  1436. let result = Array.from(
  1437. {
  1438. length,
  1439. },
  1440. (v, i) => {
  1441. return {
  1442. value: value + i,
  1443. label: value + i,
  1444. otherKey: {
  1445. role: i,
  1446. },
  1447. };
  1448. }
  1449. );
  1450. setTimeout(() => {
  1451. setOptionList(result);
  1452. setLoading(false);
  1453. }, 1000);
  1454. };
  1455. const [value, setValue] = useState(optionList[0].value);
  1456. const onChange = value => {
  1457. console.log(value);
  1458. setValue(value);
  1459. };
  1460. return (
  1461. <div>
  1462. 受控:
  1463. <Select
  1464. filter
  1465. style={{
  1466. width: '150px',
  1467. }}
  1468. onSearch={v => handleSearch(v)}
  1469. optionList={optionList}
  1470. value={value}
  1471. loading={loading}
  1472. onChange={onChange}
  1473. ></Select>
  1474. 非受控:
  1475. <Select
  1476. style={{
  1477. width: '150px',
  1478. }}
  1479. filter
  1480. onSearch={v => handleSearch(v)}
  1481. optionList={optionList}
  1482. loading={loading}
  1483. onChange={onChange}
  1484. ></Select>
  1485. 多选非受控
  1486. <Select
  1487. style={{
  1488. width: '150px',
  1489. }}
  1490. filter
  1491. multiple
  1492. onSearch={v => handleSearch(v)}
  1493. optionList={optionList}
  1494. loading={loading}
  1495. onChange={onChange}
  1496. ></Select>
  1497. </div>
  1498. );
  1499. };
  1500. import debounce from 'lodash/debounce';
  1501. class SearchDemo2 extends React.Component {
  1502. constructor() {
  1503. super();
  1504. this.state = {
  1505. loading: false,
  1506. optionList: [
  1507. {
  1508. value: 'abc',
  1509. label: '抖音',
  1510. type: 1,
  1511. },
  1512. {
  1513. value: 'hotsoon',
  1514. label: '火山小视频',
  1515. type: 2,
  1516. },
  1517. {
  1518. value: 'pipixia',
  1519. label: '皮皮虾',
  1520. type: 3,
  1521. },
  1522. {
  1523. value: 'toutiao',
  1524. label: '今日头条',
  1525. type: 4,
  1526. },
  1527. ],
  1528. value: [],
  1529. };
  1530. this.handleSearch = debounce(this.handleSearch, 800).bind(this);
  1531. this.onChange = this.onChange.bind(this);
  1532. this.customRender = this.customRender.bind(this);
  1533. }
  1534. handleSearch(inputValue) {
  1535. this.setState({
  1536. loading: true,
  1537. });
  1538. let length = Math.ceil(Math.random() * 100);
  1539. let result = Array.from(
  1540. {
  1541. length,
  1542. },
  1543. (v, i) => {
  1544. return {
  1545. value: inputValue + i,
  1546. label: 'label' + i,
  1547. type: i + 1,
  1548. };
  1549. }
  1550. );
  1551. setTimeout(() => {
  1552. this.setState({
  1553. optionList: result,
  1554. loading: false,
  1555. });
  1556. }, 2000);
  1557. }
  1558. onChange(value) {
  1559. this.setState({
  1560. value,
  1561. });
  1562. console.log(value);
  1563. }
  1564. customRender(optionNode) {
  1565. return optionNode.value + optionNode.label;
  1566. }
  1567. render() {
  1568. const { loading, optionList, value } = this.state;
  1569. return (
  1570. <div>
  1571. <Select
  1572. style={{
  1573. width: 150,
  1574. }}
  1575. filter
  1576. labelInValue
  1577. onSearch={this.handleSearch}
  1578. optionList={optionList}
  1579. loading={loading}
  1580. onChange={this.onChange}
  1581. placeholder="请选择"
  1582. ></Select>
  1583. <br />
  1584. <br />
  1585. <Select
  1586. style={{
  1587. width: 180,
  1588. }}
  1589. filter // labelInValue
  1590. multiple
  1591. value={value}
  1592. renderSelectedItem={this.customRender}
  1593. onSearch={this.handleSearch}
  1594. optionList={optionList}
  1595. loading={loading}
  1596. onChange={this.onChange}
  1597. placeholder="请选择"
  1598. ></Select>
  1599. </div>
  1600. );
  1601. }
  1602. }
  1603. export const Search = () => (
  1604. <>
  1605. <SearchDemo1 />
  1606. <SearchDemo2 />
  1607. </>
  1608. );
  1609. Search.story = {
  1610. name: 'search',
  1611. };
  1612. export const IncomeDetail = ({ config = {}, params = {} }) => {
  1613. const [detailList, setDetailList] = useState([]);
  1614. const [hasMore, setHasMore] = useState(true);
  1615. const [loading, setLoading] = useState();
  1616. let lock;
  1617. const fetchData = (outParams = {}) => {
  1618. if (lock) {
  1619. return;
  1620. }
  1621. setLoading(true); // 参数
  1622. // 请求
  1623. fetch({
  1624. url: URL.user_profit,
  1625. method: 'get',
  1626. baseURL: config.webcast_host,
  1627. params,
  1628. })
  1629. .then(res => {
  1630. lock = false;
  1631. setLoading(false);
  1632. console.log('++++', data);
  1633. })
  1634. .catch(() => {
  1635. setLoading(false);
  1636. Toast.show('网络异常,请稍后重试');
  1637. });
  1638. };
  1639. useEffect(fetchData, []); // 监听滚动设置吸顶 以及加载更多
  1640. useEffect(() => {
  1641. window.addEventListener('scroll', function() {
  1642. // 加载更多
  1643. const scrollY = window.scrollY;
  1644. const scrollHeight = document.documentElement.scrollHeight;
  1645. const screenHeight = screen.height;
  1646. if (!loading && hasMore && scrollY + screenHeight + 300 > scrollHeight) {
  1647. fetchData();
  1648. }
  1649. });
  1650. }, [detailList.length]);
  1651. return (
  1652. <div>
  1653. <Select></Select>
  1654. </div>
  1655. );
  1656. };
  1657. export const AllowCreate = () => (
  1658. <Select
  1659. style={{
  1660. width: 500,
  1661. }}
  1662. optionList={optionList}
  1663. allowCreate={true}
  1664. multiple={true}
  1665. filter={true}
  1666. onChange={v => console.log(v)}
  1667. ></Select>
  1668. );
  1669. AllowCreate.story = {
  1670. name: 'allowCreate',
  1671. };
  1672. export const AllowCreateCustomRender = () => (
  1673. <Select
  1674. style={{
  1675. width: 500,
  1676. }}
  1677. optionList={optionList}
  1678. allowCreate={true}
  1679. multiple={true}
  1680. filter={true}
  1681. onChange={v => console.log(v)}
  1682. renderCreateItem={v => `semi: ${v}`}
  1683. ></Select>
  1684. );
  1685. AllowCreateCustomRender.story = {
  1686. name: 'allowCreate custom render',
  1687. };
  1688. let AllowCreateControledDemo = () => {
  1689. let [value, setValue] = useState();
  1690. const optionList = [
  1691. {
  1692. value: 'abc',
  1693. label: '抖音',
  1694. },
  1695. {
  1696. value: 'hotsoon',
  1697. label: '火山小视频',
  1698. },
  1699. {
  1700. value: 'pipixia',
  1701. label: '皮皮虾',
  1702. },
  1703. {
  1704. value: 'toutiao',
  1705. label: '今日头条',
  1706. },
  1707. ];
  1708. const [list, setList] = useState(optionList);
  1709. const handleSelect = v => {
  1710. var lastOne = v[v.length - 1];
  1711. if (lastOne && list.findIndex(item => item.value === lastOne) == -1) {
  1712. list.push({
  1713. value: lastOne,
  1714. label: lastOne,
  1715. });
  1716. }
  1717. setList(list);
  1718. setValue(v);
  1719. };
  1720. return (
  1721. <Select
  1722. style={{
  1723. width: 400,
  1724. }}
  1725. optionList={list}
  1726. allowCreate={true}
  1727. multiple={true}
  1728. filter={true}
  1729. value={value}
  1730. onChange={handleSelect}
  1731. ></Select>
  1732. );
  1733. };
  1734. const AllowCreateDemo = () => {
  1735. let [value, setValue] = useState();
  1736. const optionList = [
  1737. {
  1738. value: 'abc',
  1739. label: '抖音',
  1740. },
  1741. {
  1742. value: 'hotsoon',
  1743. label: '火山小视频',
  1744. },
  1745. {
  1746. value: 'pipixia',
  1747. label: '皮皮虾',
  1748. },
  1749. {
  1750. value: 'toutiao',
  1751. label: '今日头条',
  1752. },
  1753. ];
  1754. const [list, setList] = useState(optionList);
  1755. const handleSelect = v => {
  1756. var lastOne = v[v.length - 1];
  1757. if (lastOne && list.findIndex(item => item.value === lastOne) == -1) {
  1758. list.push({
  1759. value: lastOne,
  1760. label: lastOne,
  1761. });
  1762. }
  1763. setList(list); // setValue(v)
  1764. };
  1765. return (
  1766. <Select
  1767. style={{
  1768. width: 400,
  1769. }}
  1770. optionList={list}
  1771. defaultValue={['abc', 'hotsoon']}
  1772. allowCreate={true}
  1773. multiple={true}
  1774. filter={true}
  1775. onChange={handleSelect}
  1776. ></Select>
  1777. );
  1778. };
  1779. export const AllowCreateWithDefaultValue = () => <AllowCreateDemo />;
  1780. AllowCreateWithDefaultValue.story = {
  1781. name: 'allowCreate with defaultValue',
  1782. };
  1783. class HideDemo extends React.Component {
  1784. constructor(props) {
  1785. super(props);
  1786. this.state = {
  1787. optionList: [
  1788. {
  1789. value: 'abc',
  1790. label: '抖音',
  1791. },
  1792. {
  1793. value: 'hotsoon',
  1794. label: '火山小视频',
  1795. },
  1796. {
  1797. value: 'pipixia',
  1798. label: '皮皮虾',
  1799. },
  1800. {
  1801. value: 'toutiao',
  1802. label: '今日头条',
  1803. },
  1804. ],
  1805. selectedItems: [],
  1806. };
  1807. this.onChange = this.onChange.bind(this);
  1808. }
  1809. onChange(selectedItems) {
  1810. this.setState({
  1811. selectedItems,
  1812. });
  1813. }
  1814. render() {
  1815. let { optionList, selectedItems } = this.state;
  1816. let filterOptions = optionList.filter(option => !selectedItems.includes(option.value));
  1817. return (
  1818. <Select
  1819. value={selectedItems}
  1820. multiple
  1821. style={{
  1822. width: 300,
  1823. }}
  1824. onChange={this.onChange}
  1825. optionList={filterOptions}
  1826. ></Select>
  1827. );
  1828. }
  1829. }
  1830. export const AutoHiddenSelectedItem = () => <HideDemo></HideDemo>;
  1831. AutoHiddenSelectedItem.story = {
  1832. name: 'auto hidden selected item',
  1833. };
  1834. class CustomCreate extends React.Component {
  1835. constructor(props) {
  1836. super(props);
  1837. this.state = {
  1838. optionList: [
  1839. {
  1840. value: 'abc',
  1841. label: '抖音',
  1842. },
  1843. {
  1844. value: 'hotsoon',
  1845. label: '火山小视频',
  1846. },
  1847. {
  1848. value: 'pipixia',
  1849. label: '皮皮虾',
  1850. },
  1851. {
  1852. value: 'toutiao',
  1853. label: '今日头条',
  1854. },
  1855. {
  1856. value: 0,
  1857. label: 0,
  1858. },
  1859. ],
  1860. selectedItems: ['fefe'],
  1861. };
  1862. this.onChange = this.onChange.bind(this);
  1863. this.customRender = this.customRender.bind(this);
  1864. this.search = this.search.bind(this);
  1865. }
  1866. onChange(selectedItems) {
  1867. console.log(selectedItems); // this.setState({ selectedItems, optionList: [] });
  1868. this.setState({
  1869. selectedItems,
  1870. }); // this.setState({ optionList: [] });
  1871. }
  1872. customRender(v) {
  1873. return (
  1874. <>
  1875. <span>label:{v.label}</span>
  1876. <span>value:{v.value}</span>
  1877. </>
  1878. );
  1879. }
  1880. customCreate(inputValue, isFocus) {
  1881. let style = {
  1882. padding: 12,
  1883. cursor: 'pointer',
  1884. backgroundColor: isFocus ? 'var(--semi-color-fill-0)' : '#FFF',
  1885. };
  1886. return <div style={style}>{'create' + inputValue}</div>;
  1887. }
  1888. search(inputValue) {
  1889. let length = Math.ceil(Math.random() * 10);
  1890. let result = Array.from(
  1891. {
  1892. length,
  1893. },
  1894. (v, i) => {
  1895. return {
  1896. value: inputValue + i,
  1897. label: inputValue + i,
  1898. type: i + 1,
  1899. };
  1900. }
  1901. );
  1902. console.log(result); // result = result.concat(selectedOption);
  1903. this.setState({
  1904. optionList: result,
  1905. });
  1906. }
  1907. render() {
  1908. let { optionList, selectedItems } = this.state;
  1909. return (
  1910. <>
  1911. <Select
  1912. defaultValue={['abc']}
  1913. filter
  1914. style={{
  1915. width: 300,
  1916. }}
  1917. multiple
  1918. optionList={optionList}
  1919. onSearch={this.search}
  1920. onChange={this.onChange}
  1921. emptyContent={null} // onChangeWithObject
  1922. ></Select>
  1923. </>
  1924. );
  1925. }
  1926. }
  1927. export const _CustomCreate = () => <CustomCreate></CustomCreate>;
  1928. _CustomCreate.story = {
  1929. name: 'CustomCreate',
  1930. };
  1931. class OptionGroupDemo extends React.Component {
  1932. constructor(props) {
  1933. super(props);
  1934. this.handleSearch = this.handleSearch.bind(this);
  1935. this.state = {
  1936. groups: [
  1937. {
  1938. label: 'Asia',
  1939. children: [
  1940. {
  1941. label: 'China',
  1942. value: 'zhongguo',
  1943. },
  1944. {
  1945. label: 'Koera',
  1946. value: 'hanguo',
  1947. },
  1948. ],
  1949. },
  1950. {
  1951. label: 'Europe',
  1952. children: [
  1953. {
  1954. label: 'Germany',
  1955. value: 'deguo',
  1956. },
  1957. {
  1958. label: 'France',
  1959. value: 'faguo',
  1960. },
  1961. ],
  1962. },
  1963. {
  1964. label: 'Other',
  1965. children: [
  1966. {
  1967. label: 'vf',
  1968. value: 'Sourth',
  1969. },
  1970. ],
  1971. },
  1972. ],
  1973. };
  1974. }
  1975. handleSearch(input) {
  1976. let groups = [1, 2, 3].map(i => {
  1977. return {
  1978. label: i,
  1979. // label: Math.random(),
  1980. children: [10, 20].map(j => {
  1981. return {
  1982. label: Math.random(),
  1983. value: Math.random(),
  1984. };
  1985. }),
  1986. };
  1987. });
  1988. this.setState({
  1989. groups,
  1990. });
  1991. }
  1992. renderGroup(group, index) {
  1993. const options = group.children.map(option => (
  1994. <Select.Option value={option.value} label={option.label} key={option.label} />
  1995. ));
  1996. return <Select.OptGroup key={`${index}-${group.label}`} label={group.label}>{options}</Select.OptGroup>;
  1997. }
  1998. render() {
  1999. const { groups } = this.state;
  2000. return (
  2001. <>
  2002. <Select
  2003. placeholder=""
  2004. style={{
  2005. width: 180,
  2006. }}
  2007. filter
  2008. onSearch={this.handleSearch}
  2009. remote
  2010. >
  2011. {groups.map((group, index) => this.renderGroup(group, index))}
  2012. </Select>
  2013. </>
  2014. );
  2015. }
  2016. }
  2017. export const SelectOptionGroup = () => <OptionGroupDemo />;
  2018. SelectOptionGroup.story = {
  2019. name: 'Select OptionGroup',
  2020. };
  2021. const BlurDemo = () => {
  2022. const onBlur = (value, e) => {
  2023. console.log(value);
  2024. console.log(e);
  2025. };
  2026. const onFocus = (value, e) => {
  2027. console.log(value);
  2028. console.log(e);
  2029. };
  2030. return (
  2031. <>
  2032. <Select
  2033. filter
  2034. placeholder=""
  2035. style={{
  2036. width: 180,
  2037. }}
  2038. onBlur={onBlur}
  2039. onFocus={onFocus}
  2040. >
  2041. <Select.Option value="zhongguo">China</Select.Option>
  2042. <Select.Option value="hanguo">Koera</Select.Option>
  2043. <Select.Option value="deguo">Germany</Select.Option>
  2044. <Select.Option value="faguo">France</Select.Option>
  2045. </Select>
  2046. </>
  2047. );
  2048. };
  2049. export const SelectOnBlurOnFocus = () => <BlurDemo></BlurDemo>;
  2050. SelectOnBlurOnFocus.story = {
  2051. name: 'Select onBlur/onFocus',
  2052. };
  2053. const AutoAdjustOverflowDemo = () => {
  2054. const [list, setList] = useState([
  2055. {
  2056. value: 'abc',
  2057. label: '1111',
  2058. },
  2059. {
  2060. value: 'hotsoon',
  2061. label: '1112',
  2062. },
  2063. {
  2064. value: 'pipixia',
  2065. label: '1113',
  2066. },
  2067. {
  2068. value: 'toutiao',
  2069. label: '1114',
  2070. },
  2071. ]);
  2072. const onSearch = () => {
  2073. let newList = Array.from(
  2074. {
  2075. length: Math.floor(Math.random() * 10),
  2076. },
  2077. (v, i) => {
  2078. return {
  2079. value: i,
  2080. label: i,
  2081. };
  2082. }
  2083. );
  2084. setList(newList);
  2085. console.log(newList);
  2086. };
  2087. return (
  2088. <div
  2089. style={{
  2090. height: 180,
  2091. margin: 250,
  2092. border: '1px solid pink',
  2093. }}
  2094. >
  2095. <Select
  2096. optionList={list}
  2097. filter={true}
  2098. remote={true}
  2099. onSearch={onSearch}
  2100. style={{
  2101. width: 200,
  2102. }}
  2103. multiple
  2104. />
  2105. </div>
  2106. );
  2107. };
  2108. export const AutoAdjustOverflow = () => <AutoAdjustOverflowDemo></AutoAdjustOverflowDemo>;
  2109. AutoAdjustOverflow.story = {
  2110. name: 'autoAdjustOverflow',
  2111. };
  2112. const AllowCreateWithFilter = () => {
  2113. const [list, setList] = useState([
  2114. {
  2115. value: 'abc',
  2116. label: 'abc',
  2117. otherKey: 'abc',
  2118. },
  2119. {
  2120. value: 'hotsoon',
  2121. label: 'hotsoon',
  2122. otherKey: 'efg',
  2123. },
  2124. {
  2125. value: 'pipixia',
  2126. label: 'pipixia',
  2127. otherKey: 'hij',
  2128. },
  2129. {
  2130. value: 'toutiao',
  2131. label: 'toutiao',
  2132. otherKey: 'klm',
  2133. },
  2134. ]);
  2135. const filter = (sugInput, option) => {
  2136. let compareKey = option.otherKey ? option.otherKey.toUpperCase() : '';
  2137. let sug = sugInput.toUpperCase();
  2138. return compareKey.includes(sug);
  2139. };
  2140. return (
  2141. <div
  2142. style={{
  2143. height: 180,
  2144. margin: 250,
  2145. }}
  2146. >
  2147. <Select
  2148. optionList={list}
  2149. multiple
  2150. filter={filter}
  2151. style={{
  2152. width: 200,
  2153. }}
  2154. allowCreate
  2155. />
  2156. </div>
  2157. );
  2158. };
  2159. export const FilterAllowCreate = () => <AllowCreateWithFilter></AllowCreateWithFilter>;
  2160. FilterAllowCreate.story = {
  2161. name: 'Filter + allowCreate',
  2162. };
  2163. const SelectRefDemo = () => {
  2164. const ref = useRef();
  2165. const secondRef = useRef();
  2166. const [open, setOpen] = useState(false);
  2167. const list = [
  2168. {
  2169. value: 'abc',
  2170. label: 'Abc',
  2171. },
  2172. {
  2173. value: 'hotsoon',
  2174. label: 'Hotsoon',
  2175. },
  2176. {
  2177. value: 'pipixia',
  2178. label: 'Pipixia',
  2179. },
  2180. {
  2181. value: 'toutiao',
  2182. label: 'TooBuzz',
  2183. },
  2184. ];
  2185. const change = () => {
  2186. if (!open) {
  2187. ref.current.open();
  2188. setOpen(true);
  2189. } else {
  2190. ref.current.close();
  2191. setOpen(false);
  2192. }
  2193. };
  2194. const focus = () => {
  2195. ref.current.focus();
  2196. };
  2197. const clearInput = () => {
  2198. ref.current.clearInput();
  2199. };
  2200. const deselectAll = () => {
  2201. ref.current.deselectAll();
  2202. };
  2203. const selectAll = () => {
  2204. ref.current.selectAll();
  2205. };
  2206. return (
  2207. <>
  2208. <h4>onChangeWithObject = false</h4>
  2209. <Select
  2210. innerBottomSlot={
  2211. <div>
  2212. <Space>
  2213. <Button onClick={change}>close</Button>
  2214. <Button onClick={clearInput}>clearInput</Button>
  2215. <Button onClick={deselectAll}>deselectAll</Button>
  2216. <Button onClick={selectAll}>selectAll</Button>
  2217. </Space>
  2218. </div>
  2219. }
  2220. ref={ref}
  2221. onChange={e => console.log(e)}
  2222. placeholder="Business line"
  2223. style={{
  2224. width: 180,
  2225. }}
  2226. optionList={list}
  2227. filter
  2228. multiple
  2229. ></Select>
  2230. <Space>
  2231. <Button onClick={change}>open</Button>
  2232. <Button onClick={focus}>focus</Button>
  2233. <Button onClick={clearInput}>clearInput</Button>
  2234. <Button onClick={deselectAll}>deselectAll</Button>
  2235. <Button onClick={selectAll}>selectAll</Button>
  2236. </Space>
  2237. <h4
  2238. style={{
  2239. marginTop: 20,
  2240. }}
  2241. >
  2242. onChangeWithObject = true
  2243. </h4>
  2244. <Select
  2245. innerBottomSlot={
  2246. <div>
  2247. <Space></Space>
  2248. </div>
  2249. }
  2250. onChange={e => console.log(e)}
  2251. onChangeWithObject
  2252. ref={secondRef}
  2253. placeholder="Business line"
  2254. style={{
  2255. width: 180,
  2256. }}
  2257. optionList={list}
  2258. filter
  2259. multiple
  2260. ></Select>
  2261. <Space>
  2262. <Button onClick={() => secondRef.current.deselectAll()}>deselectAll</Button>
  2263. <Button onClick={() => secondRef.current.selectAll()}>selectAll</Button>
  2264. </Space>
  2265. </>
  2266. );
  2267. };
  2268. export const Ref = () => <SelectRefDemo />;
  2269. Ref.story = {
  2270. name: 'ref',
  2271. };
  2272. export const CustomTriggerDemo = () => <CustomTrigger />;
  2273. CustomTriggerDemo.story = {
  2274. name: 'custom trigger'
  2275. }
  2276. class VirtualizeClassDemo extends React.Component {
  2277. constructor(props) {
  2278. super(props);
  2279. // this.handleSearch = this.handleSearch.bind(this);
  2280. let newOptions = Array.from({ length: 1000 }, (v, i) => ({ label: `o-${i}`, value: `v-${v}-${i}` }));
  2281. this.state = {
  2282. optionList: newOptions,
  2283. };
  2284. }
  2285. render() {
  2286. let { groups, optionList } = this.state;
  2287. let virtualize = {
  2288. height: 300,
  2289. widht: '100%',
  2290. itemSize: 36,
  2291. };
  2292. return (
  2293. <>
  2294. <Select
  2295. placeholder=""
  2296. style={{ width: 180 }}
  2297. filter
  2298. onSearch={this.handleSearch}
  2299. virtualize={virtualize}
  2300. optionList={optionList}
  2301. ></Select>
  2302. </>
  2303. );
  2304. }
  2305. }
  2306. export const VirtualizeDemo = () => <VirtualizeClassDemo />;
  2307. VirtualizeDemo.story = {
  2308. name: 'virtualize select'
  2309. }
  2310. const SelectPosition = () => {
  2311. return (
  2312. <div
  2313. style={{
  2314. height: 500,
  2315. border: '1px solid red',
  2316. overflow: 'auto',
  2317. }}
  2318. >
  2319. <p>p</p>
  2320. <p>p</p>
  2321. <p>p</p>
  2322. <p>p</p>
  2323. <p>p</p>
  2324. <p>p</p>
  2325. <p>p</p>
  2326. <p>p</p>
  2327. <p>p</p>
  2328. <p>p</p>
  2329. <p>p</p>
  2330. <p>p</p>
  2331. <p>p</p>
  2332. <p>p</p>
  2333. <p>p</p>
  2334. <p>p</p>
  2335. <p>p</p>
  2336. <p>p</p>
  2337. <p>p</p>
  2338. <p>p</p>
  2339. <p>p</p>
  2340. <p>p</p>
  2341. <p>p</p>
  2342. <Select
  2343. defaultValue="abc"
  2344. style={{
  2345. width: 120,
  2346. }}
  2347. >
  2348. <Option value="abc">抖音</Option>
  2349. <Option value="hotsoon">火山</Option>
  2350. <Option value="pipixia" disabled>
  2351. 皮皮虾
  2352. </Option>
  2353. <Option value="xigua">西瓜视频</Option>
  2354. </Select>
  2355. <p>p</p>
  2356. <p>p</p>
  2357. <p>p</p>
  2358. <p>p</p>
  2359. <p>p</p>
  2360. <p>p</p>
  2361. <p>p</p>
  2362. <p>p</p>
  2363. <p>p</p>
  2364. <p>p</p>
  2365. <p>p</p>
  2366. <p>p</p>
  2367. <p>p</p>
  2368. <p>p</p>
  2369. <p>p</p>
  2370. <p>p</p>
  2371. <p>p</p>
  2372. <p>p</p>
  2373. <p>p</p>
  2374. <p>p</p>
  2375. <p>p</p>
  2376. <p>p</p>
  2377. <p>p</p>
  2378. <p>p</p>
  2379. <p>p</p>
  2380. <p>p</p>
  2381. <p>p</p>
  2382. <p>p</p>
  2383. <p>p</p>
  2384. <p>p</p>
  2385. <p>p</p>
  2386. <p>p</p>
  2387. <p>p</p>
  2388. <p>p</p>
  2389. <p>p</p>
  2390. <p>p</p>
  2391. <p>p</p>
  2392. <p>p</p>
  2393. <p>p</p>
  2394. <p>p</p>
  2395. <p>p</p>
  2396. <p>p</p>
  2397. </div>
  2398. );
  2399. };
  2400. export { SelectPosition };
  2401. SelectPosition.story = {
  2402. name: 'Select position problem'
  2403. }
  2404. const RenderOptionDemo = () => {
  2405. const renderOptionItem = renderProps => {
  2406. const {
  2407. disabled,
  2408. selected,
  2409. label,
  2410. value,
  2411. focused,
  2412. className,
  2413. style,
  2414. onMouseEnter,
  2415. onClick,
  2416. empty,
  2417. emptyContent,
  2418. ...rest
  2419. } = renderProps;
  2420. const optionCls = classNames({
  2421. ['custom-option-render']: true,
  2422. ['custom-option-render-focused']: focused,
  2423. ['custom-option-render-disabled']: disabled,
  2424. ['custom-option-render-selected']: selected,
  2425. }); // Notice:
  2426. // 1.props传入的style需在wrapper dom上进行消费,否则在虚拟化场景下会无法正常使用
  2427. // 2.选中(selected)、聚焦(focused)、禁用(disabled)等状态的样式需自行加上,你可以从props中获取到相对的boolean值
  2428. // 3.onMouseEnter需在wrapper dom上绑定,否则上下键盘操作时显示会有问题
  2429. return (
  2430. <div
  2431. style={style}
  2432. className={optionCls}
  2433. onClick={() => onClick()}
  2434. onMouseEnter={e => onMouseEnter()}
  2435. >
  2436. <Checkbox checked={selected} />
  2437. <div className="option-right">{label}</div>
  2438. </div>
  2439. );
  2440. };
  2441. return (
  2442. <>
  2443. <Select
  2444. filter
  2445. dropdownClassName="components-select-demo-renderOptionItem"
  2446. optionList={optionList}
  2447. style={{
  2448. width: 300,
  2449. }}
  2450. renderOptionItem={renderOptionItem}
  2451. />
  2452. <br />
  2453. <br />
  2454. <Select
  2455. filter
  2456. multiple
  2457. dropdownClassName="components-select-demo-renderOptionItem"
  2458. optionList={optionList}
  2459. style={{
  2460. width: 450,
  2461. }}
  2462. renderOptionItem={renderOptionItem}
  2463. />
  2464. </>
  2465. );
  2466. };
  2467. export const RenderOptionItem = () => <RenderOptionDemo />;
  2468. RenderOptionItem.story = {
  2469. name: 'renderOptionItem',
  2470. };
  2471. const FilterDefaultOpen = () => {
  2472. const [value1, setValue1] = useState('a-1');
  2473. return (
  2474. <>
  2475. <Select
  2476. placeholder=""
  2477. style={{
  2478. width: 180,
  2479. }}
  2480. filter
  2481. defaultOpen
  2482. >
  2483. <Select.OptGroup label="Asia">
  2484. <Select.Option value="a-1">China</Select.Option>
  2485. <Select.Option value="a-2">Koera</Select.Option>
  2486. </Select.OptGroup>
  2487. <Select.OptGroup label="Europe">
  2488. <Select.Option value="b-1">Germany</Select.Option>
  2489. <Select.Option value="b-2">France</Select.Option>
  2490. </Select.OptGroup>
  2491. <Select.OptGroup label="South America">
  2492. <Select.Option value="c-1">Peru</Select.Option>
  2493. </Select.OptGroup>
  2494. </Select>
  2495. <Select
  2496. placeholder=""
  2497. style={{
  2498. width: 180,
  2499. marginLeft: 20,
  2500. }}
  2501. filter
  2502. defaultOpen
  2503. defaultValue="a-2"
  2504. >
  2505. <Select.OptGroup label="Asia">
  2506. <Select.Option value="a-1">China</Select.Option>
  2507. <Select.Option value="a-2">Koera</Select.Option>
  2508. </Select.OptGroup>
  2509. <Select.OptGroup label="Europe">
  2510. <Select.Option value="b-1">Germany</Select.Option>
  2511. <Select.Option value="b-2">France</Select.Option>
  2512. </Select.OptGroup>
  2513. <Select.OptGroup label="South America">
  2514. <Select.Option value="c-1">Peru</Select.Option>
  2515. </Select.OptGroup>
  2516. </Select>
  2517. <Select
  2518. placeholder=""
  2519. style={{
  2520. width: 180,
  2521. marginLeft: 20,
  2522. }}
  2523. filter
  2524. defaultOpen
  2525. value={value1}
  2526. onChange={val => setValue1(val)}
  2527. >
  2528. <Select.Option value="a-1">China</Select.Option>
  2529. <Select.Option value="a-2">Koera</Select.Option>
  2530. <Select.Option value="b-1">Germany</Select.Option>
  2531. <Select.Option value="b-2">France</Select.Option>
  2532. <Select.Option value="c-1">Peru</Select.Option>
  2533. </Select>
  2534. </>
  2535. );
  2536. };
  2537. export { FilterDefaultOpen };
  2538. FilterDefaultOpen.story = {
  2539. name: 'Filter + defaultOpen'
  2540. };
  2541. const CustomSelect = props => {
  2542. const { fieldRef, ...rest } = props;
  2543. return <Select {...rest} ref={fieldRef} />;
  2544. };
  2545. const CustomFieldSelect = withField(CustomSelect);
  2546. const RefDemo = () => {
  2547. const fieldRef = useRef(null);
  2548. const onChange = () => {
  2549. console.log(fieldRef);
  2550. fieldRef.current.open();
  2551. debugger;
  2552. };
  2553. return (
  2554. <Form>
  2555. <CustomFieldSelect field="test" initValue="se" fieldRef={fieldRef}></CustomFieldSelect>
  2556. <Button onClick={onChange}>change</Button>
  2557. </Form>
  2558. );
  2559. };
  2560. export const RefFieldDemo = () => <RefDemo />;
  2561. RefFieldDemo.story = {
  2562. name: 'Ref field demo',
  2563. };
  2564. const ValueZeroDemo = () => {
  2565. const list = [
  2566. {
  2567. value: 6,
  2568. label: '抖音小视频',
  2569. otherKey: 0,
  2570. },
  2571. {
  2572. value: 1,
  2573. label: '火山小视频',
  2574. disabled: true,
  2575. otherKey: 1,
  2576. },
  2577. {
  2578. value: 'pipixia',
  2579. label: '皮皮虾',
  2580. otherKey: 2,
  2581. },
  2582. {
  2583. value: 'toutiao',
  2584. label: '今日头条',
  2585. otherKey: 3,
  2586. },
  2587. ];
  2588. return (
  2589. <Select
  2590. placeholder="请选择业务线"
  2591. style={{
  2592. width: 180,
  2593. }}
  2594. optionList={list}
  2595. value={0}
  2596. renderSelectedItem={option => option.label + 1}
  2597. ></Select>
  2598. );
  2599. };
  2600. export const Value0 = () => <ValueZeroDemo />;
  2601. Value0.story = {
  2602. name: 'value=0',
  2603. };
  2604. const Highlight = () => {
  2605. const searchWords = ['do', 'dollar'];
  2606. const sourceString = 'aaa do dollar aaa';
  2607. const result = getHighLightTextHTML({
  2608. searchWords,
  2609. sourceString,
  2610. });
  2611. const result2 = getHighLightTextHTML({
  2612. searchWords: ['z'],
  2613. sourceString: 'aaazaaazaaa',
  2614. });
  2615. return result2;
  2616. };
  2617. export const _Highlight = () => <Highlight />;
  2618. _Highlight.story = {
  2619. name: 'highlight',
  2620. };
  2621. export const ScrollIntoView = () => (
  2622. <div>
  2623. <p>single selection</p>
  2624. <Select defaultValue='v-11' defaultOpen style={{ width: 120, marginBottom: 300 }}>
  2625. {new Array(50).fill(null).map((item, idx) => (
  2626. <Option value={`v-${idx}`} key={idx}>{`option-${idx}`}</Option>
  2627. ))}
  2628. </Select>
  2629. <p>single selection with no selected item</p>
  2630. <Select style={{ marginBottom: 300, width: 120 }}>
  2631. {new Array(50).fill(null).map((item, idx) => (
  2632. <Option value={`v-${idx}`} key={idx}>{`option-${idx}`}</Option>
  2633. ))}
  2634. </Select>
  2635. <p>The selected node is the last</p>
  2636. <Select defaultValue='v-49' defaultOpen style={{ marginBottom: 300, width: 120 }}>
  2637. {new Array(50).fill(null).map((item, idx) => (
  2638. <Option value={`v-${idx}`} key={idx}>{`option-${idx}`}</Option>
  2639. ))}
  2640. </Select>
  2641. <p>The selected node is the first</p>
  2642. <Select defaultValue='v-0' style={{ marginBottom: 300, width: 120 }}>
  2643. {new Array(50).fill(null).map((item, idx) => (
  2644. <Option value={`v-${idx}`} key={idx}>{`option-${idx}`}</Option>
  2645. ))}
  2646. </Select>
  2647. <p>multiple selection</p>
  2648. <Select defaultValue={['v-25', 'v-9']} multiple style={{ marginBottom: 300, width: 220 }}>
  2649. {new Array(30).fill(null).map((item, idx) => (
  2650. <Option value={`v-${idx}`} key={idx}>{`option-${idx}`}</Option>
  2651. ))}
  2652. </Select>
  2653. <p>multiple selection with no selected item</p>
  2654. <Select multiple style={{ marginBottom: 300, width: 220 }}>
  2655. {new Array(30).fill(null).map((item, idx) => (
  2656. <Option value={`v-${idx}`} key={idx}>{`option-${idx}`}</Option>
  2657. ))}
  2658. </Select>
  2659. </div>
  2660. );
  2661. ScrollIntoView.story = {
  2662. name: 'scroll into view',
  2663. };
  2664. export const SelectInputPropsDemo = () => {
  2665. const inputProps = {
  2666. className: 'ttt',
  2667. onCompositionEnd: (v) => console.log(v.target.value)
  2668. };
  2669. return (
  2670. <Select
  2671. // onSearch={(v) => console.log(v)}
  2672. optionList={list}
  2673. inputProps={inputProps}
  2674. multiple
  2675. filter
  2676. style={{ width: 200 }}
  2677. >
  2678. </Select>
  2679. )
  2680. };
  2681. SelectInputPropsDemo.story = {
  2682. name: 'inputProps',
  2683. };
  2684. export const AutoClearSearchValue = () => {
  2685. const [val, setVal] = useState(['semi1']);
  2686. const optionList = [
  2687. { label: 'semi1', value: 'semi1' },
  2688. { label: 'semi2', value: 'semi2' },
  2689. { label: 'semi3', value: 'semi3' },
  2690. { label: 'semi4', value: 'semi4' },
  2691. { label: 'semi5', value: 'semi5' },
  2692. { label: 'semi6', value: 'semi6' },
  2693. ];
  2694. return (
  2695. <>
  2696. <h4>Controlled mode + multiple</h4>
  2697. <Select style={{ width: 400 }} multiple optionList={optionList} filter value={val} onChange={value => setVal(value)} autoClearSearchValue={false}></Select>
  2698. <br />
  2699. <br />
  2700. <h4>Uncontrolled mode + multiple</h4>
  2701. <Select style={{ width: 400 }} multiple optionList={optionList} filter autoClearSearchValue={false}></Select>
  2702. <h4>Uncontrolled mode + multiple + defaultValue</h4>
  2703. <Select style={{ width: 400 }} multiple optionList={optionList} filter defaultValue={['semi2']} autoClearSearchValue={false}></Select>
  2704. </>
  2705. )
  2706. }
  2707. SelectInputPropsDemo.story = {
  2708. name: 'AutoClearSearchValue',
  2709. };