optimizely-oui
Version:
Optimizely's Component Library.
190 lines (163 loc) • 7.44 kB
JavaScript
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/* eslint-disable react/prop-types */
import React from "react";
import { storiesOf } from "@storybook/react";
import { withKnobs } from "@storybook/addon-knobs";
import { action } from "@storybook/addon-actions";
import DragAndDropStateless from "./index";
import Token from "../Token";
import Tile from "../Tile";
import Col from "../Layout/Col";
var itemsWithoutGroups = [{
id: 1,
type: "item",
text: "Item A",
description: "ID: 0123"
}, {
id: 2,
type: "item",
text: "Item B",
description: "ID: 4567"
}, {
id: 3,
type: "item",
text: "Item C",
description: "ID: 8910"
}, {
id: 4,
type: "item",
text: "Item D",
description: "ID: 1112"
}, {
id: 5,
type: "item",
text: "Item E",
description: "ID: 1314"
}];
var renderTokenItem = function renderTokenItem(_ref) {
var item = _ref.item,
index = _ref.index,
snapshot = _ref.snapshot;
return React.createElement("div", null, React.createElement(Token, {
isDraggable: true,
name: item["text"] + "- isDragging? " + snapshot.isDragging,
order: index + 1,
showWell: false
}));
};
var renderTokenItemWithAdjustedDragging = function renderTokenItemWithAdjustedDragging(_ref2) {
var item = _ref2.item,
index = _ref2.index,
snapshot = _ref2.snapshot,
dragHandleProps = _ref2.dragHandleProps;
return React.createElement("div", null, React.createElement(Token, {
isDraggable: true,
name: item["text"] + "- isDragging? " + snapshot.isDragging,
showWell: false,
usesDragHandle: true,
dragHandleProps: dragHandleProps
}));
};
var renderTileItem = function renderTileItem(_ref3) {
var item = _ref3.item,
index = _ref3.index,
snapshot = _ref3.snapshot,
dragHandleProps = _ref3.dragHandleProps;
return React.createElement(Col, {
small: "6"
}, React.createElement(Tile, {
name: item["text"],
description: item["description"],
onPillClick: action("onPillClick"),
isDraggable: true,
testSection: "with-warning",
dragHandleProps: dragHandleProps,
onDismiss: action("on Dismiss Click")
}));
}; // Helper wrapper class to store the state so the stories are usable/interactive
var DragAndDrop =
/*#__PURE__*/
function (_React$Component) {
_inherits(DragAndDrop, _React$Component);
function DragAndDrop() {
var _getPrototypeOf2;
var _this;
_classCallCheck(this, DragAndDrop);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(DragAndDrop)).call.apply(_getPrototypeOf2, [this].concat(args)));
_defineProperty(_assertThisInitialized(_this), "state", {
items: _this.props.items
});
_defineProperty(_assertThisInitialized(_this), "updateItemOrder", function (items) {
_this.setState({
items: items
});
_this.props.onDragEnd(items);
});
return _this;
}
_createClass(DragAndDrop, [{
key: "render",
value: function render() {
var _this$props = this.props,
renderItem = _this$props.renderItem,
idForDroppableRegion = _this$props.idForDroppableRegion,
onBeforeCapture = _this$props.onBeforeCapture,
useCustomDragHandle = _this$props.useCustomDragHandle;
return React.createElement(DragAndDropStateless, {
idForDroppableRegion: idForDroppableRegion,
items: this.state.items,
onBeforeCapture: onBeforeCapture,
onDragEnd: this.updateItemOrder,
renderItem: renderItem,
useCustomDragHandle: useCustomDragHandle
});
}
}]);
return DragAndDrop;
}(React.Component);
DragAndDrop.propTypes = DragAndDropStateless.propTypes;
DragAndDrop.defaultProps = DragAndDropStateless.defaultProps;
var stories = storiesOf("Experimental|DragAndDrop", module);
stories.addDecorator(withKnobs).addDecorator(function (story) {
return React.createElement("div", {
id: "root-preview"
}, story());
});
stories.add("Reordering flat list", function () {
return React.createElement(DragAndDrop, {
idForDroppableRegion: "droppable-story-demo",
items: itemsWithoutGroups,
onBeforeCapture: action("do something before dragging begins"),
onDragEnd: action("get new order of items"),
renderItem: renderTokenItem
});
}).add("With custom drag handle", function () {
return React.createElement(DragAndDrop, {
idForDroppableRegion: "droppable-story-demo",
items: itemsWithoutGroups,
onBeforeCapture: action("do something before dragging begins"),
onDragEnd: action("get new order of items"),
useCustomDragHandle: true,
renderItem: renderTokenItemWithAdjustedDragging
});
}).add("Using Tile", function () {
return React.createElement(DragAndDrop, {
idForDroppableRegion: "droppable-story-demo",
items: itemsWithoutGroups,
onBeforeCapture: action("do something before dragging begins"),
onDragEnd: action("get new order of items"),
renderItem: renderTileItem
});
});