UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

355 lines (354 loc) 13.3 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", "label_direction", "label_sr_only", "text", "title", "readOnly", "className", "disabled", "skeleton", "variant", "left_component", "icon", "icon_size", "icon_position", "value", "id", "checked", "children", "on_change", "on_state_update"]; 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 { warn, isTrue, makeUniqueId, extendPropsWithContextInClassComponent, validateDOMAttributes, getStatusState, combineDescribedBy, dispatchCustomElementEvent, keycode } from '../../shared/component-helper'; import AlignmentHelper from '../../shared/AlignmentHelper'; import { pickFormElementProps } from '../../shared/helpers/filterValidProps'; import { spacingPropTypes, createSpacingClasses } from '../space/SpacingHelper'; import Radio from '../radio/Radio'; import Checkbox from '../checkbox/Checkbox'; import Button from '../button/Button'; import FormLabel from '../form-label/FormLabel'; import FormStatus from '../form-status/FormStatus'; import ToggleButtonGroup from './ToggleButtonGroup'; import ToggleButtonGroupContext from './ToggleButtonGroupContext'; import Context from '../../shared/Context'; import Suffix from '../../shared/helpers/Suffix'; export default class ToggleButton extends React.PureComponent { static getDerivedStateFromProps(props, state) { if (state._listenForPropChanges) { if (props.checked !== state._checked) { state.checked = ToggleButton.parseChecked(props.checked); } } state._listenForPropChanges = true; if (state.checked !== state.__checked) { dispatchCustomElementEvent({ props }, 'on_state_update', { checked: state.checked }); } state._checked = props.checked; state.__checked = state.checked; return state; } constructor(props, context) { super(props); _defineProperty(this, "onKeyDownHandler", event => { switch (keycode(event)) { case 'enter': this.onClickHandler({ event }); break; } }); _defineProperty(this, "onKeyUpHandler", event => { switch (keycode(event)) { case 'enter': this.onClickHandler({ event }); break; } }); _defineProperty(this, "onClickHandler", ({ event }) => { if (isTrue(this.props.readOnly)) { return event.preventDefault(); } event.persist(); if (!isTrue(this.context.multiselect) && this.props.value === this.context.value) { return; } const checked = !this.state.checked; this.setState({ checked, _listenForPropChanges: false }); this.callOnChange({ checked, event }); if (this._refButton.current && checked) { try { this._refButton.current._ref.current.focus(); } catch (e) { warn(e); } } }); _defineProperty(this, "callOnChange", ({ checked, event }) => { const { value } = this.props; if (this.context.onChange) { this.context.onChange({ value, event }); } dispatchCustomElementEvent(this, 'on_change', { checked, value, event }); }); this._id = props.id || makeUniqueId(); this._refButton = React.createRef(); this.state = { _listenForPropChanges: true }; if (context.name && typeof props.value !== 'undefined') { if (typeof context.value !== 'undefined') { this.state.checked = context.value === props.value; this.state._listenForPropChanges = false; } else if (context.values && Array.isArray(context.values)) { this.state.checked = context.values.includes(props.value); this.state._listenForPropChanges = false; } else if (ToggleButton.parseChecked(props.checked)) { if (context.setContext) { if (context.multiselect) { context.setContext(tmp => { return { values: tmp && Array.isArray(tmp.values) ? [...tmp.values, props.value] : [props.value] }; }); } else { context.setContext({ value: props.value }); } } } } } render() { return React.createElement(Context.Consumer, null, context => { const contextProps = extendPropsWithContextInClassComponent(this.props, ToggleButton.defaultProps, this.context); const props = extendPropsWithContextInClassComponent(this.props, ToggleButton.defaultProps, contextProps, context.translation.ToggleButton, pickFormElementProps(context.FormRow), pickFormElementProps(context.formElement), context.ToggleButton); const { status, status_state, status_props, status_no_animation, globalStatus, suffix, label, label_direction, label_sr_only, text, title, readOnly, className, disabled, skeleton, variant, left_component, icon, icon_size, icon_position, value: propValue, id: _id, checked: _checked, children, on_change, on_state_update } = props, rest = _objectWithoutProperties(props, _excluded); let { checked } = this.state; if (!isTrue(this.context.multiselect) && typeof this.context.value !== 'undefined') { const contextValue = this.context.value; if (typeof propValue === 'string' || typeof propValue === 'number') { checked = propValue === contextValue; } } else if (isTrue(this.context.multiselect) && typeof this.context.values !== 'undefined') { const contextValues = this.context.values; if (typeof propValue === 'string' || typeof propValue === 'number') { checked = contextValues.includes(propValue); } } const id = this._id; const showStatus = getStatusState(status); const mainParams = { className: classnames('dnb-toggle-button', createSpacingClasses(props), className, status && `dnb-toggle-button__status--${status_state}`, checked && `dnb-toggle-button--checked`, label_direction && `dnb-toggle-button--${label_direction}`) }; validateDOMAttributes(this.props, rest); const buttonParams = _objectSpread({ id, disabled, skeleton, text: text || children, title, icon, icon_size, icon_position, 'aria-pressed': String(checked || false) }, rest); const componentParams = { checked, disabled, element: 'span', 'data-checked': String(checked || false), 'aria-checked': undefined, role: undefined, type: undefined, name: null, title: null }; if (status) { if (status_state === 'info') { componentParams.status_state = 'info'; } else { componentParams.status = 'error'; } } if (showStatus || suffix) { buttonParams['aria-describedby'] = combineDescribedBy(buttonParams, showStatus ? id + '-status' : null, suffix ? id + '-suffix' : null); } if (readOnly) { buttonParams['aria-readonly'] = buttonParams.readOnly = true; } let leftComponent = null; switch (variant) { case 'radio': leftComponent = React.createElement(Radio, _extends({ id: `${id}-radio` }, componentParams)); break; case 'checkbox': leftComponent = React.createElement(Checkbox, _extends({ id: `${id}-checkbox` }, componentParams)); break; case 'default': default: leftComponent = left_component; break; } return React.createElement("span", mainParams, label && React.createElement(FormLabel, { id: id + '-label', forId: id, text: label, disabled: disabled, skeleton: skeleton, labelDirection: label_direction, srOnly: label_sr_only }), React.createElement("span", { className: "dnb-toggle-button__inner" }, 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__shell" }, _AlignmentHelper || (_AlignmentHelper = React.createElement(AlignmentHelper, null)), React.createElement(Button, _extends({ variant: "secondary", className: "dnb-toggle-button__button", custom_content: leftComponent && React.createElement("span", { className: "dnb-toggle-button__component" }, leftComponent) }, buttonParams, { ref: this._refButton, onClick: this.onClickHandler, onKeyDown: this.onKeyDownHandler, onKeyUp: this.onKeyUpHandler })), suffix && React.createElement(Suffix, { className: "dnb-toggle-button__suffix", id: id + '-suffix', context: props }, suffix)))); }); } } _defineProperty(ToggleButton, "Group", ToggleButtonGroup); _defineProperty(ToggleButton, "contextType", ToggleButtonGroupContext); _defineProperty(ToggleButton, "defaultProps", { text: null, label: null, label_direction: null, label_sr_only: null, title: null, checked: undefined, variant: null, size: null, left_component: null, disabled: null, skeleton: null, id: null, status: null, status_state: 'error', status_props: null, status_no_animation: null, globalStatus: null, suffix: null, value: '', icon: null, icon_position: 'right', icon_size: null, attributes: null, readOnly: false, className: null, children: null, on_change: null, on_state_update: null }); _defineProperty(ToggleButton, "parseChecked", state => /true|on/.test(String(state))); process.env.NODE_ENV !== "production" ? ToggleButton.propTypes = _objectSpread(_objectSpread({ text: PropTypes.string, 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, checked: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), variant: PropTypes.oneOf(['default', 'checkbox', 'radio']), size: PropTypes.oneOf(['default', 'small', 'medium', 'large']), left_component: PropTypes.node, disabled: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), skeleton: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), id: PropTypes.string, 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]), value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.object, PropTypes.array]), icon: PropTypes.oneOfType([PropTypes.string, PropTypes.node, PropTypes.func]), icon_position: PropTypes.oneOf(['left', 'right']), icon_size: PropTypes.string, attributes: PropTypes.oneOfType([PropTypes.string, PropTypes.object]), readOnly: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]) }, spacingPropTypes), {}, { className: PropTypes.string, children: PropTypes.oneOfType([PropTypes.string, PropTypes.func]), on_change: PropTypes.func, on_state_update: PropTypes.func }) : void 0; ToggleButton._formElement = true; ToggleButton._supportsSpacingProps = true; //# sourceMappingURL=ToggleButton.js.map