UNPKG

wix-style-react

Version:
181 lines (158 loc) • 7.1 kB
var _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; }; var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _class, _temp; 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 _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; } import React from 'react'; import PropTypes from 'prop-types'; import uniqueId from 'lodash/uniqueId'; import classnames from 'classnames'; import WixComponent from '../../BaseComponents/WixComponent'; import styles from '../RadioGroup.scss'; import { withFocusable, focusableStates } from '../../common/Focusable'; import Text from '../../Text'; var RadioButton = (_temp = _class = function (_WixComponent) { _inherits(RadioButton, _WixComponent); function RadioButton(props) { _classCallCheck(this, RadioButton); var _this = _possibleConstructorReturn(this, (RadioButton.__proto__ || Object.getPrototypeOf(RadioButton)).call(this, props)); _this.id = uniqueId(); return _this; } _createClass(RadioButton, [{ key: 'renderButton', value: function renderButton() { var _props = this.props, checked = _props.checked, disabled = _props.disabled, onChange = _props.onChange, value = _props.value, icon = _props.icon, children = _props.children; return React.createElement( 'button', { type: 'button', className: classnames(styles.radioButton, _defineProperty({}, styles.checked, checked)), checked: checked, disabled: disabled, id: this.id, onClick: function onClick() { return !checked && !disabled ? onChange(value) : null; } }, icon && React.createElement( 'span', null, icon ), children && React.createElement( 'span', null, children ) ); } }, { key: 'renderRadio', value: function renderRadio() { var _classnames2, _classnames3; var _props2 = this.props, checked = _props2.checked, children = _props2.children, content = _props2.content, disabled = _props2.disabled, lineHeight = _props2.lineHeight, name = _props2.name, _onChange = _props2.onChange, style = _props2.style, vAlign = _props2.vAlign, value = _props2.value; return React.createElement( 'div', _extends({ className: classnames(styles.radioWrapper, (_classnames2 = {}, _defineProperty(_classnames2, styles.disabled, disabled), _defineProperty(_classnames2, styles.checked, checked), _classnames2)), style: style, tabIndex: disabled ? null : 0, onFocus: this.props.focusableOnFocus, onBlur: this.props.focusableOnBlur }, focusableStates(this.props)), React.createElement('input', { type: 'radio', name: name, value: value, id: this.id, checked: checked, disabled: disabled, onChange: function onChange() { return !checked && !disabled ? _onChange(value) : null; } }), React.createElement( 'label', { 'data-hook': 'radio-label', style: { lineHeight: lineHeight }, htmlFor: this.id, className: classnames((_classnames3 = {}, _defineProperty(_classnames3, styles.vcenter, vAlign === 'center'), _defineProperty(_classnames3, styles.vtop, vAlign === 'top'), _classnames3)) }, React.createElement( 'div', { style: { height: lineHeight }, className: styles.radioButtonWrapper, 'data-hook': 'radiobutton-radio' }, React.createElement('div', { className: classnames(styles.radio, _defineProperty({}, styles.radioButtonChecked, checked)) }) ), children && React.createElement( Text, { className: styles.children, 'data-hook': 'radiobutton-children', tagName: 'div', size: 'medium', weight: 'thin', secondary: true, light: disabled }, children ) ), content && React.createElement( 'div', { 'data-hook': 'radio-button-content' }, content ) ); } }, { key: 'render', value: function render() { return this.props.type === 'button' ? this.renderButton() : this.renderRadio(); } }]); return RadioButton; }(WixComponent), _class.displayName = 'RadioGroup.Radio', _class.propTypes = { value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), vAlign: PropTypes.oneOf(['center', 'top']), name: PropTypes.string, onChange: PropTypes.func, checked: PropTypes.bool, disabled: PropTypes.bool, children: PropTypes.any, style: PropTypes.object, type: PropTypes.oneOf(['default', 'button']), lineHeight: PropTypes.string, /** optional node to be rendered under label. Clicking it will not trigger `onChange` */ content: PropTypes.node }, _class.defaultProps = { vAlign: 'center', type: 'default', content: null }, _temp); export default withFocusable(RadioButton);