UNPKG

doj-react-adminlte

Version:

Simple and easy-to-use AdminLTE components for React

171 lines (130 loc) 7.95 kB
"use strict"; 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); } Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _Radio = _interopRequireDefault(require("../Radio")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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 { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], 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 labelStyle = { display: 'inline-block', fontWeight: 'bold', width: '100%' }; var RadioGroup = /*#__PURE__*/function (_React$Component) { _inherits(RadioGroup, _React$Component); var _super = _createSuper(RadioGroup); function RadioGroup() { var _this; _classCallCheck(this, RadioGroup); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _this.handleChange = function (name, value) { // TODO: Optimize this by directly using the value instead of using find var _this$props = _this.props, options = _this$props.options, valueKey = _this$props.valueKey, simpleValue = _this$props.simpleValue; value = options.find(function (o) { return o[valueKey] === value.value; }); _this.props.onChange(_this.props.name, simpleValue ? value[valueKey] : value); }; return _this; } _createClass(RadioGroup, [{ key: "componentDidMount", value: function componentDidMount() { var _this$props2 = this.props, simpleValue = _this$props2.simpleValue, valueKey = _this$props2.valueKey; if (this.props.firstDefault && !this.props.value && this.props.options.length) { var firstOption = this.props.options[0]; this.props.onChange(this.props.name, simpleValue ? firstOption[valueKey] : firstOption); } } }, { key: "render", value: function render() { var _this2 = this; var _this$props3 = this.props, value = _this$props3.value, options = _this$props3.options, label = _this$props3.label; var checkedValue = value !== Object(value) ? value : value[this.props.valueKey]; return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, label && /*#__PURE__*/_react.default.createElement("span", { style: labelStyle }, label), /*#__PURE__*/_react.default.createElement("div", { className: "form-group" }, options.map(function (option) { return /*#__PURE__*/_react.default.createElement(_Radio.default, { key: option[_this2.props.valueKey], label: option[_this2.props.labelKey], value: option[_this2.props.valueKey], name: _this2.props.name, onChange: _this2.handleChange, checkedValue: checkedValue, inline: _this2.props.inline, disabled: _this2.props.disabled }); }))); } }]); return RadioGroup; }(_react.default.Component); RadioGroup.defaultProps = { disabled: false, firstDefault: false, inline: false, onChange: function onChange() {}, valueKey: "value", labelKey: "label", simpleValue: false }; RadioGroup.propTypes = process.env.NODE_ENV !== "production" ? { /** If true, interaction with the component is disabled */ disabled: _propTypes.default.bool, /** If true, selects the first option by default upon loading of the list of options */ firstDefault: _propTypes.default.bool, /** Set to true to make the list of options appear on the same line */ inline: _propTypes.default.bool, /** Specifies the text to use as the label */ label: _propTypes.default.node, /** Property name of the items in the options list from which the option label to be displayed will be taken. * Defaults to "label" */ labelKey: _propTypes.default.string, /** Specifies the name of the component. It is used to distinguish elements when a single form change handler is used */ name: _propTypes.default.string.isRequired, /** Callback fired when component value changes. Accepts a function with two parameters, namely field and value */ onChange: _propTypes.default.func, /** Array of objects to be used as options. By default, uses the properties "label" and "value" for the labels and * values of each of the options respectively. */ options: _propTypes.default.array.isRequired, /** If set to true, only the value property of the selected option is passed as value parameter in the * onChange handler. Otherwise, the option object itself is passed. */ simpleValue: _propTypes.default.bool, /** Specifies the currently selected option */ value: _propTypes.default.any, /** Property name of the items in the options list from which the value of the option will be taken. * Defaults to "value" */ valueKey: _propTypes.default.string } : {}; RadioGroup.isFormComponent = true; var _default = RadioGroup; exports.default = _default;