simple.tsx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import styled from 'styled-components';
  4. import { PhotoSlider } from '../src';
  5. const Container = styled.div`
  6. font-size: 32px;
  7. `;
  8. const Header = styled.header`
  9. padding: 40px;
  10. font-size: 32px;
  11. border-bottom: 1px solid #ccc;
  12. `;
  13. class Example extends React.Component {
  14. state = {
  15. photoImages: ['1.png', '2.jpg', '1.png'],
  16. photoVisible: true,
  17. photoIndex: 1,
  18. };
  19. handlePhotoClose = () => {
  20. this.setState({
  21. photoVisible: false,
  22. });
  23. }
  24. handleVisibleChange = (photoIndex) => {
  25. this.setState({
  26. photoIndex,
  27. });
  28. }
  29. render() {
  30. const { photoImages, photoVisible, photoIndex } = this.state;
  31. return (
  32. <Container>
  33. <Header>React 图片预览组件</Header>
  34. <PhotoSlider
  35. images={photoImages}
  36. index={photoIndex}
  37. onIndexChange={this.handleVisibleChange}
  38. visible={photoVisible}
  39. onClose={this.handlePhotoClose}
  40. />
  41. </Container>
  42. );
  43. }
  44. }
  45. ReactDOM.render(<Example />, document.getElementById('root'));