react-cm-ui
Version:
React UI for Healthy Church
336 lines (290 loc) • 14.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _lodash = require("lodash");
var _react = _interopRequireDefault(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _icon = _interopRequireDefault(require("../../dataDisplay/icon"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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; }
function _extends() { _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; }; return _extends.apply(this, arguments); }
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 = {
align: _propTypes["default"].oneOf(['left', 'right']),
checked: _propTypes["default"].bool,
className: _propTypes["default"].string,
/**
* Deprecated prop. Please use `disabled` instead.
*/
disable: _propTypes["default"].bool,
/**
* A Checkbox can be disabled.
*/
disabled: _propTypes["default"].bool,
fluid: _propTypes["default"].bool,
/**
* Forwarded Ref
*/
forwardedRef: _propTypes["default"].oneOfType([// Either a function
_propTypes["default"].func, // Or the instance of a DOM native element (see the note about SSR)
_propTypes["default"].shape({
current: _propTypes["default"].instanceOf(Element)
})]),
id: _propTypes["default"].string,
inverse: _propTypes["default"].bool,
label: _propTypes["default"].oneOfType([_propTypes["default"].shape({}), _propTypes["default"].string]),
labelClassName: _propTypes["default"].string,
labelClick: _propTypes["default"].bool,
labelStyle: _propTypes["default"].shape({}),
labelWeight: _propTypes["default"].oneOf(['bold', 'normal', 'semibold']),
name: _propTypes["default"].string,
onChange: _propTypes["default"].func,
size: _propTypes["default"].oneOf(['small', 'large']),
style: _propTypes["default"].shape({}),
tabIndex: _propTypes["default"].number,
toggle: _propTypes["default"].bool,
value: _propTypes["default"].oneOfType([_propTypes["default"].number, _propTypes["default"].string])
};
var defaultProps = {
align: null,
checked: false,
className: null,
disable: false,
disabled: false,
fluid: false,
forwardedRef: undefined,
id: null,
inverse: null,
label: null,
labelClassName: null,
labelClick: null,
labelStyle: null,
labelWeight: null,
name: null,
onChange: null,
size: null,
style: null,
tabIndex: -1,
toggle: null,
value: null
};
var Checkbox = /*#__PURE__*/function (_React$Component) {
_inherits(Checkbox, _React$Component);
var _super = _createSuper(Checkbox);
function Checkbox(props) {
var _props$forwardedRef;
var _this;
_classCallCheck(this, Checkbox);
_this = _super.call(this, props);
_this.state = {
isChecked: props.checked || false
};
_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.checkbox = (_props$forwardedRef = props.forwardedRef) !== null && _props$forwardedRef !== void 0 ? _props$forwardedRef : /*#__PURE__*/_react["default"].createRef();
return _this;
}
_createClass(Checkbox, [{
key: "componentDidMount",
value: function componentDidMount() {
var checked = this.props.checked;
if (checked) {
// isChecked is already set by the props.checked in the constructor.
// eslint-disable-next-line no-underscore-dangle
this.checkbox.current.checked = checked;
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var _this2 = this;
var _this$props = this.props,
checked = _this$props.checked,
onChange = _this$props.onChange;
if ((0, _lodash.isFunction)(onChange) && prevProps.checked !== checked) {
this.setState({
isChecked: checked
}, function () {
// eslint-disable-next-line no-underscore-dangle
_this2.checkbox.current.checked = checked;
});
}
}
}, {
key: "onClick",
value: function onClick(event) {
var _this3 = this;
var _this$props2 = this.props,
disable = _this$props2.disable,
disabled = _this$props2.disabled,
id = _this$props2.id,
onChange = _this$props2.onChange;
var isChecked = this.state.isChecked;
var isDisabled = disabled || disable;
if (!isDisabled) {
if ((0, _lodash.isFunction)(onChange)) {
onChange(id, !isChecked, event);
} else {
this.setState({
isChecked: !isChecked
}, function () {
_this3.checkbox.current.checked = !isChecked;
});
}
} else {
event.stopPropagation();
}
}
}, {
key: "onKeyDown",
value: function onKeyDown() {
/**
* NOTE: Need to use a prop function here someday.
*/
}
}, {
key: "onLabelClick",
value: function onLabelClick(event) {
var labelClick = this.props.labelClick;
if ((0, _lodash.isFunction)(labelClick) && labelClick === false) {
event.stopPropagation();
}
}
}, {
key: "onLabelKeyDown",
value: function onLabelKeyDown() {
/**
* NOTE: Need to use a prop function here someday
*/
}
}, {
key: "render",
value: function render() {
var _this$props3 = this.props,
align = _this$props3.align,
className = _this$props3.className,
disable = _this$props3.disable,
disabled = _this$props3.disabled,
fluid = _this$props3.fluid,
id = _this$props3.id,
inverse = _this$props3.inverse,
label = _this$props3.label,
labelClassName = _this$props3.labelClassName,
labelClick = _this$props3.labelClick,
labelStyle = _this$props3.labelStyle,
labelWeight = _this$props3.labelWeight,
name = _this$props3.name,
size = _this$props3.size,
style = _this$props3.style,
tabIndex = _this$props3.tabIndex,
toggle = _this$props3.toggle,
value = _this$props3.value;
var isChecked = this.state.isChecked;
var isDisabled = disable || disabled;
var newValue = value || '';
var containerClasses = (0, _classnames["default"])('ui', 'checkbox', className, {
'checkbox-align-left': align === 'left',
'checkbox-align-right': align === 'right',
'checkbox-disabled': isDisabled,
'checkbox-full-width': fluid,
'checkbox-inverse': inverse,
'checkbox-is-checked': isChecked,
'checkbox-size-small': size === 'small',
'checkbox-toggle': toggle
});
var labelClasses = (0, _classnames["default"])('checkbox-label', {
'label-not-clickable': (0, _lodash.isFunction)(labelClick) && labelClick === false
});
var labelTextClasses = (0, _classnames["default"])('checkbox-label-text', labelClassName, {
'checkbox-label-text-weight-bold': labelWeight === 'bold',
'checkbox-label-text-weight-normal': !labelWeight || labelWeight === 'normal',
'checkbox-label-text-weight-semibold': labelWeight === 'semibold'
});
var checkSize = size === 'small' ? 8 : 10;
var inputId = id ? "".concat(id, "_hidden_input") : null;
return (
/*#__PURE__*/
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
_react["default"].createElement("div", {
className: containerClasses,
id: id,
onClick: this.onClick,
onKeyDown: this.onKeyDown,
style: style,
tabIndex: tabIndex
}, /*#__PURE__*/_react["default"].createElement("input", {
checked: isChecked,
className: "input",
disabled: isDisabled,
id: inputId,
name: name,
readOnly: true // eslint-disable-next-line no-underscore-dangle
,
ref: this.checkbox,
type: "checkbox",
value: newValue
}), /*#__PURE__*/_react["default"].createElement("label", {
className: labelClasses
}, label &&
/*#__PURE__*/
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
_react["default"].createElement("span", {
className: labelTextClasses,
onClick: this.onLabelClick,
onKeyDown: this.onLabelKeyDown,
style: labelStyle
}, label), toggle ? /*#__PURE__*/_react["default"].createElement("div", {
className: "checkbox-toggle-text"
}, /*#__PURE__*/_react["default"].createElement("span", {
className: "checkbox-toggle-text-on"
}, "On"), /*#__PURE__*/_react["default"].createElement("span", {
className: "checkbox-toggle-text-off"
}, "Off")) : null, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
color: isDisabled ? 'static' : 'primary',
compact: true,
inverse: true,
size: checkSize,
type: "check"
})))
);
}
}]);
return Checkbox;
}(_react["default"].Component);
Checkbox.propTypes = propTypes;
Checkbox.defaultProps = defaultProps;
var CheckboxWrapper = /*#__PURE__*/_react["default"].forwardRef(function (props, ref) {
return (
/*#__PURE__*/
// eslint-disable-next-line react/jsx-props-no-spreading
_react["default"].createElement(Checkbox, _extends({}, props, {
forwardedRef: ref
}))
);
});
var wrapperPropTypes = _objectSpread({}, propTypes);
delete wrapperPropTypes.forwardedRef;
var wrapperDefaultProps = _objectSpread({}, defaultProps);
delete wrapperDefaultProps.forwardedRef;
CheckboxWrapper.displayName = 'Checkbox';
CheckboxWrapper.propTypes = wrapperPropTypes;
CheckboxWrapper.defaultProps = wrapperDefaultProps;
var _default = CheckboxWrapper;
exports["default"] = _default;