@aiquants/period-slider 0.7.0 → 0.8.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 +125 -20
- package/dist/PeriodSlider.d.ts +66 -16
- package/dist/PeriodSlider.d.ts.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1591 -1413
- package/dist/usePeriodSlider.d.ts +87 -3
- package/dist/usePeriodSlider.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/PeriodSlider.tsx +384 -157
- package/src/index.ts +7 -0
- package/src/usePeriodSlider.ts +329 -4
package/src/index.ts
CHANGED
|
@@ -23,6 +23,7 @@ export { MonthSelector } from "./MonthSelector"
|
|
|
23
23
|
export type {
|
|
24
24
|
AccessibleSliderLabels,
|
|
25
25
|
BasePeriodSliderProps,
|
|
26
|
+
BasePeriodSliderVernierProps,
|
|
26
27
|
ColorSchemePalette,
|
|
27
28
|
ConnectDragValueOptions,
|
|
28
29
|
LabelSlantConfig,
|
|
@@ -34,8 +35,11 @@ export type {
|
|
|
34
35
|
PeriodSliderRangeProps,
|
|
35
36
|
PeriodSliderSnapMode,
|
|
36
37
|
PeriodSliderUnit,
|
|
38
|
+
PeriodSliderVernierPointProps,
|
|
37
39
|
PeriodSliderVernierProps,
|
|
40
|
+
PeriodSliderVernierRangeProps,
|
|
38
41
|
VernierHandle,
|
|
42
|
+
VernierMicroShape,
|
|
39
43
|
} from "./PeriodSlider"
|
|
40
44
|
export {
|
|
41
45
|
addUnit,
|
|
@@ -69,12 +73,15 @@ export type {
|
|
|
69
73
|
UsePeriodSliderRangeOptions,
|
|
70
74
|
UsePeriodSliderRangeReturn,
|
|
71
75
|
UseVernierSliderOptions,
|
|
76
|
+
UseVernierSliderPointOptions,
|
|
77
|
+
UseVernierSliderPointReturn,
|
|
72
78
|
UseVernierSliderReturn,
|
|
73
79
|
} from "./usePeriodSlider"
|
|
74
80
|
export {
|
|
75
81
|
usePeriodSliderPoint,
|
|
76
82
|
usePeriodSliderRange,
|
|
77
83
|
useVernierSlider,
|
|
84
|
+
useVernierSliderPoint,
|
|
78
85
|
} from "./usePeriodSlider"
|
|
79
86
|
export type { YearSelectorProps } from "./YearSelector"
|
|
80
87
|
export { YearSelector } from "./YearSelector"
|
package/src/usePeriodSlider.ts
CHANGED
|
@@ -17,7 +17,18 @@
|
|
|
17
17
|
|
|
18
18
|
import type React from "react"
|
|
19
19
|
import { useCallback, useMemo, useRef, useState } from "react"
|
|
20
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
addUnit,
|
|
22
|
+
type BasePeriodSliderVernierProps,
|
|
23
|
+
findNearestDateIndex,
|
|
24
|
+
getMacroWindowEndDate,
|
|
25
|
+
getSortedAllowedDates,
|
|
26
|
+
type PeriodSliderPointProps,
|
|
27
|
+
type PeriodSliderRangeProps,
|
|
28
|
+
type PeriodSliderUnit,
|
|
29
|
+
type PeriodSliderVernierPointProps,
|
|
30
|
+
type PeriodSliderVernierRangeProps,
|
|
31
|
+
} from "./PeriodSlider"
|
|
21
32
|
|
|
22
33
|
// ======================================================================
|
|
23
34
|
// 0. Shared Validation & Helpers
|
|
@@ -149,6 +160,20 @@ function clampRangeIntoMacroWindow(range: [Date, Date], macroUnit: PeriodSliderU
|
|
|
149
160
|
return [clampDate(range[0], macroRange[0], windowEnd), clampDate(range[1], macroRange[0], windowEnd)]
|
|
150
161
|
}
|
|
151
162
|
|
|
163
|
+
/**
|
|
164
|
+
* Clamps a single micro date into the effective macro window.
|
|
165
|
+
* ミクロ時点 1 点を実効マクロ窓 (終端は getMacroWindowEndDate) の内側へ収める。
|
|
166
|
+
*
|
|
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 / マクロ窓の内側であることが保証されたミクロ日付
|
|
172
|
+
*/
|
|
173
|
+
function clampDateIntoMacroWindow(date: Date, macroUnit: PeriodSliderUnit, microUnit: PeriodSliderUnit, macroRange: [Date, Date]): Date {
|
|
174
|
+
return clampDate(date, macroRange[0], getMacroWindowEndDate(macroUnit, microUnit, macroRange[1]))
|
|
175
|
+
}
|
|
176
|
+
|
|
152
177
|
/**
|
|
153
178
|
* Steps a date through the discrete allowed set by index, clamping at both ends of the set.
|
|
154
179
|
* 離散候補集合をインデックスで歩き、集合の両端でクランプした日付を返す関数。
|
|
@@ -588,7 +613,7 @@ export function usePeriodSliderPoint(options: UsePeriodSliderPointOptions): UseP
|
|
|
588
613
|
* Options accepted by the vernier hook; slider props not owned by the hook pass straight through.
|
|
589
614
|
* ノギスフックが受け取るオプション。フックが所有しないスライダー props はそのまま素通し。
|
|
590
615
|
*/
|
|
591
|
-
export interface UseVernierSliderOptions extends Partial<Omit<
|
|
616
|
+
export interface UseVernierSliderOptions extends Partial<Omit<BasePeriodSliderVernierProps, "mode" | "unit" | "minDate" | "maxDate" | "macroUnit" | "microUnit" | "macroRange" | "onChangeMacroRange">> {
|
|
592
617
|
/** Base time resolution unit / 基本時間解像度単位 */
|
|
593
618
|
unit: PeriodSliderUnit
|
|
594
619
|
/** Minimum selectable boundary date for entire macro range (required, no fallback) / 全期間マクロの下限日付 (必須・フォールバックなし) */
|
|
@@ -645,7 +670,7 @@ export interface UseVernierSliderReturn {
|
|
|
645
670
|
/** Pre-configured props for micro end selector (maxDate is the effective macro window end) / ミクロ終了日セレクタ用 Props (maxDate は実効マクロ窓終端) */
|
|
646
671
|
microEndSelectorProps: SelectorProps
|
|
647
672
|
/** Pre-configured props to spread directly onto PeriodSlider / PeriodSlider に直接スプレッド可能な Props */
|
|
648
|
-
sliderProps:
|
|
673
|
+
sliderProps: PeriodSliderVernierRangeProps
|
|
649
674
|
}
|
|
650
675
|
|
|
651
676
|
/**
|
|
@@ -867,7 +892,7 @@ export function useVernierSlider(options: UseVernierSliderOptions): UseVernierSl
|
|
|
867
892
|
[microRange, setMicroUpperDate, macroWindowEnd, microUnit],
|
|
868
893
|
)
|
|
869
894
|
|
|
870
|
-
const sliderProps:
|
|
895
|
+
const sliderProps: PeriodSliderVernierRangeProps = useMemo(
|
|
871
896
|
() => ({
|
|
872
897
|
...uiProps,
|
|
873
898
|
mode: "vernier",
|
|
@@ -903,3 +928,303 @@ export function useVernierSlider(options: UseVernierSliderOptions): UseVernierSl
|
|
|
903
928
|
sliderProps,
|
|
904
929
|
}
|
|
905
930
|
}
|
|
931
|
+
|
|
932
|
+
// ======================================================================
|
|
933
|
+
// 4. Vernier Dual-Scale Point Hook (useVernierSliderPoint)
|
|
934
|
+
// ======================================================================
|
|
935
|
+
|
|
936
|
+
/**
|
|
937
|
+
* Options accepted by the vernier point hook; slider props not owned by the hook pass straight through.
|
|
938
|
+
* ノギス時点フックが受け取るオプション。フックが所有しないスライダー props はそのまま素通し。
|
|
939
|
+
*/
|
|
940
|
+
export interface UseVernierSliderPointOptions extends Partial<Omit<BasePeriodSliderVernierProps, "mode" | "unit" | "minDate" | "maxDate" | "macroUnit" | "microUnit" | "macroRange" | "onChangeMacroRange">> {
|
|
941
|
+
/** Base time resolution unit / 基本時間解像度単位 */
|
|
942
|
+
unit: PeriodSliderUnit
|
|
943
|
+
/** Minimum selectable boundary date for entire macro range (required, no fallback) / 全期間マクロの下限日付 (必須・フォールバックなし) */
|
|
944
|
+
minDate: Date
|
|
945
|
+
/** Maximum selectable boundary date for entire macro range, strictly later than minDate (required, no fallback) / 全期間マクロの上限日付 (必須・minDate より後・フォールバックなし) */
|
|
946
|
+
maxDate: Date
|
|
947
|
+
/** Initial macro window range [start, end] inside [minDate, maxDate] (required, no fallback) / 初期マクロ窓期間 (必須・[minDate, maxDate] 内・フォールバックなし) */
|
|
948
|
+
initialMacroRange: [Date, Date]
|
|
949
|
+
/** Initial micro selected point inside the initial macro window (required, no fallback) / 初期ミクロ選択時点 (必須・初期マクロ窓の内側・フォールバックなし) */
|
|
950
|
+
initialMicroValue: Date
|
|
951
|
+
/** Macro scale unit (defaults to unit) / マクロ単位 (既定: unit) */
|
|
952
|
+
macroUnit?: PeriodSliderUnit
|
|
953
|
+
/** Micro scale unit (defaults to unit) / ミクロ単位 (既定: unit) */
|
|
954
|
+
microUnit?: PeriodSliderUnit
|
|
955
|
+
/** Callback triggered when macro window changes / マクロ窓変更時コールバック */
|
|
956
|
+
onChangeMacroRange?: (lower: Date, upper: Date) => void
|
|
957
|
+
/** Callback triggered when the micro point changes / ミクロ選択時点変更時コールバック */
|
|
958
|
+
onChangeMicroPoint?: (value: Date) => void
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
/**
|
|
962
|
+
* Macro window state, micro point state, mutators and ready-to-spread props returned by the vernier point hook.
|
|
963
|
+
* ノギス時点フックが返すマクロ窓・ミクロ時点の状態・更新関数・スプレッド可能な props 一式。
|
|
964
|
+
*/
|
|
965
|
+
export interface UseVernierSliderPointReturn {
|
|
966
|
+
/** Macro window range tuple / マクロ窓期間タプル */
|
|
967
|
+
macroRange: [Date, Date]
|
|
968
|
+
/** State updater for macro range; replaces state without firing onChangeMacroRange / マクロ窓更新関数 (onChangeMacroRange は発火しない) */
|
|
969
|
+
setMacroRange: React.Dispatch<React.SetStateAction<[Date, Date]>>
|
|
970
|
+
/** Micro selected point / ミクロ選択時点 */
|
|
971
|
+
microValue: Date
|
|
972
|
+
/** State updater for the micro point; replaces state without firing onChangeMicroPoint / ミクロ時点更新関数 (onChangeMicroPoint は発火しない) */
|
|
973
|
+
setMicroValue: React.Dispatch<React.SetStateAction<Date>>
|
|
974
|
+
/** Sets the macro lower date, pushing the macro upper date when overtaken and clamping the window into [minDate, maxDate] / マクロ開始日更新 (上限を超えたら上限も追従・窓を [minDate, maxDate] へクランプ) */
|
|
975
|
+
setMacroLowerDate: (date: Date) => void
|
|
976
|
+
/** Sets the macro upper date, pushing the macro lower date when undercut and clamping the window into [minDate, maxDate] / マクロ終了日更新 (下限を下回ったら下限も追従・窓を [minDate, maxDate] へクランプ) */
|
|
977
|
+
setMacroUpperDate: (date: Date) => void
|
|
978
|
+
/** Sets the micro point, clamping it into the macro window / ミクロ時点更新 (マクロ窓へクランプ) */
|
|
979
|
+
setMicroDate: (date: Date) => void
|
|
980
|
+
/** Shifts the macro window by delta macro units, aborting entirely when either edge would leave [minDate, maxDate] / マクロ窓を delta マクロ単位シフト (片側でも定義域を越える場合は全体中止) */
|
|
981
|
+
shiftMacroRange: (delta: number) => void
|
|
982
|
+
/** 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
|
+
shiftMicroValue: (delta: number) => void
|
|
984
|
+
/** Reset to the initial macro window and micro point / 初期状態へリセット */
|
|
985
|
+
resetVernier: () => void
|
|
986
|
+
/** Pre-configured props for macro start selector / マクロ開始日セレクタ用 Props */
|
|
987
|
+
macroStartSelectorProps: SelectorProps
|
|
988
|
+
/** Pre-configured props for macro end selector / マクロ終了日セレクタ用 Props */
|
|
989
|
+
macroEndSelectorProps: SelectorProps
|
|
990
|
+
/** Pre-configured props for the micro point selector (bounded by the effective macro window) / ミクロ時点セレクタ用 Props (実効マクロ窓で境界付け) */
|
|
991
|
+
microSelectorProps: SelectorProps
|
|
992
|
+
/** Pre-configured props to spread directly onto PeriodSlider / PeriodSlider に直接スプレッド可能な Props */
|
|
993
|
+
sliderProps: PeriodSliderVernierPointProps
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
/**
|
|
997
|
+
* Custom hook to manage state and props for a Vernier dual-scale PeriodSlider whose micro rail selects one point.
|
|
998
|
+
* 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]).
|
|
1001
|
+
* ミクロレールが時点を選ぶノギス 2 段スケール PeriodSlider のステート管理と Props 生成を行うカスタムフック。
|
|
1002
|
+
*
|
|
1003
|
+
* @param options - Required unit, boundaries, initial macro window and micro point plus optional UI props / 必須の単位・境界・初期マクロ窓/ミクロ時点と任意の UI Props
|
|
1004
|
+
* @returns Dual-scale state, mutators and pre-configured slider/selector props / 2 段スケールのステート・更新関数・生成済み Props 群
|
|
1005
|
+
* @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 が) 初期マクロ窓の外にある場合
|
|
1006
|
+
*
|
|
1007
|
+
* @example
|
|
1008
|
+
* ```tsx
|
|
1009
|
+
* const { microValue, sliderProps, macroStartSelectorProps, microSelectorProps } = useVernierSliderPoint({
|
|
1010
|
+
* unit: "month",
|
|
1011
|
+
* minDate: new Date(2015, 0, 1),
|
|
1012
|
+
* maxDate: new Date(2030, 11, 31),
|
|
1013
|
+
* initialMacroRange: [new Date(2020, 0, 1), new Date(2026, 0, 1)],
|
|
1014
|
+
* initialMicroValue: new Date(2023, 5, 1),
|
|
1015
|
+
* macroUnit: "year",
|
|
1016
|
+
* microUnit: "month",
|
|
1017
|
+
* })
|
|
1018
|
+
* return <PeriodSlider {...sliderProps} />
|
|
1019
|
+
* ```
|
|
1020
|
+
*/
|
|
1021
|
+
export function useVernierSliderPoint(options: UseVernierSliderPointOptions): UseVernierSliderPointReturn {
|
|
1022
|
+
const { unit, minDate, maxDate, initialMacroRange, initialMicroValue, macroUnit = unit, microUnit = unit, onChangeMacroRange, onChangeMicroPoint, ...restUiProps } = options
|
|
1023
|
+
|
|
1024
|
+
assertValidBounds("useVernierSliderPoint", minDate, maxDate)
|
|
1025
|
+
assertValidRangeTuple("useVernierSliderPoint", "initialMacroRange", initialMacroRange)
|
|
1026
|
+
assertValidDate("useVernierSliderPoint", "initialMicroValue", initialMicroValue)
|
|
1027
|
+
assertRangeWithinBounds("useVernierSliderPoint", "initialMacroRange", initialMacroRange, minDate, maxDate, "[minDate, maxDate]")
|
|
1028
|
+
assertWithinBounds("useVernierSliderPoint", "initialMicroValue", initialMicroValue, initialMacroRange[0], getMacroWindowEndDate(macroUnit, microUnit, initialMacroRange[1]), "the initial macro window")
|
|
1029
|
+
|
|
1030
|
+
// レスト構文の転送 Props は毎レンダー別オブジェクトになり、依存に置くと sliderProps のメモ化が常に外れる
|
|
1031
|
+
const uiProps = useShallowStable(restUiProps)
|
|
1032
|
+
|
|
1033
|
+
const initialMacroRef = useRef<[Date, Date]>(initialMacroRange)
|
|
1034
|
+
const initialMicroRef = useRef<Date>(initialMicroValue)
|
|
1035
|
+
|
|
1036
|
+
const [macroRangeState, setMacroRange] = useState<[Date, Date]>(initialMacroRange)
|
|
1037
|
+
const [microValueState, setMicroValue] = useState<Date>(initialMicroValue)
|
|
1038
|
+
|
|
1039
|
+
// 境界オプションもマクロ窓も実行中に動き得るため、包含関係が崩れたステートはレンダー中に畳み込む (aria-valuenow が valuemin/valuemax を外れないため)
|
|
1040
|
+
let macroRange = macroRangeState
|
|
1041
|
+
const containedMacro: [Date, Date] = [clampDate(macroRangeState[0], minDate, maxDate), clampDate(macroRangeState[1], minDate, maxDate)]
|
|
1042
|
+
if (containedMacro[0] !== macroRangeState[0] || containedMacro[1] !== macroRangeState[1]) {
|
|
1043
|
+
macroRange = containedMacro
|
|
1044
|
+
setMacroRange(containedMacro)
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
let microValue = microValueState
|
|
1048
|
+
const containedMicro = clampDateIntoMacroWindow(microValueState, macroUnit, microUnit, macroRange)
|
|
1049
|
+
if (containedMicro !== microValueState) {
|
|
1050
|
+
microValue = containedMicro
|
|
1051
|
+
setMicroValue(containedMicro)
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
const macroRangeRef = useRef<[Date, Date]>(macroRange)
|
|
1055
|
+
macroRangeRef.current = macroRange
|
|
1056
|
+
const microValueRef = useRef<Date>(microValue)
|
|
1057
|
+
microValueRef.current = microValue
|
|
1058
|
+
|
|
1059
|
+
// 境界と離散候補も 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 }
|
|
1062
|
+
|
|
1063
|
+
// コールバックを ref 経由で読むことで、消費側が毎レンダー新しい関数を渡しても更新関数の同一性を保つ
|
|
1064
|
+
const onChangeMacroRangeRef = useRef(onChangeMacroRange)
|
|
1065
|
+
onChangeMacroRangeRef.current = onChangeMacroRange
|
|
1066
|
+
const onChangeMicroPointRef = useRef(onChangeMicroPoint)
|
|
1067
|
+
onChangeMicroPointRef.current = onChangeMicroPoint
|
|
1068
|
+
|
|
1069
|
+
const commitVernier = useCallback((nextMacro: [Date, Date], nextMicro: Date) => {
|
|
1070
|
+
const currentMacro = macroRangeRef.current
|
|
1071
|
+
const currentMicro = microValueRef.current
|
|
1072
|
+
// 動いていないスケールのコールバックを撃つと、消費側が無関係な再取得を行う
|
|
1073
|
+
if (nextMacro[0].getTime() !== currentMacro[0].getTime() || nextMacro[1].getTime() !== currentMacro[1].getTime()) {
|
|
1074
|
+
macroRangeRef.current = nextMacro
|
|
1075
|
+
setMacroRange(nextMacro)
|
|
1076
|
+
onChangeMacroRangeRef.current?.(nextMacro[0], nextMacro[1])
|
|
1077
|
+
}
|
|
1078
|
+
if (nextMicro.getTime() !== currentMicro.getTime()) {
|
|
1079
|
+
microValueRef.current = nextMicro
|
|
1080
|
+
setMicroValue(nextMicro)
|
|
1081
|
+
onChangeMicroPointRef.current?.(nextMicro)
|
|
1082
|
+
}
|
|
1083
|
+
}, [])
|
|
1084
|
+
|
|
1085
|
+
const handleChangeMacroRange = useCallback(
|
|
1086
|
+
(lower: Date, upper: Date) => {
|
|
1087
|
+
const { minDate: domainLower, maxDate: domainUpper } = domainRef.current
|
|
1088
|
+
const nextMacro: [Date, Date] = [clampDate(lower, domainLower, domainUpper), clampDate(upper, domainLower, domainUpper)]
|
|
1089
|
+
// マクロ窓が動くとミクロ時点がはみ出し得るため、同じ確定経路で窓の内側へ押し込む
|
|
1090
|
+
commitVernier(nextMacro, clampDateIntoMacroWindow(microValueRef.current, macroUnit, microUnit, nextMacro))
|
|
1091
|
+
},
|
|
1092
|
+
[macroUnit, microUnit, commitVernier],
|
|
1093
|
+
)
|
|
1094
|
+
|
|
1095
|
+
const handleChangeMicroPoint = useCallback(
|
|
1096
|
+
(value: Date) => {
|
|
1097
|
+
commitVernier(macroRangeRef.current, clampDateIntoMacroWindow(value, macroUnit, microUnit, macroRangeRef.current))
|
|
1098
|
+
},
|
|
1099
|
+
[macroUnit, microUnit, commitVernier],
|
|
1100
|
+
)
|
|
1101
|
+
|
|
1102
|
+
const setMacroLowerDate = useCallback(
|
|
1103
|
+
(date: Date) => {
|
|
1104
|
+
const currentUpper = macroRangeRef.current[1]
|
|
1105
|
+
const nextUpper = date > currentUpper ? date : currentUpper
|
|
1106
|
+
handleChangeMacroRange(date, nextUpper)
|
|
1107
|
+
},
|
|
1108
|
+
[handleChangeMacroRange],
|
|
1109
|
+
)
|
|
1110
|
+
|
|
1111
|
+
const setMacroUpperDate = useCallback(
|
|
1112
|
+
(date: Date) => {
|
|
1113
|
+
const currentLower = macroRangeRef.current[0]
|
|
1114
|
+
const nextLower = date < currentLower ? date : currentLower
|
|
1115
|
+
handleChangeMacroRange(nextLower, date)
|
|
1116
|
+
},
|
|
1117
|
+
[handleChangeMacroRange],
|
|
1118
|
+
)
|
|
1119
|
+
|
|
1120
|
+
const shiftMacroRange = useCallback(
|
|
1121
|
+
(delta: number) => {
|
|
1122
|
+
const { minDate: domainLower, maxDate: domainUpper } = domainRef.current
|
|
1123
|
+
const current = macroRangeRef.current
|
|
1124
|
+
const shiftedLow = addUnit(macroUnit, current[0], delta)
|
|
1125
|
+
const shiftedHigh = addUnit(macroUnit, current[1], delta)
|
|
1126
|
+
// 窓幅を保つため、片側でも境界を越えるシフトは全体を中止する
|
|
1127
|
+
if (shiftedLow < domainLower || shiftedHigh > domainUpper) return
|
|
1128
|
+
handleChangeMacroRange(shiftedLow, shiftedHigh)
|
|
1129
|
+
},
|
|
1130
|
+
[macroUnit, handleChangeMacroRange],
|
|
1131
|
+
)
|
|
1132
|
+
|
|
1133
|
+
const shiftMicroValue = useCallback(
|
|
1134
|
+
(delta: number) => {
|
|
1135
|
+
const currentMacro = macroRangeRef.current
|
|
1136
|
+
const windowEnd = getMacroWindowEndDate(macroUnit, microUnit, currentMacro[1])
|
|
1137
|
+
// 離散指定時に暦単位で進めると、スライダーが表示しない日付をステートとコールバックへ流してしまう。
|
|
1138
|
+
// 離散か否かの正準スイッチは 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)
|
|
1147
|
+
if (candidates.length > 0) {
|
|
1148
|
+
handleChangeMicroPoint(stepThroughAllowedDates(candidates, microUnit, microValueRef.current, delta))
|
|
1149
|
+
return
|
|
1150
|
+
}
|
|
1151
|
+
// 時点には保つべき幅が無く、端で中止する理由も無い。窓の端へ着けるほうがスライダーのステッパーと終状態が一致する
|
|
1152
|
+
handleChangeMicroPoint(addUnit(microUnit, microValueRef.current, delta))
|
|
1153
|
+
},
|
|
1154
|
+
[macroUnit, microUnit, handleChangeMicroPoint],
|
|
1155
|
+
)
|
|
1156
|
+
|
|
1157
|
+
const resetVernier = useCallback(() => {
|
|
1158
|
+
const initialMacro = initialMacroRef.current
|
|
1159
|
+
commitVernier(initialMacro, clampDateIntoMacroWindow(initialMicroRef.current, macroUnit, microUnit, initialMacro))
|
|
1160
|
+
}, [macroUnit, microUnit, commitVernier])
|
|
1161
|
+
|
|
1162
|
+
const macroWindowEnd = useMemo(() => getMacroWindowEndDate(macroUnit, microUnit, macroRange[1]), [macroUnit, microUnit, macroRange[1]])
|
|
1163
|
+
|
|
1164
|
+
const macroStartSelectorProps: SelectorProps = useMemo(
|
|
1165
|
+
() => ({
|
|
1166
|
+
value: macroRange[0],
|
|
1167
|
+
onChange: setMacroLowerDate,
|
|
1168
|
+
minDate,
|
|
1169
|
+
maxDate: macroRange[1],
|
|
1170
|
+
unit: macroUnit,
|
|
1171
|
+
}),
|
|
1172
|
+
[macroRange, setMacroLowerDate, minDate, macroUnit],
|
|
1173
|
+
)
|
|
1174
|
+
|
|
1175
|
+
const macroEndSelectorProps: SelectorProps = useMemo(
|
|
1176
|
+
() => ({
|
|
1177
|
+
value: macroRange[1],
|
|
1178
|
+
onChange: setMacroUpperDate,
|
|
1179
|
+
minDate: macroRange[0],
|
|
1180
|
+
maxDate,
|
|
1181
|
+
unit: macroUnit,
|
|
1182
|
+
}),
|
|
1183
|
+
[macroRange, setMacroUpperDate, maxDate, macroUnit],
|
|
1184
|
+
)
|
|
1185
|
+
|
|
1186
|
+
const microSelectorProps: SelectorProps = useMemo(
|
|
1187
|
+
() => ({
|
|
1188
|
+
value: microValue,
|
|
1189
|
+
onChange: handleChangeMicroPoint,
|
|
1190
|
+
minDate: macroRange[0],
|
|
1191
|
+
maxDate: macroWindowEnd,
|
|
1192
|
+
unit: microUnit,
|
|
1193
|
+
}),
|
|
1194
|
+
[microValue, handleChangeMicroPoint, macroRange, macroWindowEnd, microUnit],
|
|
1195
|
+
)
|
|
1196
|
+
|
|
1197
|
+
const sliderProps: PeriodSliderVernierPointProps = useMemo(
|
|
1198
|
+
() => ({
|
|
1199
|
+
...uiProps,
|
|
1200
|
+
mode: "vernier",
|
|
1201
|
+
unit,
|
|
1202
|
+
macroUnit,
|
|
1203
|
+
microUnit,
|
|
1204
|
+
minDate,
|
|
1205
|
+
maxDate,
|
|
1206
|
+
macroRange,
|
|
1207
|
+
microValue,
|
|
1208
|
+
onChangeMacroRange: handleChangeMacroRange,
|
|
1209
|
+
onChangeMicroPoint: handleChangeMicroPoint,
|
|
1210
|
+
}),
|
|
1211
|
+
[uiProps, unit, macroUnit, microUnit, minDate, maxDate, macroRange, microValue, handleChangeMacroRange, handleChangeMicroPoint],
|
|
1212
|
+
)
|
|
1213
|
+
|
|
1214
|
+
return {
|
|
1215
|
+
macroRange,
|
|
1216
|
+
setMacroRange,
|
|
1217
|
+
microValue,
|
|
1218
|
+
setMicroValue,
|
|
1219
|
+
setMacroLowerDate,
|
|
1220
|
+
setMacroUpperDate,
|
|
1221
|
+
setMicroDate: handleChangeMicroPoint,
|
|
1222
|
+
shiftMacroRange,
|
|
1223
|
+
shiftMicroValue,
|
|
1224
|
+
resetVernier,
|
|
1225
|
+
macroStartSelectorProps,
|
|
1226
|
+
macroEndSelectorProps,
|
|
1227
|
+
microSelectorProps,
|
|
1228
|
+
sliderProps,
|
|
1229
|
+
}
|
|
1230
|
+
}
|