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

Function onInput

packages/react-aria/src/datepicker/useDateSegment.ts:185–246  ·  view source on GitHub ↗
(key: string)

Source from the content-addressed store, hash-verified

183 }, [eraFormatter, state.calendar, segment.type]);
184
185 let onInput = (key: string) => {
186 if (state.isDisabled || state.isReadOnly) {
187 return;
188 }
189
190 let newValue = enteredKeys.current + key;
191
192 switch (segment.type) {
193 case 'dayPeriod':
194 if (matchesDayPeriod(am, key, startsWith)) {
195 state.setSegment('dayPeriod', 0);
196 } else if (matchesDayPeriod(pm, key, startsWith)) {
197 state.setSegment('dayPeriod', 1);
198 } else {
199 break;
200 }
201 focusManager.focusNext();
202 break;
203 case 'era': {
204 let matched = eras.find(e => startsWith(e.formatted, key));
205 if (matched) {
206 state.setSegment('era', matched.era);
207 focusManager.focusNext();
208 }
209 break;
210 }
211 case 'day':
212 case 'hour':
213 case 'minute':
214 case 'second':
215 case 'month':
216 case 'year': {
217 if (!parser.isValidPartialNumber(newValue)) {
218 return;
219 }
220
221 let numberValue = parser.parse(newValue);
222 let segmentValue = numberValue;
223 if (segment.maxValue !== undefined && numberValue > segment.maxValue) {
224 segmentValue = parser.parse(key);
225 }
226
227 if (isNaN(numberValue)) {
228 return;
229 }
230
231 state.setSegment(segment.type, segmentValue);
232
233 if (
234 segment.maxValue !== undefined &&
235 (Number(numberValue + '0') > segment.maxValue ||
236 newValue.length >= String(segment.maxValue).length)
237 ) {
238 enteredKeys.current = '';
239 focusManager.focusNext();
240 } else {
241 enteredKeys.current = newValue;
242 }

Callers 1

useDateSegmentFunction · 0.85

Calls 6

matchesDayPeriodFunction · 0.85
StringFunction · 0.85
setSegmentMethod · 0.80
focusNextMethod · 0.65
isValidPartialNumberMethod · 0.45
parseMethod · 0.45

Tested by

no test coverage detected