@klinking/colander 3.1.0-alpha.1 → 3.1.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/dist/formats/date.cjs +1 -1
  2. package/dist/formats/date.mjs +1 -1
  3. package/dist/formats/object.cjs +1 -1
  4. package/dist/formats/object.mjs +1 -1
  5. package/dist/formats/plain-date-time.cjs +1 -1
  6. package/dist/formats/plain-date-time.mjs +1 -1
  7. package/dist/formats/plain-date.cjs +1 -1
  8. package/dist/formats/plain-date.mjs +1 -1
  9. package/dist/formats/plain-month-day.cjs +1 -1
  10. package/dist/formats/plain-month-day.mjs +1 -1
  11. package/dist/formats/plain-year-month.cjs +1 -1
  12. package/dist/formats/plain-year-month.mjs +1 -1
  13. package/dist/formats/zoned-date-time.cjs +1 -1
  14. package/dist/formats/zoned-date-time.mjs +1 -1
  15. package/dist/index.cjs +1 -1
  16. package/dist/index.mjs +1 -1
  17. package/dist/{weeks-navigation-Bg0nZLgh.mjs → weeks-navigation-DEaASUAh.mjs} +125 -120
  18. package/dist/{weeks-navigation-CJzhxbgj.cjs → weeks-navigation-DvryOSWF.cjs} +125 -120
  19. package/package.json +1 -1
  20. package/src/calendar-provider.test.tsx +87 -0
  21. package/src/calendar-provider.tsx +65 -16
  22. package/src/day-cell.tsx +9 -15
  23. package/src/drag-handle.tsx +2 -2
  24. package/src/grid-header.tsx +3 -3
  25. package/src/grid.tsx +20 -61
  26. package/src/month-view.tsx +25 -2
  27. package/src/navigation.tsx +3 -2
  28. package/src/range-preview.tsx +3 -5
  29. package/src/selected-range.tsx +3 -5
  30. package/src/view-context.test.tsx +126 -0
  31. package/src/view-context.ts +43 -0
  32. package/src/week-number.tsx +3 -3
  33. package/src/weeks-navigation.tsx +3 -3
  34. package/src/weeks-view-state.ts +23 -2
  35. package/src/weeks-view.test.tsx +115 -13
  36. package/src/weeks-view.tsx +22 -51
@@ -923,6 +923,21 @@ function CalendarProvider(props) {
923
923
  rawToPlain,
924
924
  sortDates
925
925
  ]);
