Repository: adobe/react-spectrum
File: packages/@react-aria/datepicker/src/useDateSegment.ts (TypeScript)

Code before:
              break;
          }

          if (segment.value !== undefined && segment.value >= 12 && numberValue > 1) {
            numberValue += 12;
          }
        } else if (segment.maxValue !== undefined && numberValue > segment.maxValue) {
          segmentValue = parser.parse(key);
        }

        if (isNaN(numberValue)) {
          return;
        }

        let shouldSetValue = segmentValue !== 0 || allowsZero;
        if (shouldSetValue) {
          state.setSegment(segment.type, segmentValue);
        }

        if (segment.maxValue !== undefined && (Number(numberValue + '0') > segment.maxValue || newValue.length >= String(segment.maxValue).length)) {
          enteredKeys.current = '';
          if (shouldSetValue) {
            focusManager.focusNext();
          }
        } else {
          enteredKeys.current = newValue;
        }
        break;
      }
    }
  };

  let onFocus = () => {
    enteredKeys.current = '';
    if (ref.current) {
      scrollIntoViewport(ref.current, {containingElement: getScrollParent(ref.current)});
    }

    // Collapse selection to start or Chrome won't fire input events.
    let selection = window.getSelection();
    selection?.collapse(ref.current);
  };

  let documentRef = useRef(typeof document !== 'undefined' ? document : null);
  useEvent(documentRef, 'selectionchange', () => {
    // Enforce that the selection is collapsed when inside a date segment.
    // Otherwise, when tapping on a segment in Android Chrome and then entering text,
    // composition events will be fired that break the DOM structure and crash the page.
    let selection = window.getSelection();
    if (selection?.anchorNode && ref.current?.contains(selection?.anchorNode)) {
      selection.collapse(ref.current);

Review comment (suggestion):
If we wanna constraint this behavior to just the year field, can do something like 
```
} else if (!(segment.type === 'year' && !shouldSetValue)) {
``` 
instead. Would make it inconsistent with the other fields but could potentially be argued for since people don't really type leading zeros when entering years 

Code after:
              break;
          }

          if (segment.value !== undefined && segment.value >= 12 && numberValue > 1) {
            numberValue += 12;
          }
        } else if (segment.maxValue !== undefined && numberValue > segment.maxValue) {
          segmentValue = parser.parse(key);
        }

        if (isNaN(numberValue)) {
          return;
        }

        let shouldSetValue = segmentValue !== 0 || allowsZero;
        if (shouldSetValue) {
          state.setSegment(segment.type, segmentValue);
        }

        if (segment.maxValue !== undefined && (Number(numberValue + '0') > segment.maxValue || newValue.length >= String(segment.maxValue).length)) {
          enteredKeys.current = '';
          if (shouldSetValue) {
            focusManager.focusNext();
          }
        } else if (shouldSetValue) {
          // Don't accept leading zeros except for fields that accept 0 as a entire value (aka 00 for minutes/seconds/etc)
          enteredKeys.current = newValue;
        }
        break;
      }
    }
  };

  let onFocus = () => {
    enteredKeys.current = '';
    if (ref.current) {
      scrollIntoViewport(ref.current, {containingElement: getScrollParent(ref.current)});
    }

    // Collapse selection to start or Chrome won't fire input events.
    let selection = window.getSelection();
    selection?.collapse(ref.current);
  };

  let documentRef = useRef(typeof document !== 'undefined' ? document : null);
  useEvent(documentRef, 'selectionchange', () => {
    // Enforce that the selection is collapsed when inside a date segment.
    // Otherwise, when tapping on a segment in Android Chrome and then entering text,
    // composition events will be fired that break the DOM structure and crash the page.
    let selection = window.getSelection();
    if (selection?.anchorNode && ref.current?.contains(selection?.anchorNode)) {