-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(react-tag-picker): support TagPicker usage without TagPickerList
- Loading branch information
1 parent
cf68d19
commit 66bf521
Showing
4 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-tag-picker-87e138fe-62c9-4889-8ff8-a472ae227f11.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "minor", | ||
"comment": "feature: support TagPicker usage without TagPickerList", | ||
"packageName": "@fluentui/react-tag-picker", | ||
"email": "bernardo.sunderhus@gmail.com", | ||
"dependentChangeType": "patch" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...es/react-components/react-tag-picker/stories/src/TagPicker/TagPickerOnlyInput.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import * as React from 'react'; | ||
import { | ||
TagPicker, | ||
TagPickerInput, | ||
TagPickerControl, | ||
TagPickerProps, | ||
TagPickerGroup, | ||
} from '@fluentui/react-components'; | ||
import { Tag, Avatar, Field } from '@fluentui/react-components'; | ||
|
||
export const OnlyInput = () => { | ||
const [selectedOptions, setSelectedOptions] = React.useState<string[]>([]); | ||
|
||
const onOptionSelect: TagPickerProps['onOptionSelect'] = (_, data) => { | ||
setSelectedOptions(data.selectedOptions); | ||
}; | ||
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setInputValue(event.currentTarget.value); | ||
}; | ||
const handleKeyDown = (event: React.KeyboardEvent) => { | ||
if (event.key === 'Enter' && inputValue) { | ||
setInputValue(''); | ||
setSelectedOptions(curr => [...curr, inputValue]); | ||
} | ||
}; | ||
|
||
const [inputValue, setInputValue] = React.useState(''); | ||
|
||
return ( | ||
<Field label="Add Employees" style={{ maxWidth: 400 }}> | ||
<TagPicker onOptionSelect={onOptionSelect} selectedOptions={selectedOptions}> | ||
<TagPickerControl expandIcon={null}> | ||
<TagPickerGroup> | ||
{selectedOptions.map((option, index) => ( | ||
<Tag | ||
key={index} | ||
shape="rounded" | ||
media={<Avatar aria-hidden name={option} color="colorful" />} | ||
value={option} | ||
> | ||
{option} | ||
</Tag> | ||
))} | ||
</TagPickerGroup> | ||
<TagPickerInput | ||
value={inputValue} | ||
onChange={handleChange} | ||
onKeyDown={handleKeyDown} | ||
aria-label="Add Employees" | ||
/> | ||
</TagPickerControl> | ||
</TagPicker> | ||
</Field> | ||
); | ||
}; | ||
|
||
OnlyInput.parameters = { | ||
docs: { | ||
description: { | ||
story: ` | ||
You can use the \`TagPicker\` without the list of options. This is useful when you want to allow users to input their own tags. All you have to do is control the \`TagPickerInput\` value and handle the \`onKeyDown\` event to add the tag to the \`TagPicker\` when the user presses the Enter key. | ||
`, | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters