carousel.stories.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. import React, { useState } from 'react';
  2. import { Radio, RadioGroup, Button } from '@douyinfe/semi-ui';
  3. import { IconArrowLeft, IconArrowRight } from "@douyinfe/semi-icons";
  4. import Carousel from '../index';
  5. export default {
  6. title: 'Carousel',
  7. }
  8. const style = {
  9. width: '600px',
  10. height: '240px',
  11. };
  12. const contentPinkStyle = {
  13. display:'flex',
  14. justifyContent: 'center',
  15. alignItems: 'center',
  16. color: '#fff',
  17. background: 'lightpink',
  18. };
  19. const contentBlueStyle = {
  20. display:'flex',
  21. justifyContent: 'center',
  22. alignItems: 'center',
  23. color: '#fff',
  24. background: 'lightBlue',
  25. };
  26. const radioTitleStyle = {
  27. marginRight: 20
  28. }
  29. export const BasicUsage = () => (
  30. <Carousel style={style}>
  31. <div style={contentPinkStyle}>
  32. <h3>1</h3>
  33. </div>
  34. <div style={contentBlueStyle}>
  35. <h3>2</h3>
  36. </div>
  37. <div style={contentPinkStyle}>
  38. <h3>3</h3>
  39. </div>
  40. <div style={contentBlueStyle}>
  41. <h3>4</h3>
  42. </div>
  43. <div style={contentPinkStyle}>
  44. <h3>5</h3>
  45. </div>
  46. </Carousel>
  47. );
  48. BasicUsage.story = {
  49. name: 'basic usage',
  50. };
  51. // 主题切换
  52. export const theme = () => {
  53. const [theme, setTheme] = useState('primary');
  54. return (
  55. <div>
  56. <div>
  57. <span style={radioTitleStyle}>主题</span>
  58. <RadioGroup onChange={e => setTheme(e.target.value)} value={theme}>
  59. <Radio value='primary'>primary</Radio>
  60. <Radio value='light'>light</Radio>
  61. <Radio value='dark'>dark</Radio>
  62. </RadioGroup>
  63. </div>
  64. <br/>
  65. <Carousel style={style} theme={theme}>
  66. <div style={contentPinkStyle}>
  67. <h3>1</h3>
  68. </div>
  69. <div style={contentBlueStyle}>
  70. <h3>2</h3>
  71. </div>
  72. <div style={contentPinkStyle}>
  73. <h3>3</h3>
  74. </div>
  75. <div style={contentBlueStyle}>
  76. <h3>4</h3>
  77. </div>
  78. <div style={contentPinkStyle}>
  79. <h3>5</h3>
  80. </div>
  81. </Carousel>
  82. </div>
  83. );
  84. }
  85. theme.story = {
  86. name: 'theme',
  87. };
  88. // 指示器大小、类型、位置
  89. export const indicatorUsage = () => {
  90. const [size, setSize] = useState('small');
  91. const [type, setType] = useState('dot');
  92. const [position, setPosition] = useState('left');
  93. return (
  94. <div>
  95. <div>
  96. <span style={radioTitleStyle}>类型</span>
  97. <RadioGroup onChange={e => setType(e.target.value)} value={type}>
  98. <Radio value='dot'>dot</Radio>
  99. <Radio value='line'>line</Radio>
  100. <Radio value='columnar'>columnar</Radio>
  101. </RadioGroup>
  102. </div>
  103. <div>
  104. <span style={radioTitleStyle}>位置</span>
  105. <RadioGroup onChange={e => setPosition(e.target.value)} value={position}>
  106. <Radio value='left'>left</Radio>
  107. <Radio value='center'>center</Radio>
  108. <Radio value='right'>right</Radio>
  109. </RadioGroup>
  110. </div>
  111. <div>
  112. <span style={radioTitleStyle}>尺寸</span>
  113. <RadioGroup onChange={e => setSize(e.target.value)} value={size}>
  114. <Radio value={'small'}>small</Radio>
  115. <Radio value={'medium'}>medium</Radio>
  116. </RadioGroup>
  117. </div>
  118. <br/>
  119. <Carousel style={style} indicatorType={type} indicatorPosition={position} indicatorSize={size}>
  120. <div style={contentPinkStyle}>
  121. <h3>1</h3>
  122. </div>
  123. <div style={contentBlueStyle}>
  124. <h3>2</h3>
  125. </div>
  126. <div style={contentPinkStyle}>
  127. <h3>3</h3>
  128. </div>
  129. <div style={contentBlueStyle}>
  130. <h3>4</h3>
  131. </div>
  132. <div style={contentPinkStyle}>
  133. <h3>5</h3>
  134. </div>
  135. </Carousel>
  136. </div>
  137. );
  138. }
  139. indicatorUsage.story = {
  140. name: 'indicator usage',
  141. };
  142. // 箭头主题、显示时机
  143. export const arrowShow = () => {
  144. const [arrowType, setArrowTypew] = useState('always');
  145. const [show, setShow] = useState(true);
  146. return (
  147. <div>
  148. <div>
  149. <span style={radioTitleStyle}>展示箭头</span>
  150. <RadioGroup onChange={e => setShow(e.target.value)} value={show}>
  151. <Radio value={true}>展示</Radio>
  152. <Radio value={false}>不展示</Radio>
  153. </RadioGroup>
  154. </div>
  155. <div>
  156. <span style={radioTitleStyle}>展示时机</span>
  157. <RadioGroup onChange={e => setArrowTypew(e.target.value)} value={arrowType}>
  158. <Radio value='always'>always</Radio>
  159. <Radio value='hover'>hover</Radio>
  160. </RadioGroup>
  161. </div>
  162. <br/>
  163. <Carousel style={style} showArrow={show} arrowType={arrowType}>
  164. <div style={contentPinkStyle}>
  165. <h3>1</h3>
  166. </div>
  167. <div style={contentBlueStyle}>
  168. <h3>2</h3>
  169. </div>
  170. <div style={contentPinkStyle}>
  171. <h3>3</h3>
  172. </div>
  173. <div style={contentBlueStyle}>
  174. <h3>4</h3>
  175. </div>
  176. <div style={contentPinkStyle}>
  177. <h3>5</h3>
  178. </div>
  179. </Carousel>
  180. </div>
  181. )
  182. };
  183. arrowShow.story = {
  184. name: 'arrow show',
  185. };
  186. // 箭头参数
  187. export const customArrow = () => {
  188. const arrowProps = {
  189. leftArrow: { children: <IconArrowLeft size='large'/>},
  190. rightArrow: { children: <IconArrowRight size='large'/> },
  191. };
  192. return (
  193. <div>
  194. <Carousel style={style} arrowProps={arrowProps}>
  195. <div style={contentPinkStyle}>
  196. <h3>1</h3>
  197. </div>
  198. <div style={contentBlueStyle}>
  199. <h3>2</h3>
  200. </div>
  201. <div style={contentPinkStyle}>
  202. <h3>3</h3>
  203. </div>
  204. <div style={contentBlueStyle}>
  205. <h3>4</h3>
  206. </div>
  207. <div style={contentPinkStyle}>
  208. <h3>5</h3>
  209. </div>
  210. </Carousel>
  211. </div>
  212. );
  213. }
  214. customArrow.story = {
  215. name: 'custom arrow',
  216. };
  217. // 自动播放参数
  218. export const autoPlayExample = () => (
  219. <div>
  220. <Carousel style={style} autoPlay={{ interval: 1000, hoverToPause: true }}>
  221. <div style={contentPinkStyle}>
  222. <h3>1</h3>
  223. </div>
  224. <div style={contentBlueStyle}>
  225. <h3>2</h3>
  226. </div>
  227. <div style={contentPinkStyle}>
  228. <h3>3</h3>
  229. </div>
  230. <div style={contentBlueStyle}>
  231. <h3>4</h3>
  232. </div>
  233. <div style={contentPinkStyle}>
  234. <h3>5</h3>
  235. </div>
  236. </Carousel>
  237. </div>
  238. );
  239. autoPlayExample.story = {
  240. name: 'auto play example',
  241. };
  242. // 动画效果与速度
  243. export const animationUsage = () => {
  244. const [animation, setAnimation] = useState('slide');
  245. const [speed, setSpeed] = useState(1000);
  246. return (
  247. <div>
  248. <div>
  249. <span style={radioTitleStyle}>动画效果</span>
  250. <RadioGroup onChange={e => setAnimation(e.target.value)} value={animation}>
  251. <Radio value='slide'>slide</Radio>
  252. <Radio value='fade'>fade</Radio>
  253. </RadioGroup>
  254. </div>
  255. <div>
  256. <span style={radioTitleStyle}>切换速度</span>
  257. <RadioGroup onChange={e => setSpeed(e.target.value)} value={speed}>
  258. <Radio value={1000}>1000ms</Radio>
  259. <Radio value={2000}>2000ms</Radio>
  260. <Radio value={3000}>3000ms</Radio>
  261. </RadioGroup>
  262. </div>
  263. <br/>
  264. <Carousel style={style} speed={speed} animation={animation}>
  265. <div style={contentPinkStyle}>
  266. <h3>1</h3>
  267. </div>
  268. <div style={contentBlueStyle}>
  269. <h3>2</h3>
  270. </div>
  271. <div style={contentPinkStyle}>
  272. <h3>3</h3>
  273. </div>
  274. <div style={contentBlueStyle}>
  275. <h3>4</h3>
  276. </div>
  277. <div style={contentPinkStyle}>
  278. <h3>5</h3>
  279. </div>
  280. </Carousel>
  281. </div>
  282. )
  283. };
  284. animationUsage.story = {
  285. name: 'animation usage',
  286. };
  287. // 受控的轮播图
  288. class ControlledDemo extends React.Component {
  289. constructor(props) {
  290. super(props);
  291. this.ref = React.createRef();
  292. this.handleNext=this.handleNext.bind(this);
  293. this.handlePrev=this.handlePrev.bind(this);
  294. this.handleGoTo=this.handleGoTo.bind(this);
  295. this.handlePlay=this.handlePlay.bind(this);
  296. this.handleStop=this.handleStop.bind(this);
  297. this.state = {
  298. activeIndex: 0,
  299. };
  300. }
  301. handleNext(){
  302. this.ref.current.next();
  303. }
  304. handlePrev(){
  305. this.ref.current.prev();
  306. }
  307. handleGoTo(){
  308. this.ref.current.goTo(2);
  309. }
  310. handlePlay(){
  311. this.ref.current.play();
  312. }
  313. handleStop(){
  314. this.ref.current.stop();
  315. }
  316. onChange(activeIndex){
  317. this.setState({ activeIndex });
  318. }
  319. render() {
  320. return (
  321. <div>
  322. <Carousel style={style} animation='slide' ref={this.ref} activeIndex={this.state.activeIndex} onChange={this.onChange.bind(this)}>
  323. <div style={contentPinkStyle}>
  324. <h3>1</h3>
  325. </div>
  326. <div style={contentBlueStyle}>
  327. <h3>2</h3>
  328. </div>
  329. <div style={contentPinkStyle}>
  330. <h3>3</h3>
  331. </div>
  332. <div style={contentBlueStyle}>
  333. <h3>4</h3>
  334. </div>
  335. <div style={contentPinkStyle}>
  336. <h3>5</h3>
  337. </div>
  338. </Carousel>
  339. <br/>
  340. <Button onClick={this.handlePrev} style={{ marginRight: 10 }}>prev</Button>
  341. <Button onClick={this.handleNext} style={{ marginRight: 10 }}>next</Button>
  342. <Button onClick={this.handleGoTo} style={{ marginRight: 10 }}>goTo3</Button>
  343. <Button onClick={this.handlePlay} style={{ marginRight: 10 }}>play</Button>
  344. <Button onClick={this.handleStop} style={{ marginRight: 10 }}>stop</Button>
  345. </div>
  346. )
  347. }
  348. }
  349. export const controlledUsage = () => <ControlledDemo />;
  350. controlledUsage.story = {
  351. name: 'controlled usage',
  352. };
  353. class RefDemo extends React.Component {
  354. constructor(props) {
  355. super(props);
  356. this.ref = React.createRef();
  357. this.handleNext=this.handleNext.bind(this);
  358. this.handlePrev=this.handlePrev.bind(this);
  359. this.handleGoTo=this.handleGoTo.bind(this);
  360. this.handlePlay=this.handlePlay.bind(this);
  361. this.handleStop=this.handleStop.bind(this);
  362. }
  363. handleNext(){
  364. this.ref.current.next();
  365. }
  366. handlePrev(){
  367. this.ref.current.prev();
  368. }
  369. handleGoTo(){
  370. this.ref.current.goTo(2);
  371. }
  372. handlePlay(){
  373. this.ref.current.play();
  374. }
  375. handleStop(){
  376. this.ref.current.stop();
  377. }
  378. render() {
  379. return (
  380. <div>
  381. <Carousel style={style} animation='slide' ref={this.ref}>
  382. <div style={contentPinkStyle}>
  383. <h3>1</h3>
  384. </div>
  385. <div style={contentBlueStyle}>
  386. <h3>2</h3>
  387. </div>
  388. <div style={contentPinkStyle}>
  389. <h3>3</h3>
  390. </div>
  391. <div style={contentBlueStyle}>
  392. <h3>4</h3>
  393. </div>
  394. <div style={contentPinkStyle}>
  395. <h3>5</h3>
  396. </div>
  397. </Carousel>
  398. <br/>
  399. <Button onClick={this.handlePrev} style={{ marginRight: 10 }}>prev</Button>
  400. <Button onClick={this.handleNext} style={{ marginRight: 10 }}>next</Button>
  401. <Button onClick={this.handleGoTo} style={{ marginRight: 10 }}>goTo3</Button>
  402. <Button onClick={this.handlePlay} style={{ marginRight: 10 }}>play</Button>
  403. <Button onClick={this.handleStop} style={{ marginRight: 10 }}>stop</Button>
  404. </div>
  405. )
  406. }
  407. }
  408. export const refUsage = () => <RefDemo />;
  409. refUsage.story = {
  410. name: 'ref usage',
  411. };
  412. export const slideDirection = () => (
  413. <Carousel style={style} autoPlay={false} slideDirection='right'>
  414. <div style={contentPinkStyle}>
  415. <h3>index0</h3>
  416. </div>
  417. <div style={contentPinkStyle}>
  418. <h3>index1</h3>
  419. </div>
  420. <div style={contentPinkStyle}>
  421. <h3>index2</h3>
  422. </div>
  423. </Carousel>
  424. );
  425. slideDirection.story = {
  426. name: 'slide direction',
  427. };