Index.stories.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import * as React from 'react';
  2. import styled from 'styled-components';
  3. import { storiesOf } from '@storybook/react';
  4. import { PhotoProvider, PhotoConsumer } 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. `;
  49. const DefaultImage = styled.img`
  50. width: 100px;
  51. height: 100px;
  52. `;
  53. storiesOf('react-photo-view', module)
  54. .add('默认展示', () => (
  55. <PhotoProvider>
  56. <ImageList>
  57. {photoImages.map((item, index) => (
  58. <PhotoConsumer key={index} src={item} intro={item}>
  59. <ViewBox viewImage={item} />
  60. </PhotoConsumer>
  61. ))}
  62. </ImageList>
  63. </PhotoProvider>
  64. ))
  65. .add('两张预览', () => (
  66. <PhotoProvider>
  67. <ImageList>
  68. {photoImages.map((item, index) => (
  69. <PhotoConsumer key={index} src={item} intro={item}>
  70. {index < 2 ? <ViewBox viewImage={item} /> : undefined}
  71. </PhotoConsumer>
  72. ))}
  73. </ImageList>
  74. </PhotoProvider>
  75. ))
  76. .add('通过按钮触发', () => (
  77. <PhotoProvider>
  78. <ImageList>
  79. <PhotoConsumer src={image4}>
  80. <Button>打开预览</Button>
  81. </PhotoConsumer>
  82. </ImageList>
  83. </PhotoProvider>
  84. ))
  85. .add('自定义加载失败', () => (
  86. <ImageList>
  87. <PhotoProvider>
  88. <PhotoConsumer src={null}>
  89. <Button>无默认图</Button>
  90. </PhotoConsumer>
  91. </PhotoProvider>
  92. <PhotoProvider brokenElement={<DefaultImage src={defaultPhoto} />}>
  93. <PhotoConsumer src={null}>
  94. <Button>自定义默认图</Button>
  95. </PhotoConsumer>
  96. </PhotoProvider>
  97. </ImageList>
  98. ));