UNPKG

ndla-ui

Version:

UI component library for NDLA.

108 lines (92 loc) 4.58 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; }; }(); 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; } /** * Copyright (c) 2018-present, NDLA. * * This source code is licensed under the GPLv3 license found in the * LICENSE file in the root directory of this source tree. * */ import React, { Fragment, Component } from 'react'; import PropTypes from 'prop-types'; import BEMHelper from 'react-bem-helper'; import { uuid } from 'ndla-util'; var classes = BEMHelper('c-radio-button-group'); var RadioButtonGroup = function (_Component) { _inherits(RadioButtonGroup, _Component); function RadioButtonGroup(props) { _classCallCheck(this, RadioButtonGroup); var _this = _possibleConstructorReturn(this, (RadioButtonGroup.__proto__ || Object.getPrototypeOf(RadioButtonGroup)).call(this, props)); _this.state = { selected: props.selected || props.options[0].value }; _this.handleOnChange = _this.handleOnChange.bind(_this); _this.uuid = _this.props.uniqeIds && uuid(); return _this; } _createClass(RadioButtonGroup, [{ key: 'handleOnChange', value: function handleOnChange(e) { this.setState({ selected: e.target.value }); this.props.onChange(e.target.value); } }, { key: 'render', value: function render() { var _this2 = this; return React.createElement( 'section', null, React.createElement( 'div', _extends({ role: 'radiogroup' }, classes('wrapper')), this.props.label && React.createElement( 'h1', classes('label-heading'), this.props.label ), this.props.options.map(function (option) { var id = _this2.uuid ? _this2.uuid + '_' + option.value : option.value; return React.createElement( Fragment, { key: option.value }, React.createElement('input', _extends({}, classes('input'), { disabled: option.disabled, 'aria-checked': _this2.state.selected === option.value, checked: _this2.state.selected === option.value, type: 'radio', value: option.value, id: id, name: id, onChange: _this2.handleOnChange })), React.createElement( 'label', _extends({ htmlFor: id }, classes('label')), option.title ) ); }) ) ); } }]); return RadioButtonGroup; }(Component); RadioButtonGroup.propTypes = { selected: PropTypes.string, options: PropTypes.arrayOf(PropTypes.shape({ title: PropTypes.string.isRequired, value: PropTypes.string.isRequired, disabled: PropTypes.bool })).isRequired, label: PropTypes.string, uniqeIds: PropTypes.bool, onChange: PropTypes.func.isRequired }; export default RadioButtonGroup;