Index.stories.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import * as React from 'react';
  2. import styled from 'styled-components';
  3. import { storiesOf } from '@storybook/react';
  4. import { PhotoProvider, PhotoConsumer, PhotoSlider } from 'react-photo-view';
  5. import 'react-photo-view/dist/index.css';
  6. import image1 from './static/1.jpg';
  7. import image2 from './static/2.jpg';
  8. import image3 from './static/3.jpg';
  9. import image4 from './static/4.jpg';
  10. import image5 from './static/5.jpg';
  11. import image6 from './static/6.jpg';
  12. import image7 from './static/7.jpg';
  13. import image8 from './static/8.jpg';
  14. import defaultPhoto from './static/default-photo.svg';
  15. const photoImages = [
  16. image1,
  17. image2,
  18. image3,
  19. image4,
  20. image5,
  21. image6,
  22. image7,
  23. image8,
  24. ];
  25. const ImageList = styled.div`
  26. padding: 40px;
  27. display: flex;
  28. flex-wrap: wrap;
  29. align-items: center;
  30. `;
  31. const ViewBox = styled.div`
  32. margin-right: 20px;
  33. margin-bottom: 20px;
  34. width: 100px;
  35. height: 100px;
  36. cursor: pointer;
  37. background: url('${props => props.viewImage}') no-repeat center;
  38. background-size: cover;
  39. `;
  40. const Button = styled.button`
  41. padding: 6px 10px;
  42. border: 1px solid #ccc;
  43. border-radius: 2px;
  44. cursor: pointer;
  45. &:not(:last-child) {
  46. margin-right: 12px;
  47. }
  48. &[type='primary'] {
  49. background: deepskyblue;
  50. border-color: deepskyblue;
  51. color: white;
  52. }
  53. `;
  54. const DefaultImage = styled.img`
  55. width: 100px;
  56. height: 100px;
  57. `;
  58. storiesOf('react-photo-view', module)
  59. .add('默认展示', () => (
  60. <PhotoProvider>
  61. <ImageList>
  62. {photoImages.map((item, index) => (
  63. <PhotoConsumer key={index} src={item} intro={item}>
  64. <ViewBox viewImage={item} />
  65. </PhotoConsumer>
  66. ))}
  67. </ImageList>
  68. </PhotoProvider>
  69. ))
  70. .add('两张预览', () => (
  71. <PhotoProvider>
  72. <ImageList>
  73. {photoImages.map((item, index) => (
  74. <PhotoConsumer key={index} src={item}>
  75. {index < 2 ? <ViewBox viewImage={item} /> : undefined}
  76. </PhotoConsumer>
  77. ))}
  78. </ImageList>
  79. </PhotoProvider>
  80. ))
  81. .add('通过按钮触发', () => (
  82. <PhotoProvider>
  83. <ImageList>
  84. <PhotoConsumer src={image4}>
  85. <Button>打开预览</Button>
  86. </PhotoConsumer>
  87. </ImageList>
  88. </PhotoProvider>
  89. ))
  90. .add('自定义加载失败', () => (
  91. <ImageList>
  92. <PhotoProvider>
  93. <PhotoConsumer src={null}>
  94. <Button>无默认图</Button>
  95. </PhotoConsumer>
  96. </PhotoProvider>
  97. <PhotoProvider brokenElement={<DefaultImage src={defaultPhoto} />}>
  98. <PhotoConsumer src={null}>
  99. <Button>自定义默认图</Button>
  100. </PhotoConsumer>
  101. </PhotoProvider>
  102. </ImageList>
  103. ))
  104. .add('受控 PhotoSlider', () => {
  105. const [visible, setVisible] = React.useState(false);
  106. const [photoIndex, setPhotoIndex] = React.useState(0);
  107. function handleShowSlider() {
  108. setVisible(true);
  109. }
  110. function handleCloseSlider() {
  111. setVisible(false);
  112. }
  113. return (
  114. <ImageList>
  115. <Button onClick={() => setPhotoIndex(2)}>setPhotoIndex(2)</Button>
  116. <Button onClick={() => setPhotoIndex(4)}>setPhotoIndex(4)</Button>
  117. <Button onClick={handleShowSlider} type="primary">
  118. 打开 PhotoSlider
  119. </Button>
  120. <PhotoSlider
  121. images={photoImages.map(item => ({ src: item }))}
  122. visible={visible}
  123. onClose={handleCloseSlider}
  124. index={photoIndex}
  125. onIndexChange={setPhotoIndex}
  126. />
  127. </ImageList>
  128. );
  129. });