Skip to content

Commit 9c53f69

Browse files
committed
chore(@clayui/autocomplete): LPD-54854 Adds example of autocomplete that can add new items
1 parent e5557c3 commit 9c53f69

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

packages/clay-autocomplete/stories/Autocomplete.stories.tsx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,3 +506,69 @@ export const AsyncData = () => {
506506
</div>
507507
);
508508
};
509+
510+
export const AddNewItem = () => {
511+
const [items, setItems] = useState([
512+
'Footwear',
513+
'Historical Figures',
514+
'Items in a Vending Machine',
515+
'Reasons to Call 911',
516+
"Something You're Afraid Of",
517+
'Sports Played Indoors',
518+
'Things You Do at Work',
519+
'Things You Plug In',
520+
'Things You Save Up to Buy',
521+
]);
522+
523+
const [value, setValue] = useState('');
524+
525+
return (
526+
<div className="row">
527+
<div className="col-md-5">
528+
<p>This example shows how to add new items to Autocomplete.</p>
529+
<div className="sheet">
530+
<div className="form-group">
531+
<label
532+
htmlFor="clay-autocomplete-add-new-item"
533+
id="clay-autocomplete-label-add-new-item"
534+
>
535+
Categories
536+
</label>
537+
<ClayAutocomplete
538+
allowsCustomValue
539+
aria-labelledby="clay-autocomplete-label-add-new-item"
540+
defaultItems={items}
541+
id="clay-autocomplete-add-new-item"
542+
messages={{
543+
allowsCustomValue:
544+
'<span class="text-primary">{0} <i>(Add New Category)</i></span>',
545+
setAsHTML: true,
546+
}}
547+
onAddNewItem={() => {
548+
if (items.indexOf(value) < 0) {
549+
setItems(() => {
550+
const newItems = [
551+
...items,
552+
value,
553+
].sort();
554+
555+
return newItems;
556+
});
557+
}
558+
}}
559+
onChange={setValue}
560+
placeholder="Enter a category"
561+
value={value}
562+
>
563+
{items.map((item) => (
564+
<ClayAutocomplete.Item key={item}>
565+
{item}
566+
</ClayAutocomplete.Item>
567+
))}
568+
</ClayAutocomplete>
569+
</div>
570+
</div>
571+
</div>
572+
</div>
573+
);
574+
};

0 commit comments

Comments
 (0)