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

Function onInput

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

Source from the content-addressed store, hash-verified

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

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