index.js 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. var __extends = (this && this.__extends) || (function () {
  2. var extendStatics = Object.setPrototypeOf ||
  3. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  4. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  5. return function (d, b) {
  6. extendStatics(d, b);
  7. function __() { this.constructor = d; }
  8. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  9. };
  10. })();
  11. var __assign = (this && this.__assign) || Object.assign || function(t) {
  12. for (var s, i = 1, n = arguments.length; i < n; i++) {
  13. s = arguments[i];
  14. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  15. t[p] = s[p];
  16. }
  17. return t;
  18. };
  19. import * as React from 'react';
  20. import { Dragact } from '../lib/dragact';
  21. var Words = [
  22. { content: 'You can do anything, but not everything.' },
  23. { content: 'Those who dare to fail miserably can achieve greatly.' },
  24. { content: 'You miss 100 percent of the shots you never take.' },
  25. { content: 'Those who believe in telekinetics, raise my hand.' },
  26. { content: 'I’d rather live with a good question than a bad answer.' }
  27. ];
  28. var Card = function (_a) {
  29. var item = _a.item, provided = _a.provided, onDelete = _a.onDelete;
  30. return (React.createElement("div", __assign({ className: 'layout-Item' }, provided.props, provided.dragHandle),
  31. React.createElement("div", { style: {
  32. position: 'absolute',
  33. width: 10, height: 10, right: 15, top: 5, cursor: 'pointer'
  34. }, onClick: function () { return onDelete(item.key); } }, "\u274C"),
  35. React.createElement("div", { style: { padding: 5, textAlign: 'center', color: '#595959' } }, item.content)));
  36. };
  37. var fakeData = function () {
  38. var Y = 0;
  39. return Words.map(function (item, index) {
  40. if (index % 4 === 0)
  41. Y++;
  42. return __assign({}, item, { GridX: index % 4 * 4, GridY: Y * 4, w: 4, h: 3, key: index });
  43. });
  44. };
  45. var makeOne = function () {
  46. return { content: 'added', GridX: 0, GridY: 0, w: 4, h: 3, key: Date.now() };
  47. };
  48. var AddRemove = /** @class */ (function (_super) {
  49. __extends(AddRemove, _super);
  50. function AddRemove() {
  51. var _this = _super !== null && _super.apply(this, arguments) || this;
  52. _this.state = {
  53. layout: fakeData()
  54. };
  55. _this.handleClick = function () {
  56. _this.setState({
  57. layout: _this.state.layout.concat([makeOne()])
  58. });
  59. console.log(_this.state.layout);
  60. };
  61. _this.hanldeOnDelete = function (key) {
  62. var layout = _this.state.layout.filter(function (item) {
  63. if (item.key !== key) {
  64. return item;
  65. }
  66. });
  67. _this.setState({
  68. layout: layout
  69. });
  70. };
  71. return _this;
  72. }
  73. AddRemove.prototype.componentDidMount = function () {
  74. };
  75. AddRemove.prototype.render = function () {
  76. var _this = this;
  77. var margin = [5, 5];
  78. var dragactInit = {
  79. width: 600,
  80. col: 12,
  81. rowHeight: 800 / 12,
  82. margin: margin,
  83. className: 'normal-layout',
  84. layout: this.state.layout,
  85. placeholder: true
  86. };
  87. return (React.createElement("div", null,
  88. React.createElement("div", { style: { display: 'flex', justifyContent: 'center' } },
  89. React.createElement("div", null,
  90. React.createElement("h1", { style: { textAlign: 'center' } }, "AddRemove Demo"),
  91. React.createElement("button", { onClick: this.handleClick }, "\u65B0\u589E"),
  92. React.createElement(Dragact, __assign({}, dragactInit), function (item, provided) {
  93. return React.createElement(Card, { item: item, provided: provided, onDelete: _this.hanldeOnDelete });
  94. })))));
  95. };
  96. return AddRemove;
  97. }(React.Component));
  98. export { AddRemove };