@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
202 lines (201 loc) • 8.34 kB
JavaScript
"use client";
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
var _AlignmentHelper;
const _excluded = ["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", "name", "value", "children", "on_change"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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';
import { pickFormElementProps } from '../../shared/helpers/filterValidProps';
import { spacingPropTypes, createSpacingClasses } from '../space/SpacingHelper';
import AlignmentHelper from '../../shared/AlignmentHelper';
import Space from '../Space';
import FormLabel from '../FormLabel';
import FormStatus from '../FormStatus';
import Flex from '../Flex';
import Context from '../../shared/Context';
import Suffix from '../../shared/helpers/Suffix';
import RadioGroupContext from './RadioGroupContext';
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
} = props,
rest = _objectWithoutProperties(props, _excluded);
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 = _objectSpread({}, rest);
if (showStatus || suffix) {
params['aria-describedby'] = combineDescribedBy(params, showStatus ? id + '-status' : null, suffix ? id + '-suffix' : null);
}
if (label) {
params['aria-labelledby'] = combineLabelledBy(params, id + '-label');
}
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-toggle-button-group__fieldset"
}, React.createElement(Flex.Container, {
direction: vertical || label_direction === 'vertical' ? 'vertical' : 'horizontal',
gap: vertical ? 'x-small' : 'small'
}, label && React.createElement(FormLabel, {
element: "legend",
id: id + '-label',
srOnly: label_sr_only
}, label), React.createElement(Space, _extends({
element: "span",
id: id,
className: "dnb-radio-group__shell",
role: "radiogroup"
}, 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 + ', ' + id + '-label',
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 = _objectSpread(_objectSpread({
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