UNPKG

@sap-ux/ui-components

Version:

SAP UI Components Library

113 lines 4.71 kB
import * as React from 'react'; import { UIToggleGroupOption } from './UIToggleGroupOption/index.js'; import { UILabel } from '../UILabel/index.js'; import { UIFocusZone, UIFocusZoneDirection } from '../UIFocusZone/index.js'; import { getUIId } from '../../utilities/index.js'; import './UIToggleGroup.scss'; /** * UIToggleGroup component. * * @exports * @class UIToggleGroup * @extends {React.Component<UIToggleGroupProps, UIToggleGroupState>} */ export class UIToggleGroup extends React.Component { /** * Initializes component properties. * * @param {UIToggleGroupProps} props */ constructor(props) { super(props); this.labelId = this.props.labelId ? this.props.labelId : getUIId('ui-toggle-group-option-'); this.onClick = (_evt, option) => { let isSelected = false; if (option?.itemKey) { this.setState((prevState) => { const newState = { options: prevState.options.map((entry, index, options) => { if (entry.key === option.itemKey) { isSelected = !options[index].selected; return { ...entry, selected: isSelected, focused: options[index].focused }; } else { return { ...entry, selected: false, focused: false }; } }) }; if (this.props.onChange) { this.props.onChange?.(option.itemKey, isSelected); } return newState; }); } }; this.onFocus = (_evt, option) => { if (option?.itemKey) { this.setState((prevState) => ({ options: prevState.options.map((entry) => { if (entry.key === option.itemKey) { return { ...entry, focused: true }; } else { return { ...entry, focused: false }; } }) })); } }; this.onBlur = (_evt, _option) => { this.setState((prevState) => ({ options: prevState.options.map((entry) => { return { ...entry, focused: false }; }) })); }; this.state = { options: this.props.options.map((entry) => { if (entry.key === this.props.selectedKey) { return { ...entry, selected: true }; } else { return { ...entry }; } }) }; this.onClick = this.onClick.bind(this); this.onFocus = this.onFocus.bind(this); this.onBlur = this.onBlur.bind(this); } /** * @returns {JSX.Element} */ render() { return (React.createElement("div", { className: "ui-toggle-group", role: "group", ...(this.props.ariaLabel && { 'aria-labelledby': this.labelId }) }, this.props.label && (React.createElement(UILabel, { id: this.labelId, ...(this.props.ariaLabel && { 'aria-label': this.props.ariaLabel }), className: "ui-toggle-group-label", required: this.props.required, disabled: this.props.disabled }, this.props.label)), React.createElement(UIFocusZone, { as: "div", direction: UIFocusZoneDirection.horizontal, isCircularNavigation: true, shouldRaiseClicks: true, className: "ui-toggle-group-container", role: "list" }, this.state.options.map((option) => { return (React.createElement(UIToggleGroupOption, { itemKey: option.key, ...option, key: option.key, onClick: this.onClick, onFocus: this.onFocus, onBlur: this.onBlur, selected: option.selected, disabled: option.disabled || this.props.disabled })); })))); } } //# sourceMappingURL=UIToggleGroup.js.map