index.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import * as React from 'react';
  2. import { Dragact } from '../../src/lib/dragact'
  3. import { Card } from '../NormalLayout/index';
  4. import './index.css';
  5. const Words = [
  6. { content: 'You can do anything, but not everything.' },
  7. { content: 'Those who dare to fail miserably can achieve greatly.' },
  8. { content: 'You miss 100 percent of the shots you never take.' },
  9. { content: 'Those who believe in telekinetics, raise my hand.' },
  10. { content: 'I’d rather live with a good question than a bad answer.' }
  11. ]
  12. const fakeData = () => {
  13. var Y = 0;
  14. return Words.map((item, index) => {
  15. if (index % 4 === 0) Y++;
  16. return { ...item, GridX: index % 4 * 4, GridY: Y * 4, w: 4, h: 2, key: index + '' }
  17. })
  18. }
  19. var storeLayout: any = void 666;
  20. export class LayoutRestore extends React.Component<{}, {}> {
  21. dragactNode: Dragact;
  22. handleOnDragEnd = () => {
  23. const newLayout = this.dragactNode.getLayout();
  24. const parsedLayout = JSON.stringify(newLayout);
  25. localStorage.setItem('layout', parsedLayout);
  26. }
  27. componentWillMount() {
  28. const lastLayout = localStorage.getItem('layout');
  29. if (lastLayout) {
  30. storeLayout = JSON.parse(lastLayout);
  31. }
  32. }
  33. renderDragact = () => {
  34. const margin: [number, number] = [5, 5];
  35. const dragactInit = {
  36. width: 600,
  37. col: 12,
  38. rowHeight: 800 / 12,
  39. margin: margin,
  40. className: 'normal-layout',
  41. layout: storeLayout ? storeLayout : fakeData(),
  42. placeholder: true
  43. }
  44. return (
  45. <Dragact
  46. {...dragactInit}
  47. ref={node => node ? this.dragactNode = node : null}
  48. onDragEnd={this.handleOnDragEnd}
  49. >
  50. {(item: any, provided: any) => {
  51. return <Card item={item} provided={provided} />
  52. }}
  53. </Dragact>
  54. )
  55. }
  56. render() {
  57. return (
  58. <div style={{ display: 'flex', justifyContent: 'center' }}>
  59. <div>
  60. <h1 style={{ textAlign: 'center' }}>存储布局 Demo</h1>
  61. {this.renderDragact()}
  62. </div>
  63. </div>
  64. )
  65. }
  66. }