Skip to content

Commit 0900415

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

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

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

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ export const CustomItem = () => {
169169
<div className="row">
170170
<div className="col-md-5">
171171
<div className="sheet">
172+
<div className="alert alert-info">
173+
This is an example of adding custom content to
174+
`Autocomplete.Item`.
175+
</div>
172176
<div className="form-group">
173177
<label
174178
htmlFor="clay-autocomplete-2"
@@ -502,3 +506,105 @@ export const AsyncData = () => {
502506
</div>
503507
);
504508
};
509+
510+
export const AddNewItem = () => {
511+
const [items, setItems] = useState([
512+
'Things in a Medicine Cabinet',
513+
'Cars',
514+
'Words Ending in -S',
515+
'Electronic Gadgets',
516+
'Nicknames',
517+
'Words Associated With Money',
518+
'Things in a Grocery Store',
519+
'Product Names',
520+
'Dairy Products',
521+
'Childrens Books',
522+
'Things You See at the Zoo',
523+
]);
524+
525+
const [items2, setItems2] = useState([
526+
'Footwear',
527+
'Historical Figures',
528+
'Items in a Vending Machine',
529+
'Reasons to Call 911',
530+
"Something You're Afraid Of",
531+
'Sports Played Indoors',
532+
'Things You Do at Work',
533+
'Things You Plug In',
534+
'Things You Save Up to Buy',
535+
]);
536+
537+
const [value2, setValue2] = useState('');
538+
539+
return (
540+
<div className="row">
541+
<div className="col-md-5">
542+
<div className="sheet">
543+
<div className="alert alert-info">
544+
This controlled component is an example of adding
545+
additional categories to the Autocomplete List.
546+
</div>
547+
<div className="form-group">
548+
<label
549+
htmlFor="clay-autocomplete-add-new-item"
550+
id="clay-autocomplete-label-add-new-item"
551+
>
552+
Categories (Controlled Component)
553+
</label>
554+
<ClayAutocomplete
555+
allowsCustomValue
556+
aria-labelledby="clay-autocomplete-label-add-new-item"
557+
id="clay-autocomplete-add-new-item"
558+
items={items}
559+
onAddNewItem={(value) => {
560+
if (items.indexOf(value) < 0) {
561+
setItems([...items, value].sort());
562+
}
563+
}}
564+
placeholder="Enter a category"
565+
>
566+
{items.map((item) => (
567+
<ClayAutocomplete.Item key={item}>
568+
{item}
569+
</ClayAutocomplete.Item>
570+
))}
571+
</ClayAutocomplete>
572+
</div>
573+
<div className="form-group">
574+
<label
575+
htmlFor="clay-autocomplete-add-new-item2"
576+
id="clay-autocomplete-label-add-new-item2"
577+
>
578+
Categories (Uncontrolled Component)
579+
</label>
580+
<ClayAutocomplete
581+
allowsCustomValue
582+
aria-labelledby="clay-autocomplete-label-add-new-item2"
583+
defaultItems={items2}
584+
id="clay-autocomplete-add-new-item2"
585+
messages={{
586+
addCustomValue:
587+
'<span class="text-primary">{0} <i>(Add New Category)</i></span>',
588+
setAsHTML: true,
589+
}}
590+
onAddNewItem={(value) => {
591+
if (items2.indexOf(value) < 0) {
592+
setItems2([...items2, value].sort());
593+
}
594+
}}
595+
onChange={setValue2}
596+
placeholder="Enter a category"
597+
value={value2}
598+
>
599+
{items2.map((item) => (
600+
<ClayAutocomplete.Item key={item}>
601+
{item}
602+
</ClayAutocomplete.Item>
603+
))}
604+
</ClayAutocomplete>
605+
</div>
606+
</div>
607+
</div>
608+
</div>
609+
);
610+
};

0 commit comments

Comments
 (0)