react-cm-ui
Version:
React UI for Healthy Church
407 lines (363 loc) • 14.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _lodash = require("lodash");
var _classnames = _interopRequireDefault(require("classnames"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = _interopRequireDefault(require("react"));
var _keyCode = _interopRequireDefault(require("../../global/keyCode.js"));
var _withStyles = _interopRequireDefault(require("../../styles/withStyles.js"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _typeof(obj) { "@babel/helpers - typeof"; 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 _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 _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
var propTypes = {
children: _propTypes["default"].oneOfType([_propTypes["default"].arrayOf(_propTypes["default"].node), _propTypes["default"].node]),
/**
* Override or extend the styles applied to Prompt.
*/
classes: _propTypes["default"].shape({
actions: _propTypes["default"].string,
root: _propTypes["default"].string
}),
className: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].shape({})]),
id: _propTypes["default"].string,
inline: _propTypes["default"].bool,
inlineHorizontalAlign: _propTypes["default"].oneOf(['left', 'right']),
inlineMessageColor: _propTypes["default"].oneOf(['alert', 'success', 'warning']),
message: _propTypes["default"].string,
onClick: _propTypes["default"].func,
onKeyDown: _propTypes["default"].func,
onNoClick: _propTypes["default"].func,
onNoKeyDown: _propTypes["default"].func,
onYesClick: _propTypes["default"].func,
onYesKeyDown: _propTypes["default"].func,
show: _propTypes["default"].bool,
style: _propTypes["default"].shape({}),
// eslint-disable-line react/forbid-prop-types
theme: _propTypes["default"].shape({
zIndex: _propTypes["default"].shape({
modal: _propTypes["default"].number
})
})
};
var defaultProps = {
children: null,
classes: null,
className: undefined,
id: undefined,
inline: false,
inlineHorizontalAlign: 'left',
inlineMessageColor: undefined,
message: 'Are you sure?',
onClick: undefined,
onKeyDown: undefined,
onNoClick: undefined,
onNoKeyDown: undefined,
onYesClick: undefined,
onYesKeyDown: undefined,
show: undefined,
style: undefined,
theme: null
};
var styles = function styles(_ref) {
var palette = _ref.palette,
spacing = _ref.spacing,
typography = _ref.typography,
zIndex = _ref.zIndex;
return {
actions: {},
root: {
'&.prompt-inline': {
display: 'inline-block',
'& $actions': {
alignItems: 'center',
borderRadius: 3,
boxShadow: '0 4px 4px 0 rgba(0, 0, 0, .43)',
color: palette.text.contrastText,
display: 'none',
flex: '0 1 auto',
fontSize: typography.pxToRem(14),
fontWeight: typography.fontWeightMedium,
height: 33,
position: 'absolute',
zIndex: zIndex.prompt
},
'& .prompt': {
'&-no-btn, &-yes-btn': {
alignItems: 'center',
backgroundColor: palette.grey[500],
border: 0,
// boxShadow: `inset 0 0 0 1px ${palette.grey[600]}`,
color: palette.text.contrastText,
cursor: 'pointer',
display: 'inline-flex',
height: 33,
letterSpacing: 1,
lineHeight: 1,
outline: 'none',
overflow: 'hidden',
padding: [[0, spacing(1)]],
position: 'relative',
textAlign: 'center',
textDecoration: 'none',
textTransform: 'capitalize',
transition: 'background-color 125ms linear, color 125ms linear, opacity 250ms ease-out',
verticalAlign: 'top',
'&:focus': {
boxShadow: "0 0 0 1px ".concat(palette.active.primary)
},
'&:hover': {
backgroundColor: palette.active.primary
}
},
'&-no-btn': {
borderTopRightRadius: 3,
borderBottomRightRadius: 3,
'&::before': {
backgroundColor: palette.grey[600],
content: '""',
height: 33,
left: 0,
position: 'absolute',
top: 0,
width: 1
}
}
}
}
}
};
};
var Prompt = /*#__PURE__*/function (_React$Component) {
_inherits(Prompt, _React$Component);
var _super = _createSuper(Prompt);
function Prompt(props) {
var _this;
_classCallCheck(this, Prompt);
_this = _super.call(this, props);
_this.state = {
show: props.show || false,
inlineVerticalAlign: 0
};
_this.onClick = _this.onClick.bind(_assertThisInitialized(_this));
_this.onChildKeyDown = _this.onChildKeyDown.bind(_assertThisInitialized(_this));
_this.onMouseDown = _this.onMouseDown.bind(_assertThisInitialized(_this));
_this.onNoClick = _this.onNoClick.bind(_assertThisInitialized(_this));
_this.onNoKeyDown = _this.onNoKeyDown.bind(_assertThisInitialized(_this));
_this.onYesClick = _this.onYesClick.bind(_assertThisInitialized(_this));
_this.onYesKeyDown = _this.onYesKeyDown.bind(_assertThisInitialized(_this));
_this.noButtonRef = /*#__PURE__*/_react["default"].createRef();
return _this;
}
_createClass(Prompt, [{
key: "componentDidMount",
value: function componentDidMount() {
var inline = this.props.inline;
if (inline) {
this.findInlineVerticalPosition();
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var _this2 = this;
var showProp = this.props.show;
var prevShowProp = prevProps.show;
var showState = this.state.show;
var prevShowState = prevProps.show;
if (showProp !== prevShowProp) {
this.setState({
show: showProp
}, function () {
_this2.focusNoButton();
});
}
if (showState && !prevShowState) {
this.focusNoButton();
}
}
}, {
key: "onClick",
value: function onClick(option) {
var onClick = this.props.onClick;
var show = this.state.show;
if (show) {
return;
}
if (!(0, _lodash.isUndefined)(onClick)) {
onClick(option);
} else {
this.setState({
show: true
});
}
}
}, {
key: "onChildKeyDown",
value: function onChildKeyDown(event) {
var onKeyDown = this.props.onKeyDown;
if ((0, _lodash.isFunction)(onKeyDown)) {
onKeyDown(event);
} else if (event.keyCode === _keyCode["default"].Enter) {
this.onClick();
}
}
}, {
key: "onMouseDown",
value: function onMouseDown(event) {
event.preventDefault();
}
}, {
key: "onNoClick",
value: function onNoClick(event) {
var onNoClick = this.props.onNoClick;
if (!(0, _lodash.isUndefined)(onNoClick)) {
onNoClick(event);
} else {
this.setState({
show: false
});
}
}
}, {
key: "onNoKeyDown",
value: function onNoKeyDown(event) {
var onNoKeyDown = this.props.onNoKeyDown;
if ((0, _lodash.isFunction)(onNoKeyDown)) {
onNoKeyDown(event);
}
}
}, {
key: "onYesClick",
value: function onYesClick(event) {
var onYesClick = this.props.onYesClick;
if (!(0, _lodash.isUndefined)(onYesClick)) {
onYesClick(event);
} else {
this.setState({
show: false
});
}
}
}, {
key: "onYesKeyDown",
value: function onYesKeyDown(event) {
var onYesKeyDown = this.props.onYesKeyDown;
if ((0, _lodash.isFunction)(onYesKeyDown)) {
onYesKeyDown(event);
}
}
}, {
key: "findInlineVerticalPosition",
value: function findInlineVerticalPosition() {
var childHeight = this.childrenRef.offsetHeight;
var negativeSpace = 5;
this.setState({
inlineVerticalAlign: "".concat(childHeight + negativeSpace, "px")
});
}
}, {
key: "focusNoButton",
value: function focusNoButton() {
if (this.noButtonRef && this.noButtonRef.current) {
this.noButtonRef.current.focus();
}
}
}, {
key: "render",
value: function render() {
var _this3 = this;
var _this$props = this.props,
children = _this$props.children,
classes = _this$props.classes,
className = _this$props.className,
id = _this$props.id,
inline = _this$props.inline,
inlineHorizontalAlign = _this$props.inlineHorizontalAlign,
inlineMessageColor = _this$props.inlineMessageColor,
message = _this$props.message,
style = _this$props.style;
var _this$state = this.state,
show = _this$state.show,
inlineVerticalAlign = _this$state.inlineVerticalAlign;
var rootClasses = (0, _classnames["default"])('ui', 'prompt', classes.root, className, {
'prompt-show': show,
'prompt-inline': inline
});
var messageClasses = (0, _classnames["default"])('prompt-message', {
'promp-message-alert': inlineMessageColor === 'alert' || children.props.color === 'alert' || children.props.buttonColor === 'alert',
'promp-message-success': inlineMessageColor === 'success' || children.props.color === 'success' || children.props.buttonColor === 'success',
'promp-message-warning': inlineMessageColor === 'warning' || children.props.color === 'warning' || children.props.buttonColor === 'warning'
});
var promptActionsStyle = {
left: !inlineHorizontalAlign || inlineHorizontalAlign === 'left' ? 0 : null,
right: inlineHorizontalAlign === 'right' ? 0 : null,
top: inlineVerticalAlign
};
return /*#__PURE__*/_react["default"].createElement("div", {
className: rootClasses,
id: id,
style: style
}, children && children.type.name === 'Dropdown' ? /*#__PURE__*/_react["default"].createElement("div", {
ref: function ref(_ref2) {
_this3.childrenRef = _ref2;
}
}, /*#__PURE__*/_react["default"].cloneElement(children, {
className: (0, _classnames["default"])('prompt-action', children && children.props.className || null, {
'prompt-action-disable': show
}),
buttonColor: show && children.props.buttonColor !== 'transparent' ? 'disable' : children.props.buttonColor,
onChange: this.onClick
})) : /*#__PURE__*/_react["default"].createElement("div", {
onClick: this.onClick,
onKeyDown: this.onChildKeyDown,
ref: function ref(_ref3) {
_this3.childrenRef = _ref3;
},
role: "button",
tabIndex: -1,
style: {
outline: 'none'
}
}, children), /*#__PURE__*/_react["default"].createElement("div", {
className: (0, _classnames["default"])('prompt-actions', classes.actions),
style: promptActionsStyle
}, /*#__PURE__*/_react["default"].createElement("div", {
className: messageClasses
}, message), /*#__PURE__*/_react["default"].createElement("div", {
className: "prompt-yes-btn",
onClick: this.onYesClick,
onKeyDown: this.onYesKeyDown,
onMouseDown: this.onMouseDown,
role: "button",
tabIndex: 0
}, "Yes"), /*#__PURE__*/_react["default"].createElement("div", {
className: "prompt-no-btn",
onClick: this.onNoClick,
onKeyDown: this.onNoKeyDown,
onMouseDown: this.onMouseDown,
ref: this.noButtonRef,
role: "button",
tabIndex: 0
}, "No")));
}
}]);
return Prompt;
}(_react["default"].Component);
Prompt.propTypes = propTypes;
Prompt.defaultProps = defaultProps;
var _default = (0, _withStyles["default"])(styles, {
withTheme: true
})(Prompt);
exports["default"] = _default;