index.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import *as React from 'react';
  2. import { Dragact } from '../../src/lib/dragact';
  3. const Words = [
  4. { content: 'You can do anything, but not everything.' },
  5. { content: 'Those who dare to fail miserably can achieve greatly.' },
  6. { content: 'You miss 100 percent of the shots you never take.' },
  7. { content: 'Those who believe in telekinetics, raise my hand.' },
  8. { content: 'I’d rather live with a good question than a bad answer.' }
  9. ]
  10. const Card = ({ item, provided, onDelete }: any) => {
  11. return (
  12. <div
  13. className='layout-Item'
  14. {...provided.props}
  15. {...provided.dragHandle}
  16. >
  17. <div
  18. style={{
  19. position: 'absolute',
  20. width: 10, height: 10, right: 15, top: 5, cursor: 'pointer'
  21. }}
  22. onClick={() => onDelete(item.key)}
  23. >❌</div>
  24. <div style={{ padding: 5, textAlign: 'center', color: '#595959' }}>{item.content}</div>
  25. </div>
  26. )
  27. }
  28. const fakeData = () => {
  29. var Y = 0;
  30. return Words.map((item, index) => {
  31. if (index % 4 === 0) Y++;
  32. return { ...item, GridX: index % 4 * 4, GridY: Y * 4, w: 4, h: 3, key: index }
  33. })
  34. }
  35. const makeOne = () => {
  36. return { content: 'added', GridX: 0, GridY: 0, w: 4, h: 3, key: Date.now() }
  37. }
  38. export class AddRemove extends React.Component<{}, {}> {
  39. state = {
  40. layout: fakeData()
  41. }
  42. componentDidMount() {
  43. }
  44. handleClick = () => {
  45. this.setState({
  46. layout: [...this.state.layout, makeOne()]
  47. })
  48. console.log(this.state.layout)
  49. }
  50. hanldeOnDelete = (key: any) => {
  51. const layout = this.state.layout.filter((item) => {
  52. if (item.key !== key) {
  53. return item;
  54. }
  55. })
  56. this.setState({
  57. layout: layout
  58. })
  59. }
  60. render() {
  61. const margin: [number, number] = [5, 5];
  62. const dragactInit = {
  63. width: 600,
  64. col: 12,
  65. rowHeight: 800 / 12,
  66. margin: margin,
  67. className: 'normal-layout',
  68. layout: this.state.layout,
  69. placeholder: true
  70. }
  71. return (
  72. <div>
  73. <div style={{ display: 'flex', justifyContent: 'center' }} >
  74. <div>
  75. <h1 style={{ textAlign: 'center' }}>AddRemove Demo</h1>
  76. <button onClick={this.handleClick}>新增</button>
  77. <Dragact {...dragactInit} >
  78. {(item, provided) => {
  79. return <Card item={item} provided={provided} onDelete={this.hanldeOnDelete} />
  80. }}
  81. </Dragact>
  82. </div>
  83. </div>
  84. </div>
  85. )
  86. }
  87. }