Skip to content

Commit 61ffcdb

Browse files
authored
Merge pull request #496 from mashmatrix/support-slds-2-convert-comboboxes-to-buttons
Revert `combobox`es to `button`s for SLDS2
2 parents ee2e5cb + 8b87e05 commit 61ffcdb

File tree

2 files changed

+20
-70
lines changed

2 files changed

+20
-70
lines changed

src/scripts/Lookup.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,17 @@ const LookupSelectedState: FC<LookupSelectedStateProps> = ({
152152
size='small'
153153
/>
154154
)}
155-
<input
156-
type='text'
157-
readOnly
158-
disabled={disabled}
159-
value={selected.label}
155+
<button
156+
type='button'
160157
role='combobox'
161158
tabIndex={disabled ? -1 : 0}
162159
className='slds-input_faux slds-combobox__input slds-combobox__input-value'
163160
aria-controls={listboxId}
164161
aria-haspopup='listbox'
165162
aria-expanded='false'
166-
/>
163+
>
164+
<span className='slds-truncate'>{selected.label}</span>
165+
</button>
167166
<Button
168167
type='icon'
169168
icon='close'

src/scripts/Picklist.tsx

Lines changed: 15 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { FormElement, FormElementProps } from './FormElement';
1717
import { Icon } from './Icon';
1818
import { AutoAlign, RectangleAlignment } from './AutoAlign';
1919
import { DropdownMenuProps } from './DropdownMenu';
20-
import { registerStyle, isElInChildren } from './util';
20+
import { isElInChildren } from './util';
2121
import { ComponentSettingsContext } from './ComponentSettings';
2222
import { useControlledValue, useEventCallback, useMergeRefs } from './hooks';
2323
import { createFC } from './common';
@@ -67,7 +67,7 @@ function collectOptionValues(children: unknown): PicklistValue[] {
6767
function findSelectedItemLabel(
6868
children: unknown,
6969
selectedValue: PicklistValue
70-
): string | number | null {
70+
): React.ReactNode | null {
7171
return (
7272
React.Children.map(children, (child) => {
7373
if (!React.isValidElement(child)) {
@@ -107,56 +107,21 @@ function findSelectedItemLabel(
107107
typeof label === 'string' ||
108108
typeof label === 'number' ||
109109
React.isValidElement(label)
110-
? extractTextContent(label)
110+
? label
111111
: undefined;
112112
const childrenValue =
113113
typeof itemChildren === 'string' ||
114114
typeof itemChildren === 'number' ||
115115
React.isValidElement(itemChildren) ||
116116
Array.isArray(itemChildren)
117-
? extractTextContent(itemChildren)
117+
? itemChildren
118118
: undefined;
119119

120120
return labelValue || childrenValue;
121121
}).find((result) => result !== null) ?? null
122122
);
123123
}
124124

125-
/**
126-
* Extract text content from React node recursively
127-
*/
128-
function extractTextContent(node: unknown): string | number | null {
129-
if (node == null) {
130-
return null;
131-
}
132-
133-
if (typeof node === 'string' || typeof node === 'number') {
134-
return node;
135-
}
136-
137-
if (typeof node === 'boolean') {
138-
return String(node);
139-
}
140-
141-
if (Array.isArray(node)) {
142-
return node
143-
.map(extractTextContent)
144-
.filter((result) => result !== null)
145-
.join('');
146-
}
147-
148-
if (
149-
React.isValidElement(node) &&
150-
node.props &&
151-
typeof node.props === 'object' &&
152-
'children' in node.props
153-
) {
154-
return extractTextContent(node.props.children);
155-
}
156-
157-
return null;
158-
}
159-
160125
/**
161126
*
162127
*/
@@ -185,17 +150,6 @@ const PicklistContext = createContext<{
185150
optionIdPrefix: '',
186151
});
187152

188-
/**
189-
*
190-
*/
191-
function useInitComponentStyle() {
192-
useEffect(() => {
193-
registerStyle('picklist', [
194-
['.react-picklist-input:not(:disabled)', '{ cursor: pointer; }'],
195-
]);
196-
}, []);
197-
}
198-
199153
/**
200154
*
201155
*/
@@ -220,7 +174,7 @@ export type PicklistProps<MultiSelect extends boolean | undefined> = {
220174
tooltip?: ReactNode;
221175
tooltipIcon?: string;
222176
elementRef?: Ref<HTMLDivElement>;
223-
inputRef?: Ref<HTMLInputElement>;
177+
buttonRef?: Ref<HTMLButtonElement>;
224178
dropdownRef?: Ref<HTMLDivElement>;
225179
onValueChange?: Bivariant<
226180
(
@@ -262,7 +216,7 @@ export const Picklist: (<MultiSelect extends boolean | undefined>(
262216
tooltip,
263217
tooltipIcon,
264218
elementRef: elementRef_,
265-
inputRef: inputRef_,
219+
buttonRef: buttonRef_,
266220
dropdownRef: dropdownRef_,
267221
onSelect,
268222
onComplete,
@@ -273,8 +227,6 @@ export const Picklist: (<MultiSelect extends boolean | undefined>(
273227
...rprops
274228
} = props;
275229

276-
useInitComponentStyle();
277-
278230
const fallbackId = useId();
279231
const id = id_ ?? fallbackId;
280232
const listboxId = `${id}-listbox`;
@@ -386,8 +338,8 @@ export const Picklist: (<MultiSelect extends boolean | undefined>(
386338

387339
const elRef = useRef<HTMLDivElement | null>(null);
388340
const elementRef = useMergeRefs([elRef, elementRef_]);
389-
const comboboxElRef = useRef<HTMLInputElement | null>(null);
390-
const inputRef = useMergeRefs([comboboxElRef, inputRef_]);
341+
const comboboxElRef = useRef<HTMLButtonElement | null>(null);
342+
const buttonRef = useMergeRefs([comboboxElRef, buttonRef_]);
391343
const dropdownElRef = useRef<HTMLDivElement | null>(null);
392344
const dropdownRef = useMergeRefs([dropdownElRef, dropdownRef_]);
393345

@@ -570,7 +522,6 @@ export const Picklist: (<MultiSelect extends boolean | undefined>(
570522
}
571523
);
572524
const inputClassNames = classnames(
573-
'react-picklist-input',
574525
'slds-input_faux',
575526
'slds-combobox__input',
576527
{
@@ -621,9 +572,9 @@ export const Picklist: (<MultiSelect extends boolean | undefined>(
621572
className='slds-combobox__form-element slds-input-has-icon slds-input-has-icon_right'
622573
role='none'
623574
>
624-
<input
625-
type='text'
626-
ref={inputRef}
575+
<button
576+
type='button'
577+
ref={buttonRef}
627578
role='combobox'
628579
tabIndex={disabled ? -1 : 0}
629580
className={inputClassNames}
@@ -637,11 +588,11 @@ export const Picklist: (<MultiSelect extends boolean | undefined>(
637588
onClick={onClick}
638589
onKeyDown={onKeyDown}
639590
onBlur={onBlur}
640-
{...rprops}
641-
value={selectedItemLabel}
642-
readOnly
643591
disabled={disabled}
644-
/>
592+
{...rprops}
593+
>
594+
<span className='slds-truncate'>{selectedItemLabel}</span>
595+
</button>
645596
<Icon
646597
containerClassName='slds-input__icon slds-input__icon_right'
647598
category='utility'

0 commit comments

Comments
 (0)