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

Function onInput

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

Source from the content-addressed store, hash-verified

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

Callers 1

useDateSegmentFunction · 0.85

Calls 5

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

Tested by

no test coverage detected