1 | import { __rest } from "tslib";
|
2 | import * as React from 'react';
|
3 | import styles from '@patternfly/react-styles/css/components/Select/select';
|
4 | import checkStyles from '@patternfly/react-styles/css/components/Check/check';
|
5 | import { css } from '@patternfly/react-styles';
|
6 | import CheckIcon from '@patternfly/react-icons/dist/esm/icons/check-icon';
|
7 | import { SelectConsumer, SelectVariant } from './selectConstants';
|
8 | import StarIcon from '@patternfly/react-icons/dist/esm/icons/star-icon';
|
9 | import { getUniqueId } from '../../helpers/util';
|
10 | import { KeyTypes } from '../../helpers/constants';
|
11 | export class SelectOption extends React.Component {
|
12 | constructor() {
|
13 | super(...arguments);
|
14 | this.ref = React.createRef();
|
15 | this.liRef = React.createRef();
|
16 | this.favoriteRef = React.createRef();
|
17 | this.onKeyDown = (event, innerIndex, onEnter, isCheckbox) => {
|
18 | const { index, keyHandler, isLastOptionBeforeFooter } = this.props;
|
19 | let isLastItemBeforeFooter = false;
|
20 | if (isLastOptionBeforeFooter !== undefined) {
|
21 | isLastItemBeforeFooter = isLastOptionBeforeFooter(index);
|
22 | }
|
23 | if (event.key === KeyTypes.Tab) {
|
24 |
|
25 | if (isCheckbox && !isLastItemBeforeFooter) {
|
26 | if (event.shiftKey) {
|
27 | keyHandler(index, innerIndex, 'up');
|
28 | }
|
29 | else {
|
30 | keyHandler(index, innerIndex, 'down');
|
31 | }
|
32 | event.stopPropagation();
|
33 | }
|
34 | else {
|
35 | if (event.shiftKey) {
|
36 | keyHandler(index, innerIndex, 'up');
|
37 | }
|
38 | else {
|
39 | keyHandler(index, innerIndex, 'tab');
|
40 | }
|
41 | }
|
42 | }
|
43 | event.preventDefault();
|
44 | if (event.key === KeyTypes.ArrowUp) {
|
45 | keyHandler(index, innerIndex, 'up');
|
46 | }
|
47 | else if (event.key === KeyTypes.ArrowDown) {
|
48 | keyHandler(index, innerIndex, 'down');
|
49 | }
|
50 | else if (event.key === KeyTypes.ArrowLeft) {
|
51 | keyHandler(index, innerIndex, 'left');
|
52 | }
|
53 | else if (event.key === KeyTypes.ArrowRight) {
|
54 | keyHandler(index, innerIndex, 'right');
|
55 | }
|
56 | else if (event.key === KeyTypes.Enter) {
|
57 | if (onEnter !== undefined) {
|
58 | onEnter();
|
59 | }
|
60 | else {
|
61 | this.ref.current.click();
|
62 | }
|
63 | }
|
64 | };
|
65 | }
|
66 | componentDidMount() {
|
67 | this.props.sendRef(this.props.isDisabled ? null : this.ref.current, this.props.isDisabled ? null : this.favoriteRef.current, this.props.isDisabled ? null : this.liRef.current, this.props.index);
|
68 | }
|
69 | componentDidUpdate() {
|
70 | this.props.sendRef(this.props.isDisabled ? null : this.ref.current, this.props.isDisabled ? null : this.favoriteRef.current, this.props.isDisabled ? null : this.liRef.current, this.props.index);
|
71 | }
|
72 | render() {
|
73 |
|
74 | const _a = this.props, { children, className, id, description, itemCount, value, onClick, isDisabled, isPlaceholder, isNoResultsOption, isSelected, isChecked, isFocused, sendRef, keyHandler, index, component, inputId, isFavorite, ariaIsFavoriteLabel = 'starred', ariaIsNotFavoriteLabel = 'not starred', isLoad, isLoading, setViewMoreNextIndex,
|
75 |
|
76 | isLastOptionBeforeFooter, isGrouped = false } = _a, props = __rest(_a, ["children", "className", "id", "description", "itemCount", "value", "onClick", "isDisabled", "isPlaceholder", "isNoResultsOption", "isSelected", "isChecked", "isFocused", "sendRef", "keyHandler", "index", "component", "inputId", "isFavorite", "ariaIsFavoriteLabel", "ariaIsNotFavoriteLabel", "isLoad", "isLoading", "setViewMoreNextIndex", "isLastOptionBeforeFooter", "isGrouped"]);
|
77 |
|
78 | const Component = component;
|
79 | if (!id && isFavorite !== null) {
|
80 |
|
81 | console.error('Please provide an id to use the favorites feature.');
|
82 | }
|
83 | const generatedId = id || getUniqueId('select-option');
|
84 | const favoriteButton = (onFavorite) => (React.createElement("button", { className: css(styles.selectMenuItem, styles.modifiers.action, styles.modifiers.favoriteAction), "aria-label": isFavorite ? ariaIsFavoriteLabel : ariaIsNotFavoriteLabel, onClick: () => {
|
85 | onFavorite(generatedId.replace('favorite-', ''), isFavorite);
|
86 | }, onKeyDown: event => {
|
87 | this.onKeyDown(event, 1, () => onFavorite(generatedId.replace('favorite-', ''), isFavorite));
|
88 | }, ref: this.favoriteRef },
|
89 | React.createElement("span", { className: css(styles.selectMenuItemActionIcon) },
|
90 | React.createElement(StarIcon, null))));
|
91 | const itemDisplay = itemCount ? (React.createElement("span", { className: css(styles.selectMenuItemRow) },
|
92 | React.createElement("span", { className: css(styles.selectMenuItemText) }, children || (value && value.toString && value.toString())),
|
93 | React.createElement("span", { className: css(styles.selectMenuItemCount) }, itemCount))) : (children || value.toString());
|
94 | const onViewMoreClick = (event) => {
|
95 |
|
96 | setViewMoreNextIndex();
|
97 | onClick(event);
|
98 | };
|
99 | const renderOption = (onSelect, onClose, variant, inputIdPrefix, onFavorite, shouldResetOnSelect) => {
|
100 | if (variant !== SelectVariant.checkbox && isLoading && isGrouped) {
|
101 | return (React.createElement("div", { role: "presentation", className: css(styles.selectListItem, isLoading && styles.modifiers.loading, className) }, children));
|
102 | }
|
103 | else if (variant !== SelectVariant.checkbox && isLoad && isGrouped) {
|
104 | return (React.createElement("div", null,
|
105 | React.createElement("button", Object.assign({}, props, { role: "presentation", className: css(styles.selectMenuItem, styles.modifiers.load, className), onClick: (event) => {
|
106 | onViewMoreClick(event);
|
107 | event.stopPropagation();
|
108 | }, ref: this.ref, type: "button" }), children || value.toString())));
|
109 | }
|
110 | else if (variant !== SelectVariant.checkbox) {
|
111 | return (React.createElement("li", { id: generatedId, role: "presentation", className: css(isLoading && styles.selectListItem, !isLoading && styles.selectMenuWrapper, isFavorite && styles.modifiers.favorite, isFocused && styles.modifiers.focus, isLoading && styles.modifiers.loading), ref: this.liRef },
|
112 | isLoading && children,
|
113 | isLoad && !isGrouped && (React.createElement("button", Object.assign({}, props, { className: css(styles.selectMenuItem, styles.modifiers.load, className), onClick: (event) => {
|
114 | onViewMoreClick(event);
|
115 | event.stopPropagation();
|
116 | }, ref: this.ref, onKeyDown: (event) => {
|
117 | this.onKeyDown(event, 0);
|
118 | }, type: "button" }), itemDisplay)),
|
119 | !isLoading && !isLoad && (React.createElement(React.Fragment, null,
|
120 | React.createElement(Component, Object.assign({}, props, { className: css(styles.selectMenuItem, isLoad && styles.modifiers.load, isSelected && styles.modifiers.selected, isDisabled && styles.modifiers.disabled, description && styles.modifiers.description, isFavorite !== null && styles.modifiers.link, className), onClick: (event) => {
|
121 | if (!isDisabled) {
|
122 | onClick(event);
|
123 | onSelect(event, value, isPlaceholder);
|
124 | shouldResetOnSelect && onClose();
|
125 | }
|
126 | }, role: "option", "aria-selected": isSelected || null, ref: this.ref, onKeyDown: (event) => {
|
127 | this.onKeyDown(event, 0);
|
128 | }, type: "button" }),
|
129 | description && (React.createElement(React.Fragment, null,
|
130 | React.createElement("span", { className: css(styles.selectMenuItemMain) },
|
131 | itemDisplay,
|
132 | isSelected && (React.createElement("span", { className: css(styles.selectMenuItemIcon) },
|
133 | React.createElement(CheckIcon, { "aria-hidden": true })))),
|
134 | React.createElement("span", { className: css(styles.selectMenuItemDescription) }, description))),
|
135 | !description && (React.createElement(React.Fragment, null,
|
136 | itemDisplay,
|
137 | isSelected && (React.createElement("span", { className: css(styles.selectMenuItemIcon) },
|
138 | React.createElement(CheckIcon, { "aria-hidden": true })))))),
|
139 | isFavorite !== null && id && favoriteButton(onFavorite)))));
|
140 | }
|
141 | else if (variant === SelectVariant.checkbox && isLoad) {
|
142 | return (React.createElement("button", { className: css(styles.selectMenuItem, styles.modifiers.load, isFocused && styles.modifiers.focus, className), onKeyDown: (event) => {
|
143 | this.onKeyDown(event, 0, undefined, true);
|
144 | }, onClick: (event) => {
|
145 | onViewMoreClick(event);
|
146 | event.stopPropagation();
|
147 | }, ref: this.ref }, children || (value && value.toString && value.toString())));
|
148 | }
|
149 | else if (variant === SelectVariant.checkbox && isLoading) {
|
150 | return (React.createElement("div", { className: css(styles.selectListItem, isLoading && styles.modifiers.loading, className) }, children));
|
151 | }
|
152 | else if (variant === SelectVariant.checkbox && !isNoResultsOption && !isLoading && !isLoad) {
|
153 | return (React.createElement("label", Object.assign({}, props, { className: css(checkStyles.check, styles.selectMenuItem, isDisabled && styles.modifiers.disabled, description && styles.modifiers.description, className), onKeyDown: (event) => {
|
154 | this.onKeyDown(event, 0, undefined, true);
|
155 | } }),
|
156 | React.createElement("input", { id: inputId || `${inputIdPrefix}-${value.toString()}`, className: css(checkStyles.checkInput), type: "checkbox", onChange: event => {
|
157 | if (!isDisabled) {
|
158 | onClick(event);
|
159 | onSelect(event, value);
|
160 | }
|
161 | }, ref: this.ref, checked: isChecked || false, disabled: isDisabled }),
|
162 | React.createElement("span", { className: css(checkStyles.checkLabel, isDisabled && styles.modifiers.disabled) }, itemDisplay),
|
163 | description && React.createElement("div", { className: css(checkStyles.checkDescription) }, description)));
|
164 | }
|
165 | else if (variant === SelectVariant.checkbox && isNoResultsOption && !isLoading && !isLoad) {
|
166 | return (React.createElement("div", null,
|
167 | React.createElement(Component, Object.assign({}, props, { className: css(styles.selectMenuItem, isSelected && styles.modifiers.selected, isDisabled && styles.modifiers.disabled, className), role: "option", "aria-selected": isSelected || null, ref: this.ref, onKeyDown: (event) => {
|
168 | this.onKeyDown(event, 0, undefined, true);
|
169 | }, type: "button" }), itemDisplay)));
|
170 | }
|
171 | };
|
172 | return (React.createElement(SelectConsumer, null, ({ onSelect, onClose, variant, inputIdPrefix, onFavorite, shouldResetOnSelect }) => (React.createElement(React.Fragment, null, renderOption(onSelect, onClose, variant, inputIdPrefix, onFavorite, shouldResetOnSelect)))));
|
173 | }
|
174 | }
|
175 | SelectOption.displayName = 'SelectOption';
|
176 | SelectOption.defaultProps = {
|
177 | className: '',
|
178 | value: '',
|
179 | index: 0,
|
180 | isDisabled: false,
|
181 | isPlaceholder: false,
|
182 | isSelected: false,
|
183 | isChecked: false,
|
184 | isNoResultsOption: false,
|
185 | component: 'button',
|
186 | onClick: () => { },
|
187 | sendRef: () => { },
|
188 | keyHandler: () => { },
|
189 | inputId: '',
|
190 | isFavorite: null,
|
191 | isLoad: false,
|
192 | isLoading: false,
|
193 | setViewMoreNextIndex: () => { },
|
194 | isLastOptionBeforeFooter: () => false
|
195 | };
|
196 |
|
\ | No newline at end of file |