UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

246 lines (245 loc) 10.1 kB
"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_direction", "label_sr_only", "vertical", "layout_direction", "label", "variant", "left_component", "size", "disabled", "skeleton", "className", "multiselect", "id", "name", "value", "values", "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 "core-js/modules/web.dom-collections.iterator.js"; import React from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; import { isTrue, makeUniqueId, extendPropsWithContextInClassComponent, validateDOMAttributes, getStatusState, combineDescribedBy, combineLabelledBy, dispatchCustomElementEvent } from '../../shared/component-helper'; import { spacingPropTypes, createSpacingClasses } from '../space/SpacingHelper'; import AlignmentHelper from '../../shared/AlignmentHelper'; import FormLabel from '../FormLabel'; import FormStatus from '../FormStatus'; import Flex from '../Flex'; import Space from '../Space'; import Context from '../../shared/Context'; import Suffix from '../../shared/helpers/Suffix'; import ToggleButtonGroupContext from './ToggleButtonGroupContext'; import { pickFormElementProps } from '../../shared/helpers/filterValidProps'; export default class ToggleButtonGroup extends React.PureComponent { static getDerivedStateFromProps(props, state) { if (state._listenForPropChanges) { if (typeof props.value !== 'undefined' && props.value !== state.value) { state.value = props.value; } if (typeof props.values !== 'undefined' && props.values !== state.values) { state.values = ToggleButtonGroup.getValues(props); } } state._listenForPropChanges = true; return state; } static getValues(props) { if (typeof props.values === 'string' && props.values[0] === '[') { return JSON.parse(props.values); } return props.values; } constructor(props) { super(props); _defineProperty(this, "onChangeHandler", _ref => { let { value, event } = _ref; const { multiselect } = this.props; const values = this.state.values || []; if (isTrue(multiselect)) { if (!values.includes(value)) { values.push(value); } else { values.splice(values.indexOf(value), 1); } } this.setState({ value, values, _listenForPropChanges: false }); dispatchCustomElementEvent(this, 'on_change', { value, values, event }); }); this._refInput = React.createRef(); this._id = props.id || makeUniqueId(); this._name = props.name || makeUniqueId(); this.state = { _listenForPropChanges: true }; } render() { var _this$context, _this$context2; const props = extendPropsWithContextInClassComponent(this.props, ToggleButtonGroup.defaultProps, this.context.getTranslation(this.props).ToggleButton, 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.ToggleButtonGroup); const { status, status_state, status_props, status_no_animation, globalStatus, suffix, label_direction, label_sr_only, vertical, layout_direction, label, variant, left_component, size, disabled, skeleton, className, multiselect, id: _id, name: _name, value: _value, values: _values, children, on_change } = props, rest = _objectWithoutProperties(props, _excluded); const { value, values } = this.state; const id = this._id; const showStatus = getStatusState(status); const classes = classnames(`dnb-toggle-button-group dnb-toggle-button-group--${layout_direction} dnb-form-component`, createSpacingClasses(props), className, status && `dnb-toggle-button-group__status--${status_state}`, !label && 'dnb-toggle-button-group--no-label'); 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, values, size, multiselect: isTrue(multiselect), variant, left_component, disabled, skeleton, setContext: context => { if (typeof context === 'function') { context = context(this._tmp); } this._tmp = _objectSpread(_objectSpread({}, this._tmp), context); this.setState(_objectSpread(_objectSpread({}, context), {}, { _listenForPropChanges: false })); }, onChange: this.onChangeHandler }; const Fieldset = label ? 'fieldset' : 'div'; return React.createElement(ToggleButtonGroupContext.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-toggle-button-group__shell", role: "group" }, params), React.createElement(FormStatus, _extends({ show: showStatus, id: id + '-form-status', globalStatus: globalStatus, label: label, text_id: id + '-status', text: status, state: status_state, no_animation: status_no_animation, skeleton: skeleton }, status_props)), React.createElement("span", { className: `dnb-toggle-button-group__shell__children dnb-toggle-button-group__shell__children--${layout_direction}` }, children, suffix && React.createElement(Suffix, { className: "dnb-toggle-button-group__suffix", id: id + '-suffix', context: props }, suffix))))))); } } _defineProperty(ToggleButtonGroup, "contextType", Context); _defineProperty(ToggleButtonGroup, "defaultProps", { label: null, label_direction: null, label_sr_only: null, title: null, multiselect: null, variant: null, left_component: 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, values: undefined, attributes: null, className: null, children: null, on_change: null }); process.env.NODE_ENV !== "production" ? ToggleButtonGroup.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]), title: PropTypes.string, multiselect: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), variant: PropTypes.oneOf(['default', 'checkbox', 'radio']), left_component: PropTypes.node, 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]), vertical: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), layout_direction: PropTypes.oneOf(['column', 'row']), value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.object, PropTypes.array]), values: PropTypes.oneOfType([PropTypes.string, PropTypes.array]), 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; ToggleButtonGroup._supportsSpacingProps = true; //# sourceMappingURL=ToggleButtonGroup.js.map