Index.stories.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.png';
  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 defaultPhoto from './static/default-photo.svg';
  12. const photoImages = [image1, image2, image3, image4, image5];
  13. const ImageList = styled.div`
  14. padding: 40px;
  15. display: flex;
  16. flex-wrap: wrap;
  17. align-items: center;
  18. `;
  19. const SmallImage = styled.img`
  20. margin-right: 20px;
  21. margin-bottom: 20px;
  22. width: 100px;
  23. height: 100px;
  24. cursor: pointer;
  25. `;
  26. const Button = styled.button`
  27. padding: 6px 10px;
  28. border: 1px solid #ccc;
  29. border-radius: 2px;
  30. cursor: pointer;
  31. &:not(:last-child) {
  32. margin-right: 12px;
  33. }
  34. `;
  35. const DefaultImage = styled.img`
  36. width: 100px;
  37. height: 100px;
  38. `;
  39. storiesOf('基本操作', module)
  40. .add('默认预览', () => (
  41. <PhotoProvider>
  42. <ImageList>
  43. {photoImages.map((item, index) => (
  44. <PhotoConsumer key={index} src={item} intro={item}>
  45. <SmallImage src={item} />
  46. </PhotoConsumer>
  47. ))}
  48. </ImageList>
  49. </PhotoProvider>
  50. ))
  51. .add('只展示两张', () => (
  52. <PhotoProvider>
  53. <ImageList>
  54. {photoImages.map((item, index) => (
  55. <PhotoConsumer key={index} src={item} intro={item}>
  56. {index < 2 ? <SmallImage src={item} /> : undefined}
  57. </PhotoConsumer>
  58. ))}
  59. </ImageList>
  60. </PhotoProvider>
  61. ))
  62. .add('按钮触发', () => (
  63. <PhotoProvider>
  64. <ImageList>
  65. <PhotoConsumer src={image4}>
  66. <Button>打开预览</Button>
  67. </PhotoConsumer>
  68. </ImageList>
  69. </PhotoProvider>
  70. ))
  71. .add('错误图片地址', () => (
  72. <ImageList>
  73. <PhotoProvider>
  74. <PhotoConsumer src={null}>
  75. <Button>无默认图</Button>
  76. </PhotoConsumer>
  77. </PhotoProvider>
  78. <PhotoProvider brokenElement={<DefaultImage src={defaultPhoto} />}>
  79. <PhotoConsumer src={null}>
  80. <Button>自定义默认图</Button>
  81. </PhotoConsumer>
  82. </PhotoProvider>
  83. </ImageList>
  84. ));