UNPKG

@shakthillc/components

Version:

React generic components for shakthi products

100 lines (88 loc) 2.96 kB
import _Object$getPrototypeOf from "babel-runtime/core-js/object/get-prototype-of"; import _classCallCheck from "babel-runtime/helpers/classCallCheck"; import _createClass from "babel-runtime/helpers/createClass"; import _possibleConstructorReturn from "babel-runtime/helpers/possibleConstructorReturn"; import _inherits from "babel-runtime/helpers/inherits"; import React from "react"; import PropTypes from "prop-types"; import style from "./RadioGroup.module.css"; var RadioGroup = function (_React$Component) { _inherits(RadioGroup, _React$Component); function RadioGroup(props) { _classCallCheck(this, RadioGroup); var _this = _possibleConstructorReturn(this, (RadioGroup.__proto__ || _Object$getPrototypeOf(RadioGroup)).call(this, props)); _this.state = { selectedOption: props.selectedOption }; return _this; } _createClass(RadioGroup, [{ key: "handleOptionChange", value: function handleOptionChange(id, data) { var onChange = this.props.onChange; this.setState({ selectedOption: id }); onChange && onChange(id, data); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps, prevState) { if (this.props.selectedOption != null && prevProps.selectedOption != this.props.selectedOption) { this.setState({ selectedOption: this.props.selectedOption }); } } }, { key: "render", value: function render() { var _this2 = this; var options = this.props.options; var optionHtml = options.map(function (obj, i) { return React.createElement( "label", { key: i, className: style["radio-button"] }, React.createElement( "div", { style: { cursor: "pointer" } }, obj.value ), React.createElement("input", { type: "radio", checked: _this2.state.selectedOption === obj.id, name: "radio", onClick: _this2.handleOptionChange.bind(_this2, obj.id, obj) }), React.createElement("span", { className: style["radio-button__checkmark"], value: obj.value, id: obj.id }) ); }); return React.createElement( "div", { className: style["radio-group"] }, optionHtml ); } }]); return RadioGroup; }(React.Component); export default RadioGroup; RadioGroup.defaultProps = { isChecked: false, disabled: false, dataId: "checkBoxComp", label: "CheckBox", onChange: function onChange(id, data) { console.log("inbuilt", id, data); }, options: [{ id: "1", value: "Option 1" }, { id: "2", value: "Option 2" }, { id: "3", value: "Option 3" }, { id: "4", value: "Option 4" }] }; RadioGroup.propTypes = { isChecked: PropTypes.bool, disabled: PropTypes.bool, dataId: PropTypes.string, label: PropTypes.string, options: PropTypes.array };