1 | import { __rest } from "tslib";
|
2 | import * as React from 'react';
|
3 | import styles from '@patternfly/react-styles/css/components/Select/select';
|
4 | import formStyles from '@patternfly/react-styles/css/components/Form/form';
|
5 | import { css } from '@patternfly/react-styles';
|
6 | import { SelectOption } from './SelectOption';
|
7 | import { SelectConsumer, SelectPosition, SelectVariant } from './selectConstants';
|
8 | import { SelectGroup } from './SelectGroup';
|
9 | import { Divider } from '../Divider/Divider';
|
10 | class SelectMenuWithRef extends React.Component {
|
11 | extendChildren(randomId) {
|
12 | const { children, hasInlineFilter, isGrouped } = this.props;
|
13 | const childrenArray = children;
|
14 | let index = hasInlineFilter ? 1 : 0;
|
15 | if (isGrouped) {
|
16 | return React.Children.map(childrenArray, (group) => {
|
17 | if (group.type === SelectGroup) {
|
18 | return React.cloneElement(group, {
|
19 | titleId: group.props.label && group.props.label.replace(/\W/g, '-'),
|
20 | children: React.Children.map(group.props.children, (option) => this.cloneOption(option, index++, randomId))
|
21 | });
|
22 | }
|
23 | else {
|
24 | return this.cloneOption(group, index++, randomId);
|
25 | }
|
26 | });
|
27 | }
|
28 | return React.Children.map(childrenArray, (child) => this.cloneOption(child, index++, randomId));
|
29 | }
|
30 | cloneOption(child, index, randomId) {
|
31 | const { selected, sendRef, keyHandler } = this.props;
|
32 | const isSelected = this.checkForValue(child.props.value, selected);
|
33 | if (child.type === Divider) {
|
34 | return child;
|
35 | }
|
36 | return React.cloneElement(child, {
|
37 | inputId: `${randomId}-${index}`,
|
38 | isSelected,
|
39 | sendRef,
|
40 | keyHandler,
|
41 | index
|
42 | });
|
43 | }
|
44 | checkForValue(valueToCheck, options) {
|
45 | if (!options || !valueToCheck) {
|
46 | return false;
|
47 | }
|
48 | const isSelectOptionObject = typeof valueToCheck !== 'string' &&
|
49 | valueToCheck.toString &&
|
50 | valueToCheck.compareTo;
|
51 | if (Array.isArray(options)) {
|
52 | if (isSelectOptionObject) {
|
53 | return options.some(option => option.compareTo(valueToCheck));
|
54 | }
|
55 | else {
|
56 | return options.includes(valueToCheck);
|
57 | }
|
58 | }
|
59 | else {
|
60 | if (isSelectOptionObject) {
|
61 | return options.compareTo(valueToCheck);
|
62 | }
|
63 | else {
|
64 | return options === valueToCheck;
|
65 | }
|
66 | }
|
67 | }
|
68 | extendCheckboxChildren(children) {
|
69 | const { isGrouped, checked, sendRef, keyHandler, hasInlineFilter, isLastOptionBeforeFooter } = this.props;
|
70 | let index = hasInlineFilter ? 1 : 0;
|
71 | if (isGrouped) {
|
72 | return React.Children.map(children, (group) => {
|
73 | if (group.type === Divider) {
|
74 | return group;
|
75 | }
|
76 | else if (group.type === SelectOption) {
|
77 | return React.cloneElement(group, {
|
78 | isChecked: this.checkForValue(group.props.value, checked),
|
79 | sendRef,
|
80 | keyHandler,
|
81 | index: index++,
|
82 | isLastOptionBeforeFooter
|
83 | });
|
84 | }
|
85 | return React.cloneElement(group, {
|
86 | titleId: group.props.label && group.props.label.replace(/\W/g, '-'),
|
87 | children: group.props.children ? (React.createElement("fieldset", { "aria-labelledby": group.props.label && group.props.label.replace(/\W/g, '-'), className: css(styles.selectMenuFieldset) }, React.Children.map(group.props.children, (option) => option.type === Divider
|
88 | ? option
|
89 | : React.cloneElement(option, {
|
90 | isChecked: this.checkForValue(option.props.value, checked),
|
91 | sendRef,
|
92 | keyHandler,
|
93 | index: index++,
|
94 | isLastOptionBeforeFooter
|
95 | })))) : null
|
96 | });
|
97 | });
|
98 | }
|
99 | return React.Children.map(children, (child) => child.type === Divider
|
100 | ? child
|
101 | : React.cloneElement(child, {
|
102 | isChecked: this.checkForValue(child.props.value, checked),
|
103 | sendRef,
|
104 | keyHandler,
|
105 | index: index++,
|
106 | isLastOptionBeforeFooter
|
107 | }));
|
108 | }
|
109 | renderSelectMenu({ variant, inputIdPrefix }) {
|
110 |
|
111 | const _a = this.props, { children, isCustomContent, className, isExpanded, openedOnEnter, selected, checked, isGrouped, position, sendRef, keyHandler, maxHeight, noResultsFoundText, createText, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, hasInlineFilter, innerRef, footer, footerRef, isLastOptionBeforeFooter } = _a, props = __rest(_a, ["children", "isCustomContent", "className", "isExpanded", "openedOnEnter", "selected", "checked", "isGrouped", "position", "sendRef", "keyHandler", "maxHeight", "noResultsFoundText", "createText", 'aria-label', 'aria-labelledby', "hasInlineFilter", "innerRef", "footer", "footerRef", "isLastOptionBeforeFooter"]);
|
112 |
|
113 | let Component = 'div';
|
114 | const variantProps = Object.assign({ ref: innerRef, className: css(!footer ? styles.selectMenu : 'pf-c-select__menu-list', position === SelectPosition.right && styles.modifiers.alignRight, className) }, (maxHeight && { style: { maxHeight, overflow: 'auto' } }));
|
115 | const extendedChildren = () => variant === SelectVariant.checkbox
|
116 | ? this.extendCheckboxChildren(children)
|
117 | : this.extendChildren(inputIdPrefix);
|
118 | if (isCustomContent) {
|
119 | variantProps.children = children;
|
120 | }
|
121 | else if (hasInlineFilter) {
|
122 | if (React.Children.count(children) === 0) {
|
123 | variantProps.children = React.createElement("fieldset", { className: css(styles.selectMenuFieldset) });
|
124 | }
|
125 | else {
|
126 | variantProps.children = (React.createElement("fieldset", { "aria-label": ariaLabel, "aria-labelledby": (!ariaLabel && ariaLabelledBy) || null, className: css(formStyles.formFieldset) },
|
127 | children.shift(),
|
128 | extendedChildren()));
|
129 | }
|
130 | }
|
131 | else {
|
132 | variantProps.children = extendedChildren();
|
133 | if (!isGrouped) {
|
134 | Component = 'ul';
|
135 | variantProps.role = 'listbox';
|
136 | variantProps['aria-label'] = ariaLabel;
|
137 | variantProps['aria-labelledby'] = (!ariaLabel && ariaLabelledBy) || null;
|
138 | }
|
139 | }
|
140 | return (React.createElement(React.Fragment, null,
|
141 | React.createElement(Component, Object.assign({}, variantProps, props)),
|
142 | footer && (React.createElement("div", { className: css(styles.selectMenuFooter), ref: footerRef }, footer))));
|
143 | }
|
144 | render() {
|
145 | return React.createElement(SelectConsumer, null, context => this.renderSelectMenu(context));
|
146 | }
|
147 | }
|
148 | SelectMenuWithRef.displayName = 'SelectMenu';
|
149 | SelectMenuWithRef.defaultProps = {
|
150 | className: '',
|
151 | isExpanded: false,
|
152 | isGrouped: false,
|
153 | openedOnEnter: false,
|
154 | selected: '',
|
155 | maxHeight: '',
|
156 | position: SelectPosition.left,
|
157 | sendRef: () => { },
|
158 | keyHandler: () => { },
|
159 | isCustomContent: false,
|
160 | hasInlineFilter: false,
|
161 | isLastOptionBeforeFooter: () => { }
|
162 | };
|
163 | export const SelectMenu = React.forwardRef((props, ref) => (React.createElement(SelectMenuWithRef, Object.assign({ innerRef: ref }, props), props.children)));
|
164 |
|
\ | No newline at end of file |