@aiquants/period-slider 0.8.0 → 0.9.0

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.
package/src/index.ts CHANGED
@@ -58,10 +58,14 @@ export {
58
58
  formatScreenReaderLabel,
59
59
  getActiveDragHandle,
60
60
  getMacroWindowEndDate,
61
+ getMacroWindowStartDate,
61
62
  getSortedAllowedDates,
62
63
  normalizeDateByUnit,
63
64
  PeriodSlider,
65
+ resolveMacroWindow,
66
+ resolveVernierRailZone,
64
67
  resolveVernierTargetHandle,
68
+ resolveWheelStep,
65
69
  } from "./PeriodSlider"
66
70
  // ======================================================================
67
71
  // State Management Hooks (Zero-Boilerplate React Hooks)
@@ -72,16 +76,16 @@ export type {
72
76
  UsePeriodSliderPointReturn,
73
77
  UsePeriodSliderRangeOptions,
74
78
  UsePeriodSliderRangeReturn,
75
- UseVernierSliderOptions,
76
79
  UseVernierSliderPointOptions,
77
80
  UseVernierSliderPointReturn,
78
- UseVernierSliderReturn,
81
+ UseVernierSliderRangeOptions,
82
+ UseVernierSliderRangeReturn,
79
83
  } from "./usePeriodSlider"
80
84
  export {
81
85
  usePeriodSliderPoint,
82
86
  usePeriodSliderRange,
83
- useVernierSlider,
84
87
  useVernierSliderPoint,
88
+ useVernierSliderRange,
85
89
  } from "./usePeriodSlider"
86
90
  export type { YearSelectorProps } from "./YearSelector"
87
91
  export { YearSelector } from "./YearSelector"
@@ -978,3 +978,28 @@
978
978
  .period-slider-tooltip {
979
979
  will-change: transform;
980
980
  }
981
+
982
+ /* ============================================================= */
983
+ /* ネイティブ日付入力 (type="date" / type="month") の字送り */
984
+ /* 隣り合うドロップダウン形態と同じ位置に数字が来るようにする。 */
985
+ /* Tailwind preflight が ::-webkit-datetime-edit を inline-flex にするため */
986
+ /* text-align は余白を配れず素通りする。編集領域を全幅化して中央寄せし、 */
987
+ /* 起動子は絶対配置で浮かせて桁送りから外す (起動子を消すと暦が開けなくなる)。 */
988
+ /* preflight 非適用のホストでは編集領域は flex にならず、これらは無害に無視される。 */
989
+ /* ============================================================= */
990
+ .period-slider-native-date-field {
991
+ position: relative;
992
+ }
993
+
994
+ .period-slider-native-date-field::-webkit-datetime-edit {
995
+ flex: 1;
996
+ justify-content: center;
997
+ }
998
+
999
+ .period-slider-native-date-field::-webkit-calendar-picker-indicator {
1000
+ position: absolute;
1001
+ top: 0;
1002
+ right: 2px;
1003
+ bottom: 0;
1004
+ margin-block: auto;
1005
+ }
@@ -7,12 +7,14 @@
7
7
  * Every hook takes exactly one canonical option per concept and fails fast with a
8
8
  * RangeError when a required boundary or initial date is missing, invalid, inverted or
9
9
  * outside the domain it declares. Committed state is always kept inside
10
- * [minDate, maxDate] - and, for the Vernier micro range, inside the current macro window.
10
+ * [minDate, maxDate] - and, for the Vernier micro selection, inside the current macro window
11
+ * intersected with that domain, which is the very window the slider draws its micro rail over.
11
12
  *
12
13
  * レンジ・ポイント・ノギス型 2 段スケール PeriodSlider のステート管理ボイラープレートを
13
14
  * 最小化する標準 React カスタムフック群を提供します。各概念につき正準オプションは 1 つのみで、
14
15
  * 必須の境界日付・初期日付が欠落・不正・逆転・定義域外の場合は RangeError で即時失敗します。
15
- * 確定済みステートは常に [minDate, maxDate] の内側 (ノギスのミクロ選択は現在のマクロ窓の内側) に保たれます。
16
+ * 確定済みステートは常に [minDate, maxDate] の内側 (ノギスのミクロ選択は現在のマクロ窓を定義域で交差させた
17
+ * 窓、すなわちスライダーがミクロレールを描く窓そのものの内側) に保たれます。
16
18
  */
17
19
 
18
20
  import type React from "react"
@@ -21,13 +23,13 @@ import {
21
23
  addUnit,
22
24
  type BasePeriodSliderVernierProps,
23
25
  findNearestDateIndex,
24
- getMacroWindowEndDate,
25
26
  getSortedAllowedDates,
26
27
  type PeriodSliderPointProps,
27
28
  type PeriodSliderRangeProps,
28
29
  type PeriodSliderUnit,
29
30
  type PeriodSliderVernierPointProps,
30
31
  type PeriodSliderVernierRangeProps,
32
+ resolveMacroWindow,
31
33
  } from "./PeriodSlider"
32
34
 
33
35
  // ======================================================================
@@ -144,34 +146,66 @@ function clampDate(date: Date, min: Date, max: Date): Date {
144
146
  }
145
147
 
