@teamsnap/teamsnap-ui
Version:
a CSS component library for TeamSnap
138 lines (137 loc) • 7.08 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = __importStar(require("react"));
var PropTypes = __importStar(require("prop-types"));
var PopUpAction = /** @class */ (function (_super) {
__extends(PopUpAction, _super);
function PopUpAction(props) {
var _this = _super.call(this, props) || this;
_this.state = {
isPopupOpen: false,
isConfirmOpen: false,
selectedAction: {}
};
return _this;
}
PopUpAction.prototype.componentDidMount = function () {
document.body.addEventListener("click", this.handleBodyClick.bind(this), { capture: true });
};
PopUpAction.prototype.componentWillUnmount = function () {
document.body.removeEventListener("click", this.handleBodyClick.bind(this));
};
PopUpAction.prototype.handleBodyClick = function () {
this.setState({
isPopupOpen: false,
isConfirmOpen: false
});
};
PopUpAction.prototype.togglePopup = function () {
var isPopupOpen = this.state.isPopupOpen;
this.setState({
isPopupOpen: !isPopupOpen
});
};
PopUpAction.prototype.handleActionClick = function (action) {
if (action.requiresConfirmation) {
this.setState(__assign(__assign({}, this.state), { isConfirmOpen: true, selectedAction: action }));
}
else {
action.callback();
}
};
PopUpAction.prototype.render = function () {
var _this = this;
var dirString = this.props.direction.reduce(function (acc, cur) {
return acc + (" Popup-container--" + cur);
}, "");
return (React.createElement(React.Fragment, null,
React.createElement("div", { className: "Popup Popup--hover" },
React.createElement("button", { className: "Button Button--small", onClick: this.togglePopup.bind(this) }, this.props.text),
React.createElement("div", { className: "Popup-container " + dirString + " " + (this.state.isPopupOpen ? "is-open" : ""), style: this.props.popupStyle },
React.createElement("div", { className: "Popup-content" },
React.createElement("div", { className: "u-textLeft" }, this.props.actions.map(function (action) {
return (React.createElement("div", { key: action.text },
React.createElement("button", { tabIndex: 0, className: "u-padEndsSm u-padSidesMd", style: {
cursor: "pointer",
appearance: "none",
border: "none",
background: "none",
width: "100%",
textAlign: "left"
}, onClick: _this.handleActionClick.bind(_this, action) }, action.text),
React.createElement("hr", { className: "Divider u-spaceEndsNone" })));
}))))),
React.createElement("div", { className: "Popup" },
React.createElement("div", { className: "Popup-container Popup-container--overlay" +
(this.state.selectedAction.requiresConfirmation &&
this.state.isConfirmOpen
? " is-open"
: "") },
React.createElement("div", { className: "Popup-content u-padMd" },
React.createElement("h3", { className: "u-spaceBottomSm u-textCenter" }, "Are you sure?"),
React.createElement("p", { className: "u-textLeft" }, this.state.selectedAction.confirmationText),
React.createElement("div", { className: "u-textCenter u-spaceTopMd" },
React.createElement("button", { onClick: function () {
return _this.setState(__assign(__assign({}, _this.state), { isConfirmOpen: false, selectedAction: {} }));
}, className: "u-spaceRightSm Button Button--negative" }, "Cancel"),
React.createElement("button", { onClick: this.state.selectedAction.callback, className: "Button Button--primary" }, "Confirm")))))));
};
PopUpAction.propTypes = {
text: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
actions: PropTypes.arrayOf(PropTypes.shape({
text: PropTypes.string.isRequired,
callback: PropTypes.func.isRequired,
requiresConfirmation: PropTypes.bool,
confirmationText: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
})),
popupStyle: PropTypes.object,
direction: PropTypes.arrayOf(PropTypes.oneOf(["down", "right", "left", "rightHang", "leftHang", "overlay"])),
};
return PopUpAction;
}(React.Component));
exports.default = PopUpAction;