UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

202 lines (201 loc) 7.25 kB
"use client"; import _extends from "@babel/runtime-corejs3/helpers/esm/extends"; import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty"; var _AlignmentHelper; import React from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; import { extendPropsWithContextInClassComponent, makeUniqueId, validateDOMAttributes, getStatusState, combineDescribedBy, combineLabelledBy, dispatchCustomElementEvent } from "../../shared/component-helper.js"; import { pickFormElementProps } from "../../shared/helpers/filterValidProps.js"; import { spacingPropTypes, createSpacingClasses } from "../space/SpacingHelper.js"; import AlignmentHelper from "../../shared/AlignmentHelper.js"; import Space from "../Space.js"; import FormLabel from "../FormLabel.js"; import FormStatus from "../FormStatus.js"; import Flex from "../Flex.js"; import Context from "../../shared/Context.js"; import Suffix from "../../shared/helpers/Suffix.js"; import RadioGroupContext from "./RadioGroupContext.js"; export default class RadioGroup extends React.PureComponent { static getDerivedStateFromProps(props, state) { if (state._listenForPropChanges) { if (props.value !== state._value) { state.value = props.value; } if (typeof props.value !== 'undefined') { state._value = props.value; } } state._listenForPropChanges = true; return state; } constructor(props) { super(props); _defineProperty(this, "onChangeHandler", ({ value, event }) => { this.setState({ value, _listenForPropChanges: false }); dispatchCustomElementEvent(this, 'on_change', { value, event }); }); this._refInput = React.createRef(); this._id = props.id || makeUniqueId(); this._name = props.name || this._id; this.state = { _listenForPropChanges: true }; } render() { var _this$context, _this$context2; const props = extendPropsWithContextInClassComponent(this.props, RadioGroup.defaultProps, pickFormElementProps((_this$context = this.context) === null || _this$context === void 0 ? void 0 : _this$context.FormRow), pickFormElementProps((_this$context2 = this.context) === null || _this$context2 === void 0 ? void 0 : _this$context2.formElement), this.context.RadioGroup); const { status, status_state, status_props, status_no_animation, globalStatus, suffix, label, label_direction, label_sr_only, label_position, vertical, layout_direction, size, disabled, skeleton, className, id: _id, name: _name, value: _value, children, on_change, ...rest } = props; const { value } = this.state; const id = this._id; const showStatus = getStatusState(status); const classes = classnames(`dnb-radio-group dnb-radio-group--${layout_direction} dnb-form-component`, createSpacingClasses(props), className, status && `dnb-radio-group__status--${status_state}`); const params = { ...rest }; const legendId = id + '-label'; if (showStatus || suffix) { params['aria-describedby'] = combineDescribedBy(params, showStatus ? id + '-status' : null, suffix ? id + '-suffix' : null); } if (label) { params['aria-labelledby'] = combineLabelledBy(params, legendId); } validateDOMAttributes(this.props, params); const context = { name: this._name, value, size, disabled, label_position, onChange: this.onChangeHandler }; const Fieldset = label ? 'fieldset' : 'div'; return React.createElement(RadioGroupContext.Provider, { value: context }, React.createElement("div", { className: classes }, _AlignmentHelper || (_AlignmentHelper = React.createElement(AlignmentHelper, null)), React.createElement(Fieldset, { className: "dnb-radio-group__fieldset", "aria-labelledby": label ? legendId : undefined, role: "radiogroup" }, React.createElement(Flex.Container, { direction: vertical || label_direction === 'vertical' ? 'vertical' : 'horizontal', gap: vertical ? 'x-small' : 'small' }, label && React.createElement(FormLabel, { element: "legend", id: legendId, srOnly: label_sr_only }, label), React.createElement(Space, _extends({ element: "span", id: id, className: "dnb-radio-group__shell" }, params), children, suffix && React.createElement(Suffix, { className: "dnb-radio-group__suffix", id: id + '-suffix', context: props }, suffix), React.createElement(FormStatus, _extends({ show: showStatus, id: id + '-form-status', globalStatus: globalStatus, label: label, text: status, state: status_state, text_id: id + '-status', width_selector: id + ', ' + legendId, no_animation: status_no_animation, skeleton: skeleton }, status_props))))))); } } _defineProperty(RadioGroup, "contextType", Context); _defineProperty(RadioGroup, "defaultProps", { label: null, label_direction: null, label_sr_only: null, label_position: null, title: null, disabled: null, skeleton: null, id: null, name: null, size: null, status: null, status_state: 'error', status_props: null, status_no_animation: null, globalStatus: null, suffix: null, vertical: null, layout_direction: 'row', value: undefined, attributes: null, className: null, children: null, on_change: null }); _defineProperty(RadioGroup, "parseChecked", state => /true|on/.test(String(state))); process.env.NODE_ENV !== "production" ? RadioGroup.propTypes = { label: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.node]), label_direction: PropTypes.oneOf(['horizontal', 'vertical']), label_sr_only: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), label_position: PropTypes.oneOf(['left', 'right']), title: PropTypes.string, disabled: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), skeleton: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), id: PropTypes.string, name: PropTypes.string, size: PropTypes.oneOf(['default', 'medium', 'large']), status: PropTypes.oneOfType([PropTypes.string, PropTypes.bool, PropTypes.func, PropTypes.node]), status_state: PropTypes.string, status_props: PropTypes.object, status_no_animation: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), globalStatus: PropTypes.shape({ id: PropTypes.string, message: PropTypes.oneOfType([PropTypes.string, PropTypes.node]) }), suffix: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.node]), layout_direction: PropTypes.oneOf(['column', 'row']), vertical: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), value: PropTypes.string, attributes: PropTypes.oneOfType([PropTypes.string, PropTypes.object]), ...spacingPropTypes, className: PropTypes.string, children: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.node]), on_change: PropTypes.func } : void 0; RadioGroup._supportsSpacingProps = true; //# sourceMappingURL=RadioGroup.js.map