MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useTokenField

Function useTokenField

packages/react-aria/src/tokenfield/useTokenField.ts:102–614  ·  view source on GitHub ↗
(
  props: AriaTokenFieldProps<T>,
  state: TokenFieldState,
  ref: RefObject<HTMLDivElement | null>
)

Source from the content-addressed store, hash-verified

100 * @param state - State for the token field, as returned by `useTokenFieldState`.
101 */
102export function useTokenField<T extends TokenFieldValue = TokenFieldValue>(
103 props: AriaTokenFieldProps<T>,
104 state: TokenFieldState,
105 ref: RefObject<HTMLDivElement | null>
106): TokenFieldAria {
107 let {
108 role = 'textbox',
109 allowsNewlines: multiline = false,
110 isReadOnly = false,
111 isDisabled = false,
112 'aria-details': ariaDetails
113 } = props;
114
115 let {value} = state;
116 let {locale} = useLocale();
117 let graphemeSegmenter = useMemo(
118 () => new Intl.Segmenter(locale, {granularity: 'grapheme'}),
119 [locale]
120 );
121 let wordSegmenter = useMemo(() => new Intl.Segmenter(locale, {granularity: 'word'}), [locale]);
122
123 let dropPosition = useRef<Position | null>(null);
124 let transferredData = useRef<TokenFieldSegment[] | null>(null);
125
126 let nextValue = useRef<TokenFieldValue | null>(null);
127 let apply = (fn: (value: TokenFieldValue) => TokenFieldValue) => {
128 state.setValue(value => {
129 let newValue = fn(value);
130 nextValue.current = newValue;
131 return newValue;
132 });
133 };
134
135 // Composition events are not cancelable. The browser will mutate the DOM, making it out of sync with React.
136 // To account for this, we prevent React from re-rendering during composition, and track DOM mutations performed
137 // by the browser. When composition ends, we revert the DOM to its original state, and re-render with React.
138 // Mutating the DOM in any way during composition breaks the IME, causing composition to end unexpectedly.
139 // During composition, we still emit updates via onChange to ensure that things like autocomplete work,
140 // but we don't actually re-render to the DOM unless the value changes from what we expect (e.g. inserting a completion).
141 let mutationTracker = useMutationTracker(ref);
142 let startComposition = useCallback(() => {
143 mutationTracker.start();
144 state.setComposing(true);
145 }, [state, mutationTracker]);
146 let stopComposition = useCallback(() => {
147 mutationTracker.stop();
148 state.setComposing(false);
149 }, [state, mutationTracker]);
150
151 useEvent(ref, 'compositionstart', () => {
152 startComposition();
153
154 let range = window.getSelection()?.getRangeAt(0);
155 if (range) {
156 let [start, end] = rangeToPositions(ref.current!, range);
157
158 // Normalize the range to ensure it is not inside a token, otherwise the browser
159 // will attempt to insert the composed text into the token instead of replacing it.

Callers 1

TokenField.tsxFile · 0.90

Calls 15

useLocaleFunction · 0.90
useEventFunction · 0.90
getActiveElementFunction · 0.90
getOwnerDocumentFunction · 0.90
useKeyboardFunction · 0.90
useFocusableFunction · 0.90
useFieldFunction · 0.90
setInteractionModalityFunction · 0.90
mergePropsFunction · 0.90
useMutationTrackerFunction · 0.85
rangeToPositionsFunction · 0.85
createDOMRangeFunction · 0.85

Tested by

no test coverage detected