@aiquants/period-slider 0.7.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/README.md +137 -25
- package/dist/MonthSelector.d.ts.map +1 -1
- package/dist/PeriodSlider.d.ts +144 -16
- package/dist/PeriodSlider.d.ts.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1743 -1431
- package/dist/styles/period-slider.css +1 -1
- package/dist/styles/period-slider.standalone.css +1 -1
- package/dist/usePeriodSlider.d.ts +96 -10
- package/dist/usePeriodSlider.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/DateSelector.tsx +1 -1
- package/src/MonthSelector.tsx +4 -1
- package/src/PeriodSlider.tsx +751 -218
- package/src/index.ts +14 -3
- package/src/styles/period-slider.css +25 -0
- package/src/usePeriodSlider.ts +510 -45
package/src/usePeriodSlider.ts
CHANGED
|
@@ -7,17 +7,30 @@
|
|
|
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
|
|
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"
|
|
19
21
|
import { useCallback, useMemo, useRef, useState } from "react"
|
|
20
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
addUnit,
|
|
24
|
+
type BasePeriodSliderVernierProps,
|
|
25
|
+
findNearestDateIndex,
|
|
26
|
+
getSortedAllowedDates,
|
|
27
|
+
type PeriodSliderPointProps,
|
|
28
|
+
type PeriodSliderRangeProps,
|
|
29
|
+
type PeriodSliderUnit,
|
|
30
|
+
type PeriodSliderVernierPointProps,
|
|
31
|
+
type PeriodSliderVernierRangeProps,
|
|
32
|
+
resolveMacroWindow,
|
|
33
|
+
} from "./PeriodSlider"
|
|
21
34
|
|
|
22
35
|
// ======================================================================
|
|
23
36
|
// 0. Shared Validation & Helpers
|
|
@@ -133,20 +146,66 @@ function clampDate(date: Date, min: Date, max: Date): Date {
|
|
|
133
146
|
}
|
|
134
147
|
|
|
135
148
|
/**
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
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 が送出するため。
|
|
140
156
|
*
|
|
141
|
-
* @param
|
|
142
|
-
* @param
|
|
143
|
-
* @param
|
|
144
|
-
* @param
|
|
145
|
-
* @
|
|
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 つも定義域に入らない場合
|
|
146
165
|
*/
|
|
147
|
-
function
|
|
148
|
-
|
|
149
|
-
|
|
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)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
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
|
+
* 本体側と同一の規則であり、フックがレールに無い値を確定させることがない。
|
|
181
|
+
*
|
|
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 / レール上の日付 (既に載っていれば入力そのもの)
|
|
187
|
+
*/
|
|
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]
|
|
150
209
|
}
|
|
151
210
|
|
|
152
211
|
/**
|
|
@@ -581,14 +640,14 @@ export function usePeriodSliderPoint(options: UsePeriodSliderPointOptions): UseP
|
|
|
581
640
|
}
|
|
582
641
|
|
|
583
642
|
// ======================================================================
|
|
584
|
-
// 3. Vernier Dual-Scale Hook (
|
|
643
|
+
// 3. Vernier Dual-Scale Hook (useVernierSliderRange)
|
|
585
644
|
// ======================================================================
|
|
586
645
|
|
|
587
646
|
/**
|
|
588
647
|
* Options accepted by the vernier hook; slider props not owned by the hook pass straight through.
|
|
589
648
|
* ノギスフックが受け取るオプション。フックが所有しないスライダー props はそのまま素通し。
|
|
590
649
|
*/
|
|
591
|
-
export interface
|
|
650
|
+
export interface UseVernierSliderRangeOptions extends Partial<Omit<BasePeriodSliderVernierProps, "mode" | "unit" | "minDate" | "maxDate" | "macroUnit" | "microUnit" | "macroRange" | "onChangeMacroRange">> {
|
|
592
651
|
/** Base time resolution unit / 基本時間解像度単位 */
|
|
593
652
|
unit: PeriodSliderUnit
|
|
594
653
|
/** Minimum selectable boundary date for entire macro range (required, no fallback) / 全期間マクロの下限日付 (必須・フォールバックなし) */
|
|
@@ -613,7 +672,7 @@ export interface UseVernierSliderOptions extends Partial<Omit<PeriodSliderVernie
|
|
|
613
672
|
* Macro/micro state, mutators and ready-to-spread props returned by the vernier hook.
|
|
614
673
|
* ノギスフックが返すマクロ・ミクロの状態・更新関数・スプレッド可能な props 一式。
|
|
615
674
|
*/
|
|
616
|
-
export interface
|
|
675
|
+
export interface UseVernierSliderRangeReturn {
|
|
617
676
|
/** Macro window range tuple / マクロ窓期間タプル */
|
|
618
677
|
macroRange: [Date, Date]
|
|
619
678
|
/** State updater for macro range; replaces state without firing onChangeMacroRange / マクロ窓更新関数 (onChangeMacroRange は発火しない) */
|
|
@@ -630,9 +689,9 @@ export interface UseVernierSliderReturn {
|
|
|
630
689
|
setMicroLowerDate: (date: Date) => void
|
|
631
690
|
/** Sets the micro upper date, pushing the micro lower date when undercut and clamping into the macro window / ミクロ終了日更新 (下限を下回ったら下限も追従・マクロ窓へクランプ) */
|
|
632
691
|
setMicroUpperDate: (date: Date) => void
|
|
633
|
-
/** Shifts the macro window by delta macro units
|
|
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 個分) シフト (片側でも定義域・候補集合を越える場合は全体中止) */
|
|
634
693
|
shiftMacroRange: (delta: number) => void
|
|
635
|
-
/** Shifts the micro range by delta micro units
|
|
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 個分) シフト (片側でもマクロ窓・候補集合を越える場合は全体中止) */
|
|
636
695
|
shiftMicroRange: (delta: number) => void
|
|
637
696
|
/** Reset to initial macro and micro ranges / 初期状態へリセット */
|
|
638
697
|
resetVernier: () => void
|
|
@@ -645,14 +704,15 @@ export interface UseVernierSliderReturn {
|
|
|
645
704
|
/** Pre-configured props for micro end selector (maxDate is the effective macro window end) / ミクロ終了日セレクタ用 Props (maxDate は実効マクロ窓終端) */
|
|
646
705
|
microEndSelectorProps: SelectorProps
|
|
647
706
|
/** Pre-configured props to spread directly onto PeriodSlider / PeriodSlider に直接スプレッド可能な Props */
|
|
648
|
-
sliderProps:
|
|
707
|
+
sliderProps: PeriodSliderVernierRangeProps
|
|
649
708
|
}
|
|
650
709
|
|
|
651
710
|
/**
|
|
652
711
|
* Custom hook to manage state and props for a Vernier dual-scale PeriodSlider.
|
|
653
712
|
* Two containment invariants hold after every mutation and every render: the macro window
|
|
654
|
-
* stays inside [minDate, maxDate], and the micro range stays inside
|
|
655
|
-
*
|
|
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.
|
|
656
716
|
* Vernier 2 段スケール PeriodSlider のステート管理と Props 生成を行うカスタムフック。
|
|
657
717
|
*
|
|
658
718
|
* @param options - Required unit, boundaries, initial macro/micro ranges plus optional UI props / 必須の単位・境界・初期マクロ/ミクロ期間と任意の UI Props
|
|
@@ -661,7 +721,7 @@ export interface UseVernierSliderReturn {
|
|
|
661
721
|
*
|
|
662
722
|
* @example
|
|
663
723
|
* ```tsx
|
|
664
|
-
* const { macroRange, microRange, sliderProps, macroStartSelectorProps, macroEndSelectorProps } =
|
|
724
|
+
* const { macroRange, microRange, sliderProps, macroStartSelectorProps, macroEndSelectorProps } = useVernierSliderRange({
|
|
665
725
|
* unit: "month",
|
|
666
726
|
* minDate: new Date(2015, 0, 1),
|
|
667
727
|
* maxDate: new Date(2030, 11, 31),
|
|
@@ -673,14 +733,15 @@ export interface UseVernierSliderReturn {
|
|
|
673
733
|
* return <PeriodSlider {...sliderProps} />
|
|
674
734
|
* ```
|
|
675
735
|
*/
|
|
676
|
-
export function
|
|
736
|
+
export function useVernierSliderRange(options: UseVernierSliderRangeOptions): UseVernierSliderRangeReturn {
|
|
677
737
|
const { unit, minDate, maxDate, initialMacroRange, initialMicroRange, macroUnit = unit, microUnit = unit, onChangeMacroRange, onChangeMicroRange, ...restUiProps } = options
|
|
678
738
|
|
|
679
|
-
assertValidBounds("
|
|
680
|
-
assertValidRangeTuple("
|
|
681
|
-
assertValidRangeTuple("
|
|
682
|
-
assertRangeWithinBounds("
|
|
683
|
-
|
|
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")
|
|
684
745
|
|
|
685
746
|
// レスト構文の転送 Props は毎レンダー別オブジェクトになり、依存に置くと sliderProps のメモ化が常に外れる
|
|
686
747
|
const uiProps = useShallowStable(restUiProps)
|
|
@@ -699,8 +760,25 @@ export function useVernierSlider(options: UseVernierSliderOptions): UseVernierSl
|
|
|
699
760
|
setMacroRange(containedMacro)
|
|
700
761
|
}
|
|
701
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
|
+
|
|
702
780
|
let microRange = microRangeState
|
|
703
|
-
const containedMicro =
|
|
781
|
+
const containedMicro = resolveMicroRailRange(microRangeState, microUnit, microWindow, microCandidates)
|
|
704
782
|
if (containedMicro[0] !== microRangeState[0] || containedMicro[1] !== microRangeState[1]) {
|
|
705
783
|
microRange = containedMicro
|
|
706
784
|
setMicroRange(containedMicro)
|
|
@@ -712,8 +790,8 @@ export function useVernierSlider(options: UseVernierSliderOptions): UseVernierSl
|
|
|
712
790
|
microRangeRef.current = microRange
|
|
713
791
|
|
|
714
792
|
// 境界を ref 経由で読むことで、消費側が毎レンダー新しい Date を渡しても更新関数の同一性を保つ
|
|
715
|
-
const domainRef = useRef({ minDate, maxDate })
|
|
716
|
-
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 }
|
|
717
795
|
|
|
718
796
|
// コールバックを ref 経由で読むことで、消費側が毎レンダー新しい関数を渡しても更新関数の同一性を保つ
|
|
719
797
|
const onChangeMacroRangeRef = useRef(onChangeMacroRange)
|
|
@@ -721,6 +799,23 @@ export function useVernierSlider(options: UseVernierSliderOptions): UseVernierSl
|
|
|
721
799
|
const onChangeMicroRangeRef = useRef(onChangeMicroRange)
|
|
722
800
|
onChangeMicroRangeRef.current = onChangeMicroRange
|
|
723
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
|
+
|
|
724
819
|
const commitVernier = useCallback((nextMacro: [Date, Date], nextMicro: [Date, Date]) => {
|
|
725
820
|
const currentMacro = macroRangeRef.current
|
|
726
821
|
const currentMicro = microRangeRef.current
|
|
@@ -742,16 +837,18 @@ export function useVernierSlider(options: UseVernierSliderOptions): UseVernierSl
|
|
|
742
837
|
const { minDate: domainLower, maxDate: domainUpper } = domainRef.current
|
|
743
838
|
const nextMacro: [Date, Date] = [clampDate(lower, domainLower, domainUpper), clampDate(upper, domainLower, domainUpper)]
|
|
744
839
|
// マクロ窓が動くとミクロ選択がはみ出し得るため、同じ確定経路で窓の内側へ押し込む
|
|
745
|
-
|
|
840
|
+
const nextWindow = resolveWindow(nextMacro)
|
|
841
|
+
commitVernier(nextMacro, resolveMicroRailRange(microRangeRef.current, microUnit, nextWindow, resolveCandidates(nextWindow)))
|
|
746
842
|
},
|
|
747
|
-
[
|
|
843
|
+
[microUnit, resolveWindow, resolveCandidates, commitVernier],
|
|
748
844
|
)
|
|
749
845
|
|
|
750
846
|
const handleChangeMicroRange = useCallback(
|
|
751
847
|
(lower: Date, upper: Date) => {
|
|
752
|
-
|
|
848
|
+
const currentWindow = resolveWindow(macroRangeRef.current)
|
|
849
|
+
commitVernier(macroRangeRef.current, resolveMicroRailRange([lower, upper], microUnit, currentWindow, resolveCandidates(currentWindow)))
|
|
753
850
|
},
|
|
754
|
-
[
|
|
851
|
+
[microUnit, resolveWindow, resolveCandidates, commitVernier],
|
|
755
852
|
)
|
|
756
853
|
|
|
757
854
|
const setMacroLowerDate = useCallback(
|
|
@@ -794,6 +891,17 @@ export function useVernierSlider(options: UseVernierSliderOptions): UseVernierSl
|
|
|
794
891
|
(delta: number) => {
|
|
795
892
|
const { minDate: domainLower, maxDate: domainUpper } = domainRef.current
|
|
796
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
|
+
}
|
|
797
905
|
const shiftedLow = addUnit(macroUnit, current[0], delta)
|
|
798
906
|
const shiftedHigh = addUnit(macroUnit, current[1], delta)
|
|
799
907
|
// 窓幅を保つため、片側でも境界を越えるシフトは全体を中止する
|
|
@@ -807,21 +915,34 @@ export function useVernierSlider(options: UseVernierSliderOptions): UseVernierSl
|
|
|
807
915
|
(delta: number) => {
|
|
808
916
|
const currentMacro = macroRangeRef.current
|
|
809
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
|
+
}
|
|
810
930
|
const shiftedLow = addUnit(microUnit, currentMicro[0], delta)
|
|
811
931
|
const shiftedHigh = addUnit(microUnit, currentMicro[1], delta)
|
|
812
932
|
// ミクロ選択範囲はマクロ窓の内側に限られるため、はみ出すシフトは中止する
|
|
813
|
-
if (shiftedLow <
|
|
933
|
+
if (shiftedLow < windowStart || shiftedHigh > windowEnd) return
|
|
814
934
|
handleChangeMicroRange(shiftedLow, shiftedHigh)
|
|
815
935
|
},
|
|
816
|
-
[
|
|
936
|
+
[microUnit, resolveWindow, resolveCandidates, handleChangeMicroRange],
|
|
817
937
|
)
|
|
818
938
|
|
|
819
939
|
const resetVernier = useCallback(() => {
|
|
820
940
|
const initialMacro = initialMacroRef.current
|
|
821
|
-
|
|
822
|
-
|
|
941
|
+
const initialWindow = resolveWindow(initialMacro)
|
|
942
|
+
commitVernier(initialMacro, resolveMicroRailRange(initialMicroRef.current, microUnit, initialWindow, resolveCandidates(initialWindow)))
|
|
943
|
+
}, [microUnit, resolveWindow, resolveCandidates, commitVernier])
|
|
823
944
|
|
|
824
|
-
const
|
|
945
|
+
const [macroWindowStart, macroWindowEnd] = microWindow
|
|
825
946
|
|
|
826
947
|
const macroStartSelectorProps: SelectorProps = useMemo(
|
|
827
948
|
() => ({
|
|
@@ -849,11 +970,11 @@ export function useVernierSlider(options: UseVernierSliderOptions): UseVernierSl
|
|
|
849
970
|
() => ({
|
|
850
971
|
value: microRange[0],
|
|
851
972
|
onChange: setMicroLowerDate,
|
|
852
|
-
minDate:
|
|
973
|
+
minDate: macroWindowStart,
|
|
853
974
|
maxDate: microRange[1],
|
|
854
975
|
unit: microUnit,
|
|
855
976
|
}),
|
|
856
|
-
[microRange, setMicroLowerDate,
|
|
977
|
+
[microRange, setMicroLowerDate, macroWindowStart, microUnit],
|
|
857
978
|
)
|
|
858
979
|
|
|
859
980
|
const microEndSelectorProps: SelectorProps = useMemo(
|
|
@@ -867,7 +988,7 @@ export function useVernierSlider(options: UseVernierSliderOptions): UseVernierSl
|
|
|
867
988
|
[microRange, setMicroUpperDate, macroWindowEnd, microUnit],
|
|
868
989
|
)
|
|
869
990
|
|
|
870
|
-
const sliderProps:
|
|
991
|
+
const sliderProps: PeriodSliderVernierRangeProps = useMemo(
|
|
871
992
|
() => ({
|
|
872
993
|
...uiProps,
|
|
873
994
|
mode: "vernier",
|
|
@@ -903,3 +1024,347 @@ export function useVernierSlider(options: UseVernierSliderOptions): UseVernierSl
|
|
|
903
1024
|
sliderProps,
|
|
904
1025
|
}
|
|
905
1026
|
}
|
|
1027
|
+
|
|
1028
|
+
// ======================================================================
|
|
1029
|
+
// 4. Vernier Dual-Scale Point Hook (useVernierSliderPoint)
|
|
1030
|
+
// ======================================================================
|
|
1031
|
+
|
|
1032
|
+
/**
|
|
1033
|
+
* Options accepted by the vernier point hook; slider props not owned by the hook pass straight through.
|
|
1034
|
+
* ノギス時点フックが受け取るオプション。フックが所有しないスライダー props はそのまま素通し。
|
|
1035
|
+
*/
|
|
1036
|
+
export interface UseVernierSliderPointOptions extends Partial<Omit<BasePeriodSliderVernierProps, "mode" | "unit" | "minDate" | "maxDate" | "macroUnit" | "microUnit" | "macroRange" | "onChangeMacroRange">> {
|
|
1037
|
+
/** Base time resolution unit / 基本時間解像度単位 */
|
|
1038
|
+
unit: PeriodSliderUnit
|
|
1039
|
+
/** Minimum selectable boundary date for entire macro range (required, no fallback) / 全期間マクロの下限日付 (必須・フォールバックなし) */
|
|
1040
|
+
minDate: Date
|
|
1041
|
+
/** Maximum selectable boundary date for entire macro range, strictly later than minDate (required, no fallback) / 全期間マクロの上限日付 (必須・minDate より後・フォールバックなし) */
|
|
1042
|
+
maxDate: Date
|
|
1043
|
+
/** Initial macro window range [start, end] inside [minDate, maxDate] (required, no fallback) / 初期マクロ窓期間 (必須・[minDate, maxDate] 内・フォールバックなし) */
|
|
1044
|
+
initialMacroRange: [Date, Date]
|
|
1045
|
+
/** Initial micro selected point inside the initial macro window (required, no fallback) / 初期ミクロ選択時点 (必須・初期マクロ窓の内側・フォールバックなし) */
|
|
1046
|
+
initialMicroValue: Date
|
|
1047
|
+
/** Macro scale unit (defaults to unit) / マクロ単位 (既定: unit) */
|
|
1048
|
+
macroUnit?: PeriodSliderUnit
|
|
1049
|
+
/** Micro scale unit (defaults to unit) / ミクロ単位 (既定: unit) */
|
|
1050
|
+
microUnit?: PeriodSliderUnit
|
|
1051
|
+
/** Callback triggered when macro window changes / マクロ窓変更時コールバック */
|
|
1052
|
+
onChangeMacroRange?: (lower: Date, upper: Date) => void
|
|
1053
|
+
/** Callback triggered when the micro point changes / ミクロ選択時点変更時コールバック */
|
|
1054
|
+
onChangeMicroPoint?: (value: Date) => void
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
/**
|
|
1058
|
+
* Macro window state, micro point state, mutators and ready-to-spread props returned by the vernier point hook.
|
|
1059
|
+
* ノギス時点フックが返すマクロ窓・ミクロ時点の状態・更新関数・スプレッド可能な props 一式。
|
|
1060
|
+
*/
|
|
1061
|
+
export interface UseVernierSliderPointReturn {
|
|
1062
|
+
/** Macro window range tuple / マクロ窓期間タプル */
|
|
1063
|
+
macroRange: [Date, Date]
|
|
1064
|
+
/** State updater for macro range; replaces state without firing onChangeMacroRange / マクロ窓更新関数 (onChangeMacroRange は発火しない) */
|
|
1065
|
+
setMacroRange: React.Dispatch<React.SetStateAction<[Date, Date]>>
|
|
1066
|
+
/** Micro selected point / ミクロ選択時点 */
|
|
1067
|
+
microValue: Date
|
|
1068
|
+
/** State updater for the micro point; replaces state without firing onChangeMicroPoint / ミクロ時点更新関数 (onChangeMicroPoint は発火しない) */
|
|
1069
|
+
setMicroValue: React.Dispatch<React.SetStateAction<Date>>
|
|
1070
|
+
/** Sets the macro lower date, pushing the macro upper date when overtaken and clamping the window into [minDate, maxDate] / マクロ開始日更新 (上限を超えたら上限も追従・窓を [minDate, maxDate] へクランプ) */
|
|
1071
|
+
setMacroLowerDate: (date: Date) => void
|
|
1072
|
+
/** Sets the macro upper date, pushing the macro lower date when undercut and clamping the window into [minDate, maxDate] / マクロ終了日更新 (下限を下回ったら下限も追従・窓を [minDate, maxDate] へクランプ) */
|
|
1073
|
+
setMacroUpperDate: (date: Date) => void
|
|
1074
|
+
/** Sets the micro point, clamping it into the macro window / ミクロ時点更新 (マクロ窓へクランプ) */
|
|
1075
|
+
setMicroDate: (date: Date) => void
|
|
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 個分) シフト (片側でも定義域・候補集合を越える場合は全体中止) */
|
|
1077
|
+
shiftMacroRange: (delta: number) => void
|
|
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 個分) シフトしマクロ窓へクランプ */
|
|
1079
|
+
shiftMicroValue: (delta: number) => void
|
|
1080
|
+
/** Reset to the initial macro window and micro point / 初期状態へリセット */
|
|
1081
|
+
resetVernier: () => void
|
|
1082
|
+
/** Pre-configured props for macro start selector / マクロ開始日セレクタ用 Props */
|
|
1083
|
+
macroStartSelectorProps: SelectorProps
|
|
1084
|
+
/** Pre-configured props for macro end selector / マクロ終了日セレクタ用 Props */
|
|
1085
|
+
macroEndSelectorProps: SelectorProps
|
|
1086
|
+
/** Pre-configured props for the micro point selector (bounded by the effective macro window) / ミクロ時点セレクタ用 Props (実効マクロ窓で境界付け) */
|
|
1087
|
+
microSelectorProps: SelectorProps
|
|
1088
|
+
/** Pre-configured props to spread directly onto PeriodSlider / PeriodSlider に直接スプレッド可能な Props */
|
|
1089
|
+
sliderProps: PeriodSliderVernierPointProps
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
/**
|
|
1093
|
+
* Custom hook to manage state and props for a Vernier dual-scale PeriodSlider whose micro rail selects one point.
|
|
1094
|
+
* Two containment invariants hold after every mutation and every render: the macro window stays inside
|
|
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.
|
|
1098
|
+
* ミクロレールが時点を選ぶノギス 2 段スケール PeriodSlider のステート管理と Props 生成を行うカスタムフック。
|
|
1099
|
+
*
|
|
1100
|
+
* @param options - Required unit, boundaries, initial macro window and micro point plus optional UI props / 必須の単位・境界・初期マクロ窓/ミクロ時点と任意の UI Props
|
|
1101
|
+
* @returns Dual-scale state, mutators and pre-configured slider/selector props / 2 段スケールのステート・更新関数・生成済み Props 群
|
|
1102
|
+
* @throws {RangeError} When a boundary, the initial macro window or the initial micro point is missing, invalid, inverted, outside [minDate, maxDate], or (for initialMicroValue) outside the initial macro window / 境界・初期マクロ窓・初期ミクロ時点が欠落、不正、逆転、[minDate, maxDate] 外、または (initialMicroValue が) 初期マクロ窓の外にある場合
|
|
1103
|
+
*
|
|
1104
|
+
* @example
|
|
1105
|
+
* ```tsx
|
|
1106
|
+
* const { microValue, sliderProps, macroStartSelectorProps, microSelectorProps } = useVernierSliderPoint({
|
|
1107
|
+
* unit: "month",
|
|
1108
|
+
* minDate: new Date(2015, 0, 1),
|
|
1109
|
+
* maxDate: new Date(2030, 11, 31),
|
|
1110
|
+
* initialMacroRange: [new Date(2020, 0, 1), new Date(2026, 0, 1)],
|
|
1111
|
+
* initialMicroValue: new Date(2023, 5, 1),
|
|
1112
|
+
* macroUnit: "year",
|
|
1113
|
+
* microUnit: "month",
|
|
1114
|
+
* })
|
|
1115
|
+
* return <PeriodSlider {...sliderProps} />
|
|
1116
|
+
* ```
|
|
1117
|
+
*/
|
|
1118
|
+
export function useVernierSliderPoint(options: UseVernierSliderPointOptions): UseVernierSliderPointReturn {
|
|
1119
|
+
const { unit, minDate, maxDate, initialMacroRange, initialMicroValue, macroUnit = unit, microUnit = unit, onChangeMacroRange, onChangeMicroPoint, ...restUiProps } = options
|
|
1120
|
+
|
|
1121
|
+
assertValidBounds("useVernierSliderPoint", minDate, maxDate)
|
|
1122
|
+
assertValidRangeTuple("useVernierSliderPoint", "initialMacroRange", initialMacroRange)
|
|
1123
|
+
assertValidDate("useVernierSliderPoint", "initialMicroValue", initialMicroValue)
|
|
1124
|
+
assertRangeWithinBounds("useVernierSliderPoint", "initialMacroRange", initialMacroRange, minDate, maxDate, "[minDate, maxDate]")
|
|
1125
|
+
const initialMicroWindow = resolveMacroWindow(macroUnit, microUnit, initialMacroRange, minDate, maxDate)
|
|
1126
|
+
assertWithinBounds("useVernierSliderPoint", "initialMicroValue", initialMicroValue, initialMicroWindow[0], initialMicroWindow[1], "the initial macro window")
|
|
1127
|
+
|
|
1128
|
+
// レスト構文の転送 Props は毎レンダー別オブジェクトになり、依存に置くと sliderProps のメモ化が常に外れる
|
|
1129
|
+
const uiProps = useShallowStable(restUiProps)
|
|
1130
|
+
|
|
1131
|
+
const initialMacroRef = useRef<[Date, Date]>(initialMacroRange)
|
|
1132
|
+
const initialMicroRef = useRef<Date>(initialMicroValue)
|
|
1133
|
+
|
|
1134
|
+
const [macroRangeState, setMacroRange] = useState<[Date, Date]>(initialMacroRange)
|
|
1135
|
+
const [microValueState, setMicroValue] = useState<Date>(initialMicroValue)
|
|
1136
|
+
|
|
1137
|
+
// 境界オプションもマクロ窓も実行中に動き得るため、包含関係が崩れたステートはレンダー中に畳み込む (aria-valuenow が valuemin/valuemax を外れないため)
|
|
1138
|
+
let macroRange = macroRangeState
|
|
1139
|
+
const containedMacro: [Date, Date] = [clampDate(macroRangeState[0], minDate, maxDate), clampDate(macroRangeState[1], minDate, maxDate)]
|
|
1140
|
+
if (containedMacro[0] !== macroRangeState[0] || containedMacro[1] !== macroRangeState[1]) {
|
|
1141
|
+
macroRange = containedMacro
|
|
1142
|
+
setMacroRange(containedMacro)
|
|
1143
|
+
}
|
|
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
|
+
|
|
1162
|
+
let microValue = microValueState
|
|
1163
|
+
const containedMicro = resolveMicroRailDate(microValueState, microUnit, microWindow, microCandidates)
|
|
1164
|
+
if (containedMicro !== microValueState) {
|
|
1165
|
+
microValue = containedMicro
|
|
1166
|
+
setMicroValue(containedMicro)
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
const macroRangeRef = useRef<[Date, Date]>(macroRange)
|
|
1170
|
+
macroRangeRef.current = macroRange
|
|
1171
|
+
const microValueRef = useRef<Date>(microValue)
|
|
1172
|
+
microValueRef.current = microValue
|
|
1173
|
+
|
|
1174
|
+
// 境界と離散候補も ref 経由で読むことで、消費側が毎レンダー新しい Date や配列を渡しても更新関数の同一性を保つ
|
|
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 }
|
|
1177
|
+
|
|
1178
|
+
// コールバックを ref 経由で読むことで、消費側が毎レンダー新しい関数を渡しても更新関数の同一性を保つ
|
|
1179
|
+
const onChangeMacroRangeRef = useRef(onChangeMacroRange)
|
|
1180
|
+
onChangeMacroRangeRef.current = onChangeMacroRange
|
|
1181
|
+
const onChangeMicroPointRef = useRef(onChangeMicroPoint)
|
|
1182
|
+
onChangeMicroPointRef.current = onChangeMicroPoint
|
|
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
|
+
|
|
1201
|
+
const commitVernier = useCallback((nextMacro: [Date, Date], nextMicro: Date) => {
|
|
1202
|
+
const currentMacro = macroRangeRef.current
|
|
1203
|
+
const currentMicro = microValueRef.current
|
|
1204
|
+
// 動いていないスケールのコールバックを撃つと、消費側が無関係な再取得を行う
|
|
1205
|
+
if (nextMacro[0].getTime() !== currentMacro[0].getTime() || nextMacro[1].getTime() !== currentMacro[1].getTime()) {
|
|
1206
|
+
macroRangeRef.current = nextMacro
|
|
1207
|
+
setMacroRange(nextMacro)
|
|
1208
|
+
onChangeMacroRangeRef.current?.(nextMacro[0], nextMacro[1])
|
|
1209
|
+
}
|
|
1210
|
+
if (nextMicro.getTime() !== currentMicro.getTime()) {
|
|
1211
|
+
microValueRef.current = nextMicro
|
|
1212
|
+
setMicroValue(nextMicro)
|
|
1213
|
+
onChangeMicroPointRef.current?.(nextMicro)
|
|
1214
|
+
}
|
|
1215
|
+
}, [])
|
|
1216
|
+
|
|
1217
|
+
const handleChangeMacroRange = useCallback(
|
|
1218
|
+
(lower: Date, upper: Date) => {
|
|
1219
|
+
const { minDate: domainLower, maxDate: domainUpper } = domainRef.current
|
|
1220
|
+
const nextMacro: [Date, Date] = [clampDate(lower, domainLower, domainUpper), clampDate(upper, domainLower, domainUpper)]
|
|
1221
|
+
// マクロ窓が動くとミクロ時点がはみ出し得るため、同じ確定経路で窓の内側へ押し込む
|
|
1222
|
+
const nextWindow = resolveWindow(nextMacro)
|
|
1223
|
+
commitVernier(nextMacro, resolveMicroRailDate(microValueRef.current, microUnit, nextWindow, resolveCandidates(nextWindow)))
|
|
1224
|
+
},
|
|
1225
|
+
[microUnit, resolveWindow, resolveCandidates, commitVernier],
|
|
1226
|
+
)
|
|
1227
|
+
|
|
1228
|
+
const handleChangeMicroPoint = useCallback(
|
|
1229
|
+
(value: Date) => {
|
|
1230
|
+
const currentWindow = resolveWindow(macroRangeRef.current)
|
|
1231
|
+
commitVernier(macroRangeRef.current, resolveMicroRailDate(value, microUnit, currentWindow, resolveCandidates(currentWindow)))
|
|
1232
|
+
},
|
|
1233
|
+
[microUnit, resolveWindow, resolveCandidates, commitVernier],
|
|
1234
|
+
)
|
|
1235
|
+
|
|
1236
|
+
const setMacroLowerDate = useCallback(
|
|
1237
|
+
(date: Date) => {
|
|
1238
|
+
const currentUpper = macroRangeRef.current[1]
|
|
1239
|
+
const nextUpper = date > currentUpper ? date : currentUpper
|
|
1240
|
+
handleChangeMacroRange(date, nextUpper)
|
|
1241
|
+
},
|
|
1242
|
+
[handleChangeMacroRange],
|
|
1243
|
+
)
|
|
1244
|
+
|
|
1245
|
+
const setMacroUpperDate = useCallback(
|
|
1246
|
+
(date: Date) => {
|
|
1247
|
+
const currentLower = macroRangeRef.current[0]
|
|
1248
|
+
const nextLower = date < currentLower ? date : currentLower
|
|
1249
|
+
handleChangeMacroRange(nextLower, date)
|
|
1250
|
+
},
|
|
1251
|
+
[handleChangeMacroRange],
|
|
1252
|
+
)
|
|
1253
|
+
|
|
1254
|
+
const shiftMacroRange = useCallback(
|
|
1255
|
+
(delta: number) => {
|
|
1256
|
+
const { minDate: domainLower, maxDate: domainUpper } = domainRef.current
|
|
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
|
+
}
|
|
1269
|
+
const shiftedLow = addUnit(macroUnit, current[0], delta)
|
|
1270
|
+
const shiftedHigh = addUnit(macroUnit, current[1], delta)
|
|
1271
|
+
// 窓幅を保つため、片側でも境界を越えるシフトは全体を中止する
|
|
1272
|
+
if (shiftedLow < domainLower || shiftedHigh > domainUpper) return
|
|
1273
|
+
handleChangeMacroRange(shiftedLow, shiftedHigh)
|
|
1274
|
+
},
|
|
1275
|
+
[macroUnit, handleChangeMacroRange],
|
|
1276
|
+
)
|
|
1277
|
+
|
|
1278
|
+
const shiftMicroValue = useCallback(
|
|
1279
|
+
(delta: number) => {
|
|
1280
|
+
const currentMacro = macroRangeRef.current
|
|
1281
|
+
const currentWindow = resolveWindow(currentMacro)
|
|
1282
|
+
// 離散指定時に暦単位で進めると、スライダーが表示しない日付をステートとコールバックへ流してしまう。
|
|
1283
|
+
// 離散か否かの正準スイッチは microIsDiscrete であり、候補配列の有無で決めるとスライダー側の
|
|
1284
|
+
// microIsDiscreteScale と食い違い、フックだけが候補集合の上を歩く
|
|
1285
|
+
const candidates = resolveCandidates(currentWindow)
|
|
1286
|
+
if (candidates.length > 0) {
|
|
1287
|
+
handleChangeMicroPoint(stepThroughAllowedDates(candidates, microUnit, microValueRef.current, delta))
|
|
1288
|
+
return
|
|
1289
|
+
}
|
|
1290
|
+
// 時点には保つべき幅が無く、端で中止する理由も無い。窓の端へ着けるほうがスライダーのステッパーと終状態が一致する
|
|
1291
|
+
handleChangeMicroPoint(addUnit(microUnit, microValueRef.current, delta))
|
|
1292
|
+
},
|
|
1293
|
+
[microUnit, resolveWindow, resolveCandidates, handleChangeMicroPoint],
|
|
1294
|
+
)
|
|
1295
|
+
|
|
1296
|
+
const resetVernier = useCallback(() => {
|
|
1297
|
+
const initialMacro = initialMacroRef.current
|
|
1298
|
+
const initialWindow = resolveWindow(initialMacro)
|
|
1299
|
+
commitVernier(initialMacro, resolveMicroRailDate(initialMicroRef.current, microUnit, initialWindow, resolveCandidates(initialWindow)))
|
|
1300
|
+
}, [microUnit, resolveWindow, resolveCandidates, commitVernier])
|
|
1301
|
+
|
|
1302
|
+
const [macroWindowStart, macroWindowEnd] = microWindow
|
|
1303
|
+
|
|
1304
|
+
const macroStartSelectorProps: SelectorProps = useMemo(
|
|
1305
|
+
() => ({
|
|
1306
|
+
value: macroRange[0],
|
|
1307
|
+
onChange: setMacroLowerDate,
|
|
1308
|
+
minDate,
|
|
1309
|
+
maxDate: macroRange[1],
|
|
1310
|
+
unit: macroUnit,
|
|
1311
|
+
}),
|
|
1312
|
+
[macroRange, setMacroLowerDate, minDate, macroUnit],
|
|
1313
|
+
)
|
|
1314
|
+
|
|
1315
|
+
const macroEndSelectorProps: SelectorProps = useMemo(
|
|
1316
|
+
() => ({
|
|
1317
|
+
value: macroRange[1],
|
|
1318
|
+
onChange: setMacroUpperDate,
|
|
1319
|
+
minDate: macroRange[0],
|
|
1320
|
+
maxDate,
|
|
1321
|
+
unit: macroUnit,
|
|
1322
|
+
}),
|
|
1323
|
+
[macroRange, setMacroUpperDate, maxDate, macroUnit],
|
|
1324
|
+
)
|
|
1325
|
+
|
|
1326
|
+
const microSelectorProps: SelectorProps = useMemo(
|
|
1327
|
+
() => ({
|
|
1328
|
+
value: microValue,
|
|
1329
|
+
onChange: handleChangeMicroPoint,
|
|
1330
|
+
minDate: macroWindowStart,
|
|
1331
|
+
maxDate: macroWindowEnd,
|
|
1332
|
+
unit: microUnit,
|
|
1333
|
+
}),
|
|
1334
|
+
[microValue, handleChangeMicroPoint, macroWindowStart, macroWindowEnd, microUnit],
|
|
1335
|
+
)
|
|
1336
|
+
|
|
1337
|
+
const sliderProps: PeriodSliderVernierPointProps = useMemo(
|
|
1338
|
+
() => ({
|
|
1339
|
+
...uiProps,
|
|
1340
|
+
mode: "vernier",
|
|
1341
|
+
unit,
|
|
1342
|
+
macroUnit,
|
|
1343
|
+
microUnit,
|
|
1344
|
+
minDate,
|
|
1345
|
+
maxDate,
|
|
1346
|
+
macroRange,
|
|
1347
|
+
microValue,
|
|
1348
|
+
onChangeMacroRange: handleChangeMacroRange,
|
|
1349
|
+
onChangeMicroPoint: handleChangeMicroPoint,
|
|
1350
|
+
}),
|
|
1351
|
+
[uiProps, unit, macroUnit, microUnit, minDate, maxDate, macroRange, microValue, handleChangeMacroRange, handleChangeMicroPoint],
|
|
1352
|
+
)
|
|
1353
|
+
|
|
1354
|
+
return {
|
|
1355
|
+
macroRange,
|
|
1356
|
+
setMacroRange,
|
|
1357
|
+
microValue,
|
|
1358
|
+
setMicroValue,
|
|
1359
|
+
setMacroLowerDate,
|
|
1360
|
+
setMacroUpperDate,
|
|
1361
|
+
setMicroDate: handleChangeMicroPoint,
|
|
1362
|
+
shiftMacroRange,
|
|
1363
|
+
shiftMicroValue,
|
|
1364
|
+
resetVernier,
|
|
1365
|
+
macroStartSelectorProps,
|
|
1366
|
+
macroEndSelectorProps,
|
|
1367
|
+
microSelectorProps,
|
|
1368
|
+
sliderProps,
|
|
1369
|
+
}
|
|
1370
|
+
}
|