Skip to content

Commit

Permalink
Set value to be controlled by the consumer
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhabib committed Apr 15, 2024
1 parent 12422d8 commit c24a2bf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const docs: ComponentDocs = {
{ description: 'the other', id: 'theOtherStandardOptions' },
{ description: 'another one', id: 'anotherOneStandardOptions' },
];
const [standardValue, setStandardValue] = React.useState<string>('');

const [preSelectedTags, setPreSelectedTags] = React.useState<Tag[]>([
{ description: 'first', id: 'firstPreSelected' },
Expand All @@ -28,6 +29,7 @@ const docs: ComponentDocs = {
{ description: 'the other', id: 'theOtherPreOptions' },
{ description: 'another one', id: 'anotherOnePreOptions' },
];
const [preValue, setPreValue] = React.useState<string>('');

return source(
<Stack space="xxlarge">
Expand All @@ -44,6 +46,8 @@ const docs: ComponentDocs = {
: [...tags, tag],
);
}}
value={standardValue}
onChange={(event) => setStandardValue(event.target.value)}
/>
</Stack>
<Stack space="small">
Expand All @@ -59,6 +63,8 @@ const docs: ComponentDocs = {
: [...tags, tag],
);
}}
value={preValue}
onChange={(event) => setPreValue(event.target.value)}
/>
</Stack>
</Stack>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,18 @@ export interface TagSelectorProps {
options: Tag[];
selectedTags?: Tag[];
ariaLabel?: string;
onSelect?: (tag: Tag) => void;
onSelect: (tag: Tag) => void;
value: string;
onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
}

export const TagSelector = ({
options,
selectedTags,
ariaLabel,
onSelect,
value,
onChange,
}: TagSelectorProps) => {
const uniqueSelectedTags = [
...(selectedTags || []).filter(
Expand All @@ -103,7 +107,6 @@ export const TagSelector = ({
];
const dropdownOptions = [...uniqueSelectedTags, ...options];

const [input, setInput] = useState('');
const [isFocussed, setIsFocussed] = useState(false);
const [activeOption, setActiveOption] = useState<string | undefined>(
dropdownOptions[0].id,
Expand Down Expand Up @@ -159,9 +162,8 @@ export const TagSelector = ({
<div className="combo-wrap">
<input
type="text"
value={input}
// Todo - make this element controlled by the consumer
onChange={(event) => setInput(event.target.value)}
value={value}
onChange={onChange}
id="tag-selector"
role="combobox"
aria-controls="available-tags"
Expand Down

0 comments on commit c24a2bf

Please sign in to comment.