UNPKG

@sap-ux/ui-components

Version:
140 lines 5.93 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UIToggleGroup = void 0; const React = __importStar(require("react")); const UIToggleGroupOption_1 = require("./UIToggleGroupOption"); const UILabel_1 = require("../UILabel"); const UIFocusZone_1 = require("../UIFocusZone"); const utilities_1 = require("../../utilities"); require("./UIToggleGroup.scss"); /** * UIToggleGroup component. * * @exports * @class UIToggleGroup * @extends {React.Component<UIToggleGroupProps, UIToggleGroupState>} */ class UIToggleGroup extends React.Component { /** * Initializes component properties. * * @param {UIToggleGroupProps} props */ constructor(props) { super(props); this.labelId = this.props.labelId ? this.props.labelId : (0, utilities_1.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_1.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_1.UIFocusZone, { as: "div", direction: UIFocusZone_1.UIFocusZoneDirection.horizontal, isCircularNavigation: true, shouldRaiseClicks: true, className: "ui-toggle-group-container", role: "list" }, this.state.options.map((option) => { return (React.createElement(UIToggleGroupOption_1.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 })); })))); } } exports.UIToggleGroup = UIToggleGroup; //# sourceMappingURL=UIToggleGroup.js.map