146
148
  /**
147
- * Clamps both ends of a micro range into the effective macro window.
148
- * The window ends at getMacroWindowEndDate so that a micro date the slider can legally
149
- * reach (e.g. December 31 for a yearly macro scale) is not truncated to the raw macro end.
150
- * ミクロ期間の両端を実効マクロ窓 (終端は getMacroWindowEndDate) の内側へ収める。
149
+ * Resolves the micro candidates a window contains, empty while the micro rail is continuous.
150
+ * Candidates are resolved over the domain first and only then filtered to the window: resolving them
151
+ * against the window directly makes getSortedAllowedDates throw for a window that holds none, which a
152
+ * pan reaches easily.
153
+ * 窓が含むミクロ候補を解決する関数 (ミクロレールが連続の間は空)。
154
+ * 候補は定義域で解決してから窓で絞る。窓を境界として解決すると、候補を 1 つも含まない窓
155
+ * (パンで容易に到達する) で getSortedAllowedDates が送出するため。
151
156
  *
152
- * @param range - Micro range tuple to contain / 収める対象のミクロ期間タプル
153
- * @param macroUnit - Macro scale unit / マクロ側の時間解像度単位
154
- * @param microUnit - Micro scale unit / ミクロ側の時間解像度単位
155
- * @param macroRange - Current macro window tuple / 現在のマクロ窓タプル
156
- * @returns Micro range tuple guaranteed to lie inside the macro window / マクロ窓の内側であることが保証されたミクロ期間タプル
157
+ * @param window - Resolved macro window tuple / 解決済みマクロ窓タプル
158
+ * @param unit - Micro scale unit / ミクロ側の時間解像度単位
159
+ * @param isDiscrete - Canonical discrete switch / 離散指定の正準スイッチ
160
+ * @param allowed - Candidate dates the consumer declared / 利用側が宣言した候補日付
161
+ * @param minDate - Domain lower boundary / 定義域の下限
162
+ * @param maxDate - Domain upper boundary / 定義域の上限
163
+ * @returns Candidates inside the window, ascending / 窓の内側の候補 (昇順)
164
+ * @throws {RangeError} When every declared candidate falls outside [minDate, maxDate] / 宣言された候補が 1 つも定義域に入らない場合
157
165
  */
