Skip to content

Commit 161a953

Browse files
feat: add any markup inside the item label
1 parent b78cce6 commit 161a953

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

packages/kit-headless/src/components/combobox/combobox-inline.tsx

+18-10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ export const HComboboxRoot: Component<InternalComboboxProps & HComboboxRootImplP
4545
const HComboboxItemLabel = UserItemLabel ?? InternalComboboxItemLabel;
4646
const HComboboxEmpty = InternalComboboxEmpty;
4747

48+
// recursively extracts the markup (ex: styling characters based on search)
49+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
50+
function getInnerText(node: any): string {
51+
if (typeof node === 'string') return node;
52+
if (Array.isArray(node)) return node.map(getInnerText).join('');
53+
if (node && typeof node === 'object') {
54+
if ('props' in node && 'children' in node.props) {
55+
return getInnerText(node.props.children);
56+
} else if ('children' in node) {
57+
return getInnerText(node.children);
58+
}
59+
}
60+
return '';
61+
}
62+
4863
// source of truth
4964
const itemsMap = new Map();
5065
let currItemIndex = 0;
@@ -94,7 +109,9 @@ export const HComboboxRoot: Component<InternalComboboxProps & HComboboxRootImplP
94109
}
95110

96111
case HComboboxItemLabel: {
97-
const displayValue = child.props.children as string;
112+
const displayValue = getInnerText(child.props.children);
113+
114+
console.log(displayValue);
98115

99116
// distinct value, or the display value is the same as the value
100117
const value = (givenItemValue !== null ? givenItemValue : displayValue) as string;
@@ -114,15 +131,6 @@ export const HComboboxRoot: Component<InternalComboboxProps & HComboboxRootImplP
114131
_value = value;
115132
}
116133

117-
const isString = typeof child.props.children === 'string';
118-
119-
if (!isString) {
120-
throw new Error(
121-
`Qwik UI: select item label passed was not a string. It was a ${typeof child
122-
.props.children}.`,
123-
);
124-
}
125-
126134
// increment after processing children
127135
currItemIndex++;
128136

0 commit comments

Comments
 (0)