Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. var _extends = Object.assign || function (target) {for (var i = 1; i < arguments.length; i++) {var source = arguments[i];for (var key in source) {if (Object.prototype.hasOwnProperty.call(source, key)) {target[key] = source[key];}}}return target;};var _createClass = function () {function defineProperties(target, props) {for (var i = 0; i < props.length; i++) {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}();function _toConsumableArray(arr) {if (Array.isArray(arr)) {for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {arr2[i] = arr[i];}return arr2;} else {return Array.from(arr);}}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}function _possibleConstructorReturn(self, call) {if (!self) {throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return call && (typeof call === "object" || typeof call === "function") ? call : self;}function _inherits(subClass, superClass) {if (typeof superClass !== "function" && superClass !== null) {throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);}subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } });if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;}var App = function (_React$Component) {_inherits(App, _React$Component);
  2. function App(p) {_classCallCheck(this, App);var _this = _possibleConstructorReturn(this, (App.__proto__ || Object.getPrototypeOf(App)).call(this,
  3. p));
  4. _this.state = { x: 0, y: 0, circles: [] };return _this;
  5. }_createClass(App, [{ key: "setCoordinates", value: function setCoordinates(
  6. newX, newY) {
  7. this.setState({ x: newX, y: newY });
  8. } }, { key: "clicked", value: function clicked()
  9. {var _this2 = this;
  10. //add a new circle
  11. this.setState(
  12. {
  13. circles: [].concat(_toConsumableArray(this.state.circles), [this.makeCircle()]) },
  14.  
  15. function () {
  16. console.log("New state", _this2.state);
  17. });
  18.  
  19. } }, { key: "removeCircleByIndex", value: function removeCircleByIndex(
  20.  
  21. index) {
  22. this.setState({
  23. circles: this.state.circles.filter(function (c) {
  24. return c.index !== index; //filter: return true to keep item, return false to discard item.
  25. }) });
  26.  
  27. } }, { key: "makeCircle", value: function makeCircle()
  28.  
  29. {
  30. return {
  31. x: this.state.x,
  32. y: this.state.y,
  33. index: this.getNextIndex() };
  34.  
  35. } }, { key: "getNextIndex", value: function getNextIndex()
  36.  
  37. {
  38. // if we have items, return next id, else return 0
  39. var lastItem = this.state.circles[this.state.circles.length - 1];
  40. if (lastItem) {
  41. return lastItem.index + 1;
  42. } else {
  43. return 0;
  44. }
  45. } }, { key: "render", value: function render()
  46.  
  47. {var _this3 = this;
  48. return (
  49. React.createElement("div", { className: "App" },
  50. React.createElement("div", {
  51. onMouseMove: function onMouseMove(e) {
  52. _this3.setCoordinates(e.clientX, e.clientY);
  53. },
  54. onClick: function onClick() {
  55. _this3.clicked();
  56. },
  57. className: "box" }),
  58.  
  59. this.state.circles.map(function (c) {
  60. //loop though all circles
  61. return (
  62. React.createElement(Circle, _extends({
  63. onClick: function onClick() {
  64. _this3.removeCircleByIndex(c.index);
  65. },
  66. key: c.index },
  67. c)));
  68.  
  69.  
  70. }),
  71.  
  72. React.createElement("p", null,
  73. this.state.x, " - ", this.state.y)));
  74.  
  75.  
  76.  
  77. } }]);return App;}(React.Component);
  78.  
  79.  
  80. var rootElement = document.getElementById("root");
  81. ReactDOM.render(React.createElement(App, null), rootElement);
  82.  
  83. var Circle = function Circle(props) {
  84. return (
  85. React.createElement("div", {
  86. onClick: function onClick() {
  87. props.onClick();
  88. },
  89. style: {
  90. position: "absolute",
  91. cursor: "pointer",
  92. left: props.x - 25,
  93. top: props.y - 25,
  94. width: 50,
  95. height: 50,
  96. borderRadius: 25,
  97. border: "1px solid black",
  98. textAlign: "center",
  99. lineHeight: "50px" } },
  100.  
  101.  
  102. props.index));
  103.  
  104.  
  105. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement