Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add any markup inside the item label #954

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions packages/kit-headless/src/components/combobox/combobox-inline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ export const HComboboxRoot: Component<InternalComboboxProps & HComboboxRootImplP
const HComboboxItemLabel = UserItemLabel ?? InternalComboboxItemLabel;
const HComboboxEmpty = InternalComboboxEmpty;

// recursively extracts the markup (ex: styling characters based on search)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getInnerText(node: any): string {
if (typeof node === 'string') return node;
if (Array.isArray(node)) return node.map(getInnerText).join('');
if (node && typeof node === 'object') {
if ('props' in node && 'children' in node.props) {
return getInnerText(node.props.children);
} else if ('children' in node) {
return getInnerText(node.children);
}
}
return '';
}

// source of truth
const itemsMap = new Map();
let currItemIndex = 0;
Expand Down Expand Up @@ -94,7 +109,7 @@ export const HComboboxRoot: Component<InternalComboboxProps & HComboboxRootImplP
}

case HComboboxItemLabel: {
const displayValue = child.props.children as string;
const displayValue = getInnerText(child.props.children);

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

const isString = typeof child.props.children === 'string';

if (!isString) {
throw new Error(
`Qwik UI: select item label passed was not a string. It was a ${typeof child
.props.children}.`,
);
}

// increment after processing children
currItemIndex++;

Expand Down
Loading