@aiquants/period-slider 0.17.0 → 0.18.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 +144 -0
- package/README.md +54 -48
- package/dist/DateSelector.d.ts.map +1 -1
- package/dist/MonthSelector.d.ts.map +1 -1
- package/dist/PeriodSlider.d.ts +113 -31
- package/dist/PeriodSlider.d.ts.map +1 -1
- package/dist/VirtualDateDropdown.d.ts +19 -1
- package/dist/VirtualDateDropdown.d.ts.map +1 -1
- package/dist/YearSelector.d.ts.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +2604 -2593
- package/dist/inputValue.d.ts +20 -12
- package/dist/inputValue.d.ts.map +1 -1
- package/dist/optionText.d.ts +1 -1
- package/dist/optionText.d.ts.map +1 -1
- package/dist/selectorStep.d.ts +14 -22
- package/dist/selectorStep.d.ts.map +1 -1
- package/dist/useCommittedRef.d.ts +38 -19
- package/dist/useCommittedRef.d.ts.map +1 -1
- package/dist/usePeriodSlider.d.ts +32 -22
- package/dist/usePeriodSlider.d.ts.map +1 -1
- package/dist/useUncontrolledSelection.d.ts +26 -13
- package/dist/useUncontrolledSelection.d.ts.map +1 -1
- package/dist/vernierStep.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/DateSelector.tsx +23 -54
- package/src/MonthSelector.tsx +17 -46
- package/src/PeriodSlider.tsx +216 -82
- package/src/VirtualDateDropdown.tsx +90 -46
- package/src/YearSelector.tsx +18 -47
- package/src/inputValue.ts +20 -12
- package/src/optionText.ts +3 -5
- package/src/selectorStep.ts +25 -32
- package/src/useCommittedRef.ts +54 -24
- package/src/usePeriodSlider.ts +211 -98
- package/src/useUncontrolledSelection.ts +51 -22
- package/src/vernierStep.ts +7 -7
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useUncontrolledSelection.d.ts","sourceRoot":"","sources":["../src/useUncontrolledSelection.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useUncontrolledSelection.d.ts","sourceRoot":"","sources":["../src/useUncontrolledSelection.ts"],"names":[],"mappings":"AAkCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,wBAAgB,wBAAwB,CAAC,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,YAAY,EAAE,OAAO,EAAE,SAAS,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,EAAE,KAAK,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,CAAC,GAAG,SAAS,CAAC,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,IAAI,CAAC,CAUhL"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vernierStep.d.ts","sourceRoot":"","sources":["../src/vernierStep.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAGH,OAAO,
|
|
1
|
+
{"version":3,"file":"vernierStep.d.ts","sourceRoot":"","sources":["../src/vernierStep.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAGH,OAAO,EAA0C,KAAK,iBAAiB,EAAuB,KAAK,gBAAgB,EAAuE,MAAM,gBAAgB,CAAA;AAChN,OAAO,EAA0F,KAAK,iBAAiB,EAA+B,KAAK,oBAAoB,EAAE,MAAM,gBAAgB,CAAA;AACvM,OAAO,EAAE,KAAK,WAAW,EAAiL,MAAM,YAAY,CAAA;AAE5N;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,GAAG,IAAI,CAIhE;AAED,2DAA2D;AAC3D,KAAK,UAAU,GAAG,OAAO,CAAC,WAAW,EAAE;IAAE,IAAI,EAAE,YAAY,CAAA;CAAE,CAAC,CAAA;AAK9D;;;;;;;;;;;;;GAaG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,SAAS,CAAC,IAAI,EAAE,IAAI,CAAC,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iscreteIndexWindow,
|
|
46
46
|
toDaySerial,
|
|
47
47
|
} from "./dateOptionDomain"
|
|
48
|
-
import { dateMemoKey, describeValue,
|
|
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 { formatLabel, type PeriodOptionFormatter, type PeriodSliderUnit } from "./PeriodSlider"
|
|
51
|
+
import { assertLocaleTag, encodeInsideDateRange, formatLabel, type PeriodOptionFormatter, type PeriodSliderUnit, sentinelBoundRemedy } from "./PeriodSlider"
|
|
52
52
|
import { STEPPER_ROW_GAP_CLASS, StepperButton } from "./StepperButton"
|
|
53
53
|
import { areSelectorDatePropsEqual } from "./selectorMemo"
|
|
54
|
-
import {
|
|
54
|
+
import { assertSelectorStepper, type SelectorStepper } from "./selectorStep"
|
|
55
55
|
import { useResolvedLabels } from "./useResolvedLabels"
|
|
56
56
|
import { useUncontrolledSelection } from "./useUncontrolledSelection"
|
|
57
57
|
import { VirtualDateDropdown } from "./VirtualDateDropdown"
|
|
@@ -135,13 +135,13 @@ const toStartOfDay = (date: Date): Date => {
|
|
|
135
135
|
* Remedy the error names for a sentinel bound under `variant="date"` / `"dropdown"`; values and `allowedDates` elements are not bounds and get none.
|
|
136
136
|
* `variant="date"` / `"dropdown"` で番兵の境界に対するエラーが示す直し方 (値と `allowedDates` の要素は境界ではないため添えない)。
|
|
137
137
|
*/
|
|
138
|
-
const SENTINEL_BOUND_REMEDY =
|
|
138
|
+
const SENTINEL_BOUND_REMEDY = sentinelBoundRemedy("leave the bound out where the variant allows it, or declare the days to offer with minYear / maxYear or allowedDates")
|
|
139
139
|
|
|
140
140
|
/**
|
|
141
141
|
* Remedy the error names for a sentinel bound under `variant="split-dropdown"`, which needs both bounds and rejects `allowedDates`, so only the years are left to declare.
|
|
142
142
|
* `variant="split-dropdown"` で番兵の境界に対するエラーが示す直し方 (この変種は境界の双方を要し `allowedDates` も拒むため、宣言できるのは年だけ)。
|
|
143
143
|
*/
|
|
144
|
-
const SPLIT_SENTINEL_BOUND_REMEDY = '
|
|
144
|
+
const SPLIT_SENTINEL_BOUND_REMEDY = sentinelBoundRemedy('variant="split-dropdown" requires both bounds, so declare the years to offer with minYear / maxYear')
|
|
145
145
|
|
|
146
146
|
/**
|
|
147
147
|
* Largest day number of any calendar month; the split day list tries every day up to it and keeps the ones that exist.
|
|
@@ -155,10 +155,11 @@ const MAX_DAY_OF_MONTH = 31
|
|
|
155
155
|
*
|
|
156
156
|
* The component reasons on whole days, so a Date whose own day cannot start inside the Date range (the
|
|
157
157
|
* `new Date(-8.64e15)` "earliest date" sentinel in any time zone other than UTC) is as unusable as an Invalid
|
|
158
|
-
* Date and is rejected here, naming the prop, rather than deep inside the day arithmetic.
|
|
158
|
+
* Date and is rejected here, naming the prop, rather than deep inside the day arithmetic. The day-start rule and its message
|
|
159
|
+
* are encodeInsideDateRange's, at unit day.
|
|
159
160
|
* 本コンポーネントは日単位で判断するため、その日の始まりが Date の表現範囲に入らない日付
|
|
160
161
|
* (UTC 以外のタイムゾーンでの `new Date(-8.64e15)` のような「最古の日付」番兵) は Invalid Date と同じく使えない。
|
|
161
|
-
* 日の算術の奥ではなくここで、prop
|
|
162
|
+
* 日の算術の奥ではなくここで、prop 名を名指して拒否する。日の始まりの規則と文言は encodeInsideDateRange のもの (単位は日)。
|
|
162
163
|
*
|
|
163
164
|
* @param propName - Prop name reported in the error message / エラーメッセージに載せるプロパティ名
|
|
164
165
|
* @param date - Date to validate / 検証対象の日付
|
|
@@ -170,30 +171,9 @@ const assertValidDateProp = (propName: string, date: Date, remedy: string): void
|
|
|
170
171
|
// Date 以外が渡ると getTime が存在せず TypeError になり、prop 名を名指さないまま描画が落ちる。
|
|
171
172
|
// 型検査を受けない JavaScript の利用側からも届く入口であるため、ここで形から拒否する
|
|
172
173
|
if (!isValidDate(date)) {
|
|
173
|
-
throw new RangeError(`DateSelector:
|
|
174
|
-
}
|
|
175
|
-
if (Number.isNaN(toStartOfDay(date).getTime())) {
|
|
176
|
-
throw new RangeError(`DateSelector: "${propName}" must fall on a day that starts inside the Date range (received ${describeValue(date)})${remedy}`)
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Asserts that a formatLocale prop carries a language tag Intl.DateTimeFormat accepts, failing fast otherwise.
|
|
182
|
-
* formatLocale プロパティが Intl.DateTimeFormat の受理する言語タグであることの表明。無効時は即時失敗。
|
|
183
|
-
*
|
|
184
|
-
* @param formatLocale - Prop value to validate / 検証対象のプロパティ値
|
|
185
|
-
* @returns Nothing / 戻り値なし
|
|
186
|
-
* @throws RangeError when the tag is empty or Intl rejects it / 空文字列や Intl が拒否するタグの場合
|
|
187
|
-
*/
|
|
188
|
-
const assertFormatLocaleProp = (formatLocale: string | undefined): void => {
|
|
189
|
-
if (formatLocale === undefined) return
|
|
190
|
-
try {
|
|
191
|
-
// 空文字列や構造的に不正なタグは Intl の送出する例外でしか判定できない。
|
|
192
|
-
// 自前の正規表現で代用すると ICU が受理するタグを取りこぼす
|
|
193
|
-
Intl.DateTimeFormat.supportedLocalesOf([formatLocale])
|
|
194
|
-
} catch {
|
|
195
|
-
throw new RangeError(`DateSelector: "formatLocale" must be a valid BCP 47 language tag (received ${describeValue(formatLocale)})`)
|
|
174
|
+
throw new RangeError(`DateSelector: ${propName} must be a valid Date (received: ${describeValue(date)})`)
|
|
196
175
|
}
|
|
176
|
+
encodeInsideDateRange("DateSelector", propName, date, "day", 0, remedy)
|
|
197
177
|
}
|
|
198
178
|
|
|
199
179
|
/**
|
|
@@ -320,7 +300,7 @@ const splitDropdownDayStart = (date: Date): Date => (yearStartsInsideDateRange(d
|
|
|
320
300
|
*/
|
|
321
301
|
const assertSplitYearProp = (propName: string, date: Date, remedy: string): void => {
|
|
322
302
|
if (!yearStartsInsideDateRange(date)) {
|
|
323
|
-
throw new RangeError(`DateSelector:
|
|
303
|
+
throw new RangeError(`DateSelector: ${propName} must fall in a year that starts inside the Date range, because variant="split-dropdown" draws its year (received: ${describeValue(date)})${remedy}`)
|
|
324
304
|
}
|
|
325
305
|
}
|
|
326
306
|
|
|
@@ -385,7 +365,7 @@ const listPartOptions = (first: number, last: number, anchorOf: (value: number)
|
|
|
385
365
|
const DateSelectorComponent = (props: DateSelectorProps): React.JSX.Element => {
|
|
386
366
|
const { value, initialDate, onChange, stepper, children, minYear, maxYear, minDate, maxDate, allowedDates, variant = "date", className = "", style, disabled = false, locale, labels, formatLocale, formatOption } = props
|
|
387
367
|
|
|
388
|
-
|
|
368
|
+
if (formatLocale !== undefined) assertLocaleTag("DateSelector", "formatLocale", formatLocale)
|
|
389
369
|
// 分割セレクトは境界の双方を要し allowedDates も拒む。境界を省く・許可日で宣言するという直し方は、従うと別の RangeError になる
|
|
390
370
|
const boundRemedy = variant === "split-dropdown" ? SPLIT_SENTINEL_BOUND_REMEDY : SENTINEL_BOUND_REMEDY
|
|
391
371
|
if (value !== undefined) assertValidDateProp("value", value, "")
|
|
@@ -400,41 +380,30 @@ const DateSelectorComponent = (props: DateSelectorProps): React.JSX.Element => {
|
|
|
400
380
|
if (maxDate !== undefined) assertSplitYearProp("maxDate", maxDate, boundRemedy)
|
|
401
381
|
}
|
|
402
382
|
if (minYear !== undefined && !Number.isInteger(minYear)) {
|
|
403
|
-
throw new RangeError(`DateSelector:
|
|
383
|
+
throw new RangeError(`DateSelector: minYear must be an integer year (received: ${describeValue(minYear)})`)
|
|
404
384
|
}
|
|
405
385
|
if (maxYear !== undefined && !Number.isInteger(maxYear)) {
|
|
406
|
-
throw new RangeError(`DateSelector:
|
|
386
|
+
throw new RangeError(`DateSelector: maxYear must be an integer year (received: ${describeValue(maxYear)})`)
|
|
407
387
|
}
|
|
408
388
|
// 年の境界が名指すのは端の日 (1 月 1 日・12 月 31 日) であり、その日が表現できない年は境界として守れない
|
|
409
389
|
if (minYear !== undefined && Number.isNaN(startOfYear(minYear).getTime())) {
|
|
410
|
-
throw new RangeError(`DateSelector:
|
|
390
|
+
throw new RangeError(`DateSelector: minYear must name a year whose first day starts inside the Date range (received: ${describeValue(minYear)})`)
|
|
411
391
|
}
|
|
412
392
|
if (maxYear !== undefined && Number.isNaN(endOfYear(maxYear).getTime())) {
|
|
413
|
-
throw new RangeError(`DateSelector:
|
|
393
|
+
throw new RangeError(`DateSelector: maxYear must name a year whose last day starts inside the Date range (received: ${describeValue(maxYear)})`)
|
|
414
394
|
}
|
|
415
395
|
if (variant === "split-dropdown" && allowedDates !== undefined) {
|
|
416
|
-
throw new RangeError('DateSelector: variant="split-dropdown" cannot honour
|
|
396
|
+
throw new RangeError('DateSelector: variant="split-dropdown" cannot honour allowedDates; use variant="dropdown" or variant="date" for a discrete date set')
|
|
417
397
|
}
|
|
418
398
|
// 配列でない値 (null・単独の Date・文字列) は、下の境界の要否の判定を `=== undefined` で素通りし、キーの文字列化の `.map` で
|
|
419
399
|
// 名前を名乗らない TypeError になる
|
|
420
|
-
|
|
421
|
-
throw new RangeError(`DateSelector: "allowedDates" must be an array of Dates (received ${describeValue(allowedDates)})`)
|
|
422
|
-
}
|
|
400
|
+
assertCandidateListInput("DateSelector", "allowedDates", allowedDates)
|
|
423
401
|
|
|
424
402
|
const effectiveDate = value ?? initialDate
|
|
425
403
|
if (effectiveDate === undefined) {
|
|
426
|
-
throw new RangeError("DateSelector: either
|
|
427
|
-
}
|
|
428
|
-
if (stepper !== undefined) {
|
|
429
|
-
const fault = findSelectorStepperFault(stepper)
|
|
430
|
-
if (fault !== null) {
|
|
431
|
-
throw new RangeError(`DateSelector: "${fault.field}" must be ${fault.expected} (received ${fault.received})`)
|
|
432
|
-
}
|
|
433
|
-
// 引き受けた所有者が値を動かす。非制御の initialDate は外から動かす口が無く、押しても表示が変わらないまま固まる
|
|
434
|
-
if (value === undefined) {
|
|
435
|
-
throw new RangeError('DateSelector: "stepper" requires the controlled "value"; the owner that takes the ◀▶ over moves the value itself, so an uncontrolled "initialDate" would never change')
|
|
436
|
-
}
|
|
404
|
+
throw new RangeError("DateSelector: either value (controlled) or initialDate (uncontrolled) is required; the selected date is never inferred from the wall clock")
|
|
437
405
|
}
|
|
406
|
+
if (stepper !== undefined) assertSelectorStepper("DateSelector", stepper, value !== undefined)
|
|
438
407
|
|
|
439
408
|
// ラベル解決は最初のフック。日付 Props の検証を全て済ませてから呼び、両方が不正なら日付側を先に報告する
|
|
440
409
|
const resolvedLabels = useResolvedLabels(locale, labels)
|
|
@@ -449,7 +418,7 @@ const DateSelectorComponent = (props: DateSelectorProps): React.JSX.Element => {
|
|
|
449
418
|
// 置いたまま分割セレクトへ切り替えると、Props の検証を全て通ったまま年の部位を名指せない日を描かせるため、ここで止める
|
|
450
419
|
if (variant === "split-dropdown" && !isControlled && !yearStartsInsideDateRange(activeDate)) {
|
|
451
420
|
throw new RangeError(
|
|
452
|
-
`DateSelector: variant="split-dropdown" cannot draw the uncontrolled selection held from an earlier render (holding ${describeValue(activeDate)}), because that selection's year starts outside the Date range; control the date with
|
|
421
|
+
`DateSelector: variant="split-dropdown" cannot draw the uncontrolled selection held from an earlier render (holding ${describeValue(activeDate)}), because that selection's year starts outside the Date range; control the date with value to decide what the split dropdown holds`,
|
|
453
422
|
)
|
|
454
423
|
}
|
|
455
424
|
|
|
@@ -477,10 +446,10 @@ const DateSelectorComponent = (props: DateSelectorProps): React.JSX.Element => {
|
|
|
477
446
|
const maxBoundTime = maxDateBound?.getTime() ?? null
|
|
478
447
|
|
|
479
448
|
if (minDateBound !== null && maxDateBound !== null && minDateBound.getTime() > maxDateBound.getTime()) {
|
|
480
|
-
throw new RangeError(`DateSelector: the resolved
|
|
449
|
+
throw new RangeError(`DateSelector: the resolved upper bound must not precede the resolved lower bound (lower: ${formatIsoDay(minDateBound)}, upper: ${formatIsoDay(maxDateBound)})`)
|
|
481
450
|
}
|
|
482
451
|
if (variant !== "date" && allowedDates === undefined && (minDateBound === null || maxDateBound === null)) {
|
|
483
|
-
throw new RangeError(`DateSelector: variant=${describeValue(variant)}
|
|
452
|
+
throw new RangeError(`DateSelector: variant=${describeValue(variant)} requires a lower bound (minYear or minDate) and an upper bound (maxYear or maxDate)${variant === "dropdown" ? ", or allowedDates" : ""}; the option list is never invented`)
|
|
484
453
|
}
|
|
485
454
|
|
|
486
455
|
// 配列の同一性が変わらない限り作り直さない。本体直下に置くと、無関係な再描画のたびに
|
package/src/MonthSelector.tsx
CHANGED
|
@@ -23,12 +23,12 @@
|
|
|
23
23
|
|
|
24
24
|
import type React from "react"
|
|
25
25
|
import { memo, useMemo } from "react"
|
|
26
|
-
import {
|
|
26
|
+
import { assertCandidateListInput, describeValue, isValidDate } from "./inputValue"
|
|
27
27
|
import { areLabelOverridesEqual, type PeriodSliderLabelOverrides, type PeriodSliderLocale } from "./labels"
|
|
28
|
-
import { formatIsoYear, formatLabel, type PeriodOptionFormatter } from "./PeriodSlider"
|
|
28
|
+
import { assertLocaleTag, encodeInsideDateRange, formatIsoYear, formatLabel, type PeriodOptionFormatter, sentinelBoundRemedy } from "./PeriodSlider"
|
|
29
29
|
import { STEPPER_ROW_GAP_CLASS, StepperButton } from "./StepperButton"
|
|
30
30
|
import { areSelectorDatePropsEqual } from "./selectorMemo"
|
|
31
|
-
import {
|
|
31
|
+
import { assertSelectorStepper, type SelectorStepper } from "./selectorStep"
|
|
32
32
|
import { useResolvedLabels } from "./useResolvedLabels"
|
|
33
33
|
import { useUncontrolledSelection } from "./useUncontrolledSelection"
|
|
34
34
|
import { type DrawnValues, drawnValuesHas, drawnValuesNeighbour } from "./valueSet"
|
|
@@ -145,7 +145,7 @@ function formatMonth(date: Date): string {
|
|
|
145
145
|
* Remedy the error names for a sentinel bound; values and `allowedMonths` elements are not bounds and get none.
|
|
146
146
|
* 番兵の境界に対するエラーが示す直し方 (値と `allowedMonths` の要素は境界ではないため添えない)。
|
|
147
147
|
*/
|
|
148
|
-
const SENTINEL_BOUND_REMEDY =
|
|
148
|
+
const SENTINEL_BOUND_REMEDY = sentinelBoundRemedy("leave the bound out where the variant allows it, or declare the months to offer with minYear / maxYear or allowedMonths")
|
|
149
149
|
|
|
150
150
|
/**
|
|
151
151
|
* Throw when a Date-typed prop does not hold a real calendar date whose month starts inside the Date range.
|
|
@@ -173,7 +173,9 @@ function assertValidDateProp(date: Date | undefined, propName: string, remedy: s
|
|
|
173
173
|
* 値が、その月の始まりが Date の表現範囲に入る実在日付でない場合の即時エラー送出 (`undefined` も拒否)。
|
|
174
174
|
*
|
|
175
175
|
* An `allowedMonths` element reads this directly, so a hole or an `undefined` element is named instead of crashing deeper in.
|
|
176
|
+
* The month-start rule and its message are encodeInsideDateRange's, at unit month.
|
|
176
177
|
* `allowedMonths` の要素はこれを直接通るため、穴や `undefined` の要素も奥で落ちずに名指される。
|
|
178
|
+
* 月の始まりの規則と文言は encodeInsideDateRange のもの (単位は月)。
|
|
177
179
|
*
|
|
178
180
|
* @param date - Value to validate / 検証対象の値
|
|
179
181
|
* @param propName - Prop name used in the error message / エラーメッセージ用 prop 名
|
|
@@ -182,11 +184,9 @@ function assertValidDateProp(date: Date | undefined, propName: string, remedy: s
|
|
|
182
184
|
*/
|
|
183
185
|
function assertValidDateValue(date: Date, propName: string, remedy: string): void {
|
|
184
186
|
if (!isValidDate(date)) {
|
|
185
|
-
throw new RangeError(`MonthSelector:
|
|
186
|
-
}
|
|
187
|
-
if (Number.isNaN(monthStartOf(date).getTime())) {
|
|
188
|
-
throw new RangeError(`MonthSelector: \`${propName}\` must fall in a month that starts inside the Date range, received ${describeValue(date)}${remedy}.`)
|
|
187
|
+
throw new RangeError(`MonthSelector: ${propName} must be a valid Date (received: ${describeValue(date)})`)
|
|
189
188
|
}
|
|
189
|
+
encodeInsideDateRange("MonthSelector", propName, date, "month", 0, remedy)
|
|
190
190
|
}
|
|
191
191
|
|
|
192
192
|
/**
|
|
@@ -201,30 +201,12 @@ function assertValidDateValue(date: Date, propName: string, remedy: string): voi
|
|
|
201
201
|
function assertValidYearProp(year: number | undefined, propName: string, boundaryMonth: 0 | 11): void {
|
|
202
202
|
if (year === undefined) return
|
|
203
203
|
if (!Number.isInteger(year)) {
|
|
204
|
-
throw new RangeError(`MonthSelector:
|
|
204
|
+
throw new RangeError(`MonthSelector: ${propName} must be an integer year (received: ${describeValue(year)})`)
|
|
205
205
|
}
|
|
206
206
|
// 年の境界が名指すのは端の月 (下限なら 1 月、上限なら 12 月) だけで、その月が表現できない年は境界として守れない。
|
|
207
207
|
// 反対側の端まで要求すると、表現範囲の端の年を正しく名指す境界 (minYear=275760 など) まで拒む
|
|
208
208
|
if (Number.isNaN(monthStart(year, boundaryMonth).getTime())) {
|
|
209
|
-
throw new RangeError(`MonthSelector:
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Throw when the formatLocale prop does not hold a language tag Intl.DateTimeFormat accepts.
|
|
215
|
-
* formatLocale prop が Intl.DateTimeFormat の受理する言語タグでない場合の即時エラー送出。
|
|
216
|
-
*
|
|
217
|
-
* @param formatLocale - Prop value to validate / 検証対象の prop 値
|
|
218
|
-
* @returns Nothing; throws RangeError on a tag Intl rejects / 戻り値なし (Intl が拒否するタグは RangeError)
|
|
219
|
-
*/
|
|
220
|
-
function assertFormatLocaleProp(formatLocale: string | undefined): void {
|
|
221
|
-
if (formatLocale === undefined) return
|
|
222
|
-
try {
|
|
223
|
-
// 空文字列や構造的に不正なタグは Intl の送出する例外でしか判定できない。
|
|
224
|
-
// 自前の正規表現で代用すると ICU が受理するタグを取りこぼす
|
|
225
|
-
Intl.DateTimeFormat.supportedLocalesOf([formatLocale])
|
|
226
|
-
} catch {
|
|
227
|
-
throw new RangeError(`MonthSelector: \`formatLocale\` must be a valid BCP 47 language tag, received ${describeValue(formatLocale)}.`)
|
|
209
|
+
throw new RangeError(`MonthSelector: ${propName} must name a year whose ${boundaryMonth === 0 ? "January" : "December"} starts inside the Date range (received: ${describeValue(year)})`)
|
|
228
210
|
}
|
|
229
211
|
}
|
|
230
212
|
|
|
@@ -375,7 +357,7 @@ function toDescendingOptions(monthsAscending: Date[], formatLocale: string | und
|
|
|
375
357
|
const MonthSelectorComponent = (props: MonthSelectorProps): React.JSX.Element => {
|
|
376
358
|
const { value, initialDate, onChange, stepper, children, variant = "date", minYear, maxYear, minDate, maxDate, allowedMonths, isWarning = false, className = "", style, disabled = false, locale, labels, formatLocale, formatOption } = props
|
|
377
359
|
|
|
378
|
-
|
|
360
|
+
if (formatLocale !== undefined) assertLocaleTag("MonthSelector", "formatLocale", formatLocale)
|
|
379
361
|
assertValidDateProp(value, "value", "")
|
|
380
362
|
assertValidDateProp(initialDate, "initialDate", "")
|
|
381
363
|
assertValidDateProp(minDate, "minDate", SENTINEL_BOUND_REMEDY)
|
|
@@ -384,9 +366,7 @@ const MonthSelectorComponent = (props: MonthSelectorProps): React.JSX.Element =>
|
|
|
384
366
|
assertValidYearProp(maxYear, "maxYear", 11)
|
|
385
367
|
// 配列でない値を要素の検証へ回すと、文字列は 1 文字ずつ要素として読まれて一覧ではなく `allowedMonths[0]` を名指し、null や単独の Date は
|
|
386
368
|
// 名前を名乗らない TypeError になる
|
|
387
|
-
|
|
388
|
-
throw new RangeError(`MonthSelector: \`allowedMonths\` must be an array of Dates, received ${describeValue(allowedMonths)}.`)
|
|
389
|
-
}
|
|
369
|
+
assertCandidateListInput("MonthSelector", "allowedMonths", allowedMonths)
|
|
390
370
|
if (allowedMonths !== undefined) {
|
|
391
371
|
// 添字で回すのは疎配列の穴も要素として名指すため (forEach は穴を飛ばし、後段の走査が undefined を読んで落ちる)
|
|
392
372
|
for (let index = 0; index < allowedMonths.length; index++) {
|
|
@@ -396,18 +376,9 @@ const MonthSelectorComponent = (props: MonthSelectorProps): React.JSX.Element =>
|
|
|
396
376
|
|
|
397
377
|
const effectiveDate = value ?? initialDate
|
|
398
378
|
if (effectiveDate === undefined) {
|
|
399
|
-
throw new RangeError("MonthSelector: either
|
|
400
|
-
}
|
|
401
|
-
if (stepper !== undefined) {
|
|
402
|
-
const fault = findSelectorStepperFault(stepper)
|
|
403
|
-
if (fault !== null) {
|
|
404
|
-
throw new RangeError(`MonthSelector: \`${fault.field}\` must be ${fault.expected}, received ${fault.received}.`)
|
|
405
|
-
}
|
|
406
|
-
// 引き受けた所有者が値を動かす。非制御の initialDate は外から動かす口が無く、押しても表示が変わらないまま固まる
|
|
407
|
-
if (value === undefined) {
|
|
408
|
-
throw new RangeError("MonthSelector: `stepper` requires the controlled `value`; the owner that takes the ◀▶ over moves the value itself, so an uncontrolled `initialDate` would never change.")
|
|
409
|
-
}
|
|
379
|
+
throw new RangeError("MonthSelector: either value (controlled) or initialDate (uncontrolled) is required; the selected month is never inferred from the wall clock")
|
|
410
380
|
}
|
|
381
|
+
if (stepper !== undefined) assertSelectorStepper("MonthSelector", stepper, value !== undefined)
|
|
411
382
|
|
|
412
383
|
// ラベル解決は最初のフック。日付 Props の検証を全て済ませてから呼び、両方が不正なら日付側を先に報告する
|
|
413
384
|
const resolvedLabels = useResolvedLabels(locale, labels)
|
|
@@ -425,10 +396,10 @@ const MonthSelectorComponent = (props: MonthSelectorProps): React.JSX.Element =>
|
|
|
425
396
|
const maxBoundTime = maxDateBound?.getTime() ?? null
|
|
426
397
|
|
|
427
398
|
if (minDateBound !== null && maxDateBound !== null && minDateBound > maxDateBound) {
|
|
428
|
-
throw new RangeError(`MonthSelector: the resolved lower bound ${formatMonth(minDateBound)}
|
|
399
|
+
throw new RangeError(`MonthSelector: the resolved upper bound must not precede the resolved lower bound (lower: ${formatMonth(minDateBound)}, upper: ${formatMonth(maxDateBound)})`)
|
|
429
400
|
}
|
|
430
401
|
if (variant === "dropdown" && allowedMonths === undefined && (minDateBound === null || maxDateBound === null)) {
|
|
431
|
-
throw new RangeError('MonthSelector: variant="dropdown" requires a lower bound (
|
|
402
|
+
throw new RangeError('MonthSelector: variant="dropdown" requires a lower bound (minYear or minDate) and an upper bound (maxYear or maxDate), or allowedMonths; the option list is never invented')
|
|
432
403
|
}
|
|
433
404
|
|
|
434
405
|
const allowedKey = allowedMonths === undefined ? null : allowedMonths.map((d) => d.getTime()).join(",")
|
|
@@ -452,7 +423,7 @@ const MonthSelectorComponent = (props: MonthSelectorProps): React.JSX.Element =>
|
|
|
452
423
|
const emit = (nextDate: Date) => {
|
|
453
424
|
if (disabled) return
|
|
454
425
|
if (!isSelectable(nextDate)) {
|
|
455
|
-
throw new RangeError(`MonthSelector: refusing to emit ${formatMonth(nextDate)}, which lies outside the selectable months
|
|
426
|
+
throw new RangeError(`MonthSelector: refusing to emit ${formatMonth(nextDate)}, which lies outside the selectable months`)
|
|
456
427
|
}
|
|
457
428
|
|
|
458
429
|
if (!isControlled) {
|