@aiquants/period-slider 0.18.0 → 0.20.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/CHANGELOG.md +209 -0
- package/README.md +26 -7
- package/dist/DateSelector.d.ts +9 -9
- package/dist/DateSelector.d.ts.map +1 -1
- package/dist/MonthSelector.d.ts +5 -4
- package/dist/MonthSelector.d.ts.map +1 -1
- package/dist/PeriodSlider.d.ts +23 -4
- package/dist/PeriodSlider.d.ts.map +1 -1
- package/dist/VirtualDateDropdown.d.ts +25 -10
- package/dist/VirtualDateDropdown.d.ts.map +1 -1
- package/dist/YearSelector.d.ts +7 -4
- package/dist/YearSelector.d.ts.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +2859 -2394
- package/dist/optionText.d.ts +1 -1
- package/dist/optionText.d.ts.map +1 -1
- package/dist/renderScope.d.ts +486 -0
- package/dist/renderScope.d.ts.map +1 -0
- package/dist/selectorMemo.d.ts +42 -22
- package/dist/selectorMemo.d.ts.map +1 -1
- package/dist/useCommittedRef.d.ts +64 -71
- package/dist/useCommittedRef.d.ts.map +1 -1
- package/dist/usePeriodSlider.d.ts +56 -12
- package/dist/usePeriodSlider.d.ts.map +1 -1
- package/dist/useUncontrolledSelection.d.ts +7 -7
- package/dist/useUncontrolledSelection.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/DateSelector.tsx +45 -20
- package/src/MonthSelector.tsx +41 -19
- package/src/PeriodSlider.tsx +39 -14
- package/src/VirtualDateDropdown.tsx +163 -92
- package/src/YearSelector.tsx +26 -27
- package/src/optionText.ts +6 -4
- package/src/renderScope.ts +766 -0
- package/src/selectorMemo.ts +53 -25
- package/src/useCommittedRef.ts +51 -155
- package/src/usePeriodSlider.ts +316 -138
- package/src/useUncontrolledSelection.ts +8 -9
package/src/DateSelector.tsx
CHANGED
|
@@ -48,9 +48,9 @@ import {
|
|
|
48
48
|
import { assertCandidateListInput, dateMemoKey, describeValue, isValidDate } from "./inputValue"
|
|
49
49
|
import { areLabelOverridesEqual, type PeriodSliderLabelOverrides, type PeriodSliderLocale } from "./labels"
|
|
50
50
|
import { formatOptionText } from "./optionText"
|
|
51
|
-
import { assertLocaleTag, encodeInsideDateRange, formatLabel, type PeriodOptionFormatter, type PeriodSliderUnit, sentinelBoundRemedy } from "./PeriodSlider"
|
|
51
|
+
import { assertLocaleTag, encodeInsideDateRange, formatLabel, keyInsideDateRange, type PeriodOptionFormatter, type PeriodSliderUnit, sentinelBoundRemedy } from "./PeriodSlider"
|
|
52
52
|
import { STEPPER_ROW_GAP_CLASS, StepperButton } from "./StepperButton"
|
|
53
|
-
import { areSelectorDatePropsEqual } from "./selectorMemo"
|
|
53
|
+
import { areSelectorDatePropsEqual, selectorUnitKey } from "./selectorMemo"
|
|
54
54
|
import { assertSelectorStepper, type SelectorStepper } from "./selectorStep"
|
|
55
55
|
import { useResolvedLabels } from "./useResolvedLabels"
|
|
56
56
|
import { useUncontrolledSelection } from "./useUncontrolledSelection"
|
|
@@ -263,14 +263,37 @@ const namesLocalDay = (date: Date, year: number, month: number, day: number): bo
|
|
|
263
263
|
* -271821 年 1 月 1 日はどのタイムゾーンでも表現範囲の外 (範囲は同年 4 月 20 日 UTC から) で、同年 1〜4 月の 1 日も外。年の始まりが範囲に入れば、
|
|
264
264
|
* 1 月からその日付の月までの各 1 日も入る (1 月 1 日と、始まりの表現を既に要求している日との間にあるため)。その日付より後の月は `listPartOptions` が区切る。
|
|
265
265
|
*
|
|
266
|
+
* The predicate is keyInsideDateRange's at unit year under the calendar year, the function encodeInsideDateRange validates with, and
|
|
267
|
+
* `assertSplitYearProp`, the held-selection check and the split-dropdown memo key (`splitDropdownDayKey`) all read it, so they reject
|
|
268
|
+
* exactly the same dates. keyInsideDateRange reads a date by its time value in the system time zone (`normalizeDateByUnit` reads a
|
|
269
|
+
* plain copy at every unit), so the raw prop, its day start (`toStartOfDay`, which the held-selection check reads) and the drawn year
|
|
270
|
+
* (`activeDate.getFullYear()`) name the same year, a Date subclass whose getters report another zone included.
|
|
271
|
+
* 判定は暦年のもとの年の単位の keyInsideDateRange (encodeInsideDateRange が検証に使う関数) であり、`assertSplitYearProp`・保持中の選択の検査・
|
|
272
|
+
* 分割セレクトのメモのキー (`splitDropdownDayKey`) がどれもこれを読むため、拒む日付はちょうど一致する。keyInsideDateRange は日付を時刻値の
|
|
273
|
+
* システムのタイムゾーンで読む (`normalizeDateByUnit` はどの単位も素の複製を読む) ため、Props そのもの・その日の始まり (`toStartOfDay`。
|
|
274
|
+
* 保持中の選択の検査が読む)・描く年 (`activeDate.getFullYear()`) は同じ年を名乗る (ゲッターが別のタイムゾーンを名乗る Date の派生クラスも)。
|
|
275
|
+
*
|
|
266
276
|
* @param date - Valid Date; the render passes one whose day already starts inside the Date range, the memo comparator any valid Date / 有効な Date (描画はその日の始まりが表現範囲に入ると検証済みのものを、メモ比較器は任意の有効な Date を渡す)
|
|
267
|
-
* @returns True when January 1 of its
|
|
277
|
+
* @returns True when January 1 of its year starts inside the Date range / その年の 1 月 1 日の始まりが表現範囲に入れば true
|
|
278
|
+
* @throws RangeError when date is not a valid Date (raised by keyInsideDateRange); every caller passes a valid one / date が有効な Date でない場合 (keyInsideDateRange が送出)。呼び出し側はどれも有効な Date を渡す
|
|
279
|
+
*/
|
|
280
|
+
const yearStartsInsideDateRange = (date: Date): boolean => keyInsideDateRange(date, "year", 0) !== null
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Unit key of a valid Date for this selector: the `selectorUnitKey` of the day the validation reads.
|
|
284
|
+
* 本セレクタでの有効な Date の単位のキー (検証が読む日の `selectorUnitKey`)。
|
|
285
|
+
*
|
|
286
|
+
* The memo comparator and the uncontrolled selection share it, so a render the comparator skips is one that would not adopt.
|
|
287
|
+
* メモ比較器と非制御の選択が共有するため、比較器が省く描画は取り込みを起こさない描画と一致する。
|
|
288
|
+
*
|
|
289
|
+
* @param date - Valid Date / 有効な Date
|
|
290
|
+
* @returns `selectorUnitKey` at unit day, or `NaN` when the day starts outside the Date range / 日の単位の `selectorUnitKey` (日の始まりが表現範囲外なら `NaN`)
|
|
268
291
|
*/
|
|
269
|
-
const
|
|
292
|
+
const dayKey = (date: Date): number => selectorUnitKey(date, "day", 0)
|
|
270
293
|
|
|
271
294
|
/**
|
|
272
|
-
*
|
|
273
|
-
* `variant="split-dropdown"` の検証どおりの、有効な Date
|
|
295
|
+
* Unit key of a valid Date as `variant="split-dropdown"` validates it: `dayKey`, or `NaN` when the date's year does not start inside the Date range.
|
|
296
|
+
* `variant="split-dropdown"` の検証どおりの、有効な Date の単位のキー (`dayKey`。その年の始まりが Date の表現範囲に入らなければ `NaN`)。
|
|
274
297
|
*
|
|
275
298
|
* The memo comparator keys the Date props of a split-dropdown render by it, so a key is `NaN` exactly where `assertSplitYearProp`
|
|
276
299
|
* (the same predicate) or the day-start check rejects the prop, and a render that rejects one is never skipped.
|
|
@@ -278,9 +301,9 @@ const yearStartsInsideDateRange = (date: Date): boolean => !Number.isNaN(startOf
|
|
|
278
301
|
* 日の始まりの検査がその Props を拒む場合とちょうど一致し、拒む描画は省かれない。
|
|
279
302
|
*
|
|
280
303
|
* @param date - Valid Date / 有効な Date
|
|
281
|
-
* @returns
|
|
304
|
+
* @returns `dayKey(date)`, or `NaN` when its year or its day starts outside the Date range / `dayKey(date)` (年か日の始まりが表現範囲の外なら `NaN`)
|
|
282
305
|
*/
|
|
283
|
-
const
|
|
306
|
+
const splitDropdownDayKey = (date: Date): number => (yearStartsInsideDateRange(date) ? dayKey(date) : Number.NaN)
|
|
284
307
|
|
|
285
308
|
/**
|
|
286
309
|
* Asserts that a Date prop of `variant="split-dropdown"` falls in a year whose January 1 starts inside the Date range, failing fast otherwise.
|
|
@@ -291,6 +314,8 @@ const splitDropdownDayStart = (date: Date): Date => (yearStartsInsideDateRange(d
|
|
|
291
314
|
* handed to `formatOption`). `variant="date"` and `"dropdown"` draw only days and keep accepting such a date.
|
|
292
315
|
* `YearSelector` が年の始まりを表現できない日付を拒むのと同じく、ここで prop 名を名指して拒否する (拒まなければ、その年の年と月の部位が名指す日付の無い
|
|
293
316
|
* 選択肢になり、テキストは空、`formatOption` には Invalid Date が届く)。日しか描かない `variant="date"` / `"dropdown"` は引き続き受け付ける。
|
|
317
|
+
* The year test is `yearStartsInsideDateRange`, the predicate the split-dropdown memo key (`splitDropdownDayKey`) reads.
|
|
318
|
+
* 年の判定は `yearStartsInsideDateRange` で、分割セレクトのメモのキー (`splitDropdownDayKey`) が読む述語と同じ。
|
|
294
319
|
*
|
|
295
320
|
* @param propName - Prop name reported in the error message / エラーメッセージに載せるプロパティ名
|
|
296
321
|
* @param date - Date already accepted by `assertValidDateProp` / `assertValidDateProp` を通過した日付
|
|
@@ -409,8 +434,8 @@ const DateSelectorComponent = (props: DateSelectorProps): React.JSX.Element => {
|
|
|
409
434
|
const resolvedLabels = useResolvedLabels(locale, labels)
|
|
410
435
|
|
|
411
436
|
const isControlled = value !== undefined
|
|
412
|
-
// 分割セレクトの比較器が読む
|
|
413
|
-
const [currentDate, setCurrentDate] = useUncontrolledSelection(effectiveDate, isControlled,
|
|
437
|
+
// 分割セレクトの比較器が読む splitDropdownDayKey は、最初のフックより前の検証が拒む日付でだけ dayKey と異なるため、取り込みのキーはどの変種でも dayKey で足りる
|
|
438
|
+
const [currentDate, setCurrentDate] = useUncontrolledSelection(effectiveDate, isControlled, dayKey, toStartOfDay)
|
|
414
439
|
const valueTime = value?.getTime()
|
|
415
440
|
const activeDate = useMemo(() => (value !== undefined ? toStartOfDay(value) : currentDate), [valueTime, currentDate])
|
|
416
441
|
// 非制御の選択は、非制御になった描画と initialDate が別の日へ動いた描画でこの描画のうちに取り込み、それ以外は利用者の選択を
|
|
@@ -723,13 +748,13 @@ const DateSelectorComponent = (props: DateSelectorProps): React.JSX.Element => {
|
|
|
723
748
|
* Memo comparator of `DateSelector`: true only when every prop its output depends on is unchanged, never for a prop the render rejects.
|
|
724
749
|
* `DateSelector` のメモ比較器 (描画結果が依存する Props がすべて不変のときだけ true。描画が拒否する Props では常に false)。
|
|
725
750
|
*
|
|
726
|
-
* Date props are compared by the day
|
|
727
|
-
* render-time validation and throws the same named RangeError as on the first mount; the comparator itself never
|
|
728
|
-
* next render is `variant="split-dropdown"`, a date whose year starts outside the Date range keys to `NaN`
|
|
729
|
-
* so the render that rejects it is not skipped either, including a controlled `initialDate` moved into such a year.
|
|
730
|
-
* Date 型 Props は `areSelectorDatePropsEqual`
|
|
751
|
+
* Date props are compared through `areSelectorDatePropsEqual` by `dayKey`, the day the validation reads, so an invalid one on a
|
|
752
|
+
* re-render reaches the render-time validation and throws the same named RangeError as on the first mount; the comparator itself never
|
|
753
|
+
* throws. When the next render is `variant="split-dropdown"`, a date whose year starts outside the Date range keys to `NaN`
|
|
754
|
+
* (`splitDropdownDayKey`), so the render that rejects it is not skipped either, including a controlled `initialDate` moved into such a year.
|
|
755
|
+
* Date 型 Props は `areSelectorDatePropsEqual` で `dayKey` (検証が読む日) により比べるため、再描画で渡した不正な値も描画時の検証へ届き、
|
|
731
756
|
* 初回のマウントと同じく Props 名を名指す RangeError になる (比較器自身は例外を投げない)。今回の描画が `variant="split-dropdown"` なら、
|
|
732
|
-
* 年の始まりが表現範囲の外にある日付のキーは `NaN` になり (`
|
|
757
|
+
* 年の始まりが表現範囲の外にある日付のキーは `NaN` になり (`splitDropdownDayKey`)、制御中の `initialDate` をその年へ動かした描画も含め、
|
|
733
758
|
* それを拒む描画も省かない。
|
|
734
759
|
*
|
|
735
760
|
* @param prev - Props of the previous render / 前回の描画の Props
|
|
@@ -739,7 +764,7 @@ const DateSelectorComponent = (props: DateSelectorProps): React.JSX.Element => {
|
|
|
739
764
|
export const areDateSelectorPropsEqual = (prev: DateSelectorProps, next: DateSelectorProps): boolean => {
|
|
740
765
|
// 分割セレクトは日付の年の始まりも検証するため、その検査が拒む日付をキーでも NaN にする。variant が変わる描画は下の比較が必ず選ぶので、
|
|
741
766
|
// 今回の描画の variant で読めば足りる
|
|
742
|
-
if (!areSelectorDatePropsEqual(prev, next, next.variant === "split-dropdown" ?
|
|
767
|
+
if (!areSelectorDatePropsEqual(prev, next, next.variant === "split-dropdown" ? splitDropdownDayKey : dayKey)) return false
|
|
743
768
|
if (prev.variant !== next.variant) return false
|
|
744
769
|
if (prev.disabled !== next.disabled) return false
|
|
745
770
|
if (prev.className !== next.className) return false
|
|
@@ -770,11 +795,11 @@ export const areDateSelectorPropsEqual = (prev: DateSelectorProps, next: DateSel
|
|
|
770
795
|
* `allowedDates` is compared by reference, so consumers memoize the array; `stepper` is compared field by field, and a render that adds or
|
|
771
796
|
* removes it, or that switches between controlled and uncontrolled, is never skipped (the stepper has to be validated, and the
|
|
772
797
|
* component has to change which owner it renders).
|
|
773
|
-
* Date props go through `areSelectorDatePropsEqual
|
|
774
|
-
* and so does a date `variant="split-dropdown"` rejects for its year.
|
|
798
|
+
* Date props go through `areSelectorDatePropsEqual` by the key the validation's own function computes, so an invalid one on a re-render
|
|
799
|
+
* throws the same named RangeError as on the first mount, and so does a date `variant="split-dropdown"` rejects for its year.
|
|
775
800
|
* 描画結果が依存する Props の変化時のみ再描画する日単位セレクタ。`allowedDates` は参照比較のため利用側でメモ化、`stepper` はフィールドごとに比較し、
|
|
776
801
|
* 付け外しと、制御・非制御の切り替えは描画を省かない (引き受けを検証し、描く持ち主を切り替えるため)。
|
|
777
|
-
* Date 型 Props は `areSelectorDatePropsEqual`
|
|
802
|
+
* Date 型 Props は `areSelectorDatePropsEqual` で検証と同じ関数が求めるキーにより比べるため、再描画で渡した不正な値も、`variant="split-dropdown"` が年で拒む日付も、
|
|
778
803
|
* 初回のマウントと同じ名指しの RangeError になる。
|
|
779
804
|
*/
|
|
780
805
|
export const DateSelector = memo(DateSelectorComponent, areDateSelectorPropsEqual)
|
package/src/MonthSelector.tsx
CHANGED
|
@@ -25,9 +25,9 @@ import type React from "react"
|
|
|
25
25
|
import { memo, useMemo } from "react"
|
|
26
26
|
import { assertCandidateListInput, describeValue, isValidDate } from "./inputValue"
|
|
27
27
|
import { areLabelOverridesEqual, type PeriodSliderLabelOverrides, type PeriodSliderLocale } from "./labels"
|
|
28
|
-
import { assertLocaleTag, encodeInsideDateRange, formatIsoYear, formatLabel, type PeriodOptionFormatter, sentinelBoundRemedy } from "./PeriodSlider"
|
|
28
|
+
import { assertLocaleTag, decode, encodeInsideDateRange, formatIsoYear, formatLabel, keyInsideDateRange, type PeriodOptionFormatter, sentinelBoundRemedy } from "./PeriodSlider"
|
|
29
29
|
import { STEPPER_ROW_GAP_CLASS, StepperButton } from "./StepperButton"
|
|
30
|
-
import { areSelectorDatePropsEqual } from "./selectorMemo"
|
|
30
|
+
import { areSelectorDatePropsEqual, selectorUnitKey } from "./selectorMemo"
|
|
31
31
|
import { assertSelectorStepper, type SelectorStepper } from "./selectorStep"
|
|
32
32
|
import { useResolvedLabels } from "./useResolvedLabels"
|
|
33
33
|
import { useUncontrolledSelection } from "./useUncontrolledSelection"
|
|
@@ -113,16 +113,37 @@ function monthStart(year: number, monthIndex: number): Date {
|
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
/**
|
|
116
|
-
* Resolves the first day of the month a date falls in.
|
|
117
|
-
*
|
|
116
|
+
* Resolves the first day of the month a date falls in, as the selector's validation reads that month: the month of the date's time value in the system time zone.
|
|
117
|
+
* セレクタの検証が読むとおりの、日付が属する月の初日を解決する関数 (日付の時刻値のシステムのタイムゾーンでの月)。
|
|
118
|
+
*
|
|
119
|
+
* The month is keyInsideDateRange's at unit month, the function encodeInsideDateRange validates every Date prop with, so the drawn
|
|
120
|
+
* month, both bounds, the allowedMonths entries, the memo comparator and the uncontrolled selection read the month the validation
|
|
121
|
+
* judged. A Date whose own getters report another time zone (a Date subclass such as a `TZDate` of `@date-fns/tz`) is read by its
|
|
122
|
+
* time value as well.
|
|
123
|
+
* 月は keyInsideDateRange の月 (encodeInsideDateRange がすべての Date 型 Props の検証に使う関数) であり、描く月・両境界・allowedMonths の要素・
|
|
124
|
+
* メモ比較器・非制御の選択は、検証が判定した月を読む。自身のゲッターが別のタイムゾーンを名乗る Date (`@date-fns/tz` の `TZDate` のような Date の派生クラス)
|
|
125
|
+
* も時刻値で読む。
|
|
118
126
|
*
|
|
119
127
|
* @param date - Valid Date / 有効な日付
|
|
120
|
-
* @returns First
|
|
128
|
+
* @returns First instant of that month's first day (the decode of its key), or an Invalid Date when the month starts outside the Date range / その月の初日の最初の時刻 (キーの decode。月の始まりが表現範囲外なら Invalid Date)
|
|
121
129
|
*/
|
|
122
130
|
function monthStartOf(date: Date): Date {
|
|
123
|
-
|
|
131
|
+
const key = keyInsideDateRange(date, "month", 0)
|
|
132
|
+
return decode("month", key === null ? Number.NaN : key, 0)
|
|
124
133
|
}
|
|
125
134
|
|
|
135
|
+
/**
|
|
136
|
+
* Unit key of a valid Date for this selector: the `selectorUnitKey` of the month the validation reads.
|
|
137
|
+
* 本セレクタでの有効な Date の単位のキー (検証が読む月の `selectorUnitKey`)。
|
|
138
|
+
*
|
|
139
|
+
* The memo comparator and the uncontrolled selection share it, so a render the comparator skips is one that would not adopt.
|
|
140
|
+
* メモ比較器と非制御の選択が共有するため、比較器が省く描画は取り込みを起こさない描画と一致する。
|
|
141
|
+
*
|
|
142
|
+
* @param date - Valid Date / 有効な日付
|
|
143
|
+
* @returns `selectorUnitKey` at unit month, or `NaN` when the month starts outside the Date range / 月の単位の `selectorUnitKey` (月の始まりが表現範囲外なら `NaN`)
|
|
144
|
+
*/
|
|
145
|
+
const monthKey = (date: Date): number => selectorUnitKey(date, "month", 0)
|
|
146
|
+
|
|
126
147
|
/**
|
|
127
148
|
* Format a date as the yyyy-MM string used by the input, the select and the option list.
|
|
128
149
|
* input / select / 選択肢で共用する yyyy-MM 文字列への整形。
|
|
@@ -215,12 +236,12 @@ function assertValidYearProp(year: number | undefined, propName: string, boundar
|
|
|
215
236
|
* `minYear` と `minDate` の双方を満たす最も厳しい下限月の解決。
|
|
216
237
|
*
|
|
217
238
|
* @param minYear - Lower bound year / 下限年
|
|
218
|
-
* @param minDate - Lower bound date /
|
|
239
|
+
* @param minDate - Lower bound date, already validated, read by the month the validation judged (`monthStartOf`) / 検証済みの下限日付 (検証が判定した月で読む。`monthStartOf`)
|
|
219
240
|
* @returns First day of the strictest lower month, or null when neither is declared / 最も厳しい下限月の 1 日。いずれも未宣言なら null
|
|
220
241
|
*/
|
|
221
242
|
function resolveLowerBound(minYear: number | undefined, minDate: Date | undefined): Date | null {
|
|
222
243
|
const fromYear = minYear === undefined ? null : monthStart(minYear, 0)
|
|
223
|
-
const fromDate = minDate === undefined ? null :
|
|
244
|
+
const fromDate = minDate === undefined ? null : monthStartOf(minDate)
|
|
224
245
|
if (fromYear !== null && fromDate !== null) return fromYear >= fromDate ? fromYear : fromDate
|
|
225
246
|
if (fromYear !== null) return fromYear
|
|
226
247
|
return fromDate
|
|
@@ -231,12 +252,12 @@ function resolveLowerBound(minYear: number | undefined, minDate: Date | undefine
|
|
|
231
252
|
* `maxYear` と `maxDate` の双方を満たす最も厳しい上限月の解決。
|
|
232
253
|
*
|
|
233
254
|
* @param maxYear - Upper bound year / 上限年
|
|
234
|
-
* @param maxDate - Upper bound date /
|
|
255
|
+
* @param maxDate - Upper bound date, already validated, read by the month the validation judged (`monthStartOf`) / 検証済みの上限日付 (検証が判定した月で読む。`monthStartOf`)
|
|
235
256
|
* @returns First day of the strictest upper month, or null when neither is declared / 最も厳しい上限月の 1 日。いずれも未宣言なら null
|
|
236
257
|
*/
|
|
237
258
|
function resolveUpperBound(maxYear: number | undefined, maxDate: Date | undefined): Date | null {
|
|
238
259
|
const fromYear = maxYear === undefined ? null : monthStart(maxYear, 11)
|
|
239
|
-
const fromDate = maxDate === undefined ? null :
|
|
260
|
+
const fromDate = maxDate === undefined ? null : monthStartOf(maxDate)
|
|
240
261
|
if (fromYear !== null && fromDate !== null) return fromYear <= fromDate ? fromYear : fromDate
|
|
241
262
|
if (fromYear !== null) return fromYear
|
|
242
263
|
return fromDate
|
|
@@ -287,7 +308,7 @@ function representableMonthOrNull(month: Date): Date | null {
|
|
|
287
308
|
* the resolved bounds and sort the rest ascending.
|
|
288
309
|
* 離散月配列を月初へ正規化・重複排除し、解決済み境界の外の要素を除外して昇順に並べる関数。
|
|
289
310
|
*
|
|
290
|
-
* @param allowedMonths - Caller supplied months, already validated,
|
|
311
|
+
* @param allowedMonths - Caller supplied months, already validated, each read by the month the validation judged (`monthStartOf`; the day part is ignored) / 検証済みの呼び出し側指定月 (各要素を検証が判定した月で読む。`monthStartOf`。日は無視)
|
|
291
312
|
* @param minBound - Resolved lower bound month, or null when unbounded / 解決済み下限月 (無制限時は null)
|
|
292
313
|
* @param maxBound - Resolved upper bound month, or null when unbounded / 解決済み上限月 (無制限時は null)
|
|
293
314
|
* @returns Ascending, deduplicated first-of-month dates inside the bounds (possibly empty) / 境界内の昇順・重複排除済み月初日付 (空の場合あり)
|
|
@@ -295,7 +316,7 @@ function representableMonthOrNull(month: Date): Date | null {
|
|
|
295
316
|
function resolveSelectableMonths(allowedMonths: Date[], minBound: Date | null, maxBound: Date | null): Date[] {
|
|
296
317
|
const byTime = new Map<number, Date>()
|
|
297
318
|
for (const entry of allowedMonths) {
|
|
298
|
-
const normalized =
|
|
319
|
+
const normalized = monthStartOf(entry)
|
|
299
320
|
if (minBound !== null && normalized < minBound) continue
|
|
300
321
|
if (maxBound !== null && normalized > maxBound) continue
|
|
301
322
|
if (!byTime.has(normalized.getTime())) byTime.set(normalized.getTime(), normalized)
|
|
@@ -384,8 +405,8 @@ const MonthSelectorComponent = (props: MonthSelectorProps): React.JSX.Element =>
|
|
|
384
405
|
const resolvedLabels = useResolvedLabels(locale, labels)
|
|
385
406
|
|
|
386
407
|
const isControlled = value !== undefined
|
|
387
|
-
const [currentDate, setCurrentDate] = useUncontrolledSelection(effectiveDate, isControlled,
|
|
388
|
-
const activeDate = useMemo(() => (value !== undefined ?
|
|
408
|
+
const [currentDate, setCurrentDate] = useUncontrolledSelection(effectiveDate, isControlled, monthKey, monthStartOf)
|
|
409
|
+
const activeDate = useMemo(() => (value !== undefined ? monthStartOf(value) : currentDate), [value?.getTime(), currentDate])
|
|
389
410
|
|
|
390
411
|
const minTime = minDate?.getTime()
|
|
391
412
|
const maxTime = maxDate?.getTime()
|
|
@@ -551,17 +572,18 @@ const MonthSelectorComponent = (props: MonthSelectorProps): React.JSX.Element =>
|
|
|
551
572
|
* Memo comparator of `MonthSelector`: true only when every prop its output depends on is unchanged, never for a prop the render rejects.
|
|
552
573
|
* `MonthSelector` のメモ比較器 (描画結果が依存する Props がすべて不変のときだけ true。描画が拒否する Props では常に false)。
|
|
553
574
|
*
|
|
554
|
-
* Date props are compared
|
|
555
|
-
* render-time validation and throws the same named RangeError as on the first mount
|
|
556
|
-
*
|
|
557
|
-
*
|
|
575
|
+
* Date props are compared through `areSelectorDatePropsEqual` by `monthKey`, the month the validation and the render read, so an
|
|
576
|
+
* invalid one on a re-render reaches the render-time validation and throws the same named RangeError as on the first mount, and a
|
|
577
|
+
* skipped render draws the month a fresh mount would; the comparator itself never throws.
|
|
578
|
+
* Date 型 Props は `areSelectorDatePropsEqual` で `monthKey` (検証と描画が読む月) により比べるため、再描画で渡した不正な値も描画時の検証へ届き、
|
|
579
|
+
* 初回のマウントと同じく Props 名を名指す RangeError になり、省いた描画は新規のマウントと同じ月を描く (比較器自身は例外を投げない)。
|
|
558
580
|
*
|
|
559
581
|
* @param prev - Props of the previous render / 前回の描画の Props
|
|
560
582
|
* @param next - Props of the next render / 今回の描画の Props
|
|
561
583
|
* @returns True when the next render may be skipped / 今回の描画を省けるなら true
|
|
562
584
|
*/
|
|
563
585
|
export const areMonthSelectorPropsEqual = (prev: MonthSelectorProps, next: MonthSelectorProps): boolean => {
|
|
564
|
-
if (!areSelectorDatePropsEqual(prev, next,
|
|
586
|
+
if (!areSelectorDatePropsEqual(prev, next, monthKey)) return false
|
|
565
587
|
if (prev.variant !== next.variant) return false
|
|
566
588
|
if (prev.disabled !== next.disabled) return false
|
|
567
589
|
if (prev.className !== next.className) return false
|
package/src/PeriodSlider.tsx
CHANGED
|
@@ -662,8 +662,16 @@ export const yearKeyOfCivil = (year: number, monthIndex: number, yearStartMonth:
|
|
|
662
662
|
* Normalizes a date to the start of the specified unit.
|
|
663
663
|
* 指定した単位 (year, month, day) の先頭に日付を正規化します。
|
|
664
664
|
*
|
|
665
|
+
* Every unit reads d by its time value in the system time zone, through a plain copy, never through d's own getters: a Date
|
|
666
|
+
* subclass whose getters report another zone (a `TZDate` of `@date-fns/tz`, a `UTCDate` of `@date-fns/utc`) falls in the same
|
|
667
|
+
* unit as a plain Date of the same instant. This is the package's one reading of a Date; encode, keyInsideDateRange and every
|
|
668
|
+
* unit decision built on them (the body, the hooks, the three selectors) inherit it.
|
|
669
|
+
* どの単位も d を、自身のゲッターではなく時刻値のシステムのタイムゾーンでの読み (素の Date の複製) で読む。ゲッターが別のタイムゾーンを名乗る
|
|
670
|
+
* Date の派生クラス (`@date-fns/tz` の `TZDate`、`@date-fns/utc` の `UTCDate`) も、同じ時刻の素の Date と同じ単位に属する。これがパッケージの
|
|
671
|
+
* 唯一の Date の読み方で、encode・keyInsideDateRange とその上の単位の判定 (本体・フック・3 セレクタ) はすべてこれを継ぐ。
|
|
672
|
+
*
|
|
665
673
|
* @param unit - Time resolution unit / 時間解像度単位
|
|
666
|
-
* @param d - Date to normalize / 正規化対象の日付
|
|
674
|
+
* @param d - Date to normalize, read by its time value / 正規化対象の日付 (時刻値で読む)
|
|
667
675
|
* @param yearStartMonth - Calendar month a `unit: "year"` period starts in, 0-based; 0 keeps the calendar year (default: 0) / `unit: "year"` の 1 年が始まる暦月 (0 始まり)。0 は暦年 (既定: 0)
|
|
668
676
|
* @returns New Date pinned to the start of the unit / 単位先頭に揃えた新しい Date
|
|
669
677
|
* @throws RangeError when unit is not "year", "month" or "day", judged first (`normalizeDateByUnit: unit must be one of "year", "month", "day" (received: ...)`), when d is absent or an Invalid Date, or when yearStartMonth is not an integer in [0, 11] / unit が "year"・"month"・"day" でない場合 (最初に判定)、d が未指定または Invalid Date の場合、yearStartMonth が 0 以上 11 以下の整数でない場合
|
|
@@ -674,8 +682,9 @@ export const normalizeDateByUnit = (unit: PeriodSliderUnit, d: Date, yearStartMo
|
|
|
674
682
|
assertYearStartMonth("normalizeDateByUnit", yearStartMonth)
|
|
675
683
|
const normalized = new Date(d.getTime())
|
|
676
684
|
if (unit === "year") {
|
|
685
|
+
// 年月は d のゲッターでなく複製から読む。派生クラスのゲッターは別のタイムゾーンの年月を名乗り、複製を読む月・日の単位と別の暦になる。
|
|
677
686
|
// 月と日を 1 回で書かないと、月だけ先に書いた時点で月末日が桁溢れし、1 か月先へ飛ぶ
|
|
678
|
-
normalized.setFullYear(yearKeyOfCivil(
|
|
687
|
+
normalized.setFullYear(yearKeyOfCivil(normalized.getFullYear(), normalized.getMonth(), yearStartMonth), yearStartMonth, 1)
|
|
679
688
|
normalized.setHours(0, 0, 0, 0)
|
|
680
689
|
} else if (unit === "month") {
|
|
681
690
|
normalized.setDate(1)
|
|
@@ -691,7 +700,7 @@ export const normalizeDateByUnit = (unit: PeriodSliderUnit, d: Date, yearStartMo
|
|
|
691
700
|
* 日付を比較用数値キーにエンコードします。
|
|
692
701
|
*
|
|
693
702
|
* @param unit - Time resolution unit / 時間解像度単位
|
|
694
|
-
* @param d - Date to encode / エンコード対象の日付
|
|
703
|
+
* @param d - Date to encode, read by its time value in the system time zone (normalizeDateByUnit) / エンコード対象の日付 (時刻値をシステムのタイムゾーンで読む。normalizeDateByUnit)
|
|
695
704
|
* @param yearStartMonth - Calendar month a `unit: "year"` period starts in, 0-based; 0 keeps the calendar year (default: 0) / `unit: "year"` の 1 年が始まる暦月 (0 始まり)。0 は暦年 (既定: 0)
|
|
696
705
|
* @returns Integer key that is strictly monotonic in time for the given unit / 単位ごとに時間について厳密単調な整数キー
|
|
697
706
|
* @throws RangeError when unit is not "year", "month" or "day", judged first (`encode: unit must be one of "year", "month", "day" (received: ...)`), when d is absent or an Invalid Date, or when yearStartMonth is not an integer in [0, 11] / unit が "year"・"month"・"day" でない場合 (最初に判定)、d が未指定または Invalid Date の場合、yearStartMonth が 0 以上 11 以下の整数でない場合
|
|
@@ -830,8 +839,13 @@ export const keyInsideDateRange = (date: Date, unit: PeriodSliderUnit, yearStart
|
|
|
830
839
|
* 月・年の加算は shiftCivilMonths に従い年月日を 1 度で書くため、表現範囲の最後の月への加算も月末日からその日付を返す。
|
|
831
840
|
* Invalid Date になるのは和そのものが Date の表現範囲を越えるときだけ。
|
|
832
841
|
*
|
|
842
|
+
* The calendar fields are read from a plain copy of d's time value in the system time zone, as normalizeDateByUnit reads them,
|
|
843
|
+
* so a Date subclass whose getters report another zone steps from the same date as a plain Date of the same instant.
|
|
844
|
+
* 暦の欄は normalizeDateByUnit と同じく d の時刻値の複製からシステムのタイムゾーンで読むため、ゲッターが別のタイムゾーンを名乗る Date の派生クラスも、
|
|
845
|
+
* 同じ時刻の素の Date と同じ日付から進む。
|
|
846
|
+
*
|
|
833
847
|
* @param unit - Time resolution unit to step by / 加減算する時間解像度単位
|
|
834
|
-
* @param d - Base date / 起点日付
|
|
848
|
+
* @param d - Base date, read by its time value / 起点日付 (時刻値で読む)
|
|
835
849
|
* @param amount - Signed whole number of units to add / 加減算する整数の単位数 (符号付き)
|
|
836
850
|
* @returns New Date advanced by amount units, or an Invalid Date when the sum lies past the Date range / amount 単位進めた新しい Date (和が Date の表現範囲を越える場合は Invalid Date)
|
|
837
851
|
* @throws RangeError when unit is not "year", "month" or "day", judged first (`addUnit: unit must be one of "year", "month", "day" (received: ...)`), when d is absent or an Invalid Date (`addUnit: d must be a valid Date (received: ...)`), or when amount is not an integer (`addUnit: amount must be an integer number of units (received: ...)`) / unit が "year"・"month"・"day" でない場合 (最初に判定)、d が未指定または Invalid Date の場合、amount が整数でない場合
|
|
@@ -847,7 +861,7 @@ export const addUnit = (unit: PeriodSliderUnit, d: Date, amount: number): Date =
|
|
|
847
861
|
if (unit === "year" || unit === "month") {
|
|
848
862
|
// date-fns の加算は加算先の月末を先に組むため、表現範囲の最後の月へ届く加算は結果が表現できても Invalid Date になる。
|
|
849
863
|
// 年月日を 1 度で書けば月末日からでも桁溢れしない
|
|
850
|
-
shifted.setFullYear(...shiftCivilMonths(
|
|
864
|
+
shifted.setFullYear(...shiftCivilMonths(shifted.getFullYear(), shifted.getMonth(), shifted.getDate(), unit === "year" ? amount * 12 : amount))
|
|
851
865
|
return shifted
|
|
852
866
|
}
|
|
853
867
|
shifted.setDate(shifted.getDate() + amount)
|
|
@@ -927,6 +941,8 @@ export const getMacroWindowStartDate = (macroUnit: PeriodSliderUnit, microUnit:
|
|
|
927
941
|
* (年の末尾は 12 月ではなく `yearStartMonth` から数えて 12 番目の月。現地 0 時、0 時の無い日はその日の最初の瞬間)。
|
|
928
942
|
* 最後の単位はキーで数えるため、0-99 年は自分の年のまま、始まりが表現範囲の外にある期間も最後の単位を返す。
|
|
929
943
|
* それ以外の組では d 自身のミクロ単位の始まり。
|
|
944
|
+
* d is read by its time value in the system time zone (a plain copy), as normalizeDateByUnit reads it.
|
|
945
|
+
* d は normalizeDateByUnit と同じく時刻値の複製からシステムのタイムゾーンで読む。
|
|
930
946
|
*
|
|
931
947
|
* @param macroUnit - Macro scale unit / マクロ側の時間解像度単位
|
|
932
948
|
* @param microUnit - Micro scale unit / ミクロ側の時間解像度単位
|
|
@@ -941,12 +957,14 @@ export const getMacroWindowEndDate = (macroUnit: PeriodSliderUnit, microUnit: Pe
|
|
|
941
957
|
if (!isValidDate(d)) throw new RangeError(`getMacroWindowEndDate: d must be a valid Date (received: ${describeValue(d)})`)
|
|
942
958
|
assertYearStartMonth("getMacroWindowEndDate", yearStartMonth)
|
|
943
959
|
let lastKey: number
|
|
960
|
+
// 期間の年月は d のゲッターでなく複製から読む。派生クラスのゲッターは別のタイムゾーンの年月を名乗り、残りの組 (normalizeDateByUnit) と別の期間を指す
|
|
961
|
+
const plain = new Date(d.getTime())
|
|
944
962
|
if (macroUnit === "year" && microUnit !== "year") {
|
|
945
963
|
// 年の末尾は開始月から数えて 12 番目の月。暦年固定で 12 月を書くと、開始月をずらした年は自分が含まない月を末尾として名乗る
|
|
946
|
-
const [lastYear, lastMonth] = shiftCivilMonths(yearKeyOfCivil(
|
|
964
|
+
const [lastYear, lastMonth] = shiftCivilMonths(yearKeyOfCivil(plain.getFullYear(), plain.getMonth(), yearStartMonth), yearStartMonth, 1, 11)
|
|
947
965
|
lastKey = microUnit === "month" ? lastYear * 12 + lastMonth : dayKeyOfCivil(lastYear, lastMonth, daysInMonth(lastYear, lastMonth))
|
|
948
966
|
} else if (macroUnit === "month" && microUnit === "day") {
|
|
949
|
-
lastKey = dayKeyOfCivil(
|
|
967
|
+
lastKey = dayKeyOfCivil(plain.getFullYear(), plain.getMonth(), daysInMonth(plain.getFullYear(), plain.getMonth()))
|
|
950
968
|
} else {
|
|
951
969
|
// d 自身のミクロ単位の始まりを返す組。その始まりが範囲の外だと正規化は送出せずに Invalid Date を返し、clampDateInto を素通りして別の関数の名で落ちる
|
|
952
970
|
encodeInsideDateRange("getMacroWindowEndDate", "d", d, microUnit, yearStartMonth, "")
|
|
@@ -1064,7 +1082,7 @@ export interface PeriodQuarter {
|
|
|
1064
1082
|
* 四半期の境界は `yearStartMonth` を 3 で割った剰余だけで決まり、番号は `yearStartMonth` そのもので決まる。
|
|
1065
1083
|
* 0・3・6・9 では境界が暦四半期と一致して番号だけがずれ、他の 8 通りでは境界自体が動く。
|
|
1066
1084
|
*
|
|
1067
|
-
* @param d - Date to classify / 判定対象の日付
|
|
1085
|
+
* @param d - Date to classify, read by its time value in the system time zone (a plain copy), as normalizeDateByUnit reads it / 判定対象の日付 (normalizeDateByUnit と同じく時刻値の複製をシステムのタイムゾーンで読む)
|
|
1068
1086
|
* @param yearStartMonth - Calendar month a year starts in, 0-based; required, so the calendar year is never assumed / 1 年が始まる暦月 (0 始まり)。必須であり暦年を暗黙に仮定しない
|
|
1069
1087
|
* @returns Quarter coordinates of the date / 日付の四半期座標
|
|
1070
1088
|
* @throws RangeError when d is absent or an Invalid Date, or when yearStartMonth is not an integer in [0, 11] / d が未指定または Invalid Date の場合、yearStartMonth が 0 以上 11 以下の整数でない場合
|
|
@@ -1075,11 +1093,12 @@ export const resolveQuarter = (d: Date, yearStartMonth: number): PeriodQuarter =
|
|
|
1075
1093
|
// ずらした年の定義はパッケージに 1 つしかない。ここで年を導き直すと、レールが名乗る年と
|
|
1076
1094
|
// ラベルが印字する年が別の規則で決まることになる
|
|
1077
1095
|
const year = normalizeDateByUnit("year", d, yearStartMonth).getFullYear()
|
|
1096
|
+
// 月も年と同じく d のゲッターでなく複製から読む。派生クラスのゲッターは別のタイムゾーンの月を名乗り、年と別の暦の四半期を指す
|
|
1097
|
+
const startDate = new Date(d.getTime())
|
|
1078
1098
|
// 剰余は開始月より前の暦月で負を返し、四半期が 0 以下へ落ちる。+12 してもう一度畳む
|
|
1079
|
-
const monthsIntoYear = (((
|
|
1099
|
+
const monthsIntoYear = (((startDate.getMonth() - yearStartMonth) % 12) + 12) % 12
|
|
1080
1100
|
// monthsIntoYear は 0-11 に閉じており商は 0-3。序数が値域を外れることはない
|
|
1081
1101
|
const quarter = (Math.floor(monthsIntoYear / 3) + 1) as 1 | 2 | 3 | 4
|
|
1082
|
-
const startDate = new Date(d.getTime())
|
|
1083
1102
|
// 年・月・日を 1 回で書かないと、月だけ先に書いた時点で月末日が桁溢れし 1 か月先へ飛ぶ。
|
|
1084
1103
|
// 第 2 引数は 12 を超え得るが、同じ呼び出しなら翌年へ正しく繰り上がる
|
|
1085
1104
|
startDate.setFullYear(year, yearStartMonth + 3 * (quarter - 1), 1)
|
|
@@ -1149,15 +1168,17 @@ export const formatIsoYear = (year: number): string => (year >= 0 ? String(year)
|
|
|
1149
1168
|
* 指定単位における日付の ISO 8601 暦表記 (ロケール非依存) を生成する関数。
|
|
1150
1169
|
*
|
|
1151
1170
|
* @param unit - Time resolution unit deciding the precision / 精度を決める時間解像度単位
|
|
1152
|
-
* @param d - Valid date to render / 表記対象の有効な日付
|
|
1171
|
+
* @param d - Valid date to render, read by its time value in the system time zone (a plain copy) / 表記対象の有効な日付 (時刻値の複製をシステムのタイムゾーンで読む)
|
|
1153
1172
|
* @returns "2025" for year, "2025-01" for month, "2025-01-15" for day / year は "2025"、month は "2025-01"、day は "2025-01-15"
|
|
1154
1173
|
*/
|
|
1155
1174
|
const formatIsoByUnit = (unit: PeriodSliderUnit, d: Date): string => {
|
|
1156
|
-
|
|
1175
|
+
// Intl の経路は時刻値をシステムのタイムゾーンで読む。派生クラスのゲッターを読むと、formatLocale の有無で別の日を名乗る
|
|
1176
|
+
const plain = new Date(d.getTime())
|
|
1177
|
+
const year = formatIsoYear(plain.getFullYear())
|
|
1157
1178
|
if (unit === "year") return year
|
|
1158
|
-
const month = String(
|
|
1179
|
+
const month = String(plain.getMonth() + 1).padStart(2, "0")
|
|
1159
1180
|
if (unit === "month") return `${year}-${month}`
|
|
1160
|
-
const day = String(
|
|
1181
|
+
const day = String(plain.getDate()).padStart(2, "0")
|
|
1161
1182
|
return `${year}-${month}-${day}`
|
|
1162
1183
|
}
|
|
1163
1184
|
|
|
@@ -1171,6 +1192,10 @@ const formatIsoByUnit = (unit: PeriodSliderUnit, d: Date): string => {
|
|
|
1171
1192
|
* into a generic package, and Intl output drifts between ICU versions.
|
|
1172
1193
|
* 解決順序は customFormat が最優先、次に formatLocale 指定時の Intl.DateTimeFormat、未指定時はロケール非依存の ISO 8601。
|
|
1173
1194
|
* ISO 表記は本パッケージの仕様上の既定値であり代替値ではない (自然言語既定は汎用パッケージに 1 言語を焼き付け、Intl 出力は ICU 版で揺れるため)。
|
|
1195
|
+
* Both built-in tiers read d by its time value in the system time zone, so a Date subclass whose getters report another zone is
|
|
1196
|
+
* labelled as a plain Date of the same instant; customFormat receives d as passed.
|
|
1197
|
+
* 組み込みの 2 段はどちらも d を時刻値のシステムのタイムゾーンで読むため、ゲッターが別のタイムゾーンを名乗る Date の派生クラスも、同じ時刻の
|
|
1198
|
+
* 素の Date と同じ表記になる (customFormat には渡された d をそのまま渡す)。
|
|
1174
1199
|
*
|
|
1175
1200
|
* @param unit - Time resolution unit deciding the precision / 精度を決める時間解像度単位
|
|
1176
1201
|
* @param d - Date to format / 書式化対象の日付
|