158
- function clampRangeIntoMacroWindow(range: [Date, Date], macroUnit: PeriodSliderUnit, microUnit: PeriodSliderUnit, macroRange: [Date, Date]): [Date, Date] {
159
- const windowEnd = getMacroWindowEndDate(macroUnit, microUnit, macroRange[1])
160
- return [clampDate(range[0], macroRange[0], windowEnd), clampDate(range[1], macroRange[0], windowEnd)]
166
+ function resolveMicroCandidatesIn(window: [Date, Date], unit: PeriodSliderUnit, isDiscrete: boolean | undefined, allowed: Date[] | undefined, minDate: Date, maxDate: Date): Date[] {
167
+ if (isDiscrete !== true) return []
168
+ const windowStartMs = window[0].getTime()
169
+ const windowEndMs = window[1].getTime()
170
+ return getSortedAllowedDates(allowed, unit, minDate, maxDate).filter((d) => d.getTime() >= windowStartMs && d.getTime() <= windowEndMs)
161
171
  }
162
172
 
163
173
  /**
164
- * Clamps a single micro date into the effective macro window.
165
- * ミクロ時点 1 点を実効マクロ窓 (終端は getMacroWindowEndDate) の内側へ収める。
174
+ * Resolves the value a micro date legally takes on the rail the slider draws.
175
+ * A discrete rail draws nothing but its candidate set, so a date that is not a candidate resolves to the
176
+ * nearest one; a continuous rail only clamps into the window. This mirrors the component's own rule, so
177
+ * a hook can never commit a value the rail does not draw.
178
+ * スライダーが描くレール上で、ミクロ日付が取る正当な値を解決する関数。
179
+ * 離散レールは候補集合しか描かないため、候補でない日付は最近傍候補へ解決される。連続レールは窓へ収めるだけ。
180
+ * 本体側と同一の規則であり、フックがレールに無い値を確定させることがない。
166
181
  *
167
- * @param date - Micro date to contain / 収める対象のミクロ日付
168
- * @param macroUnit - Macro scale unit / マクロ側の時間解像度単位
169
- * @param microUnit - Micro scale unit / ミクロ側の時間解像度単位
170
- * @param macroRange - Current macro window tuple / 現在のマクロ窓タプル
171
- * @returns Micro date guaranteed to lie inside the macro window / マクロ窓の内側であることが保証されたミクロ日付
182
+ * @param date - Micro date to place / 位置を求めるミクロ日付
183
+ * @param unit - Micro scale unit / ミクロ側の時間解像度単位
184
+ * @param window - Resolved macro window tuple / 解決済みマクロ窓タプル
185
+ * @param candidates - Candidates the window contains (empty for a continuous rail) / 窓が含む候補 (連続レールでは空)
186
+ * @returns Date on the rail; the input itself when it already sits there / レール上の日付 (既に載っていれば入力そのもの)
172
187
  */
173
- function clampDateIntoMacroWindow(date: Date, macroUnit: PeriodSliderUnit, microUnit: PeriodSliderUnit, macroRange: [Date, Date]): Date {
174
- return clampDate(date, macroRange[0], getMacroWindowEndDate(macroUnit, microUnit, macroRange[1]))
188
+ function resolveMicroRailDate(date: Date, unit: PeriodSliderUnit, window: [Date, Date], candidates: Date[]): Date {
189
+ if (candidates.length === 0) return clampDate(date, window[0], window[1])
190
+ // 値が同じなら入力をそのまま返す。別インスタンスを返すと、レンダー中の同一性比較による畳み込みが止まらない
191
+ const snapped = candidates[findNearestDateIndex(candidates, date, unit)]
192
+ return snapped.getTime() === date.getTime() ? date : snapped
193
+ }
194
+
195
+ /**
196
+ * Resolves both ends of a micro range onto the rail, keeping the upper end at or after the lower.
197
+ * ミクロ期間の両端をレール上へ解決する関数 (上限は下限以降を保つ)。
198
+ *
199
+ * @param range - Micro range tuple to place / 解決対象のミクロ期間タプル
200
+ * @param unit - Micro scale unit / ミクロ側の時間解像度単位
201
+ * @param window - Resolved macro window tuple / 解決済みマクロ窓タプル
202
+ * @param candidates - Candidates the window contains (empty for a continuous rail) / 窓が含む候補 (連続レールでは空)
203
+ * @returns Micro range tuple guaranteed to sit on the rail / レール上にあることが保証されたミクロ期間タプル
204
+ */
205
+ function resolveMicroRailRange(range: [Date, Date], unit: PeriodSliderUnit, window: [Date, Date], candidates: Date[]): [Date, Date] {
206
+ const low = resolveMicroRailDate(range[0], unit, window, candidates)
207
+ const high = resolveMicroRailDate(range[1], unit, window, candidates)
208
+ return [low, high < low ? low : high]
175
209
  }
176
210
 
177
211
  /**
@@ -606,14 +640,14 @@ export function usePeriodSliderPoint(options: UsePeriodSliderPointOptions): UseP
606
640
  }
607
641
 
608
642
  // ======================================================================
609
- // 3. Vernier Dual-Scale Hook (useVernierSlider)
643
+ // 3. Vernier Dual-Scale Hook (useVernierSliderRange)
610
644
  // ======================================================================
611
645
 
612
646
  /**
613
647
  * Options accepted by the vernier hook; slider props not owned by the hook pass straight through.
614
648
  * ノギスフックが受け取るオプション。フックが所有しないスライダー props はそのまま素通し。
615
649
  */
616
- export interface UseVernierSliderOptions extends Partial<Omit<BasePeriodSliderVernierProps, "mode" | "unit" | "minDate" | "maxDate" | "macroUnit" | "microUnit" | "macroRange" | "onChangeMacroRange">> {
650
+ export interface UseVernierSliderRangeOptions extends Partial<Omit<BasePeriodSliderVernierProps, "mode" | "unit" | "minDate" | "maxDate" | "macroUnit" | "microUnit" | "macroRange" | "onChangeMacroRange">> {
617
651
  /** Base time resolution unit / 基本時間解像度単位 */
618
652
  unit: PeriodSliderUnit
619
653
  /** Minimum selectable boundary date for entire macro range (required, no fallback) / 全期間マクロの下限日付 (必須・フォールバックなし) */
@@ -638,7 +672,7 @@ export interface UseVernierSliderOptions extends Partial<Omit<BasePeriodSliderVe
638
672
  * Macro/micro state, mutators and ready-to-spread props returned by the vernier hook.
639
673
  * ノギスフックが返すマクロ・ミクロの状態・更新関数・スプレッド可能な props 一式。
640
674
  */
641
- export interface UseVernierSliderReturn {
675
+ export interface UseVernierSliderRangeReturn {
642
676
  /** Macro window range tuple / マクロ窓期間タプル */
643
677
  macroRange: [Date, Date]
644
678
  /** State updater for macro range; replaces state without firing onChangeMacroRange / マクロ窓更新関数 (onChangeMacroRange は発火しない) */
@@ -655,9 +689,9 @@ export interface UseVernierSliderReturn {
655
689
  setMicroLowerDate: (date: Date) => void
656
690
  /** Sets the micro upper date, pushing the micro lower date when undercut and clamping into the macro window / ミクロ終了日更新 (下限を下回ったら下限も追従・マクロ窓へクランプ) */
657
691
  setMicroUpperDate: (date: Date) => void
658
- /** Shifts the macro window by delta macro units, aborting entirely when either edge would leave [minDate, maxDate] / マクロ窓を delta マクロ単位シフト (片側でも定義域を越える場合は全体中止) */
692
+ /** Shifts the macro window by delta macro units - or delta positions through the macro candidate set when macroIsDiscrete is on - aborting entirely when either edge would leave [minDate, maxDate] or the candidate set / マクロ窓を delta マクロ単位 (macroIsDiscrete 有効時は候補集合上を delta 個分) シフト (片側でも定義域・候補集合を越える場合は全体中止) */
659
693
  shiftMacroRange: (delta: number) => void
660
- /** Shifts the micro range by delta micro units, aborting entirely when either edge would leave the macro window / ミクロ選択範囲を delta ミクロ単位シフト (片側でもマクロ窓を越える場合は全体中止) */
694
+ /** Shifts the micro range by delta micro units - or delta positions through the micro candidate set when microIsDiscrete is on - aborting entirely when either edge would leave the macro window / ミクロ選択範囲を delta ミクロ単位 (microIsDiscrete 有効かつ窓内に候補があるときは候補集合上を delta 個分) シフト (片側でもマクロ窓・候補集合を越える場合は全体中止) */
661
695
  shiftMicroRange: (delta: number) => void
662
696
  /** Reset to initial macro and micro ranges / 初期状態へリセット */
663
697
  resetVernier: () => void
@@ -676,8 +710,9 @@ export interface UseVernierSliderReturn {
676
710
  /**
677
711
  * Custom hook to manage state and props for a Vernier dual-scale PeriodSlider.
678
712
  * Two containment invariants hold after every mutation and every render: the macro window
679
- * stays inside [minDate, maxDate], and the micro range stays inside the macro window whose
680
- * end is getMacroWindowEndDate(macroUnit, microUnit, macroRange[1]).
713
+ * stays inside [minDate, maxDate], and the micro range stays inside
714
+ * resolveMacroWindow(macroUnit, microUnit, macroRange, minDate, maxDate) - the same window the
715
+ * slider draws its micro rail over, so no committed micro date can fall outside that rail.
681
716
  * Vernier 2 段スケール PeriodSlider のステート管理と Props 生成を行うカスタムフック。
682
717
  *
683
718
  * @param options - Required unit, boundaries, initial macro/micro ranges plus optional UI props / 必須の単位・境界・初期マクロ/ミクロ期間と任意の UI Props
@@ -686,7 +721,7 @@ export interface UseVernierSliderReturn {
686
721
  *
687
722
  * @example
688
723
  * ```tsx
689
- * const { macroRange, microRange, sliderProps, macroStartSelectorProps, macroEndSelectorProps } = useVernierSlider({
724
+ * const { macroRange, microRange, sliderProps, macroStartSelectorProps, macroEndSelectorProps } = useVernierSliderRange({
690
725
  * unit: "month",
691
726
  * minDate: new Date(2015, 0, 1),
692
727
  * maxDate: new Date(2030, 11, 31),
@@ -698,14 +733,15 @@ export interface UseVernierSliderReturn {
698
733
  * return <PeriodSlider {...sliderProps} />
699
734
  * ```
700
735
  */
701
- export function useVernierSlider(options: UseVernierSliderOptions): UseVernierSliderReturn {
736
+ export function useVernierSliderRange(options: UseVernierSliderRangeOptions): UseVernierSliderRangeReturn {
702
737
  const { unit, minDate, maxDate, initialMacroRange, initialMicroRange, macroUnit = unit, microUnit = unit, onChangeMacroRange, onChangeMicroRange, ...restUiProps } = options
703
738
 
704
- assertValidBounds("useVernierSlider", minDate, maxDate)
705
- assertValidRangeTuple("useVernierSlider", "initialMacroRange", initialMacroRange)
706
- assertValidRangeTuple("useVernierSlider", "initialMicroRange", initialMicroRange)
707
- assertRangeWithinBounds("useVernierSlider", "initialMacroRange", initialMacroRange, minDate, maxDate, "[minDate, maxDate]")
708
- assertRangeWithinBounds("useVernierSlider", "initialMicroRange", initialMicroRange, initialMacroRange[0], getMacroWindowEndDate(macroUnit, microUnit, initialMacroRange[1]), "the initial macro window")
739
+ assertValidBounds("useVernierSliderRange", minDate, maxDate)
740
+ assertValidRangeTuple("useVernierSliderRange", "initialMacroRange", initialMacroRange)
741
+ assertValidRangeTuple("useVernierSliderRange", "initialMicroRange", initialMicroRange)
742
+ assertRangeWithinBounds("useVernierSliderRange", "initialMacroRange", initialMacroRange, minDate, maxDate, "[minDate, maxDate]")
743
+ const initialMicroWindow = resolveMacroWindow(macroUnit, microUnit, initialMacroRange, minDate, maxDate)
744
+ assertRangeWithinBounds("useVernierSliderRange", "initialMicroRange", initialMicroRange, initialMicroWindow[0], initialMicroWindow[1], "the initial macro window")
709
745
 
710
746
  // レスト構文の転送 Props は毎レンダー別オブジェクトになり、依存に置くと sliderProps のメモ化が常に外れる
711
747
  const uiProps = useShallowStable(restUiProps)
@@ -724,8 +760,25 @@ export function useVernierSlider(options: UseVernierSliderOptions): UseVernierSl
724
760
  setMacroRange(containedMacro)
725
761
  }
726
762
 
763
+ // レール・収め込み・セレクタが同じ窓を読むよう、マクロ窓と定義域の交差はレンダーごとに 1 度だけ解決する
764
+ const microWindow = useMemo<[Date, Date]>(() => resolveMacroWindow(macroUnit, microUnit, macroRange, minDate, maxDate), [macroUnit, microUnit, macroRange, minDate, maxDate])
765
+
766
+ // レールが描く候補集合。離散指定が誤っていれば getSortedAllowedDates はここで送出する (更新関数の中まで遅らせない)
767
+ const microCandidates = useMemo(
768
+ () => resolveMicroCandidatesIn(microWindow, microUnit, uiProps.microIsDiscrete, uiProps.microAllowedDates ?? uiProps.allowedDates, minDate, maxDate),
769
+ [microWindow, microUnit, uiProps.microIsDiscrete, uiProps.microAllowedDates, uiProps.allowedDates, minDate, maxDate],
770
+ )
771
+
772
+ // マクロ候補も同じ理由でレンダー中に解決する。更新関数の中で解決すると、誤設定がクリックまで隠れる
773
+ const macroCandidates = useMemo(
774
+ () => (uiProps.macroIsDiscrete === true ? getSortedAllowedDates(uiProps.macroAllowedDates ?? uiProps.allowedDates, macroUnit, minDate, maxDate) : []),
775
+ [uiProps.macroIsDiscrete, uiProps.macroAllowedDates, uiProps.allowedDates, macroUnit, minDate, maxDate],
776
+ )
777
+ const macroCandidatesRef = useRef<Date[]>(macroCandidates)
778
+ macroCandidatesRef.current = macroCandidates
779
+
727
780
  let microRange = microRangeState
728
- const containedMicro = clampRangeIntoMacroWindow(microRangeState, macroUnit, microUnit, macroRange)
781
+ const containedMicro = resolveMicroRailRange(microRangeState, microUnit, microWindow, microCandidates)
729
782
  if (containedMicro[0] !== microRangeState[0] || containedMicro[1] !== microRangeState[1]) {
730
783
  microRange = containedMicro
731
784
  setMicroRange(containedMicro)
@@ -737,8 +790,8 @@ export function useVernierSlider(options: UseVernierSliderOptions): UseVernierSl
737
790
  microRangeRef.current = microRange
738
791
 
739
792
  // 境界を ref 経由で読むことで、消費側が毎レンダー新しい Date を渡しても更新関数の同一性を保つ
740
- const domainRef = useRef({ minDate, maxDate })
741
- domainRef.current = { minDate, maxDate }
793
+ const domainRef = useRef({ minDate, maxDate, macroIsDiscrete: uiProps.macroIsDiscrete, macroAllowedDates: uiProps.macroAllowedDates, microIsDiscrete: uiProps.microIsDiscrete, microAllowedDates: uiProps.microAllowedDates, allowedDates: uiProps.allowedDates })
794
+ domainRef.current = { minDate, maxDate, macroIsDiscrete: uiProps.macroIsDiscrete, macroAllowedDates: uiProps.macroAllowedDates, microIsDiscrete: uiProps.microIsDiscrete, microAllowedDates: uiProps.microAllowedDates, allowedDates: uiProps.allowedDates }
742
795
 
743
796
  // コールバックを ref 経由で読むことで、消費側が毎レンダー新しい関数を渡しても更新関数の同一性を保つ
744
797
  const onChangeMacroRangeRef = useRef(onChangeMacroRange)
@@ -746,6 +799,23 @@ export function useVernierSlider(options: UseVernierSliderOptions): UseVernierSl
746
799
  const onChangeMicroRangeRef = useRef(onChangeMicroRange)
747
800
  onChangeMicroRangeRef.current = onChangeMicroRange
748
801
 
802
+ // 更新関数の中では境界を ref 経由で読むため、窓の解決も同じ経路に載せて関数の同一性を保つ
803
+ const resolveWindow = useCallback(
804
+ (macro: [Date, Date]): [Date, Date] => {
805
+ const { minDate: domainLower, maxDate: domainUpper } = domainRef.current
806
+ return resolveMacroWindow(macroUnit, microUnit, macro, domainLower, domainUpper)
807
+ },
808
+ [macroUnit, microUnit],
809
+ )
810
+
811
+ const resolveCandidates = useCallback(
812
+ (window: [Date, Date]): Date[] => {
813
+ const { microIsDiscrete, microAllowedDates, allowedDates, minDate: domainLower, maxDate: domainUpper } = domainRef.current
814
+ return resolveMicroCandidatesIn(window, microUnit, microIsDiscrete, microAllowedDates ?? allowedDates, domainLower, domainUpper)
815
+ },
816
+ [microUnit],
817
+ )
818
+
749
819
  const commitVernier = useCallback((nextMacro: [Date, Date], nextMicro: [Date, Date]) => {
750
820
  const currentMacro = macroRangeRef.current
751
821
  const currentMicro = microRangeRef.current
@@ -767,16 +837,18 @@ export function useVernierSlider(options: UseVernierSliderOptions): UseVernierSl
767
837
  const { minDate: domainLower, maxDate: domainUpper } = domainRef.current
768
838
  const nextMacro: [Date, Date] = [clampDate(lower, domainLower, domainUpper), clampDate(upper, domainLower, domainUpper)]
769
839
  // マクロ窓が動くとミクロ選択がはみ出し得るため、同じ確定経路で窓の内側へ押し込む
770
- commitVernier(nextMacro, clampRangeIntoMacroWindow(microRangeRef.current, macroUnit, microUnit, nextMacro))
840
+ const nextWindow = resolveWindow(nextMacro)
841
+ commitVernier(nextMacro, resolveMicroRailRange(microRangeRef.current, microUnit, nextWindow, resolveCandidates(nextWindow)))
771
842
  },
772
- [macroUnit, microUnit, commitVernier],
843
+ [microUnit, resolveWindow, resolveCandidates, commitVernier],
773
844
  )
774
845
 
775
846
  const handleChangeMicroRange = useCallback(
776
847
  (lower: Date, upper: Date) => {
777
- commitVernier(macroRangeRef.current, clampRangeIntoMacroWindow([lower, upper], macroUnit, microUnit, macroRangeRef.current))
848
+ const currentWindow = resolveWindow(macroRangeRef.current)
849
+ commitVernier(macroRangeRef.current, resolveMicroRailRange([lower, upper], microUnit, currentWindow, resolveCandidates(currentWindow)))
778
850
  },
779
- [macroUnit, microUnit, commitVernier],
851
+ [microUnit, resolveWindow, resolveCandidates, commitVernier],
780
852
  )
781
853
 
782
854
  const setMacroLowerDate = useCallback(
@@ -819,6 +891,17 @@ export function useVernierSlider(options: UseVernierSliderOptions): UseVernierSl
819
891
  (delta: number) => {
820
892
  const { minDate: domainLower, maxDate: domainUpper } = domainRef.current
821
893
  const current = macroRangeRef.current
894
+ // 離散指定時に暦単位で進めると、スライダーが表示しない日付をステートとコールバックへ流してしまう。
895
+ // 候補集合はレンダー中に解決済み (誤設定はここではなく描画時点で送出される)
896
+ const candidates = macroCandidatesRef.current
897
+ if (candidates.length > 0) {
898
+ const nextLowIdx = findNearestDateIndex(candidates, current[0], macroUnit) + delta
899
+ const nextHighIdx = findNearestDateIndex(candidates, current[1], macroUnit) + delta
900
+ // 窓幅 (候補個数) を保つため、片側でも候補集合を外れるシフトは全体を中止する
901
+ if (nextLowIdx < 0 || nextHighIdx > candidates.length - 1) return
902
+ handleChangeMacroRange(candidates[nextLowIdx], candidates[nextHighIdx])
903
+ return
904
+ }
822
905
  const shiftedLow = addUnit(macroUnit, current[0], delta)
823
906
  const shiftedHigh = addUnit(macroUnit, current[1], delta)
824
907
  // 窓幅を保つため、片側でも境界を越えるシフトは全体を中止する
@@ -832,21 +915,34 @@ export function useVernierSlider(options: UseVernierSliderOptions): UseVernierSl
832
915
  (delta: number) => {
833
916
  const currentMacro = macroRangeRef.current
834
917
  const currentMicro = microRangeRef.current
918
+ const currentWindow = resolveWindow(currentMacro)
919
+ const [windowStart, windowEnd] = currentWindow
920
+ // 離散指定時に暦単位で進めると、スライダーが表示しない日付をステートとコールバックへ流してしまう
921
+ const candidates = resolveCandidates(currentWindow)
922
+ if (candidates.length > 0) {
923
+ const nextLowIdx = findNearestDateIndex(candidates, currentMicro[0], microUnit) + delta
924
+ const nextHighIdx = findNearestDateIndex(candidates, currentMicro[1], microUnit) + delta
925
+ // 幅 (候補個数) を保つため、片側でも候補集合を外れるシフトは全体を中止する
926
+ if (nextLowIdx < 0 || nextHighIdx > candidates.length - 1) return
927
+ handleChangeMicroRange(candidates[nextLowIdx], candidates[nextHighIdx])
928
+ return
929
+ }
835
930
  const shiftedLow = addUnit(microUnit, currentMicro[0], delta)
836
931
  const shiftedHigh = addUnit(microUnit, currentMicro[1], delta)
837
932
  // ミクロ選択範囲はマクロ窓の内側に限られるため、はみ出すシフトは中止する
838
- if (shiftedLow < currentMacro[0] || shiftedHigh > getMacroWindowEndDate(macroUnit, microUnit, currentMacro[1])) return
933
+ if (shiftedLow < windowStart || shiftedHigh > windowEnd) return
839
934
  handleChangeMicroRange(shiftedLow, shiftedHigh)
840
935
  },
841
- [macroUnit, microUnit, handleChangeMicroRange],
936
+ [microUnit, resolveWindow, resolveCandidates, handleChangeMicroRange],
842
937
  )
843
938
 
844
939
  const resetVernier = useCallback(() => {
845
940
  const initialMacro = initialMacroRef.current
846
- commitVernier(initialMacro, clampRangeIntoMacroWindow(initialMicroRef.current, macroUnit, microUnit, initialMacro))
847
- }, [macroUnit, microUnit, commitVernier])
941
+ const initialWindow = resolveWindow(initialMacro)
942
+ commitVernier(initialMacro, resolveMicroRailRange(initialMicroRef.current, microUnit, initialWindow, resolveCandidates(initialWindow)))
943
+ }, [microUnit, resolveWindow, resolveCandidates, commitVernier])
848
944
 
849
- const macroWindowEnd = useMemo(() => getMacroWindowEndDate(macroUnit, microUnit, macroRange[1]), [macroUnit, microUnit, macroRange[1]])
945
+ const [macroWindowStart, macroWindowEnd] = microWindow
850
946
 
851
947
  const macroStartSelectorProps: SelectorProps = useMemo(
852
948
  () => ({
@@ -874,11 +970,11 @@ export function useVernierSlider(options: UseVernierSliderOptions): UseVernierSl
874
970
  () => ({
875
971
  value: microRange[0],
876
972
  onChange: setMicroLowerDate,
877
- minDate: macroRange[0],
973
+ minDate: macroWindowStart,
878
974
  maxDate: microRange[1],
879
975
  unit: microUnit,
880
976
  }),
881
- [microRange, setMicroLowerDate, macroRange, microUnit],
977
+ [microRange, setMicroLowerDate, macroWindowStart, microUnit],
882
978
  )
883
979
 
884
980
  const microEndSelectorProps: SelectorProps = useMemo(
@@ -977,7 +1073,7 @@ export interface UseVernierSliderPointReturn {
977
1073
  setMacroUpperDate: (date: Date) => void
978
1074
  /** Sets the micro point, clamping it into the macro window / ミクロ時点更新 (マクロ窓へクランプ) */
979
1075
  setMicroDate: (date: Date) => void
980
- /** Shifts the macro window by delta macro units, aborting entirely when either edge would leave [minDate, maxDate] / マクロ窓を delta マクロ単位シフト (片側でも定義域を越える場合は全体中止) */
1076
+ /** Shifts the macro window by delta macro units - or delta positions through the macro candidate set when macroIsDiscrete is on - aborting entirely when either edge would leave [minDate, maxDate] or the candidate set / マクロ窓を delta マクロ単位 (macroIsDiscrete 有効時は候補集合上を delta 個分) シフト (片側でも定義域・候補集合を越える場合は全体中止) */
981
1077
  shiftMacroRange: (delta: number) => void
982
1078
  /** Shifts the micro point by delta micro units - or delta positions through the micro candidate set when microIsDiscrete is on - clamping into the macro window / ミクロ時点を delta ミクロ単位 (microIsDiscrete 有効かつ窓内に候補があるときは候補集合上を delta 個分) シフトしマクロ窓へクランプ */
983
1079
  shiftMicroValue: (delta: number) => void
@@ -996,8 +1092,9 @@ export interface UseVernierSliderPointReturn {
996
1092
  /**
997
1093
  * Custom hook to manage state and props for a Vernier dual-scale PeriodSlider whose micro rail selects one point.
998
1094
  * Two containment invariants hold after every mutation and every render: the macro window stays inside
999
- * [minDate, maxDate], and the micro point stays inside the macro window whose end is
1000
- * getMacroWindowEndDate(macroUnit, microUnit, macroRange[1]).
1095
+ * [minDate, maxDate], and the micro point stays inside
1096
+ * resolveMacroWindow(macroUnit, microUnit, macroRange, minDate, maxDate) - the same window the
1097
+ * slider draws its micro rail over, so no committed micro date can fall outside that rail.
1001
1098
  * ミクロレールが時点を選ぶノギス 2 段スケール PeriodSlider のステート管理と Props 生成を行うカスタムフック。
1002
1099
  *
1003
1100
  * @param options - Required unit, boundaries, initial macro window and micro point plus optional UI props / 必須の単位・境界・初期マクロ窓/ミクロ時点と任意の UI Props
@@ -1025,7 +1122,8 @@ export function useVernierSliderPoint(options: UseVernierSliderPointOptions): Us
1025
1122
  assertValidRangeTuple("useVernierSliderPoint", "initialMacroRange", initialMacroRange)
1026
1123
  assertValidDate("useVernierSliderPoint", "initialMicroValue", initialMicroValue)
1027
1124
  assertRangeWithinBounds("useVernierSliderPoint", "initialMacroRange", initialMacroRange, minDate, maxDate, "[minDate, maxDate]")
1028
- assertWithinBounds("useVernierSliderPoint", "initialMicroValue", initialMicroValue, initialMacroRange[0], getMacroWindowEndDate(macroUnit, microUnit, initialMacroRange[1]), "the initial macro window")
1125
+ const initialMicroWindow = resolveMacroWindow(macroUnit, microUnit, initialMacroRange, minDate, maxDate)
1126
+ assertWithinBounds("useVernierSliderPoint", "initialMicroValue", initialMicroValue, initialMicroWindow[0], initialMicroWindow[1], "the initial macro window")
1029
1127
 
1030
1128
  // レスト構文の転送 Props は毎レンダー別オブジェクトになり、依存に置くと sliderProps のメモ化が常に外れる
1031
1129
  const uiProps = useShallowStable(restUiProps)
@@ -1044,8 +1142,25 @@ export function useVernierSliderPoint(options: UseVernierSliderPointOptions): Us
1044
1142
  setMacroRange(containedMacro)
1045
1143
  }
1046
1144
 
1145
+ // レール・収め込み・セレクタが同じ窓を読むよう、マクロ窓と定義域の交差はレンダーごとに 1 度だけ解決する
1146
+ const microWindow = useMemo<[Date, Date]>(() => resolveMacroWindow(macroUnit, microUnit, macroRange, minDate, maxDate), [macroUnit, microUnit, macroRange, minDate, maxDate])
1147
+
1148
+ // レールが描く候補集合。離散指定が誤っていれば getSortedAllowedDates はここで送出する (更新関数の中まで遅らせない)
1149
+ const microCandidates = useMemo(
1150
+ () => resolveMicroCandidatesIn(microWindow, microUnit, uiProps.microIsDiscrete, uiProps.microAllowedDates ?? uiProps.allowedDates, minDate, maxDate),
1151
+ [microWindow, microUnit, uiProps.microIsDiscrete, uiProps.microAllowedDates, uiProps.allowedDates, minDate, maxDate],
1152
+ )
1153
+
1154
+ // マクロ候補も同じ理由でレンダー中に解決する。更新関数の中で解決すると、誤設定がクリックまで隠れる
1155
+ const macroCandidates = useMemo(
1156
+ () => (uiProps.macroIsDiscrete === true ? getSortedAllowedDates(uiProps.macroAllowedDates ?? uiProps.allowedDates, macroUnit, minDate, maxDate) : []),
1157
+ [uiProps.macroIsDiscrete, uiProps.macroAllowedDates, uiProps.allowedDates, macroUnit, minDate, maxDate],
1158
+ )
1159
+ const macroCandidatesRef = useRef<Date[]>(macroCandidates)
1160
+ macroCandidatesRef.current = macroCandidates
1161
+
1047
1162
  let microValue = microValueState
1048
- const containedMicro = clampDateIntoMacroWindow(microValueState, macroUnit, microUnit, macroRange)
1163
+ const containedMicro = resolveMicroRailDate(microValueState, microUnit, microWindow, microCandidates)
1049
1164
  if (containedMicro !== microValueState) {
1050
1165
  microValue = containedMicro
1051
1166
  setMicroValue(containedMicro)
@@ -1057,8 +1172,8 @@ export function useVernierSliderPoint(options: UseVernierSliderPointOptions): Us
1057
1172
  microValueRef.current = microValue
1058
1173
 
1059
1174
  // 境界と離散候補も ref 経由で読むことで、消費側が毎レンダー新しい Date や配列を渡しても更新関数の同一性を保つ
1060
- const domainRef = useRef({ minDate, maxDate, microIsDiscrete: uiProps.microIsDiscrete, microAllowedDates: uiProps.microAllowedDates, allowedDates: uiProps.allowedDates })
1061
- domainRef.current = { minDate, maxDate, microIsDiscrete: uiProps.microIsDiscrete, microAllowedDates: uiProps.microAllowedDates, allowedDates: uiProps.allowedDates }
1175
+ const domainRef = useRef({ minDate, maxDate, macroIsDiscrete: uiProps.macroIsDiscrete, macroAllowedDates: uiProps.macroAllowedDates, microIsDiscrete: uiProps.microIsDiscrete, microAllowedDates: uiProps.microAllowedDates, allowedDates: uiProps.allowedDates })
1176
+ domainRef.current = { minDate, maxDate, macroIsDiscrete: uiProps.macroIsDiscrete, macroAllowedDates: uiProps.macroAllowedDates, microIsDiscrete: uiProps.microIsDiscrete, microAllowedDates: uiProps.microAllowedDates, allowedDates: uiProps.allowedDates }
1062
1177
 
1063
1178
  // コールバックを ref 経由で読むことで、消費側が毎レンダー新しい関数を渡しても更新関数の同一性を保つ
1064
1179
  const onChangeMacroRangeRef = useRef(onChangeMacroRange)
@@ -1066,6 +1181,23 @@ export function useVernierSliderPoint(options: UseVernierSliderPointOptions): Us
1066
1181
  const onChangeMicroPointRef = useRef(onChangeMicroPoint)
1067
1182
  onChangeMicroPointRef.current = onChangeMicroPoint
1068
1183
 
1184
+ // 更新関数の中では境界を ref 経由で読むため、窓の解決も同じ経路に載せて関数の同一性を保つ
1185
+ const resolveWindow = useCallback(
1186
+ (macro: [Date, Date]): [Date, Date] => {
1187
+ const { minDate: domainLower, maxDate: domainUpper } = domainRef.current
1188
+ return resolveMacroWindow(macroUnit, microUnit, macro, domainLower, domainUpper)
1189
+ },
1190
+ [macroUnit, microUnit],
1191
+ )
1192
+
1193
+ const resolveCandidates = useCallback(
1194
+ (window: [Date, Date]): Date[] => {
1195
+ const { microIsDiscrete, microAllowedDates, allowedDates, minDate: domainLower, maxDate: domainUpper } = domainRef.current
1196
+ return resolveMicroCandidatesIn(window, microUnit, microIsDiscrete, microAllowedDates ?? allowedDates, domainLower, domainUpper)
1197
+ },
1198
+ [microUnit],
1199
+ )
1200
+
1069
1201
  const commitVernier = useCallback((nextMacro: [Date, Date], nextMicro: Date) => {
1070
1202
  const currentMacro = macroRangeRef.current
1071
1203
  const currentMicro = microValueRef.current
@@ -1087,16 +1219,18 @@ export function useVernierSliderPoint(options: UseVernierSliderPointOptions): Us
1087
1219
  const { minDate: domainLower, maxDate: domainUpper } = domainRef.current
1088
1220
  const nextMacro: [Date, Date] = [clampDate(lower, domainLower, domainUpper), clampDate(upper, domainLower, domainUpper)]
1089
1221
  // マクロ窓が動くとミクロ時点がはみ出し得るため、同じ確定経路で窓の内側へ押し込む
1090
- commitVernier(nextMacro, clampDateIntoMacroWindow(microValueRef.current, macroUnit, microUnit, nextMacro))
1222
+ const nextWindow = resolveWindow(nextMacro)
1223
+ commitVernier(nextMacro, resolveMicroRailDate(microValueRef.current, microUnit, nextWindow, resolveCandidates(nextWindow)))
1091
1224
  },
1092
- [macroUnit, microUnit, commitVernier],
1225
+ [microUnit, resolveWindow, resolveCandidates, commitVernier],
1093
1226
  )
1094
1227
 
1095
1228
  const handleChangeMicroPoint = useCallback(
1096
1229
  (value: Date) => {
1097
- commitVernier(macroRangeRef.current, clampDateIntoMacroWindow(value, macroUnit, microUnit, macroRangeRef.current))
1230
+ const currentWindow = resolveWindow(macroRangeRef.current)
1231
+ commitVernier(macroRangeRef.current, resolveMicroRailDate(value, microUnit, currentWindow, resolveCandidates(currentWindow)))
1098
1232
  },
1099
- [macroUnit, microUnit, commitVernier],
1233
+ [microUnit, resolveWindow, resolveCandidates, commitVernier],
1100
1234
  )
1101
1235
 
1102
1236
  const setMacroLowerDate = useCallback(
@@ -1121,6 +1255,17 @@ export function useVernierSliderPoint(options: UseVernierSliderPointOptions): Us
1121
1255
  (delta: number) => {
1122
1256
  const { minDate: domainLower, maxDate: domainUpper } = domainRef.current
1123
1257
  const current = macroRangeRef.current
1258
+ // 離散指定時に暦単位で進めると、スライダーが表示しない日付をステートとコールバックへ流してしまう。
1259
+ // 候補集合はレンダー中に解決済み (誤設定はここではなく描画時点で送出される)
1260
+ const candidates = macroCandidatesRef.current
1261
+ if (candidates.length > 0) {
1262
+ const nextLowIdx = findNearestDateIndex(candidates, current[0], macroUnit) + delta
1263
+ const nextHighIdx = findNearestDateIndex(candidates, current[1], macroUnit) + delta
1264
+ // 窓幅 (候補個数) を保つため、片側でも候補集合を外れるシフトは全体を中止する
1265
+ if (nextLowIdx < 0 || nextHighIdx > candidates.length - 1) return
1266
+ handleChangeMacroRange(candidates[nextLowIdx], candidates[nextHighIdx])
1267
+ return
1268
+ }
1124
1269
  const shiftedLow = addUnit(macroUnit, current[0], delta)
1125
1270
  const shiftedHigh = addUnit(macroUnit, current[1], delta)
1126
1271
  // 窓幅を保つため、片側でも境界を越えるシフトは全体を中止する
@@ -1133,17 +1278,11 @@ export function useVernierSliderPoint(options: UseVernierSliderPointOptions): Us
1133
1278
  const shiftMicroValue = useCallback(
1134
1279
  (delta: number) => {
1135
1280
  const currentMacro = macroRangeRef.current
1136
- const windowEnd = getMacroWindowEndDate(macroUnit, microUnit, currentMacro[1])
1281
+ const currentWindow = resolveWindow(currentMacro)
1137
1282
  // 離散指定時に暦単位で進めると、スライダーが表示しない日付をステートとコールバックへ流してしまう。
1138
1283
  // 離散か否かの正準スイッチは microIsDiscrete であり、候補配列の有無で決めるとスライダー側の
1139
- // microIsDiscreteScale と食い違い、フックだけが候補集合の上を歩く。
1140
- // 候補集合はスライダーと同じく定義域で解決してから窓で絞る。窓を境界として渡すと、
1141
- // 候補を 1 つも含まない窓 (パンで容易に到達する) で getSortedAllowedDates が送出してしまう
1142
- const { microIsDiscrete, microAllowedDates, allowedDates, minDate: domainLower, maxDate: domainUpper } = domainRef.current
1143
- const domainCandidates = microIsDiscrete === true ? getSortedAllowedDates(microAllowedDates ?? allowedDates, microUnit, domainLower, domainUpper) : []
1144
- const windowStartMs = currentMacro[0].getTime()
1145
- const windowEndMs = windowEnd.getTime()
1146
- const candidates = domainCandidates.filter((candidate) => candidate.getTime() >= windowStartMs && candidate.getTime() <= windowEndMs)
1284
+ // microIsDiscreteScale と食い違い、フックだけが候補集合の上を歩く
1285
+ const candidates = resolveCandidates(currentWindow)
1147
1286
  if (candidates.length > 0) {
1148
1287
  handleChangeMicroPoint(stepThroughAllowedDates(candidates, microUnit, microValueRef.current, delta))
1149
1288
  return
@@ -1151,15 +1290,16 @@ export function useVernierSliderPoint(options: UseVernierSliderPointOptions): Us
1151
1290
  // 時点には保つべき幅が無く、端で中止する理由も無い。窓の端へ着けるほうがスライダーのステッパーと終状態が一致する
1152
1291
  handleChangeMicroPoint(addUnit(microUnit, microValueRef.current, delta))
1153
1292
  },
1154
- [macroUnit, microUnit, handleChangeMicroPoint],
1293
+ [microUnit, resolveWindow, resolveCandidates, handleChangeMicroPoint],
1155
1294
  )
1156
1295
 
1157
1296
  const resetVernier = useCallback(() => {
1158
1297
  const initialMacro = initialMacroRef.current
1159
- commitVernier(initialMacro, clampDateIntoMacroWindow(initialMicroRef.current, macroUnit, microUnit, initialMacro))
1160
- }, [macroUnit, microUnit, commitVernier])
1298
+ const initialWindow = resolveWindow(initialMacro)
1299
+ commitVernier(initialMacro, resolveMicroRailDate(initialMicroRef.current, microUnit, initialWindow, resolveCandidates(initialWindow)))
1300
+ }, [microUnit, resolveWindow, resolveCandidates, commitVernier])
1161
1301
 
1162
- const macroWindowEnd = useMemo(() => getMacroWindowEndDate(macroUnit, microUnit, macroRange[1]), [macroUnit, microUnit, macroRange[1]])
1302
+ const [macroWindowStart, macroWindowEnd] = microWindow
1163
1303
 
1164
1304
  const macroStartSelectorProps: SelectorProps = useMemo(
1165
1305
  () => ({
@@ -1187,11 +1327,11 @@ export function useVernierSliderPoint(options: UseVernierSliderPointOptions): Us
1187
1327
  () => ({
1188
1328
  value: microValue,
1189
1329
  onChange: handleChangeMicroPoint,
1190
- minDate: macroRange[0],
1330
+ minDate: macroWindowStart,
1191
1331
  maxDate: macroWindowEnd,
1192
1332
  unit: microUnit,
1193
1333
  }),
1194
- [microValue, handleChangeMicroPoint, macroRange, macroWindowEnd, microUnit],
1334
+ [microValue, handleChangeMicroPoint, macroWindowStart, macroWindowEnd, microUnit],
1195
1335
  )
1196
1336
 
1197
1337
  const sliderProps: PeriodSliderVernierPointProps = useMemo(