926
+ const warnedMalformedRef = (0, react.useRef)(false);
927
+ (0, react.useEffect)(() => {
928
+ if (!(valueProp != null && (isRange && !isDateRange(valueProp) || isMultiple && !Array.isArray(valueProp)))) {
929
+ warnedMalformedRef.current = false;
930
+ return;
931
+ }
932
+ if (warnedMalformedRef.current) return;
933
+ warnedMalformedRef.current = true;
934
+ if (isRange) console.warn(`[DatePicker] selectionMode="range" expects \`value\` to be a { start, end } DateRange or null, but received ${Array.isArray(valueProp) ? "an array" : typeof valueProp}. Ignoring it and falling back to uncontrolled.`);
935
+ else console.warn(`[DatePicker] selectionMode="multiple" expects \`value\` to be an array or null, but received ${typeof valueProp}. Ignoring it and falling back to uncontrolled.`);
936
+ }, [
937
+ valueProp,
938
+ isRange,
939
+ isMultiple
940
+ ]);
926
941
  const [internalDates, setInternalDates] = (0, react.useState)((0, react.useMemo)(() => {
927
942
  if (isMultiple) {
928
943
  if (multipleDefault) return sortDates(multipleDefault.map(rawToPlain));
@@ -1092,22 +1107,35 @@ function CalendarProvider(props) {
1092
1107
  plainToFormatValue,
1093
1108
  isControlled
1094
1109
  ]);
1110
+ const warnedOutOfBoundsRef = (0, react.useRef)(false);
1095
1111
  (0, react.useEffect)(() => {
1096
1112
  if (isRange || isMultiple) return;
1097
1113
  const start = committedStart;
1098
- if (!start) return;
1099
- if (minValue && T.PlainDate.compare(start, minValue) < 0 || maxValue && T.PlainDate.compare(start, maxValue) > 0) {
1100
- const prev = currentSingleFormatted();
1101
- commitSelection({
1102
- newDates: [],
1103
- fireCallback: (onValueChange) => {
1104
- onValueChange?.(null, {
1105
- date: void 0,
1106
- previous: prev
1107
- });
1108
- }
1109
- });
1114
+ if (!start) {
1115
+ warnedOutOfBoundsRef.current = false;
1116
+ return;
1117
+ }
1118
+ if (!(minValue && T.PlainDate.compare(start, minValue) < 0 || maxValue && T.PlainDate.compare(start, maxValue) > 0)) {
1119
+ warnedOutOfBoundsRef.current = false;
1120
+ return;
1121
+ }
1122
+ if (isControlled) {
1123
+ if (!warnedOutOfBoundsRef.current) {
1124
+ warnedOutOfBoundsRef.current = true;
1125
+ console.warn(`[DatePicker] The controlled \`value\` is outside the min/max range. It stays selected (rendered disabled) until you update \`value\`; the component will not clear it for you.`);
1126
+ }
1127
+ return;
1110
1128
  }
1129
+ const prev = currentSingleFormatted();
1130
+ commitSelection({
1131
+ newDates: [],
1132
+ fireCallback: (onValueChange) => {
1133
+ onValueChange?.(null, {
1134
+ date: void 0,
1135
+ previous: prev
1136
+ });
1137
+ }
1138
+ });
1111
1139
  }, [
1112
1140
  minValue,
1113
1141
  maxValue,
@@ -1116,7 +1144,8 @@ function CalendarProvider(props) {
1116
1144
  currentSingleFormatted,
1117
1145
  T,
1118
1146
  isRange,
1119
- isMultiple
1147
+ isMultiple,
1148
+ isControlled
1120
1149
  ]);
1121
1150
  const onSelect = (0, react.useCallback)((date) => {
1122
1151
  const result = computeSelectionUpdate({
@@ -1296,6 +1325,22 @@ function useViewContext() {
1296
1325
  if (!ctx) throw new Error("useViewContext must be used within MonthView or WeeksView");
1297
1326
  return ctx;
1298
1327
  }
1328
+ /** @internal Context for {@link ViewStableContextValue}. */
1329
+ const ViewStableContext = (0, react.createContext)(null);
1330
+ /** @internal Context for {@link ViewStateContextValue}. */
1331
+ const ViewStateContext = (0, react.createContext)(null);
1332
+ /** Returns the nearest view's stable values. */
1333
+ function useViewStable() {
1334
+ const ctx = (0, react.useContext)(ViewStableContext);
1335
+ if (!ctx) throw new Error("useViewStable must be used within MonthView or WeeksView");
1336
+ return ctx;
1337
+ }
1338
+ /** Returns the nearest view's volatile state. */
1339
+ function useViewState() {
1340
+ const ctx = (0, react.useContext)(ViewStateContext);
1341
+ if (!ctx) throw new Error("useViewState must be used within MonthView or WeeksView");
1342
+ return ctx;
1343
+ }
1299
1344
  //#endregion
1300
1345
  //#region src/month-view.tsx
1301
1346
  function MonthViewRoot(props) {
@@ -1617,13 +1662,27 @@ function MonthViewRoot(props) {
1617
1662
  tabTargetDate,
1618
1663
  gridHasFocus
1619
1664
  ]);
1665
+ const viewStableCtx = (0, react.useMemo)(() => ({
1666
+ gridFocusedRef,
1667
+ outsideDays
1668
+ }), [outsideDays]);
1669
+ const viewStateCtx = (0, react.useMemo)(() => ({
1670
+ currentDateTime,
1671
+ rootState
1672
+ }), [currentDateTime, rootState]);
1620
1673
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(MonthViewStableContext.Provider, {
1621
1674
  value: stableCtx,
1622
1675
  children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(MonthViewStateContext.Provider, {
1623
1676
  value: stateCtx,
1624
- children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ViewContext.Provider, {
1625
- value: viewCtx,
1626
- children
1677
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ViewStableContext.Provider, {
1678
+ value: viewStableCtx,
1679
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ViewStateContext.Provider, {
1680
+ value: viewStateCtx,
1681
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ViewContext.Provider, {
1682
+ value: viewCtx,
1683
+ children
1684
+ })
1685
+ })
1627
1686
  })
1628
1687
  })
1629
1688
  });
@@ -1659,7 +1718,7 @@ const GridMonthContext = (0, react.createContext)(null);
1659
1718
  //#region src/grid-header.tsx
1660
1719
  function useGridHeaderCellState(index) {
1661
1720
  const { locale, temporal: T, weekStartDay } = useCalendarStable();
1662
- const { rootState } = useMonthViewState();
1721
+ const { rootState } = useViewState();
1663
1722
  const weekdayNames = (0, react.useMemo)(() => getWeekdayNames(locale, T, weekStartDay), [
1664
1723
  locale,
1665
1724
  T,
@@ -1735,7 +1794,7 @@ function GridHeaderCellFn(props, ref) {
1735
1794
  const GridHeaderCell = (0, react.forwardRef)(GridHeaderCellFn);
1736
1795
  function GridHeaderFn(props, ref) {
1737
1796
  const { render, children, ...otherProps } = props;
1738
- const { rootState } = useMonthViewState();
1797
+ const { rootState } = useViewState();
1739
1798
  const state = (0, react.useMemo)(() => ({ root: rootState }), [rootState]);
1740
1799
  return (0, _base_ui_react_use_render.useRender)({
1741
1800
  defaultTagName: "thead",
@@ -2445,9 +2504,9 @@ function DayCellTemplate(props) {
2445
2504
  const { selectedDates, rangeStart, rangeEnd, previewStart, previewEnd } = useCalendarState();
2446
2505
  const viewCtx = useViewContext();
2447
2506
  const { focusedDate, tabTargetDate } = viewCtx;
2448
- const monthStable = (0, react.useContext)(MonthViewStableContext);
2449
- const { rootState, weeks, currentMonth: currentDateTime } = useMonthViewState();
2450
- const outsideDays = monthStable?.outsideDays ?? "enabled";
2507
+ const { rootState, currentDateTime } = useViewState();
2508
+ const { outsideDays } = useViewStable();
2509
+ const monthState = (0, react.useContext)(MonthViewStateContext);
2451
2510
  const isWeeksView = viewCtx.viewType === "weeks";
2452
2511
  const cellMonth = isWeeksView ? null : weekData?.gridMonth ?? currentDateTime;
2453
2512
  const { orientation } = (0, react.useContext)(GridContext);
@@ -2463,7 +2522,7 @@ function DayCellTemplate(props) {
2463
2522
  _derivedState: derived
2464
2523
  });
2465
2524
  }
2466
- const days = weekData ? weekData.days : weeks.flat();
2525
+ const days = weekData ? weekData.days : monthState?.weeks.flat() ?? [];
2467
2526
  const perWeek = weekData != null;
2468
2527
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: days.map((day, i) => {
2469
2528
  const daysInWeek = day.daysInWeek;
@@ -2483,7 +2542,7 @@ function DayButtonInnerFn(props, ref) {
2483
2542
  const { render, date, _derivedState, ...otherProps } = props;
2484
2543
  const { onSelect, locale, readOnly, setHoveredDate } = useCalendarStable();
2485
2544
  const { setFocusedDate } = useViewContext();
2486
- const gridFocusedRef = (0, react.useContext)(MonthViewStableContext).gridFocusedRef;
2545
+ const { gridFocusedRef } = useViewStable();
2487
2546
  const internalRef = (0, react.useRef)(null);
2488
2547
  const isHidden = _derivedState?.hidden ?? false;
2489
2548
  const isFocused = _derivedState?.focused ?? false;
@@ -2567,9 +2626,8 @@ function DayButtonFallbackFn(props, ref) {
2567
2626
  const { selectedDates, rangeStart, rangeEnd, previewStart, previewEnd } = useCalendarState();
2568
2627
  const viewCtx = useViewContext();
2569
2628
  const { focusedDate, tabTargetDate } = viewCtx;
2570
- const monthStable = (0, react.useContext)(MonthViewStableContext);
2571
- const { rootState, currentMonth: currentDateTime } = useMonthViewState();
2572
- const outsideDays = monthStable?.outsideDays ?? "enabled";
2629
+ const { rootState, currentDateTime } = useViewState();
2630
+ const { outsideDays } = useViewStable();
2573
2631
  const cellMonth = viewCtx.viewType === "weeks" ? {
2574
2632
  year: date.year,
2575
2633
  month: date.month
@@ -2685,17 +2743,15 @@ const gridStateAttributesMapping = {
2685
2743
  * delegates to the appropriate internal renderer.
2686
2744
  */
2687
2745
  function GridFn(props, ref) {
2688
- const monthState = (0, react.useContext)(MonthViewStableContext);
2689
- const weeksState = (0, react.useContext)(WeeksViewStateContext);
2690
- if (monthState) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(MonthGrid, {
2746
+ const view = (0, react.useContext)(ViewContext);
2747
+ if (!view) throw new Error("Grid must be used inside MonthView.Root or WeeksView.Root.");
2748
+ return view.viewType === "weeks" ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(WeeksViewGrid, {
2691
2749
  ref,
2692
2750
  ...props
2693
- });
2694
- if (weeksState) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(WeeksViewGrid, {
2751
+ }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(MonthGrid, {
2695
2752
  ref,
2696
2753
  ...props
2697
2754
  });
2698
- throw new Error("Grid must be used inside MonthView.Root or WeeksView.Root.");
2699
2755
  }
2700
2756
  const Grid = (0, react.forwardRef)(GridFn);
2701
2757
  function MonthGridFn(props, ref) {
@@ -2802,7 +2858,7 @@ function WeeksViewGridFn(props, ref) {
2802
2858
  const weeksStable = useWeeksViewStable();
2803
2859
  const weeksState = useWeeksViewState();
2804
2860
  const { gridFocusedRef } = weeksStable;
2805
- const { gridLabelIds, weeks, currentDateTime } = weeksState;
2861
+ const { gridLabelIds, weeks } = weeksState;
2806
2862
  const handleKeyDown = useWeeksGridKeyboard();
2807
2863
  const gridRef = (0, react.useRef)(null);
2808
2864
  (0, react.useEffect)(() => {
@@ -2873,47 +2929,16 @@ function WeeksViewGridFn(props, ref) {
2873
2929
  props: (0, _base_ui_react_merge_props.mergeProps)(defaultProps, otherProps)
2874
2930
  });
2875
2931
  const orientationCtx = (0, react.useMemo)(() => ({ orientation: resolvedOrientation }), [resolvedOrientation]);
2876
- const monthViewStableShim = (0, react.useMemo)(() => ({
2877
- numberOfMonths: 1,
2878
- fixedWeeks: false,
2879
- outsideDays: "enabled",
2880
- outOfRangeBehavior: "unbounded",
2881
- goNextMonth: () => {},
2882
- goPrevMonth: () => {},
2883
- setGridLabelId: () => {},
2884
- gridFocusedRef
2885
- }), [gridFocusedRef]);
2886
- const monthViewStateShim = (0, react.useMemo)(() => ({
2887
- currentMonth: {
2888
- year: currentDateTime.year,
2889
- month: currentDateTime.month
2890
- },
2891
- weeks: [],
2892
- allMonths: [],
2893
- currentDateTime,
2894
- gridLabelIds,
2895
- rootState: weeksState.rootState
2896
- }), [
2897
- currentDateTime,
2898
- gridLabelIds,
2899
- weeksState.rootState
2900
- ]);
2901
- return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(MonthViewStableContext.Provider, {
2902
- value: monthViewStableShim,
2903
- children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(MonthViewStateContext.Provider, {
2904
- value: monthViewStateShim,
2905
- children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GridContext.Provider, {
2906
- value: orientationCtx,
2907
- children: el
2908
- })
2909
- })
2932
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GridContext.Provider, {
2933
+ value: orientationCtx,
2934
+ children: el
2910
2935
  });
2911
2936
  }
2912
2937
  const WeeksViewGrid = (0, react.forwardRef)(WeeksViewGridFn);
2913
2938
  const gridBodyStateAttributesMapping = { root: () => null };
2914
2939
  /** Table body wrapping the week rows. Renders a `<tbody>` by default. */
2915
2940
  function GridBodyFn(props, ref) {
2916
- if ((0, react.useContext)(WeeksViewStateContext)) {
2941
+ if ((0, react.useContext)(ViewContext)?.viewType === "weeks") {
2917
2942
  const { children, className, style } = props;
2918
2943
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(WeeksGrid, {
2919
2944
  className,
@@ -2929,7 +2954,7 @@ function GridBodyFn(props, ref) {
2929
2954
  const GridBody = (0, react.forwardRef)(GridBodyFn);
2930
2955
  function MonthGridBodyFn(props, ref) {
2931
2956
  const { render, ...otherProps } = props;
2932
- const { rootState } = useMonthViewState();
2957
+ const { rootState } = useViewState();
2933
2958
  const state = (0, react.useMemo)(() => ({ root: rootState }), [rootState]);
2934
2959
  const defaultProps = { children: props.children ?? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(WeekTemplate, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DayCellTemplate, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DayButton, {}) }) }) };
2935
2960
  const { children: _children, ...restOtherProps } = otherProps;
@@ -2951,7 +2976,7 @@ const weekInstanceStateAttributesMapping = {
2951
2976
  function WeekInstance(props) {
2952
2977
  const { render, ...otherProps } = props;
2953
2978
  const weekData = (0, react.useContext)(WeekDataContext);
2954
- const { rootState } = useMonthViewState();
2979
+ const { rootState } = useViewState();
2955
2980
  return (0, _base_ui_react_use_render.useRender)({
2956
2981
  defaultTagName: "tr",
2957
2982
  render,
@@ -2976,8 +3001,8 @@ function WeekInstance(props) {
2976
3001
  function WeekTemplate(props) {
2977
3002
  const outerWeekData = (0, react.useContext)(WeekDataContext);
2978
3003
  const gridMonthCtx = (0, react.useContext)(GridMonthContext);
2979
- const { weeks: defaultWeeks } = useMonthViewState();
2980
- const weeks = gridMonthCtx?.weeks ?? defaultWeeks;
3004
+ const monthState = (0, react.useContext)(MonthViewStateContext);
3005
+ const weeks = gridMonthCtx?.weeks ?? monthState?.weeks ?? [];
2981
3006
  const gridMonth = (0, react.useMemo)(() => gridMonthCtx ? {
2982
3007
  year: gridMonthCtx.year,
2983
3008
  month: gridMonthCtx.month
@@ -3020,7 +3045,7 @@ function DateStringFn(props, ref) {
3020
3045
  const { render, locales, options, ...otherProps } = props;
3021
3046
  const { locale, temporal: T, timeZone } = useCalendarStable();
3022
3047
  const { selected } = useCalendarState();
3023
- const { currentDateTime, rootState } = useMonthViewState();
3048
+ const { currentDateTime, rootState } = useViewState();
3024
3049
  const selectedZdt = selectedToZdt(selected, timeZone, T);
3025
3050
  const displayDate = selectedZdt ? zdtToNativeDate(selectedZdt) : new Date(currentDateTime.year, currentDateTime.month - 1, 1);
3026
3051
  const formatted = displayDate.toLocaleDateString(locales ?? locale, options);
@@ -3059,7 +3084,7 @@ function TimeStringFn(props, ref) {
3059
3084
  const { render, locales, options, ...otherProps } = props;
3060
3085
  const { locale, temporal: T, timeZone } = useCalendarStable();
3061
3086
  const { selected } = useCalendarState();
3062
- const { rootState } = useMonthViewState();
3087
+ const { rootState } = useViewState();
3063
3088
  const selZdt = selectedToZdt(selected, timeZone, T);
3064
3089
  const displayDate = selZdt ? zdtToNativeDate(selZdt) : zdtToNativeDate(T.Now.zonedDateTimeISO(timeZone));
3065
3090
  const mergedOptions = {
@@ -3325,9 +3350,8 @@ function RangeSelectedInnerFn(props, ref) {
3325
3350
  const { orientation } = (0, react.useContext)(GridContext);
3326
3351
  const { temporal: T } = useCalendarStable();
3327
3352
  const { rangeStart, rangeEnd } = useCalendarState();
3328
- const monthStable = (0, react.useContext)(MonthViewStableContext);
3329
- const { rootState } = useMonthViewState();
3330
- const outsideDays = monthStable?.outsideDays ?? "enabled";
3353
+ const { rootState } = useViewState();
3354
+ const { outsideDays } = useViewStable();
3331
3355
  const days = weekData?.days ?? [];
3332
3356
  const gridMonth = weekData?.gridMonth;
3333
3357
  const info = (0, react.useMemo)(() => computeClippedRangeInfo(days, rangeStart, rangeEnd, T, outsideDays, gridMonth), [
@@ -3400,9 +3424,8 @@ function RangePreviewInnerFn(props, ref) {
3400
3424
  const { orientation } = (0, react.useContext)(GridContext);
3401
3425
  const { temporal: T } = useCalendarStable();
3402
3426
  const { previewStart, previewEnd } = useCalendarState();
3403
- const monthStable = (0, react.useContext)(MonthViewStableContext);
3404
- const { rootState } = useMonthViewState();
3405
- const outsideDays = monthStable?.outsideDays ?? "enabled";
3427
+ const { rootState } = useViewState();
3428
+ const { outsideDays } = useViewStable();
3406
3429
  const days = weekData?.days ?? [];
3407
3430
  const gridMonth = weekData?.gridMonth;
3408
3431
  const info = (0, react.useMemo)(() => computeClippedRangeInfo(days, previewStart, previewEnd, T, outsideDays, gridMonth), [
@@ -3464,7 +3487,7 @@ const dragHandleStateAttributesMapping = {
3464
3487
  function useDragHandle(edge, { dragging: draggingProp }) {
3465
3488
  const { temporal: T, setHoveredDate, readOnly, disabled } = useCalendarStable();
3466
3489
  const { rangeStart, rangeEnd } = useCalendarState();
3467
- const { rootState } = useMonthViewState();
3490
+ const { rootState } = useViewState();
3468
3491
  const cellData = (0, react.useContext)(DayCellDataContext);
3469
3492
  const { orientation } = (0, react.useContext)(GridContext);
3470
3493
  const date = cellData?.date;
@@ -3547,7 +3570,7 @@ const weekNumberCellStateAttributesMapping = {
3547
3570
  function WeekNumberCellFn(props, ref) {
3548
3571
  const { render, ...otherProps } = props;
3549
3572
  const weekData = (0, react.useContext)(WeekDataContext);
3550
- const { rootState } = useMonthViewState();
3573
+ const { rootState } = useViewState();
3551
3574
  const { temporal: T } = useCalendarStable();
3552
3575
  const days = weekData?.days ?? [];
3553
3576
  const weekNumber = (0, react.useMemo)(() => {
@@ -3585,7 +3608,7 @@ const WeekNumberCell = (0, react.forwardRef)(WeekNumberCellFn);
3585
3608
  const weekNumberHeaderStateAttributesMapping = { root: () => null };
3586
3609
  function WeekNumberHeaderFn(props, ref) {
3587
3610
  const { render, ...otherProps } = props;
3588
- const { rootState } = useMonthViewState();
3611
+ const { rootState } = useViewState();
3589
3612
  const state = (0, react.useMemo)(() => ({ root: rootState }), [rootState]);
3590
3613
  return (0, _base_ui_react_use_render.useRender)({
3591
3614
  defaultTagName: "th",
@@ -4182,6 +4205,14 @@ function useWeeksViewRootState(props) {
4182
4205
  tabTargetDate,
4183
4206
  gridHasFocus
4184
4207
  ]),
4208
+ viewStableCtx: (0, react.useMemo)(() => ({
4209
+ gridFocusedRef,
4210
+ outsideDays: "enabled"
4211
+ }), []),
4212
+ viewStateCtx: (0, react.useMemo)(() => ({
4213
+ currentDateTime,
4214
+ rootState
4215
+ }), [currentDateTime, rootState]),
4185
4216
  scrollToWeek
4186
4217
  };
4187
4218
  }
@@ -4189,42 +4220,16 @@ function useWeeksViewRootState(props) {
4189
4220
  //#region src/weeks-view.tsx
4190
4221
  function WeeksViewRootFn(props, ref) {
4191
4222
  const { children } = props;
4192
- const { stableCtx, stateCtx, viewCtx, scrollToWeek } = useWeeksViewRootState(props);
4223
+ const { stableCtx, stateCtx, viewCtx, viewStableCtx, viewStateCtx, scrollToWeek } = useWeeksViewRootState(props);
4193
4224
  (0, react.useImperativeHandle)(ref, () => ({ scrollToWeek }), [scrollToWeek]);
4194
- const gridFocusedRef = (0, react.useRef)(false);
4195
- const monthViewStableShim = (0, react.useMemo)(() => ({
4196
- numberOfMonths: 1,
4197
- fixedWeeks: false,
4198
- outsideDays: "enabled",
4199
- outOfRangeBehavior: "unbounded",
4200
- goNextMonth: () => {},
4201
- goPrevMonth: () => {},
4202
- setGridLabelId: () => {},
4203
- gridFocusedRef
4204
- }), []);
4205
- const monthViewStateShim = (0, react.useMemo)(() => ({
4206
- currentMonth: {
4207
- year: stateCtx.currentDateTime.year,
4208
- month: stateCtx.currentDateTime.month
4209
- },
4210
- weeks: [],
4211
- allMonths: [],
4212
- currentDateTime: stateCtx.currentDateTime,
4213
- gridLabelIds: stateCtx.gridLabelIds,
4214
- rootState: stateCtx.rootState
4215
- }), [
4216
- stateCtx.currentDateTime,
4217
- stateCtx.gridLabelIds,
4218
- stateCtx.rootState
4219
- ]);
4220
- return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(MonthViewStableContext.Provider, {
4221
- value: monthViewStableShim,
4222
- children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(MonthViewStateContext.Provider, {
4223
- value: monthViewStateShim,
4224
- children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(WeeksViewStableContext.Provider, {
4225
- value: stableCtx,
4226
- children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(WeeksViewStateContext.Provider, {
4227
- value: stateCtx,
4225
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(WeeksViewStableContext.Provider, {
4226
+ value: stableCtx,
4227
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(WeeksViewStateContext.Provider, {
4228
+ value: stateCtx,
4229
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ViewStableContext.Provider, {
4230
+ value: viewStableCtx,
4231
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ViewStateContext.Provider, {
4232
+ value: viewStateCtx,
4228
4233
  children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ViewContext.Provider, {
4229
4234
  value: viewCtx,
4230
4235
  children
@@ -4269,7 +4274,7 @@ function useWeeksNavButton(direction, shiftByProp) {
4269
4274
  const { disabled: globalDisabled, minValue, maxValue, temporal: T, weekStartDay } = useCalendarStable();
4270
4275
  const { weekCount, outOfRangeBehavior, goNext, goPrev } = useWeeksViewStable();
4271
4276
  const { windowInfo } = useWeeksViewState();
4272
- const { rootState } = useMonthViewState();
4277
+ const { rootState } = useViewState();
4273
4278
  const currentFirstWeek = windowInfo.windowStart;
4274
4279
  const isDisabled = (0, react.useMemo)(() => {
4275
4280
  if (globalDisabled) return true;
@@ -4355,7 +4360,7 @@ const NextWeeksButton = (0, react.forwardRef)(NextWeeksButtonFn);
4355
4360
  function WeekCountFn(props, ref) {
4356
4361
  const { render, ...otherProps } = props;
4357
4362
  const { windowInfo } = useWeeksViewState();
4358
- const { rootState } = useMonthViewState();
4363
+ const { rootState } = useViewState();
4359
4364
  const count = windowInfo.weekCount;
4360
4365
  const state = (0, react.useMemo)(() => ({
4361
4366
  root: rootState,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@klinking/colander",
3
- "version": "3.1.0-alpha.1",
3
+ "version": "3.1.0-alpha.3",
4
4
  "homepage": "https://github.com/klink-ing/colander#readme",
5
5
  "bugs": "https://github.com/klink-ing/colander/issues",
6
6
  "license": "MIT",
@@ -1,5 +1,6 @@
1
1
  import { Temporal } from "@js-temporal/polyfill";
2
2
  import { render, act, cleanup } from "@testing-library/react";
3
+ import type { ComponentProps } from "react";
3
4
  import { describe, it, expect, vi, afterEach } from "vitest";
4
5
  import { useCalendarStable, useCalendarState } from "./calendar-context";
5
6
  import { CalendarProvider } from "./calendar-provider";
@@ -135,3 +136,89 @@ describe("CalendarProvider", () => {
135
136
  spy.mockRestore();
136
137
  });
137
138
  });
139
+
140
+ describe("CalendarProvider — controlled-value integrity (M7)", () => {
141
+ it.each([
142
+ {
143
+ description: "range value that is not a { start, end } DateRange",
144
+ selectionMode: "range" as const,
145
+ value: "2026-03-15",
146
+ expected: { matches: /range/i },
147
+ },
148
+ {
149
+ description: "multiple value that is not an array",
150
+ selectionMode: "multiple" as const,
151
+ value: T.PlainDate.from("2026-03-15"),
152
+ expected: { matches: /multiple|array/i },
153
+ },
154
+ ])(
155
+ "warns instead of silently going uncontrolled for a malformed $description",
156
+ ({ selectionMode, value, expected }) => {
157
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
158
+ render(
159
+ // selectionMode is a union here (it.each), which can't narrow the
160
+ // discriminated props union — and value is intentionally wrong-shaped.
161
+ <CalendarProvider
162
+ {...({
163
+ temporal: T,
164
+ selectionMode,
165
+ value,
166
+ onValueChange: vi.fn(),
167
+ } as unknown as ComponentProps<typeof CalendarProvider>)}
168
+ >
169
+ <div />
170
+ </CalendarProvider>,
171
+ );
172
+ expect(warn).toHaveBeenCalledTimes(1);
173
+ expect(warn).toHaveBeenCalledWith(
174
+ expect.stringContaining("[DatePicker]"),
175
+ );
176
+ expect(warn.mock.calls[0]?.[0]).toMatch(expected.matches);
177
+ warn.mockRestore();
178
+ },
179
+ );
180
+
181
+ it("warns once (not every render) for an out-of-bounds controlled value and never fires onValueChange", () => {
182
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
183
+ const onChange = vi.fn();
184
+ const make = (onValueChange: () => void) => (
185
+ <CalendarProvider
186
+ temporal={T}
187
+ format="PlainDate"
188
+ selectionMode="single"
189
+ value={T.PlainDate.from("2020-06-15")}
190
+ min={T.PlainDate.from("2026-01-01")}
191
+ max={T.PlainDate.from("2026-12-31")}
192
+ onValueChange={onValueChange}
193
+ >
194
+ <div />
195
+ </CalendarProvider>
196
+ );
197
+ const { rerender } = render(make(onChange));
198
+ // Re-render with a fresh inline onValueChange (the common case): the
199
+ // out-of-bounds effect re-runs, but the warning must not repeat.
200
+ rerender(make(vi.fn()));
201
+ expect(onChange).not.toHaveBeenCalled();
202
+ expect(warn).toHaveBeenCalledTimes(1);
203
+ warn.mockRestore();
204
+ });
205
+
206
+ it("still clears and fires onValueChange for an out-of-bounds uncontrolled defaultValue", () => {
207
+ const onChange = vi.fn();
208
+ render(
209
+ <CalendarProvider
210
+ temporal={T}
211
+ format="PlainDate"
212
+ selectionMode="single"
213
+ defaultValue={T.PlainDate.from("2020-06-15")}
214
+ min={T.PlainDate.from("2026-01-01")}
215
+ max={T.PlainDate.from("2026-12-31")}
216
+ onValueChange={onChange}
217
+ >
218
+ <div />
219
+ </CalendarProvider>,
220
+ );
221
+ expect(onChange).toHaveBeenCalledTimes(1);
222
+ expect(onChange).toHaveBeenCalledWith(null, expect.anything());
223
+ });
224
+ });
@@ -199,6 +199,35 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
199
199
  sortDates,
200
200
  ]);
201
201
 
202
+ // Surface (instead of silently self-managing) a controlled `value` whose
203
+ // shape doesn't match the selection mode — otherwise range/multiple values
204
+ // fall through to `undefined` above and the component quietly becomes
205
+ // uncontrolled, which looks like the prop being ignored. Warn once per
206
+ // malformed episode: this effect re-runs whenever the parent inlines a fresh
207
+ // `value` reference, but we only warn when entering the malformed state.
208
+ const warnedMalformedRef = useRef(false);
209
+ useEffect(() => {
210
+ const malformed =
211
+ valueProp != null &&
212
+ ((isRange && !isDateRange<F>(valueProp)) ||
213
+ (isMultiple && !Array.isArray(valueProp)));
214
+ if (!malformed) {
215
+ warnedMalformedRef.current = false;
216
+ return;
217
+ }
218
+ if (warnedMalformedRef.current) return;
219
+ warnedMalformedRef.current = true;
220
+ if (isRange) {
221
+ console.warn(
222
+ `[DatePicker] selectionMode="range" expects \`value\` to be a { start, end } DateRange or null, but received ${Array.isArray(valueProp) ? "an array" : typeof valueProp}. Ignoring it and falling back to uncontrolled.`,
223
+ );
224
+ } else {
225
+ console.warn(
226
+ `[DatePicker] selectionMode="multiple" expects \`value\` to be an array or null, but received ${typeof valueProp}. Ignoring it and falling back to uncontrolled.`,
227
+ );
228
+ }
229
+ }, [valueProp, isRange, isMultiple]);
230
+
202
231
  const defaultDates = useMemo<(Temporal.PlainDate | null)[]>(() => {
203
232
  if (isMultiple) {
204
233
  if (multipleDefault) return sortDates(multipleDefault.map(rawToPlain));
@@ -423,29 +452,48 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
423
452
  ]);
424
453
 
425
454
  // --- Out-of-bounds cleanup for single mode ---
455
+ const warnedOutOfBoundsRef = useRef(false);
426
456
  useEffect(() => {
427
457
  if (isRange || isMultiple) return;
428
458
  const start = committedStart;
429
- if (!start) return;
459
+ if (!start) {
460
+ warnedOutOfBoundsRef.current = false;
461
+ return;
462
+ }
430
463
  const outOfBounds =
431
464
  (minValue && T.PlainDate.compare(start, minValue) < 0) ||
432
465
  (maxValue && T.PlainDate.compare(start, maxValue) > 0);
433
- if (outOfBounds) {
434
- const prev = currentSingleFormatted();
435
- commitSelection({
436
- newDates: [],
437
- fireCallback: (onValueChange) => {
438
- (
439
- onValueChange as
440
- | ((
441
- v: RawValueForFormat<F> | null,
442
- m: { date: undefined; previous: RawValueForFormat<F> | null },
443
- ) => void)
444
- | undefined
445
- )?.(null, { date: undefined, previous: prev });
446
- },
447
- });
466
+ if (!outOfBounds) {
467
+ warnedOutOfBoundsRef.current = false;
468
+ return;
469
+ }
470
+ if (isControlled) {
471
+ // The parent owns a controlled `value`. Don't clear it or fire
472
+ // onValueChange on mount — that fights the parent and risks a render
473
+ // loop. Warn once (this effect re-runs whenever the parent inlines
474
+ // `onValueChange`) so they can reconcile `value` with min/max.
475
+ if (!warnedOutOfBoundsRef.current) {
476
+ warnedOutOfBoundsRef.current = true;
477
+ console.warn(
478
+ `[DatePicker] The controlled \`value\` is outside the min/max range. It stays selected (rendered disabled) until you update \`value\`; the component will not clear it for you.`,
479
+ );
480
+ }
481
+ return;
448
482
  }
483
+ const prev = currentSingleFormatted();
484
+ commitSelection({
485
+ newDates: [],
486
+ fireCallback: (onValueChange) => {
487
+ (
488
+ onValueChange as
489
+ | ((
490
+ v: RawValueForFormat<F> | null,
491
+ m: { date: undefined; previous: RawValueForFormat<F> | null },
492
+ ) => void)
493
+ | undefined
494
+ )?.(null, { date: undefined, previous: prev });
495
+ },
496
+ });
449
497
  }, [
450
498
  minValue,
451
499
  maxValue,
@@ -455,6 +503,7 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
455
503
  T,
456
504
  isRange,
457
505
  isMultiple,
506
+ isControlled,
458
507
  ]);
459
508
 
460
509
  // --- onSelect ---