Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 6 additions & 3 deletions src/components/experimental/ComboBox/ComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,18 @@ const ComboBoxInput = React.forwardRef<HTMLDivElement, ComboBoxFieldProps>(
<FakeInput $isVisuallyFocused={state?.isOpen} ref={forwardedRef} onClick={() => inputRef.current?.focus()}>
{leadingIcon}
<InnerWrapper>
<Label $flying={Boolean(placeholder || state?.inputValue.length > 0)}>{label}</Label>
<Label $flying={Boolean(placeholder || state?.inputValue?.length > 0)}>{label}</Label>
<Input placeholder={placeholder} ref={inputRef} />
</InnerWrapper>
{state?.inputValue.length > 0 ? (
{state?.inputValue?.length > 0 ? (
<Button
// Don't inherit default Button behavior from ComboBox.
slot={null}
aria-label={ariaStrings.clearFieldButton}
onPress={() => state?.setSelectedKey(null)}
onPress={() => {
state?.setSelectedKey(null);
state?.setInputValue('');
}}
>
<XCrossCircleIcon />
</Button>
Expand Down
49 changes: 49 additions & 0 deletions src/components/experimental/ComboBox/docs/ComboBox.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,52 @@ export const AsyncValues: StoryObj<typeof ComboBox<Character>> = {
);
}
};

export const FullyControlled: Story = {
render: () => {
const [items, setItems] = React.useState<Character[]>([]);
const [filterText, setFilterText] = React.useState('');
const [key, setKey] = React.useState<string | null>(null);

React.useEffect(() => {
let ignore = false;

async function startFetching() {
const res = await fetch(`https://swapi.py4e.com/api/people/?search=${filterText}`);
const json = await res.json();

if (!ignore) {
setItems(json.results);
}
}

// eslint-disable-next-line no-void
void startFetching();

return () => {
ignore = true;
};
}, [filterText]);

return (
<ComboBox
label="Star Wars Character"
items={items}
inputValue={filterText}
selectedKey={key}
onSelectionChange={newKey => {
setFilterText(newKey as string);
setKey(newKey as string);
}}
onInputChange={input => {
setFilterText(input);
if (input === '') {
setKey(null);
}
}}
>
{item => <ListBoxItem id={item.name}>{item.name}</ListBoxItem>}
</ComboBox>
);
}
};
Loading