react-cm-ui
Version:
React UI for Healthy Church
520 lines (471 loc) • 19.4 kB
JavaScript
"use strict";
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"));
var _radioItem = _interopRequireDefault(require("./radioItem"));
var _withStyles = _interopRequireDefault(require("../../styles/withStyles"));
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); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
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; }
var propTypes = {
align: _propTypes["default"].oneOf(['left', 'right']),
checked: _propTypes["default"].oneOfType([_propTypes["default"].bool, _propTypes["default"].number]),
children: _propTypes["default"].node,
classes: _propTypes["default"].shape({
input: _propTypes["default"].string,
isAlignedRight: _propTypes["default"].string,
isChecked: _propTypes["default"].string,
isDisabled: _propTypes["default"].string,
isFluid: _propTypes["default"].string,
isPill: _propTypes["default"].string,
label: _propTypes["default"].string,
labelNotClickable: _propTypes["default"].string,
root: _propTypes["default"].string
}),
className: _propTypes["default"].string,
/**
* Deprecated prop. Please use `disabled` instead.
*/
disable: _propTypes["default"].bool,
/**
* A Radio can be disabled.
*/
disabled: _propTypes["default"].bool,
fluid: _propTypes["default"].bool,
id: _propTypes["default"].string,
label: _propTypes["default"].string,
labelClick: _propTypes["default"].bool,
multi: _propTypes["default"].bool,
name: _propTypes["default"].string,
onChange: _propTypes["default"].func,
onKeyDown: _propTypes["default"].func,
pill: _propTypes["default"].bool,
style: _propTypes["default"].shape({}),
tabIndex: _propTypes["default"].number,
value: _propTypes["default"].string
};
var defaultProps = {
align: null,
checked: null,
children: null,
classes: null,
className: null,
disable: false,
disabled: false,
fluid: false,
id: null,
label: null,
labelClick: true,
multi: false,
name: null,
onChange: null,
onKeyDown: null,
pill: false,
style: null,
tabIndex: -1,
value: ''
};
var isCheckedSingle = function isCheckedSingle(id, index, isChecked) {
return id === isChecked || index === isChecked;
};
var isCheckedMulti = function isCheckedMulti(id, index, isChecked) {
var newId = id || index;
return (0, _lodash.includes)(isChecked, newId);
};
var styles = function styles(theme) {
var size = 22;
var sizeDot = 14;
var inputStyle = {
display: 'none',
'&:checked +': {
'& $label::after': {
opacity: 1
}
}
};
return {
isAlignedRight: {},
isDisabled: {},
isFluid: {},
isPill: {},
input: _objectSpread({}, inputStyle),
label: {},
labelNotClickable: {},
root: {
display: 'inline-block',
marginRight: theme.spacing(2),
minHeight: size,
outline: 'none',
position: 'relative',
textAlign: 'left',
'&:focus': {
boxShadow: "0 0 0 1px ".concat(theme.palette.active.primary)
},
'& .label': {
color: theme.palette.text.primary,
cursor: 'pointer',
display: 'block',
fontSize: 14,
position: 'relative',
'&::before, &::after': {
content: '""',
height: size,
left: 0,
position: 'absolute',
top: 0,
transition: 'opacity 150ms ease',
width: size
},
'&::before': {
background: theme.palette.background.primary,
border: "1px solid ".concat(theme.palette.border.primary),
borderRadius: 11
},
'&::after': {
backgroundColor: theme.palette.cyan[500],
borderRadius: sizeDot / 2,
height: sizeDot,
margin: (size - sizeDot) / 2,
opacity: 0,
width: sizeDot
},
'&$labelNotClickable': {
cursor: 'auto',
'&::before, &::after': {
cursor: 'pointer'
}
},
'& > span': {
display: 'inline-block',
paddingLeft: 33,
paddingTop: 2
}
},
'&$isAlignedRight': {
marginLeft: 11,
marginRight: 0,
textAlign: 'left',
'& .label': {
'&::before, &::after': {
left: 'auto',
right: 0
},
'& > span': {
paddingLeft: 0,
paddingRight: 33
}
}
},
'&$isDisabled': {
'& .radio-item .label': {
cursor: 'auto',
'&::before': {
background: theme.palette.background.secondary
},
'&::after': {
backgroundColor: theme.palette.grey[400]
}
},
'&$isPill': {
'& .radio-item': {
'& .label': {
cursor: 'default'
},
'& .input:checked + .label': {
backgroundColor: theme.palette.grey[400],
borderColor: theme.palette.grey[400]
},
'&:last-child': {
input: {
'&:checked + .label': {
borderRight: "1px solid ".concat(theme.palette.grey[400])
}
}
},
'&.radio-item-is-checked + .radio-item .label': {
borderLeft: "1px solid ".concat(theme.palette.grey[400])
}
}
}
},
'&$isFluid': {
display: 'block',
marginRight: 0
},
'&$isPill': {
'& .radio-item': {
cursor: 'pointer',
display: 'inline-block',
outline: 'none',
'&:focus': {
boxShadow: "0 0 0 1px ".concat(theme.palette.active.primary)
},
'&:first-child:focus': {
borderRadius: '16px 0 0 16px'
},
'&:last-child:focus': {
borderRadius: '0 16px 16px 0'
},
'& .input': _objectSpread({}, inputStyle),
'& .label': {
backgroundColor: theme.palette.background.primary,
border: "1px solid ".concat(theme.palette.border.primary),
borderRight: 0,
display: 'inline-block',
fontSize: 14,
fontWeight: theme.typography.fontWeightRegular,
outline: 'none',
padding: '6px 22px',
textAlign: 'center',
textDecoration: 'none',
transition: [['background-color', '125ms', 'linear'], ['border', '125ms', 'linear'], ['color', '125ms', 'linear']],
whiteSpace: 'nowrap',
zIndex: -1,
'&::before, &::after': {
display: 'none'
}
},
'& .input:checked + .label': {
backgroundColor: theme.palette.cyan[500],
borderColor: theme.palette.cyan[500],
color: theme.palette.text.contrastText
},
'&:first-child .label': {
borderRadius: '15.5px 0 0 15.5px'
},
'&:last-child': {
'& .label': {
borderRight: "1px solid ".concat(theme.palette.border.primary),
borderRadius: '0 15.5px 15.5px 0'
},
'& .input:checked + .label': {
borderRight: "1px solid ".concat(theme.palette.cyan[500])
}
},
'&.radio-item-is-checked + .radio-item .label': {
borderLeft: "1px solid ".concat(theme.palette.cyan[500])
}
}
}
}
};
};
var Radio = /*#__PURE__*/function (_React$Component) {
_inherits(Radio, _React$Component);
var _super = _createSuper(Radio);
function Radio(props) {
var _this;
_classCallCheck(this, Radio);
_this = _super.call(this, props);
_this.state = {
isChecked: props.checked
};
_this.onClick = _this.onClick.bind(_assertThisInitialized(_this));
_this.onKeyDown = _this.onKeyDown.bind(_assertThisInitialized(_this));
_this.onLabelClick = _this.onLabelClick.bind(_assertThisInitialized(_this));
_this.onLabelKeyDown = _this.onLabelKeyDown.bind(_assertThisInitialized(_this));
_this.onMouseDown = _this.onMouseDown.bind(_assertThisInitialized(_this));
return _this;
}
_createClass(Radio, [{
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var prevChecked = prevProps.checked;
var checked = this.props.checked;
if (checked !== prevChecked) {
this.setState({
isChecked: checked
});
}
}
}, {
key: "onClick",
value: function onClick(idArg, customValue) {
var _this$props = this.props,
disable = _this$props.disable,
disabled = _this$props.disabled,
id = _this$props.id,
onChange = _this$props.onChange,
pill = _this$props.pill;
var newValue = this.setIsChecked(idArg);
var isNotDisabled = !disable && !disabled;
if (isNotDisabled) {
if ((0, _lodash.isFunction)(onChange)) {
onChange(pill ? idArg : id, newValue, customValue);
} else {
this.setState({
isChecked: newValue
});
}
}
}
}, {
key: "onKeyDown",
value: function onKeyDown(event, idArg) {
var _this$props2 = this.props,
disable = _this$props2.disable,
disabled = _this$props2.disabled,
id = _this$props2.id,
onKeyDown = _this$props2.onKeyDown,
pill = _this$props2.pill;
if (event.keyCode === _keyCode["default"].Enter) {
var newValue = this.setIsChecked(idArg);
var isNotDisabled = !disable && !disabled;
if (!isNotDisabled) {
if ((0, _lodash.isFunction)(onKeyDown)) {
onKeyDown(pill ? idArg : id, newValue);
} else {
this.setState({
isChecked: newValue
});
}
}
}
}
}, {
key: "onLabelClick",
value: function onLabelClick(event) {
var labelClick = this.props.labelClick;
if (labelClick === false) {
event.stopPropagation();
}
}
}, {
key: "onLabelKeyDown",
value: function onLabelKeyDown() {
/**
* NOTE: Need to use a prop function here someday.
*/
return null;
}
}, {
key: "onMouseDown",
value: function onMouseDown(event) {
event.preventDefault();
}
}, {
key: "setIsChecked",
value: function setIsChecked(idArg) {
var _this$props3 = this.props,
pill = _this$props3.pill,
multi = _this$props3.multi;
var isChecked = this.state.isChecked;
var newValue = (0, _lodash.clone)(isChecked);
if (multi) {
if ((0, _lodash.includes)(newValue, idArg)) {
(0, _lodash.remove)(newValue, function (v) {
return v === idArg;
});
} else if ((0, _lodash.isArray)(newValue)) {
newValue.push(idArg);
} else {
newValue = [idArg];
}
} else if (pill) {
newValue = idArg;
} else {
newValue = true;
}
return newValue;
}
}, {
key: "render",
value: function render() {
var _ClassNames,
_this2 = this,
_ClassNames2;
var _this$props4 = this.props,
align = _this$props4.align,
children = _this$props4.children,
classes = _this$props4.classes,
className = _this$props4.className,
disable = _this$props4.disable,
disabled = _this$props4.disabled,
fluid = _this$props4.fluid,
id = _this$props4.id,
label = _this$props4.label,
labelClick = _this$props4.labelClick,
multi = _this$props4.multi,
name = _this$props4.name,
pill = _this$props4.pill,
style = _this$props4.style,
tabIndex = _this$props4.tabIndex,
value = _this$props4.value;
var isChecked = this.state.isChecked;
var isDisabled = disable || disabled;
var rootClasses = (0, _classnames["default"])('ui', 'radio', classes.root, className, (_ClassNames = {}, _defineProperty(_ClassNames, classes.isAlignedRight, align === 'right'), _defineProperty(_ClassNames, 'radio-align-right', align === 'right'), _defineProperty(_ClassNames, classes.isDisabled, isDisabled), _defineProperty(_ClassNames, 'radio-disabled', isDisabled), _defineProperty(_ClassNames, classes.isFluid, fluid), _defineProperty(_ClassNames, 'radio-full-width', fluid), _defineProperty(_ClassNames, classes.isChecked, !pill && isChecked), _defineProperty(_ClassNames, 'radio-is-checked', !pill && isChecked), _defineProperty(_ClassNames, classes.isPill, pill), _defineProperty(_ClassNames, 'radio-pill', pill), _ClassNames));
if (pill) {
var isCheckedItem = multi ? isCheckedMulti : isCheckedSingle;
return /*#__PURE__*/_react["default"].createElement("div", {
className: rootClasses,
style: style
}, _react["default"].Children.map(children, function (child, index) {
return /*#__PURE__*/_react["default"].cloneElement(child, {
checked: isCheckedItem(child.props.id, index, isChecked),
className: child.props.className,
id: child.props.id,
index: index,
label: child.props.label,
name: !multi ? name : null,
onClick: _this2.onClick,
onKeyDown: _this2.onKeyDown,
style: child.props.style,
tabIndex: child.props.tabIndex
});
}));
}
return /*#__PURE__*/_react["default"].createElement("div", {
"aria-checked": isChecked,
"aria-labelledby": id,
className: rootClasses,
onClick: this.onClick,
onKeyDown: this.onKeyDown,
onMouseDown: this.onMouseDown,
role: "radio",
style: style,
tabIndex: isDisabled ? -1 : tabIndex
}, /*#__PURE__*/_react["default"].createElement("input", {
checked: isChecked,
className: (0, _classnames["default"])('input', classes.input),
disabled: isDisabled,
id: id,
name: name,
readOnly: true,
type: "radio",
value: value
}), /*#__PURE__*/_react["default"].createElement("label", {
className: (0, _classnames["default"])('label', classes.label, (_ClassNames2 = {}, _defineProperty(_ClassNames2, classes.labelNotClickable, !labelClick), _defineProperty(_ClassNames2, 'label-not-clickable', labelClick === false), _ClassNames2))
}, label &&
/*#__PURE__*/
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
_react["default"].createElement("span", {
onClick: this.onLabelClick,
onKeyDown: this.onLabelKeyDown
}, label)));
}
}]);
return Radio;
}(_react["default"].Component);
Radio.Item = _radioItem["default"];
Radio.propTypes = propTypes;
Radio.defaultProps = defaultProps;
var _default = (0, _withStyles["default"])(styles)(Radio);
exports["default"] = _default;