UNPKG

sc-react-ions

Version:

An open source set of React components that implement Ambassador's Design and UX patterns.

200 lines (165 loc) 7.07 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _style = require('./style.scss'); var _style2 = _interopRequireDefault(_style); var _bind = require('classnames/bind'); var _bind2 = _interopRequireDefault(_bind); var _Icon = require('../Icon'); var _Icon2 = _interopRequireDefault(_Icon); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var Checkbox = function (_React$Component) { _inherits(Checkbox, _React$Component); function Checkbox(props) { _classCallCheck(this, Checkbox); var _this = _possibleConstructorReturn(this, (Checkbox.__proto__ || Object.getPrototypeOf(Checkbox)).call(this, props)); _this.state = { value: _this.props.value, iconName: _this.props.iconName }; _this.handleChange = function (event) { event.persist(); // Allow user to interact with locked checkboxes only if the value is false (unchecked) if (!_this.props.locked || !_this.props.value) { _this.setState({ value: event.target.checked }, function () { if (typeof _this.props.changeCallback === 'function') { _this.props.changeCallback(event); } }); } }; _this.componentWillReceiveProps = function (nextProps) { var newState = {}; if (nextProps.value !== _this.props.value) { newState.value = nextProps.value; } if (nextProps.iconName !== _this.state.iconName) { newState.iconName = nextProps.iconName; } _this.setState(newState); }; _this.getLabel = function () { if (_this.props.label && _this.props.description) { return _react2.default.createElement( 'div', { className: _style2.default['label-wrapper'] }, _react2.default.createElement( 'label', { htmlFor: _this.props.forLabelAttr }, _react2.default.createElement( 'span', { className: _style2.default['label-title'] }, _this.props.label ), _react2.default.createElement( 'span', { className: _style2.default['label-description'] }, _this.props.description ) ) ); } if (_this.props.label) { return _react2.default.createElement( 'label', { htmlFor: _this.props.forLabelAttr }, _this.props.label ); } return null; }; _this.render = function () { var _this$props = _this.props, optClass = _this$props.optClass, changeCallback = _this$props.changeCallback, value = _this$props.value, disabled = _this$props.disabled, name = _this$props.name, label = _this$props.label, forLabelAttr = _this$props.forLabelAttr, other = _objectWithoutProperties(_this$props, ['optClass', 'changeCallback', 'value', 'disabled', 'name', 'label', 'forLabelAttr']); var cx = _bind2.default.bind(_style2.default); var disabledClass = _this.props.disabled ? _style2.default['checkbox-disabled'] : ''; var allowNativeClass = _this.props.allowNative ? _style2.default['checkbox-native'] : null; var descriptionClass = _this.props.description ? _style2.default['has-description'] : null; var checkboxClass = cx(_style2.default['checkbox-component'], allowNativeClass, descriptionClass, disabledClass, optClass); var inputFillColor = _this.props.disabled ? '#9198A0' : '#3C97D3'; var labelWrapperClass = _this.props.description ? _style2.default['label-group'] : null; return _react2.default.createElement( 'div', { className: checkboxClass }, _react2.default.createElement('input', { type: 'checkbox', id: _this.props.forLabelAttr, checked: _this.state.value, onChange: _this.handleChange, value: value, disabled: disabled, name: name, label: label }), _react2.default.createElement( 'div', { className: labelWrapperClass }, _react2.default.createElement( 'div', { className: _style2.default['checkbox-input'] }, _react2.default.createElement(_Icon2.default, { name: _this.state.iconName, fill: inputFillColor }) ), _this.getLabel() ) ); }; return _this; } return Checkbox; }(_react2.default.Component); Checkbox.defaultProps = { disabled: false, iconName: 'md-check', locked: false }; Checkbox.propTypes = { /** * Displays a native checkbox, instead of the custom implementation. */ allowNative: _propTypes2.default.bool, /** * Whether the checkbox is disabled. */ disabled: _propTypes2.default.bool, /** * Value of the input. Sets whether the component is checked or not. */ value: _propTypes2.default.bool, /** * Unique string to be passed to the label 'for' attrbute and the native checkbox 'id', * to allow using label to check/uncheck */ forLabelAttr: _propTypes2.default.string, /** * Node displayed with the checkbox (can be a string). */ label: _propTypes2.default.node, /** * Optional styles to add to the checkbox. */ optClass: _propTypes2.default.string, /** * A callback function to be called when the checkbox changes. */ changeCallback: _propTypes2.default.func, /** * Icon to be used in the checkbox. */ iconName: _propTypes2.default.string, /** * Whether the checkbox is locked from change outside of receiving props. */ locked: _propTypes2.default.bool, /** * Optional description that appears below the label. */ description: _propTypes2.default.string }; exports.default = Checkbox;