select.stories.jsx 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987
  1. import React, { useState, useRef, useEffect } from 'react';
  2. import './select.scss';
  3. import { Input, Select, Button, Icon, Avatar, Checkbox, Form, withField, Space, Tag } 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. showClear
  721. autoFocus
  722. onSearch={(val) => console.log(`onSearch:${val}`)}
  723. onFocus={() => console.log('onFocus')}
  724. onBlur={() => console.log('onBlur')}
  725. >
  726. <Option value={1}>opt1</Option>
  727. <Option value={2} disabled>
  728. disabled
  729. </Option>
  730. <Option value={3}>Lucy</Option>
  731. <Option value="4">bay</Option>
  732. <Option value="5">sert</Option>
  733. <Option value="6">wym</Option>
  734. <Option value="7" disabled>
  735. meno
  736. </Option>
  737. <Option value="8">opts</Option>
  738. </Select>
  739. <h5>自定义筛选函数</h5>
  740. <Select
  741. style={{
  742. width: '250px',
  743. margin: 10,
  744. }}
  745. filter={filter}
  746. showClear
  747. onBlur={() => console.log('onBlur')}
  748. onSearch={val => console.log(val)}
  749. onFocus={() => console.log('onFocus')}
  750. >
  751. <Option value={1}>opt1(value:1)</Option>
  752. <Option value={2}>mike(value:2)</Option>
  753. <Option value={3}>Lucy(value:3)</Option>
  754. <Option value={4}>bay(value:4)</Option>
  755. </Select>
  756. <h5>filter为true,但option label为node时</h5>
  757. <Select
  758. style={{
  759. width: '250px',
  760. margin: 10,
  761. }}
  762. showClear
  763. filter={customFilter}
  764. onChange={v => console.log(v)}
  765. insetLabel="insetLabel"
  766. onFocus={() => console.log('onFocus')}
  767. onBlur={() => console.log('onBlur')}
  768. onSearch={(val) => console.log(val)}
  769. >
  770. {colorOptions.map(option => (
  771. <Option value={option.value} key={option.value}>
  772. <div style={alignStyle}>
  773. <span style={option.spanStyle}></span>
  774. {option.value}
  775. </div>
  776. </Option>
  777. ))}
  778. </Select>
  779. </div>
  780. );
  781. SelectFilterSingle.story = {
  782. name: 'select filter single',
  783. };
  784. export const SelectFilterMultiple = () => (
  785. <>
  786. <Select
  787. filter
  788. multiple={true}
  789. style={{
  790. width: '250px',
  791. }}
  792. placeholder="fefe"
  793. >
  794. <Option value={1}>opt1</Option>
  795. <Option value={2}>opt2</Option>
  796. <Option value={3}>opt22</Option>
  797. <Option value={3}>opt3</Option>
  798. <Option value={4}>opt4</Option>
  799. <Option value={5}>opt5</Option>
  800. <Option value={6}>opt6</Option>
  801. <Option value={7}>opt7</Option>
  802. <Option value={8}>opt8</Option>
  803. </Select>
  804. <Select
  805. filter
  806. multiple={true}
  807. maxTagCount={3}
  808. style={{
  809. width: '270px',
  810. }}
  811. placeholder="fefe"
  812. >
  813. <Option value={1}>opt1</Option>
  814. <Option value={2}>opt2</Option>
  815. <Option value={3}>opt22</Option>
  816. <Option value={3}>opt3</Option>
  817. <Option value={4}>opt4</Option>
  818. <Option value={5}>opt5</Option>
  819. <Option value={6}>opt6</Option>
  820. <Option value={7}>opt7</Option>
  821. <Option value={8}>opt8</Option>
  822. </Select>
  823. </>
  824. );
  825. SelectFilterMultiple.story = {
  826. name: 'select filter multiple',
  827. };
  828. const OptionLabelProp = () => {
  829. const [value, setValue] = useState(1);
  830. return (
  831. <>
  832. 设置optionLabelProp属性(默认为'children')为'value'时,回填到选择框中的文本会是Option.value
  833. <br></br>
  834. <Select
  835. style={{
  836. width: '250px',
  837. }}
  838. defaultValue={1}
  839. optionLabelProp="value"
  840. >
  841. <Option value={1}>opt1</Option>
  842. <Option value={2}>opt2</Option>
  843. <Option value={3}>
  844. <span
  845. style={{
  846. color: 'pink',
  847. }}
  848. >
  849. opt3 Node
  850. </span>
  851. </Option>
  852. <Option value="4">
  853. <span
  854. style={{
  855. color: 'red',
  856. }}
  857. >
  858. testNode
  859. </span>
  860. </Option>
  861. </Select>
  862. <br />
  863. <br />
  864. <Select
  865. style={{
  866. width: '250px',
  867. }}
  868. value={value}
  869. optionLabelProp="value"
  870. onChange={setValue}
  871. >
  872. <Option value={1}>opt1</Option>
  873. <Option value={2}>opt2</Option>
  874. <Option value={3}>
  875. <span
  876. style={{
  877. color: 'pink',
  878. }}
  879. >
  880. opt3 Node
  881. </span>
  882. </Option>
  883. <Option value="4">
  884. <span
  885. style={{
  886. color: 'red',
  887. }}
  888. >
  889. testNode
  890. </span>
  891. </Option>
  892. </Select>
  893. <br />
  894. <br />
  895. <Select
  896. style={{
  897. width: '250px',
  898. }}
  899. defaultValue={1}
  900. >
  901. <Option value={1}>children Label Text 1</Option>
  902. <Option value={2}>opt2</Option>
  903. <Option value={3}>opt3</Option>
  904. <Option value="4">
  905. <span
  906. style={{
  907. color: 'red',
  908. }}
  909. >
  910. testNode
  911. </span>
  912. </Option>
  913. </Select>
  914. <Select
  915. style={{
  916. width: '250px',
  917. }}
  918. defaultValue={1}
  919. filter
  920. optionLabelProp="value"
  921. >
  922. <Option value={1}>children Label Text 1</Option>
  923. <Option value={2}>opt2</Option>
  924. <Option value={3}>opt3</Option>
  925. <Option value="4">
  926. <span
  927. style={{
  928. color: 'red',
  929. }}
  930. >
  931. testNode
  932. </span>
  933. </Option>
  934. </Select>
  935. <br />
  936. <br />
  937. 多选
  938. <Select
  939. style={{
  940. width: '250px',
  941. }}
  942. multiple
  943. filter
  944. optionLabelProp="value"
  945. >
  946. <Option value={1}>children Label Text 1</Option>
  947. <Option value={2}>opt2</Option>
  948. <Option value={3}>opt3</Option>
  949. <Option value="4">
  950. <span
  951. style={{
  952. color: 'red',
  953. }}
  954. >
  955. testNode
  956. </span>
  957. </Option>
  958. </Select>
  959. </>
  960. );
  961. };
  962. class CustomRender extends React.Component {
  963. constructor() {
  964. super();
  965. this.state = {
  966. list: [
  967. {
  968. name: '夏可漫',
  969. email: '[email protected]',
  970. abbr: 'XK',
  971. color: 'amber',
  972. },
  973. {
  974. name: '申悦',
  975. email: '[email protected]',
  976. abbr: 'SY',
  977. color: 'indigo',
  978. },
  979. {
  980. name: '曲晨一',
  981. email: '[email protected]',
  982. abbr: 'CY',
  983. color: 'blue',
  984. },
  985. {
  986. name: '文嘉茂',
  987. email: '[email protected]',
  988. abbr: 'JM',
  989. color: 'cyan',
  990. },
  991. ],
  992. };
  993. }
  994. renderCustomOption(item, index) {
  995. const optionStyle = {
  996. display: 'flex',
  997. };
  998. return (
  999. <Option key={index} value={item.name} style={optionStyle} showTick={false} {...item}>
  1000. <Avatar color={item.color} size="small">
  1001. {item.abbr}
  1002. </Avatar>
  1003. <div
  1004. style={{
  1005. marginLeft: 4,
  1006. }}
  1007. >
  1008. <p
  1009. style={{
  1010. fontSize: 14,
  1011. margin: 4,
  1012. }}
  1013. >
  1014. {item.name}
  1015. </p>
  1016. <p
  1017. style={{
  1018. margin: 4,
  1019. }}
  1020. >
  1021. {item.email}
  1022. </p>
  1023. </div>
  1024. </Option>
  1025. );
  1026. }
  1027. renderSelectedItem(optionNode) {
  1028. return (
  1029. <div>
  1030. <Avatar color={optionNode.color} size="small">
  1031. {optionNode.abbr}
  1032. </Avatar>
  1033. <span
  1034. style={{
  1035. margin: 8,
  1036. }}
  1037. >
  1038. {optionNode.email}
  1039. </span>
  1040. </div>
  1041. );
  1042. }
  1043. renderMultipleSelectedItem(optionNode) {
  1044. let content = (
  1045. <div>
  1046. <Avatar color={optionNode.color} size="small">
  1047. {optionNode.abbr}
  1048. </Avatar>
  1049. </div>
  1050. );
  1051. return {
  1052. isRenderInTag: true,
  1053. content,
  1054. };
  1055. }
  1056. renderMultipleWithoutTag(optionNode) {
  1057. let content = (
  1058. <div>
  1059. <Avatar color={optionNode.color} size="small">
  1060. {optionNode.abbr}
  1061. </Avatar>
  1062. </div>
  1063. );
  1064. return {
  1065. isRenderInTag: false,
  1066. content,
  1067. };
  1068. }
  1069. render() {
  1070. const { list } = this.state;
  1071. return (
  1072. <React.Fragment>
  1073. <Select
  1074. style={{
  1075. width: 300,
  1076. height: 40,
  1077. }}
  1078. onChange={this.provinceChange}
  1079. defaultValue={'夏可漫'}
  1080. renderSelectedItem={this.renderSelectedItem}
  1081. >
  1082. {list.map((item, index) => this.renderCustomOption(item, index))}
  1083. </Select>
  1084. <Select
  1085. style={{
  1086. width: 360,
  1087. height: 60,
  1088. marginTop: 20,
  1089. }}
  1090. onChange={this.provinceChange}
  1091. defaultValue={['夏可漫', '申悦']}
  1092. multiple
  1093. renderSelectedItem={this.renderMultipleSelectedItem}
  1094. >
  1095. {list.map((item, index) => this.renderCustomOption(item, index))}
  1096. </Select>
  1097. <Select
  1098. style={{
  1099. width: 360,
  1100. height: 60,
  1101. marginTop: 20,
  1102. }}
  1103. onChange={this.provinceChange}
  1104. defaultValue={['夏可漫', '申悦']}
  1105. multiple
  1106. renderSelectedItem={this.renderMultipleWithoutTag}
  1107. >
  1108. {list.map((item, index) => this.renderCustomOption(item, index))}
  1109. </Select>
  1110. </React.Fragment>
  1111. );
  1112. }
  1113. }
  1114. export const RenderSelectedItem = () => (
  1115. <>
  1116. renderSelectedItem
  1117. <CustomRender />
  1118. <br />
  1119. <br />
  1120. OptionLabelProp
  1121. <OptionLabelProp />
  1122. </>
  1123. );
  1124. RenderSelectedItem.story = {
  1125. name: 'renderSelectedItem',
  1126. };
  1127. RenderSelectedItem.parameters = {
  1128. chromatic: { disableSnapshot: false },
  1129. };
  1130. const ControlledSelect = () => {
  1131. const [value, setValue] = useState('nick');
  1132. const [value2, setValue2] = useState('jerry');
  1133. const [value3, setValue3] = useState();
  1134. const [value4, setValue4] = useState(['nick']);
  1135. const [value5, setValue5] = useState();
  1136. return (
  1137. <>
  1138. <span>value + onChange</span>
  1139. <Select
  1140. value={value}
  1141. onChange={setValue}
  1142. style={{
  1143. width: 200,
  1144. }}
  1145. >
  1146. <Option value="nick">nick</Option>
  1147. <Option value="jerry">jerry</Option>
  1148. <Option value="mark">mark</Option>
  1149. </Select>
  1150. <br />
  1151. <br />
  1152. <span>只传value,不传onChange</span>
  1153. <Select
  1154. value={value2}
  1155. style={{
  1156. width: 200,
  1157. }}
  1158. >
  1159. <Option value="nick">nick</Option>
  1160. <Option value="jerry">jerry</Option>
  1161. <Option value="mark">mark</Option>
  1162. </Select>
  1163. <br />
  1164. <br />
  1165. <span>value + onChange , 多选</span>
  1166. <Select
  1167. value={value3}
  1168. onChange={setValue3}
  1169. multiple
  1170. style={{
  1171. width: 200,
  1172. }}
  1173. >
  1174. <Option value="nick">nick</Option>
  1175. <Option value="jerry">jerry</Option>
  1176. <Option value="mark">mark</Option>
  1177. <Option value="nick2">nick2</Option>
  1178. <Option value="jerry2">jerry2</Option>
  1179. <Option value="mark2">mark2</Option>
  1180. </Select>
  1181. <br />
  1182. <br />
  1183. <span>value, 多选</span>
  1184. <Select
  1185. value={value4}
  1186. multiple
  1187. style={{
  1188. width: 200,
  1189. }}
  1190. >
  1191. <Option value="nick">nick</Option>
  1192. <Option value="jerry">jerry</Option>
  1193. <Option value="mark">mark</Option>
  1194. </Select>
  1195. <br />
  1196. <h5>filter为true,但option label为node时</h5>
  1197. <Select
  1198. style={{
  1199. width: '250px',
  1200. }}
  1201. filter={customFilter}
  1202. insetLabel="insetLabel"
  1203. value={value5}
  1204. onChange={setValue5}
  1205. >
  1206. {colorOptions.map(option => (
  1207. <Option value={option.value} key={option.value}>
  1208. <div style={alignStyle}>
  1209. <span style={option.spanStyle}></span>
  1210. {option.value}
  1211. </div>
  1212. </Option>
  1213. ))}
  1214. </Select>
  1215. </>
  1216. );
  1217. };
  1218. export const Controlled = () => <ControlledSelect></ControlledSelect>;
  1219. Controlled.story = {
  1220. name: 'controlled',
  1221. };
  1222. const UnControlledSelect = () => {
  1223. const onChange = value => {
  1224. console.log(value);
  1225. };
  1226. return (
  1227. <>
  1228. <h5>defaultValue在list中不存在</h5>
  1229. <Select
  1230. defaultValue={90}
  1231. onChange={onChange}
  1232. style={{
  1233. width: 200,
  1234. }}
  1235. >
  1236. <Option value={20}>nick</Option>
  1237. <Option value={10}>jerry</Option>
  1238. <Option value={5}>mark</Option>
  1239. </Select>
  1240. <h5>defaultValue在list中存在</h5>
  1241. <Select
  1242. defaultValue={10}
  1243. onChange={onChange}
  1244. style={{
  1245. width: 200,
  1246. }}
  1247. >
  1248. <Option value={20}>nick</Option>
  1249. <Option value={10}>jerry</Option>
  1250. <Option value={5}>mark</Option>
  1251. </Select>
  1252. </>
  1253. );
  1254. };
  1255. export { UnControlledSelect };
  1256. UnControlledSelect.story = {
  1257. name: '非受控组件'
  1258. };
  1259. export const TestScroll = () => (
  1260. <div
  1261. style={{
  1262. marginTop: '600px',
  1263. marginBottom: '50px',
  1264. }}
  1265. >
  1266. <Select
  1267. style={{
  1268. width: '150px',
  1269. }}
  1270. >
  1271. <Option value="tony">IronMan</Option>
  1272. <Option value="Thor" disabled>
  1273. Thor
  1274. </Option>
  1275. <Option value="steve">Caption</Option>
  1276. <Option value="peter">SpiderBoy</Option>
  1277. </Select>
  1278. </div>
  1279. );
  1280. TestScroll.story = {
  1281. name: 'test scroll',
  1282. };
  1283. let optionList = [
  1284. {
  1285. value: 'tony',
  1286. label: 'Ironman',
  1287. },
  1288. {
  1289. value: 'Thor',
  1290. label: 'Thor',
  1291. },
  1292. {
  1293. value: 'steve',
  1294. label: 'Caption',
  1295. },
  1296. {
  1297. value: 'peter',
  1298. label: 'SpiderBoy',
  1299. },
  1300. ];
  1301. export const OptionList = () => (
  1302. <Select
  1303. style={{
  1304. width: '100px',
  1305. }}
  1306. optionList={optionList}
  1307. ></Select>
  1308. );
  1309. OptionList.story = {
  1310. name: 'optionList',
  1311. };
  1312. export const InsetLabel = () => (
  1313. <>
  1314. <Select
  1315. style={{
  1316. width: 300,
  1317. }}
  1318. insetLabel="主播类型"
  1319. placeholder="请选择"
  1320. optionList={optionList}
  1321. ></Select>
  1322. <Select
  1323. style={{
  1324. width: 300,
  1325. }}
  1326. multiple
  1327. insetLabel="主播类型"
  1328. optionList={optionList}
  1329. ></Select>
  1330. <Select
  1331. style={{
  1332. width: 300,
  1333. }}
  1334. filter
  1335. insetLabel="主播类型"
  1336. optionList={optionList}
  1337. ></Select>
  1338. <Select
  1339. style={{
  1340. width: 300,
  1341. }}
  1342. filter
  1343. multiple
  1344. insetLabel="主播类型"
  1345. optionList={optionList}
  1346. ></Select>
  1347. </>
  1348. );
  1349. InsetLabel.story = {
  1350. name: 'insetLabel',
  1351. };
  1352. export const ChangeOptionDynamic = () => {
  1353. function App() {
  1354. let [options, setOptions] = useState([]);
  1355. let [index, setIndex] = useState(0);
  1356. const addOption = () => {
  1357. const randomItem = optionList[index];
  1358. index = index + 1;
  1359. setIndex(index);
  1360. options = [...options, { ...randomItem }];
  1361. setOptions(options);
  1362. };
  1363. const reset = () => {
  1364. setOptions([]);
  1365. setIndex(0);
  1366. };
  1367. return (
  1368. <div>
  1369. <Select
  1370. style={{
  1371. width: '150px',
  1372. }}
  1373. defaultValue="tony"
  1374. >
  1375. {options.map((option, idx) => (
  1376. <Select.Option key={option.key || idx} value={option.value}>
  1377. {option.label}
  1378. </Select.Option>
  1379. ))}
  1380. </Select>
  1381. <h4>多选</h4>
  1382. <Select
  1383. style={{
  1384. width: '150px',
  1385. }}
  1386. multiple
  1387. defaultValue={['tony']}
  1388. >
  1389. {options.map((option, idx) => (
  1390. <Select.Option key={option.key || idx} value={option.value}>
  1391. {option.label}
  1392. </Select.Option>
  1393. ))}
  1394. </Select>
  1395. <button onClick={addOption}>add option</button>
  1396. <button onClick={reset}>reset</button>
  1397. </div>
  1398. );
  1399. }
  1400. return <App />;
  1401. };
  1402. ChangeOptionDynamic.story = {
  1403. name: 'change option dynamic',
  1404. };
  1405. let list = [
  1406. {
  1407. value: 'tony',
  1408. label: 'Ironman',
  1409. otherKey: {
  1410. role: 1,
  1411. },
  1412. },
  1413. {
  1414. value: 'Thor',
  1415. label: 'Thor',
  1416. otherKey: {
  1417. role: 2,
  1418. },
  1419. },
  1420. {
  1421. value: 'steve',
  1422. label: 'Caption',
  1423. otherKey: {
  1424. role: 3,
  1425. },
  1426. },
  1427. {
  1428. value: 'peter',
  1429. label: 'SpiderBoy',
  1430. otherKey: {
  1431. role: 4,
  1432. },
  1433. },
  1434. ];
  1435. const SearchDemo1 = () => {
  1436. const [optionList, setOptionList] = useState(list);
  1437. const [loading, setLoading] = useState(false);
  1438. const handleSearch = value => {
  1439. setLoading(true);
  1440. let length = Math.ceil(Math.random() * 10);
  1441. let result = Array.from(
  1442. {
  1443. length,
  1444. },
  1445. (v, i) => {
  1446. return {
  1447. value: value + i,
  1448. label: value + i,
  1449. otherKey: {
  1450. role: i,
  1451. },
  1452. };
  1453. }
  1454. );
  1455. setTimeout(() => {
  1456. setOptionList(result);
  1457. setLoading(false);
  1458. }, 1000);
  1459. };
  1460. const [value, setValue] = useState(optionList[0].value);
  1461. const onChange = value => {
  1462. console.log(value);
  1463. setValue(value);
  1464. };
  1465. return (
  1466. <div>
  1467. 受控:
  1468. <Select
  1469. filter
  1470. style={{
  1471. width: '150px',
  1472. }}
  1473. onSearch={v => handleSearch(v)}
  1474. optionList={optionList}
  1475. value={value}
  1476. loading={loading}
  1477. showClear
  1478. onChange={onChange}
  1479. ></Select>
  1480. 非受控:
  1481. <Select
  1482. style={{
  1483. width: '150px',
  1484. }}
  1485. filter
  1486. showClear
  1487. onSearch={v => handleSearch(v)}
  1488. optionList={optionList}
  1489. loading={loading}
  1490. onChange={onChange}
  1491. ></Select>
  1492. 多选非受控
  1493. <Select
  1494. style={{
  1495. width: '150px',
  1496. }}
  1497. showClear
  1498. filter
  1499. multiple
  1500. onSearch={v => handleSearch(v)}
  1501. optionList={optionList}
  1502. loading={loading}
  1503. onChange={onChange}
  1504. ></Select>
  1505. </div>
  1506. );
  1507. };
  1508. import debounce from 'lodash/debounce';
  1509. class SearchDemo2 extends React.Component {
  1510. constructor() {
  1511. super();
  1512. this.state = {
  1513. loading: false,
  1514. optionList: [
  1515. {
  1516. value: 'abc',
  1517. label: '抖音',
  1518. type: 1,
  1519. },
  1520. {
  1521. value: 'hotsoon',
  1522. label: '火山小视频',
  1523. type: 2,
  1524. },
  1525. {
  1526. value: 'pipixia',
  1527. label: '皮皮虾',
  1528. type: 3,
  1529. },
  1530. {
  1531. value: 'toutiao',
  1532. label: '今日头条',
  1533. type: 4,
  1534. },
  1535. ],
  1536. value: [],
  1537. };
  1538. this.handleSearch = debounce(this.handleSearch, 800).bind(this);
  1539. this.onChange = this.onChange.bind(this);
  1540. this.customRender = this.customRender.bind(this);
  1541. }
  1542. handleSearch(inputValue) {
  1543. this.setState({
  1544. loading: true,
  1545. });
  1546. let length = Math.ceil(Math.random() * 100);
  1547. let result = Array.from(
  1548. {
  1549. length,
  1550. },
  1551. (v, i) => {
  1552. return {
  1553. value: inputValue + i,
  1554. label: 'label' + i,
  1555. type: i + 1,
  1556. };
  1557. }
  1558. );
  1559. setTimeout(() => {
  1560. this.setState({
  1561. optionList: result,
  1562. loading: false,
  1563. });
  1564. }, 2000);
  1565. }
  1566. onChange(value) {
  1567. this.setState({
  1568. value,
  1569. });
  1570. console.log(value);
  1571. }
  1572. customRender(optionNode) {
  1573. return optionNode.value + optionNode.label;
  1574. }
  1575. render() {
  1576. const { loading, optionList, value } = this.state;
  1577. return (
  1578. <div>
  1579. <Select
  1580. style={{
  1581. width: 150,
  1582. }}
  1583. showClear
  1584. filter
  1585. labelInValue
  1586. onSearch={this.handleSearch}
  1587. optionList={optionList}
  1588. loading={loading}
  1589. onChange={this.onChange}
  1590. placeholder="请选择"
  1591. ></Select>
  1592. <br />
  1593. <br />
  1594. <Select
  1595. style={{
  1596. width: 180,
  1597. }}
  1598. filter // labelInValue
  1599. showClear
  1600. multiple
  1601. value={value}
  1602. renderSelectedItem={this.customRender}
  1603. onSearch={this.handleSearch}
  1604. optionList={optionList}
  1605. loading={loading}
  1606. onChange={this.onChange}
  1607. placeholder="请选择"
  1608. ></Select>
  1609. </div>
  1610. );
  1611. }
  1612. }
  1613. export const Search = () => (
  1614. <>
  1615. <SearchDemo1 />
  1616. <SearchDemo2 />
  1617. </>
  1618. );
  1619. Search.story = {
  1620. name: 'search',
  1621. };
  1622. export const IncomeDetail = ({ config = {}, params = {} }) => {
  1623. const [detailList, setDetailList] = useState([]);
  1624. const [hasMore, setHasMore] = useState(true);
  1625. const [loading, setLoading] = useState();
  1626. let lock;
  1627. const fetchData = (outParams = {}) => {
  1628. if (lock) {
  1629. return;
  1630. }
  1631. setLoading(true); // 参数
  1632. // 请求
  1633. fetch({
  1634. url: URL.user_profit,
  1635. method: 'get',
  1636. baseURL: config.webcast_host,
  1637. params,
  1638. })
  1639. .then(res => {
  1640. lock = false;
  1641. setLoading(false);
  1642. console.log('++++', data);
  1643. })
  1644. .catch(() => {
  1645. setLoading(false);
  1646. Toast.show('网络异常,请稍后重试');
  1647. });
  1648. };
  1649. useEffect(fetchData, []); // 监听滚动设置吸顶 以及加载更多
  1650. useEffect(() => {
  1651. window.addEventListener('scroll', function() {
  1652. // 加载更多
  1653. const scrollY = window.scrollY;
  1654. const scrollHeight = document.documentElement.scrollHeight;
  1655. const screenHeight = screen.height;
  1656. if (!loading && hasMore && scrollY + screenHeight + 300 > scrollHeight) {
  1657. fetchData();
  1658. }
  1659. });
  1660. }, [detailList.length]);
  1661. return (
  1662. <div>
  1663. <Select></Select>
  1664. </div>
  1665. );
  1666. };
  1667. export const AllowCreate = () => (
  1668. <Select
  1669. style={{
  1670. width: 500,
  1671. }}
  1672. optionList={optionList}
  1673. allowCreate={true}
  1674. multiple={true}
  1675. filter={true}
  1676. onChange={v => console.log(v)}
  1677. ></Select>
  1678. );
  1679. AllowCreate.story = {
  1680. name: 'allowCreate',
  1681. };
  1682. export const AllowCreateCustomRender = () => (
  1683. <Select
  1684. style={{
  1685. width: 500,
  1686. }}
  1687. optionList={optionList}
  1688. allowCreate={true}
  1689. multiple={true}
  1690. filter={true}
  1691. onChange={v => console.log(v)}
  1692. renderCreateItem={v => `semi: ${v}`}
  1693. ></Select>
  1694. );
  1695. AllowCreateCustomRender.story = {
  1696. name: 'allowCreate custom render',
  1697. };
  1698. let AllowCreateControlledDemo = () => {
  1699. let [value, setValue] = useState();
  1700. const optionList = [
  1701. {
  1702. value: 'abc',
  1703. label: '抖音',
  1704. },
  1705. {
  1706. value: 'hotsoon',
  1707. label: '火山小视频',
  1708. },
  1709. {
  1710. value: 'pipixia',
  1711. label: '皮皮虾',
  1712. },
  1713. {
  1714. value: 'toutiao',
  1715. label: '今日头条',
  1716. },
  1717. ];
  1718. const [list, setList] = useState(optionList);
  1719. const handleSelect = v => {
  1720. var lastOne = v[v.length - 1];
  1721. if (lastOne && list.findIndex(item => item.value === lastOne) == -1) {
  1722. list.push({
  1723. value: lastOne,
  1724. label: lastOne,
  1725. });
  1726. }
  1727. setList(list);
  1728. setValue(v);
  1729. };
  1730. return (
  1731. <Select
  1732. style={{
  1733. width: 400,
  1734. }}
  1735. optionList={list}
  1736. allowCreate={true}
  1737. multiple={true}
  1738. filter={true}
  1739. value={value}
  1740. onChange={handleSelect}
  1741. ></Select>
  1742. );
  1743. };
  1744. const AllowCreateDemo = () => {
  1745. let [value, setValue] = useState();
  1746. const optionList = [
  1747. {
  1748. value: 'abc',
  1749. label: '抖音',
  1750. },
  1751. {
  1752. value: 'hotsoon',
  1753. label: '火山小视频',
  1754. },
  1755. {
  1756. value: 'pipixia',
  1757. label: '皮皮虾',
  1758. },
  1759. {
  1760. value: 'toutiao',
  1761. label: '今日头条',
  1762. },
  1763. ];
  1764. const [list, setList] = useState(optionList);
  1765. const handleSelect = v => {
  1766. var lastOne = v[v.length - 1];
  1767. if (lastOne && list.findIndex(item => item.value === lastOne) == -1) {
  1768. list.push({
  1769. value: lastOne,
  1770. label: lastOne,
  1771. });
  1772. }
  1773. setList(list); // setValue(v)
  1774. };
  1775. return (
  1776. <Select
  1777. style={{
  1778. width: 400,
  1779. }}
  1780. optionList={list}
  1781. defaultValue={['abc', 'hotsoon']}
  1782. allowCreate={true}
  1783. multiple={true}
  1784. filter={true}
  1785. onChange={handleSelect}
  1786. ></Select>
  1787. );
  1788. };
  1789. export const AllowCreateWithDefaultValue = () => <AllowCreateDemo />;
  1790. AllowCreateWithDefaultValue.story = {
  1791. name: 'allowCreate with defaultValue',
  1792. };
  1793. class HideDemo extends React.Component {
  1794. constructor(props) {
  1795. super(props);
  1796. this.state = {
  1797. optionList: [
  1798. {
  1799. value: 'abc',
  1800. label: '抖音',
  1801. },
  1802. {
  1803. value: 'hotsoon',
  1804. label: '火山小视频',
  1805. },
  1806. {
  1807. value: 'pipixia',
  1808. label: '皮皮虾',
  1809. },
  1810. {
  1811. value: 'toutiao',
  1812. label: '今日头条',
  1813. },
  1814. ],
  1815. selectedItems: [],
  1816. };
  1817. this.onChange = this.onChange.bind(this);
  1818. }
  1819. onChange(selectedItems) {
  1820. this.setState({
  1821. selectedItems,
  1822. });
  1823. }
  1824. render() {
  1825. let { optionList, selectedItems } = this.state;
  1826. let filterOptions = optionList.filter(option => !selectedItems.includes(option.value));
  1827. return (
  1828. <Select
  1829. value={selectedItems}
  1830. multiple
  1831. style={{
  1832. width: 300,
  1833. }}
  1834. onChange={this.onChange}
  1835. optionList={filterOptions}
  1836. ></Select>
  1837. );
  1838. }
  1839. }
  1840. export const AutoHiddenSelectedItem = () => <HideDemo></HideDemo>;
  1841. AutoHiddenSelectedItem.story = {
  1842. name: 'auto hidden selected item',
  1843. };
  1844. class CustomCreate extends React.Component {
  1845. constructor(props) {
  1846. super(props);
  1847. this.state = {
  1848. optionList: [
  1849. {
  1850. value: 'abc',
  1851. label: '抖音',
  1852. },
  1853. {
  1854. value: 'hotsoon',
  1855. label: '火山小视频',
  1856. },
  1857. {
  1858. value: 'pipixia',
  1859. label: '皮皮虾',
  1860. },
  1861. {
  1862. value: 'toutiao',
  1863. label: '今日头条',
  1864. },
  1865. {
  1866. value: 0,
  1867. label: 0,
  1868. },
  1869. ],
  1870. selectedItems: ['fefe'],
  1871. };
  1872. this.onChange = this.onChange.bind(this);
  1873. this.customRender = this.customRender.bind(this);
  1874. this.search = this.search.bind(this);
  1875. }
  1876. onChange(selectedItems) {
  1877. console.log(selectedItems); // this.setState({ selectedItems, optionList: [] });
  1878. this.setState({
  1879. selectedItems,
  1880. }); // this.setState({ optionList: [] });
  1881. }
  1882. customRender(v) {
  1883. return (
  1884. <>
  1885. <span>label:{v.label}</span>
  1886. <span>value:{v.value}</span>
  1887. </>
  1888. );
  1889. }
  1890. customCreate(inputValue, isFocus) {
  1891. let style = {
  1892. padding: 12,
  1893. cursor: 'pointer',
  1894. backgroundColor: isFocus ? 'var(--semi-color-fill-0)' : '#FFF',
  1895. };
  1896. return <div style={style}>{'create' + inputValue}</div>;
  1897. }
  1898. search(inputValue) {
  1899. let length = Math.ceil(Math.random() * 10);
  1900. let result = Array.from(
  1901. {
  1902. length,
  1903. },
  1904. (v, i) => {
  1905. return {
  1906. value: inputValue + i,
  1907. label: inputValue + i,
  1908. type: i + 1,
  1909. };
  1910. }
  1911. );
  1912. console.log(result); // result = result.concat(selectedOption);
  1913. this.setState({
  1914. optionList: result,
  1915. });
  1916. }
  1917. render() {
  1918. let { optionList, selectedItems } = this.state;
  1919. return (
  1920. <>
  1921. <Select
  1922. defaultValue={['abc']}
  1923. filter
  1924. style={{
  1925. width: 300,
  1926. }}
  1927. multiple
  1928. optionList={optionList}
  1929. onSearch={this.search}
  1930. onChange={this.onChange}
  1931. emptyContent={null} // onChangeWithObject
  1932. ></Select>
  1933. </>
  1934. );
  1935. }
  1936. }
  1937. export const _CustomCreate = () => <CustomCreate></CustomCreate>;
  1938. _CustomCreate.story = {
  1939. name: 'CustomCreate',
  1940. };
  1941. class OptionGroupDemo extends React.Component {
  1942. constructor(props) {
  1943. super(props);
  1944. this.handleSearch = this.handleSearch.bind(this);
  1945. this.state = {
  1946. groups: [
  1947. {
  1948. label: 'Asia',
  1949. children: [
  1950. {
  1951. label: 'China',
  1952. value: 'zhongguo',
  1953. },
  1954. {
  1955. label: 'Koera',
  1956. value: 'hanguo',
  1957. },
  1958. ],
  1959. },
  1960. {
  1961. label: 'Europe',
  1962. children: [
  1963. {
  1964. label: 'Germany',
  1965. value: 'deguo',
  1966. },
  1967. {
  1968. label: 'France',
  1969. value: 'faguo',
  1970. },
  1971. ],
  1972. },
  1973. {
  1974. label: 'Other',
  1975. children: [
  1976. {
  1977. label: 'vf',
  1978. value: 'Sourth',
  1979. },
  1980. ],
  1981. },
  1982. ],
  1983. };
  1984. }
  1985. handleSearch(input) {
  1986. let groups = [1, 2, 3].map(i => {
  1987. return {
  1988. label: i,
  1989. // label: Math.random(),
  1990. children: [10, 20].map(j => {
  1991. return {
  1992. label: Math.random(),
  1993. value: Math.random(),
  1994. };
  1995. }),
  1996. };
  1997. });
  1998. this.setState({
  1999. groups,
  2000. });
  2001. }
  2002. renderGroup(group, index) {
  2003. const options = group.children.map(option => (
  2004. <Select.Option value={option.value} label={option.label} key={option.label} />
  2005. ));
  2006. return <Select.OptGroup key={`${index}-${group.label}`} label={group.label}>{options}</Select.OptGroup>;
  2007. }
  2008. render() {
  2009. const { groups } = this.state;
  2010. return (
  2011. <>
  2012. <Select
  2013. placeholder=""
  2014. style={{
  2015. width: 180,
  2016. }}
  2017. filter
  2018. onSearch={this.handleSearch}
  2019. remote
  2020. >
  2021. {groups.map((group, index) => this.renderGroup(group, index))}
  2022. </Select>
  2023. </>
  2024. );
  2025. }
  2026. }
  2027. export const SelectOptionGroup = () => <OptionGroupDemo />;
  2028. SelectOptionGroup.story = {
  2029. name: 'Select OptionGroup',
  2030. };
  2031. const BlurDemo = () => {
  2032. const onBlur = (value, e) => {
  2033. console.log(value);
  2034. console.log(e);
  2035. };
  2036. const onFocus = (value, e) => {
  2037. console.log(value);
  2038. console.log(e);
  2039. };
  2040. return (
  2041. <>
  2042. <Select
  2043. filter
  2044. placeholder=""
  2045. style={{
  2046. width: 180,
  2047. }}
  2048. onBlur={onBlur}
  2049. onFocus={onFocus}
  2050. >
  2051. <Select.Option value="zhongguo">China</Select.Option>
  2052. <Select.Option value="hanguo">Koera</Select.Option>
  2053. <Select.Option value="deguo">Germany</Select.Option>
  2054. <Select.Option value="faguo">France</Select.Option>
  2055. </Select>
  2056. </>
  2057. );
  2058. };
  2059. export const SelectOnBlurOnFocus = () => <BlurDemo></BlurDemo>;
  2060. SelectOnBlurOnFocus.story = {
  2061. name: 'Select onBlur/onFocus',
  2062. };
  2063. const AutoAdjustOverflowDemo = () => {
  2064. const [list, setList] = useState([
  2065. {
  2066. value: 'abc',
  2067. label: '1111',
  2068. },
  2069. {
  2070. value: 'hotsoon',
  2071. label: '1112',
  2072. },
  2073. {
  2074. value: 'pipixia',
  2075. label: '1113',
  2076. },
  2077. {
  2078. value: 'toutiao',
  2079. label: '1114',
  2080. },
  2081. ]);
  2082. const onSearch = () => {
  2083. let newList = Array.from(
  2084. {
  2085. length: Math.floor(Math.random() * 10),
  2086. },
  2087. (v, i) => {
  2088. return {
  2089. value: i,
  2090. label: i,
  2091. };
  2092. }
  2093. );
  2094. setList(newList);
  2095. console.log(newList);
  2096. };
  2097. return (
  2098. <div
  2099. style={{
  2100. height: 180,
  2101. margin: 250,
  2102. border: '1px solid pink',
  2103. }}
  2104. >
  2105. <Select
  2106. optionList={list}
  2107. filter={true}
  2108. remote={true}
  2109. onSearch={onSearch}
  2110. style={{
  2111. width: 200,
  2112. }}
  2113. multiple
  2114. />
  2115. </div>
  2116. );
  2117. };
  2118. export const AutoAdjustOverflow = () => <AutoAdjustOverflowDemo></AutoAdjustOverflowDemo>;
  2119. AutoAdjustOverflow.story = {
  2120. name: 'autoAdjustOverflow',
  2121. };
  2122. const AllowCreateWithFilter = () => {
  2123. const [list, setList] = useState([
  2124. {
  2125. value: 'abc',
  2126. label: 'abc',
  2127. otherKey: 'abc',
  2128. },
  2129. {
  2130. value: 'hotsoon',
  2131. label: 'hotsoon',
  2132. otherKey: 'efg',
  2133. },
  2134. {
  2135. value: 'pipixia',
  2136. label: 'pipixia',
  2137. otherKey: 'hij',
  2138. },
  2139. {
  2140. value: 'toutiao',
  2141. label: 'toutiao',
  2142. otherKey: 'klm',
  2143. },
  2144. ]);
  2145. const filter = (sugInput, option) => {
  2146. let compareKey = option.otherKey ? option.otherKey.toUpperCase() : '';
  2147. let sug = sugInput.toUpperCase();
  2148. return compareKey.includes(sug);
  2149. };
  2150. return (
  2151. <div
  2152. style={{
  2153. height: 180,
  2154. margin: 250,
  2155. }}
  2156. >
  2157. <Select
  2158. optionList={list}
  2159. multiple
  2160. filter={filter}
  2161. style={{
  2162. width: 200,
  2163. }}
  2164. allowCreate
  2165. />
  2166. </div>
  2167. );
  2168. };
  2169. export const FilterAllowCreate = () => <AllowCreateWithFilter></AllowCreateWithFilter>;
  2170. FilterAllowCreate.story = {
  2171. name: 'Filter + allowCreate',
  2172. };
  2173. const SelectRefDemo = () => {
  2174. const ref = useRef();
  2175. const secondRef = useRef();
  2176. const [open, setOpen] = useState(false);
  2177. const list = [
  2178. {
  2179. value: 'abc',
  2180. label: 'Abc',
  2181. },
  2182. {
  2183. value: 'hotsoon',
  2184. label: 'Hotsoon',
  2185. },
  2186. {
  2187. value: 'pipixia',
  2188. label: 'Pipixia',
  2189. },
  2190. {
  2191. value: 'toutiao',
  2192. label: 'TooBuzz',
  2193. },
  2194. ];
  2195. const change = () => {
  2196. if (!open) {
  2197. ref.current.open();
  2198. setOpen(true);
  2199. } else {
  2200. ref.current.close();
  2201. setOpen(false);
  2202. }
  2203. };
  2204. const focus = () => {
  2205. ref.current.focus();
  2206. };
  2207. const clearInput = () => {
  2208. ref.current.clearInput();
  2209. };
  2210. const deselectAll = () => {
  2211. ref.current.deselectAll();
  2212. };
  2213. const selectAll = () => {
  2214. ref.current.selectAll();
  2215. };
  2216. return (
  2217. <>
  2218. <h4>onChangeWithObject = false</h4>
  2219. <Select
  2220. innerBottomSlot={
  2221. <div>
  2222. <Space>
  2223. <Button onClick={change}>close</Button>
  2224. <Button onClick={clearInput}>clearInput</Button>
  2225. <Button onClick={deselectAll}>deselectAll</Button>
  2226. <Button onClick={selectAll}>selectAll</Button>
  2227. </Space>
  2228. </div>
  2229. }
  2230. ref={ref}
  2231. onChange={e => console.log(e)}
  2232. placeholder="Business line"
  2233. style={{
  2234. width: 180,
  2235. }}
  2236. optionList={list}
  2237. filter
  2238. multiple
  2239. ></Select>
  2240. <Space>
  2241. <Button onClick={change}>open</Button>
  2242. <Button onClick={focus}>focus</Button>
  2243. <Button onClick={clearInput}>clearInput</Button>
  2244. <Button onClick={deselectAll}>deselectAll</Button>
  2245. <Button onClick={selectAll}>selectAll</Button>
  2246. </Space>
  2247. <h4
  2248. style={{
  2249. marginTop: 20,
  2250. }}
  2251. >
  2252. onChangeWithObject = true
  2253. </h4>
  2254. <Select
  2255. innerBottomSlot={
  2256. <div>
  2257. <Space></Space>
  2258. </div>
  2259. }
  2260. onChange={e => console.log(e)}
  2261. onChangeWithObject
  2262. ref={secondRef}
  2263. placeholder="Business line"
  2264. style={{
  2265. width: 180,
  2266. }}
  2267. optionList={list}
  2268. filter
  2269. multiple
  2270. ></Select>
  2271. <Space>
  2272. <Button onClick={() => secondRef.current.deselectAll()}>deselectAll</Button>
  2273. <Button onClick={() => secondRef.current.selectAll()}>selectAll</Button>
  2274. </Space>
  2275. </>
  2276. );
  2277. };
  2278. export const Ref = () => <SelectRefDemo />;
  2279. Ref.story = {
  2280. name: 'ref',
  2281. };
  2282. export const CustomTriggerDemo = () => <CustomTrigger />;
  2283. CustomTriggerDemo.story = {
  2284. name: 'custom trigger'
  2285. }
  2286. class VirtualizeClassDemo extends React.Component {
  2287. constructor(props) {
  2288. super(props);
  2289. // this.handleSearch = this.handleSearch.bind(this);
  2290. let newOptions = Array.from({ length: 1000 }, (v, i) => ({ label: `o-${i}`, value: `v-${v}-${i}` }));
  2291. this.state = {
  2292. optionList: newOptions,
  2293. };
  2294. }
  2295. render() {
  2296. let { groups, optionList } = this.state;
  2297. let virtualize = {
  2298. height: 300,
  2299. widht: '100%',
  2300. itemSize: 36,
  2301. };
  2302. return (
  2303. <>
  2304. <Select
  2305. placeholder=""
  2306. style={{ width: 180 }}
  2307. filter
  2308. onSearch={this.handleSearch}
  2309. virtualize={virtualize}
  2310. optionList={optionList}
  2311. ></Select>
  2312. </>
  2313. );
  2314. }
  2315. }
  2316. export const VirtualizeDemo = () => <VirtualizeClassDemo />;
  2317. VirtualizeDemo.story = {
  2318. name: 'virtualize select'
  2319. }
  2320. const SelectPosition = () => {
  2321. return (
  2322. <div
  2323. style={{
  2324. height: 500,
  2325. border: '1px solid red',
  2326. overflow: 'auto',
  2327. }}
  2328. >
  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. <p>p</p>
  2343. <p>p</p>
  2344. <p>p</p>
  2345. <p>p</p>
  2346. <p>p</p>
  2347. <p>p</p>
  2348. <p>p</p>
  2349. <p>p</p>
  2350. <p>p</p>
  2351. <p>p</p>
  2352. <Select
  2353. defaultValue="abc"
  2354. style={{
  2355. width: 120,
  2356. }}
  2357. >
  2358. <Option value="abc">抖音</Option>
  2359. <Option value="hotsoon">火山</Option>
  2360. <Option value="pipixia" disabled>
  2361. 皮皮虾
  2362. </Option>
  2363. <Option value="xigua">西瓜视频</Option>
  2364. </Select>
  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. <p>p</p>
  2398. <p>p</p>
  2399. <p>p</p>
  2400. <p>p</p>
  2401. <p>p</p>
  2402. <p>p</p>
  2403. <p>p</p>
  2404. <p>p</p>
  2405. <p>p</p>
  2406. <p>p</p>
  2407. </div>
  2408. );
  2409. };
  2410. export { SelectPosition };
  2411. SelectPosition.story = {
  2412. name: 'Select position problem'
  2413. }
  2414. const RenderOptionDemo = () => {
  2415. const renderOptionItem = renderProps => {
  2416. const {
  2417. disabled,
  2418. selected,
  2419. label,
  2420. value,
  2421. focused,
  2422. className,
  2423. style,
  2424. onMouseEnter,
  2425. onClick,
  2426. empty,
  2427. emptyContent,
  2428. ...rest
  2429. } = renderProps;
  2430. const optionCls = classNames({
  2431. ['custom-option-render']: true,
  2432. ['custom-option-render-focused']: focused,
  2433. ['custom-option-render-disabled']: disabled,
  2434. ['custom-option-render-selected']: selected,
  2435. }); // Notice:
  2436. // 1.props传入的style需在wrapper dom上进行消费,否则在虚拟化场景下会无法正常使用
  2437. // 2.选中(selected)、聚焦(focused)、禁用(disabled)等状态的样式需自行加上,你可以从props中获取到相对的boolean值
  2438. // 3.onMouseEnter需在wrapper dom上绑定,否则上下键盘操作时显示会有问题
  2439. return (
  2440. <div
  2441. style={style}
  2442. className={optionCls}
  2443. onClick={() => onClick()}
  2444. onMouseEnter={e => onMouseEnter()}
  2445. >
  2446. <Checkbox checked={selected} />
  2447. <div className="option-right">{label}</div>
  2448. </div>
  2449. );
  2450. };
  2451. return (
  2452. <>
  2453. <Select
  2454. filter
  2455. dropdownClassName="components-select-demo-renderOptionItem"
  2456. optionList={optionList}
  2457. style={{
  2458. width: 300,
  2459. }}
  2460. renderOptionItem={renderOptionItem}
  2461. />
  2462. <br />
  2463. <br />
  2464. <Select
  2465. filter
  2466. multiple
  2467. dropdownClassName="components-select-demo-renderOptionItem"
  2468. optionList={optionList}
  2469. style={{
  2470. width: 450,
  2471. }}
  2472. renderOptionItem={renderOptionItem}
  2473. />
  2474. </>
  2475. );
  2476. };
  2477. export const RenderOptionItem = () => <RenderOptionDemo />;
  2478. RenderOptionItem.story = {
  2479. name: 'renderOptionItem',
  2480. };
  2481. const FilterDefaultOpen = () => {
  2482. const [value1, setValue1] = useState('a-1');
  2483. return (
  2484. <>
  2485. <Select
  2486. placeholder=""
  2487. style={{
  2488. width: 180,
  2489. }}
  2490. filter
  2491. defaultOpen
  2492. >
  2493. <Select.OptGroup label="Asia">
  2494. <Select.Option value="a-1">China</Select.Option>
  2495. <Select.Option value="a-2">Koera</Select.Option>
  2496. </Select.OptGroup>
  2497. <Select.OptGroup label="Europe">
  2498. <Select.Option value="b-1">Germany</Select.Option>
  2499. <Select.Option value="b-2">France</Select.Option>
  2500. </Select.OptGroup>
  2501. <Select.OptGroup label="South America">
  2502. <Select.Option value="c-1">Peru</Select.Option>
  2503. </Select.OptGroup>
  2504. </Select>
  2505. <Select
  2506. placeholder=""
  2507. style={{
  2508. width: 180,
  2509. marginLeft: 20,
  2510. }}
  2511. filter
  2512. defaultOpen
  2513. defaultValue="a-2"
  2514. >
  2515. <Select.OptGroup label="Asia">
  2516. <Select.Option value="a-1">China</Select.Option>
  2517. <Select.Option value="a-2">Koera</Select.Option>
  2518. </Select.OptGroup>
  2519. <Select.OptGroup label="Europe">
  2520. <Select.Option value="b-1">Germany</Select.Option>
  2521. <Select.Option value="b-2">France</Select.Option>
  2522. </Select.OptGroup>
  2523. <Select.OptGroup label="South America">
  2524. <Select.Option value="c-1">Peru</Select.Option>
  2525. </Select.OptGroup>
  2526. </Select>
  2527. <Select
  2528. placeholder=""
  2529. style={{
  2530. width: 180,
  2531. marginLeft: 20,
  2532. }}
  2533. filter
  2534. defaultOpen
  2535. value={value1}
  2536. onChange={val => setValue1(val)}
  2537. >
  2538. <Select.Option value="a-1">China</Select.Option>
  2539. <Select.Option value="a-2">Koera</Select.Option>
  2540. <Select.Option value="b-1">Germany</Select.Option>
  2541. <Select.Option value="b-2">France</Select.Option>
  2542. <Select.Option value="c-1">Peru</Select.Option>
  2543. </Select>
  2544. </>
  2545. );
  2546. };
  2547. export { FilterDefaultOpen };
  2548. FilterDefaultOpen.story = {
  2549. name: 'Filter + defaultOpen'
  2550. };
  2551. const CustomSelect = props => {
  2552. const { fieldRef, ...rest } = props;
  2553. return <Select {...rest} ref={fieldRef} />;
  2554. };
  2555. const CustomFieldSelect = withField(CustomSelect);
  2556. const RefDemo = () => {
  2557. const fieldRef = useRef(null);
  2558. const onChange = () => {
  2559. console.log(fieldRef);
  2560. fieldRef.current.open();
  2561. debugger;
  2562. };
  2563. return (
  2564. <Form>
  2565. <CustomFieldSelect field="test" initValue="se" fieldRef={fieldRef}></CustomFieldSelect>
  2566. <Button onClick={onChange}>change</Button>
  2567. </Form>
  2568. );
  2569. };
  2570. export const RefFieldDemo = () => <RefDemo />;
  2571. RefFieldDemo.story = {
  2572. name: 'Ref field demo',
  2573. };
  2574. const ValueZeroDemo = () => {
  2575. const list = [
  2576. {
  2577. value: 6,
  2578. label: '抖音小视频',
  2579. otherKey: 0,
  2580. },
  2581. {
  2582. value: 1,
  2583. label: '火山小视频',
  2584. disabled: true,
  2585. otherKey: 1,
  2586. },
  2587. {
  2588. value: 'pipixia',
  2589. label: '皮皮虾',
  2590. otherKey: 2,
  2591. },
  2592. {
  2593. value: 'toutiao',
  2594. label: '今日头条',
  2595. otherKey: 3,
  2596. },
  2597. ];
  2598. return (
  2599. <Select
  2600. placeholder="请选择业务线"
  2601. style={{
  2602. width: 180,
  2603. }}
  2604. optionList={list}
  2605. value={0}
  2606. renderSelectedItem={option => option.label + 1}
  2607. ></Select>
  2608. );
  2609. };
  2610. export const Value0 = () => <ValueZeroDemo />;
  2611. Value0.story = {
  2612. name: 'value=0',
  2613. };
  2614. const Highlight = () => {
  2615. const searchWords = ['do', 'dollar'];
  2616. const sourceString = 'aaa do dollar aaa';
  2617. const result = getHighLightTextHTML({
  2618. searchWords,
  2619. sourceString,
  2620. });
  2621. const result2 = getHighLightTextHTML({
  2622. searchWords: ['z'],
  2623. sourceString: 'aaazaaazaaa',
  2624. });
  2625. return result2;
  2626. };
  2627. export const _Highlight = () => <Highlight />;
  2628. _Highlight.story = {
  2629. name: 'highlight',
  2630. };
  2631. export const ScrollIntoView = () => (
  2632. <div>
  2633. <p>single selection</p>
  2634. <Select defaultValue='v-11' defaultOpen style={{ width: 120, marginBottom: 300 }}>
  2635. {new Array(50).fill(null).map((item, idx) => (
  2636. <Option value={`v-${idx}`} key={idx}>{`option-${idx}`}</Option>
  2637. ))}
  2638. </Select>
  2639. <p>single selection with no selected item</p>
  2640. <Select style={{ marginBottom: 300, width: 120 }}>
  2641. {new Array(50).fill(null).map((item, idx) => (
  2642. <Option value={`v-${idx}`} key={idx}>{`option-${idx}`}</Option>
  2643. ))}
  2644. </Select>
  2645. <p>The selected node is the last</p>
  2646. <Select defaultValue='v-49' defaultOpen style={{ marginBottom: 300, width: 120 }}>
  2647. {new Array(50).fill(null).map((item, idx) => (
  2648. <Option value={`v-${idx}`} key={idx}>{`option-${idx}`}</Option>
  2649. ))}
  2650. </Select>
  2651. <p>The selected node is the first</p>
  2652. <Select defaultValue='v-0' style={{ marginBottom: 300, width: 120 }}>
  2653. {new Array(50).fill(null).map((item, idx) => (
  2654. <Option value={`v-${idx}`} key={idx}>{`option-${idx}`}</Option>
  2655. ))}
  2656. </Select>
  2657. <p>multiple selection</p>
  2658. <Select defaultValue={['v-25', 'v-9']} multiple style={{ marginBottom: 300, width: 220 }}>
  2659. {new Array(30).fill(null).map((item, idx) => (
  2660. <Option value={`v-${idx}`} key={idx}>{`option-${idx}`}</Option>
  2661. ))}
  2662. </Select>
  2663. <p>multiple selection with no selected item</p>
  2664. <Select multiple style={{ marginBottom: 300, width: 220 }}>
  2665. {new Array(30).fill(null).map((item, idx) => (
  2666. <Option value={`v-${idx}`} key={idx}>{`option-${idx}`}</Option>
  2667. ))}
  2668. </Select>
  2669. </div>
  2670. );
  2671. ScrollIntoView.story = {
  2672. name: 'scroll into view',
  2673. };
  2674. export const SelectInputPropsDemo = () => {
  2675. const inputProps = {
  2676. className: 'ttt',
  2677. onCompositionEnd: (v) => console.log(v.target.value)
  2678. };
  2679. return (
  2680. <Select
  2681. // onSearch={(v) => console.log(v)}
  2682. optionList={list}
  2683. inputProps={inputProps}
  2684. multiple
  2685. filter
  2686. style={{ width: 200 }}
  2687. >
  2688. </Select>
  2689. )
  2690. };
  2691. SelectInputPropsDemo.story = {
  2692. name: 'inputProps',
  2693. };
  2694. export const AutoClearSearchValue = () => {
  2695. const [val, setVal] = useState(['semi1']);
  2696. const optionList = [
  2697. { label: 'semi1', value: 'semi1' },
  2698. { label: 'semi2', value: 'semi2' },
  2699. { label: 'semi3', value: 'semi3' },
  2700. { label: 'semi4', value: 'semi4' },
  2701. { label: 'semi5', value: 'semi5' },
  2702. { label: 'semi6', value: 'semi6' },
  2703. ];
  2704. return (
  2705. <>
  2706. <h4>Controlled mode + multiple</h4>
  2707. <Select style={{ width: 400 }} multiple optionList={optionList} filter value={val} onChange={value => setVal(value)} autoClearSearchValue={false}></Select>
  2708. <br />
  2709. <br />
  2710. <h4>Uncontrolled mode + multiple</h4>
  2711. <Select style={{ width: 400 }} multiple optionList={optionList} filter autoClearSearchValue={false}></Select>
  2712. <h4>Uncontrolled mode + multiple + defaultValue</h4>
  2713. <Select style={{ width: 400 }} multiple optionList={optionList} filter defaultValue={['semi2']} autoClearSearchValue={false}></Select>
  2714. </>
  2715. )
  2716. }
  2717. SelectInputPropsDemo.story = {
  2718. name: 'AutoClearSearchValue',
  2719. };
  2720. export const RenderSelectedItemCallCount = () => {
  2721. const list = [
  2722. { "name": "夏可漫", "email": "[email protected]", "avatar": "https://sf6-cdn-tos.douyinstatic.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/avatarDemo.jpeg" },
  2723. { "name": "申悦", "email": "[email protected]", "avatar": "https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/bag.jpeg" },
  2724. { "name": "曲晨一", "email": "[email protected]", "avatar": "https://sf6-cdn-tos.douyinstatic.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/8bd8224511db085ed74fea37205aede5.jpg" },
  2725. { "name": "文嘉茂", "email": "[email protected]", "avatar": "https://sf6-cdn-tos.douyinstatic.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/6fbafc2d-e3e6-4cff-a1e2-17709c680624.png" },
  2726. { "name": "文嘉茂2", "email": "[email protected]", "avatar": "https://sf6-cdn-tos.douyinstatic.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/6fbafc2d-e3e6-4cff-a1e2-17709c680624.png" },
  2727. { "name": "文嘉茂3", "email": "[email protected]", "avatar": "https://sf6-cdn-tos.douyinstatic.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/6fbafc2d-e3e6-4cff-a1e2-17709c680624.png" },
  2728. ]
  2729. const renderMultipleWithCustomTag = (optionNode, { onClose }) => {
  2730. console.count('rerender')
  2731. const content = (
  2732. <Tag
  2733. avatarSrc={optionNode.avatar}
  2734. avatarShape='circle'
  2735. closable={true}
  2736. onClose={onClose}
  2737. size='large'
  2738. >
  2739. {optionNode.name}
  2740. </Tag>
  2741. );
  2742. return {
  2743. isRenderInTag: false,
  2744. content
  2745. };
  2746. }
  2747. const renderCustomOption = (item, index) => {
  2748. const optionStyle = {
  2749. display: 'flex',
  2750. paddingLeft: 24,
  2751. paddingTop: 10,
  2752. paddingBottom: 10
  2753. }
  2754. return (
  2755. <Select.Option value={item.name} style={optionStyle} showTick={true} {...item} key={item.email}>
  2756. <Avatar size="small" src={item.avatar} />
  2757. <div style={{ marginLeft: 8 }}>
  2758. <div style={{ fontSize: 14 }}>{item.name}</div>
  2759. <div style={{ color: 'var(--color-text-2)', fontSize: 12, lineHeight: '16px', fontWeight: 'normal' }}>{item.email}</div>
  2760. </div>
  2761. </Select.Option>
  2762. )
  2763. }
  2764. return (
  2765. <>
  2766. <Select
  2767. placeholder='请选择'
  2768. showClear
  2769. multiple
  2770. maxTagCount={2}
  2771. style={{ width: 280, height: 40 }}
  2772. onChange={v => console.log(v)}
  2773. defaultValue={'夏可漫'}
  2774. renderSelectedItem={renderMultipleWithCustomTag}
  2775. >
  2776. {list.map((item, index) => renderCustomOption(item, index))}
  2777. </Select>
  2778. </>
  2779. );
  2780. }
  2781. RenderSelectedItemCallCount.story = {
  2782. name: 'RenderSelectedItemCallCount',
  2783. };