-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: Add TokenField to react-aria-components #10318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devongovett
wants to merge
12
commits into
main
Choose a base branch
from
tokenfield-rac
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,060
−340
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
703751b
refactor TokenField into hooks
devongovett c0b8eab
Add docs
devongovett e120029
Tokenize when pasting multiple segments
devongovett 177150d
docs updates
devongovett 0a8a21a
multiline -> allowsNewlines
devongovett 84e11b8
support visible label and description
devongovett 99fba3f
lint
devongovett d20a074
Render combobox label as span when not an input element
devongovett ae9f27c
Merge branch 'main' of github.com:adobe/react-spectrum into tokenfiel…
devongovett 6a2a467
lint
devongovett a9dce65
add starter stories
devongovett ffebffb
move isComposing state into stately
devongovett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
59 changes: 59 additions & 0 deletions
59
packages/dev/s2-docs/pages/react-aria/ComboBoxSegmentList.ts
This file contains hidden or 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,59 @@ | ||
| import {Key} from '@react-types/shared'; | ||
| import {TokenSegmentList} from 'react-aria-components/TokenField'; | ||
|
|
||
| export class ComboBoxSegmentList extends TokenSegmentList<Key> { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to maintain some of these subclasses shown in the docs as official implementations for any of these patterns? Or just rely on copy/paste for now? |
||
| getSelectedKeys(): Key[] { | ||
| return this.segments.filter(seg => seg.type === 'token').map(seg => seg.value!); | ||
| } | ||
|
|
||
| getInputValue(): string { | ||
| let segment = this.segments[this.caretPosition.index]; | ||
| return segment?.type === 'text' ? segment.text : ''; | ||
| } | ||
|
|
||
| setSelectedKeys(keys: Key[]): ComboBoxSegmentList { | ||
| let selectedKeys = this.getSelectedKeys(); | ||
| let added: Key[] = []; | ||
| for (let key of keys) { | ||
| if (!selectedKeys.includes(key)) { | ||
| added.push(key); | ||
| } | ||
| } | ||
| let removed = new Set(); | ||
| for (let key of selectedKeys) { | ||
| if (!keys.includes(key)) { | ||
| removed.add(key); | ||
| } | ||
| } | ||
|
|
||
| let value = this; | ||
| for (let key of removed) { | ||
| let index = value.segments.findIndex(seg => seg.type === 'token' && seg.value! === key); | ||
| value = value.replaceRangeWithSegments( | ||
| {index: index, offset: 0}, | ||
| {index: index, offset: value.segments[index]?.text.length ?? 0}, | ||
| [], | ||
| false | ||
| ); | ||
| } | ||
|
|
||
| if (added.length > 0) { | ||
| let segment = value.segments[value.caretPosition.index]; | ||
| value = value.replaceRangeWithSegments( | ||
| {index: value.caretPosition.index, offset: 0}, | ||
| segment?.type === 'text' | ||
| ? { | ||
| index: value.caretPosition.index, | ||
| offset: value.segments[value.caretPosition.index]?.text.length ?? 0 | ||
| } | ||
| : {index: value.caretPosition.index, offset: 0}, | ||
| added.map(value => { | ||
| return {type: 'token', text: String(value), value: value}; | ||
| }), | ||
| false | ||
| ); | ||
| } | ||
|
|
||
| return value; | ||
| } | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
packages/dev/s2-docs/pages/react-aria/TagFieldSegmentList.ts
This file contains hidden or 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,23 @@ | ||
| import {type TokenFieldSegment, TokenSegmentList} from 'react-aria-components/TokenField'; | ||
|
|
||
| export class TagFieldSegmentList extends TokenSegmentList { | ||
| tokenize(text: string): TokenFieldSegment[] { | ||
| let parts = text.split(/[, \n]/); | ||
|
|
||
| let segments: TokenFieldSegment[] = parts.map((part, i) => { | ||
| if (i === parts.length - 1 || part.length === 0) { | ||
| return {type: 'text', text: part}; | ||
| } | ||
| return {type: 'token', text: part}; | ||
| }); | ||
|
|
||
| if (parts.at(-1)?.length === 0) { | ||
| segments.pop(); | ||
| } | ||
| return segments; | ||
| } | ||
|
|
||
| toString(): string { | ||
| return this.segments.map(seg => seg.text).join(', '); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.