@aiquants/period-slider 0.2.0 → 0.4.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 +465 -85
- package/dist/DateSelector.d.ts +43 -14
- package/dist/DateSelector.d.ts.map +1 -1
- package/dist/MonthSelector.d.ts +26 -9
- package/dist/MonthSelector.d.ts.map +1 -1
- package/dist/PeriodSlider.d.ts +257 -125
- package/dist/PeriodSlider.d.ts.map +1 -1
- package/dist/YearSelector.d.ts +26 -9
- package/dist/YearSelector.d.ts.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +28 -0
- package/dist/index.d.ts +10 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1642 -1382
- package/dist/styles/period-slider.css +1 -1
- package/dist/styles/period-slider.standalone.css +1 -1
- package/dist/usePeriodSlider.d.ts +83 -78
- package/dist/usePeriodSlider.d.ts.map +1 -1
- package/package.json +17 -10
- package/src/DateSelector.tsx +495 -194
- package/src/MonthSelector.tsx +258 -137
- package/src/PeriodSlider.tsx +1109 -572
- package/src/YearSelector.tsx +174 -55
- package/src/index.ts +13 -0
- package/src/styles/period-slider.css +41 -7
- package/src/usePeriodSlider.ts +466 -277
- package/dist/period-slider.css +0 -1
package/src/DateSelector.tsx
CHANGED
|
@@ -1,34 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Date (yyyy-MM-dd) selector component with stepper buttons.
|
|
3
|
+
*
|
|
4
|
+
* Provides a day-resolution selector rendered either as a native `input[type="date"]`,
|
|
5
|
+
* a single `<select>` of yyyy-MM-dd options, or three year / month / day selects,
|
|
6
|
+
* always flanked by previous/next stepper buttons.
|
|
7
|
+
*
|
|
8
|
+
* Human-facing option text is locale-independent ISO 8601 until a `locale` tag opts into Intl
|
|
9
|
+
* formatting or a per-part formatter overrides it, while every machine-read value stays yyyy-MM-dd.
|
|
10
|
+
*
|
|
11
|
+
* 年月日セレクタコンポーネントの提供モジュール。
|
|
12
|
+
* ネイティブ `input[type="date"]`、yyyy-MM-dd の単一セレクトボックス、
|
|
13
|
+
* 年・月・日の 3 分割セレクトのいずれかを、前日/翌日ステッパーで挟んで描画。
|
|
14
|
+
* 表示テキストは既定でロケール非依存の ISO 8601 表記 (`locale` 指定時は Intl 書式、
|
|
15
|
+
* 部位別フォーマッタ指定時はそれが最優先)、機械可読値は常に yyyy-MM-dd。
|
|
16
|
+
*/
|
|
17
|
+
|
|
1
18
|
import { addDays, format, getDaysInMonth, parseISO } from "date-fns"
|
|
2
19
|
import type React from "react"
|
|
3
20
|
import { memo, useEffect, useMemo, useState } from "react"
|
|
4
|
-
import type
|
|
21
|
+
import { formatLabel, type PeriodSliderUnit } from "./PeriodSlider"
|
|
5
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Props of the day-resolution selector.
|
|
25
|
+
* 日単位セレクタの Props。
|
|
26
|
+
*
|
|
27
|
+
* Accessible names default to English and option text to locale-independent ISO 8601, because a
|
|
28
|
+
* generic package cannot know the host application's language: localisation is the consumer's
|
|
29
|
+
* decision, expressed through `locale`, the aria-label props or the per-part option formatters.
|
|
30
|
+
* アクセシブル名の既定は英語、選択肢テキストの既定はロケール非依存の ISO 8601 表記。
|
|
31
|
+
* 汎用パッケージは利用側アプリの言語を知り得ないため、地域化は `locale`・アクセシブル名 Props・
|
|
32
|
+
* 部位別フォーマッタを通じた利用側の選択。
|
|
33
|
+
*/
|
|
6
34
|
export interface DateSelectorProps {
|
|
7
|
-
/** Controlled selected date /
|
|
35
|
+
/** Controlled selected date; required unless `initialDate` is supplied; must be a valid Date / 選択日付(外部制御用・`initialDate` 未指定時は必須・Invalid Date 不可) */
|
|
8
36
|
value?: Date
|
|
9
|
-
/** Initial selected date
|
|
37
|
+
/** Initial selected date; required unless `value` is supplied; must be a valid Date / 初期選択日付(非制御用・`value` 未指定時は必須・Invalid Date 不可) */
|
|
10
38
|
initialDate?: Date
|
|
11
39
|
/** Time resolution unit (ignored by selector, accepted for hook interoperability) / 時間解像度単位 (フック相互運用用) */
|
|
12
40
|
unit?: PeriodSliderUnit
|
|
13
|
-
/** Callback triggered when date
|
|
41
|
+
/** Callback triggered when a selectable date is chosen / 選択可能な日付が選ばれた時のコールバック */
|
|
14
42
|
onChange?(date: Date): void
|
|
15
43
|
/** Child elements / 子要素 */
|
|
16
44
|
children?: React.ReactNode
|
|
17
|
-
/** Minimum selectable year /
|
|
45
|
+
/** Minimum selectable year; intersected with minDate when both are given / 選択可能範囲の最小年 (minDate 併用時は厳しい方を採用) */
|
|
18
46
|
minYear?: number
|
|
19
|
-
/** Maximum selectable year /
|
|
47
|
+
/** Maximum selectable year; intersected with maxDate when both are given / 選択可能範囲の最大年 (maxDate 併用時は厳しい方を採用) */
|
|
20
48
|
maxYear?: number
|
|
21
|
-
/** Minimum selectable date / 選択可能範囲の最小日付 */
|
|
49
|
+
/** Minimum selectable date; intersected with minYear when both are given / 選択可能範囲の最小日付 (minYear 併用時は厳しい方を採用) */
|
|
22
50
|
minDate?: Date
|
|
23
|
-
/** Maximum selectable date / 選択可能範囲の最大日付 */
|
|
51
|
+
/** Maximum selectable date; intersected with maxYear when both are given / 選択可能範囲の最大日付 (maxYear 併用時は厳しい方を採用) */
|
|
24
52
|
maxDate?: Date
|
|
25
|
-
/** Discrete allowed dates list / 選択可能な離散日付配列 */
|
|
53
|
+
/** Discrete allowed dates list; an empty array means nothing is selectable / 選択可能な離散日付配列 (空配列は「選択可能日なし」) */
|
|
26
54
|
allowedDates?: Date[]
|
|
27
55
|
/**
|
|
28
56
|
* UI Variant:
|
|
29
|
-
* - "
|
|
30
|
-
* - "
|
|
31
|
-
* - "split-dropdown": Three separate selects for year, month, and day
|
|
57
|
+
* - "date": Native input[type="date"] (default / 既定)
|
|
58
|
+
* - "dropdown": Single select element with yyyy-MM-dd options; requires a bounded range or allowedDates
|
|
59
|
+
* - "split-dropdown": Three separate selects for year, month, and day; requires a bounded range and rejects allowedDates
|
|
32
60
|
*/
|
|
33
61
|
variant?: "date" | "dropdown" | "split-dropdown"
|
|
34
62
|
/** Additional CSS class name / 追加の CSS クラス名 */
|
|
@@ -37,154 +65,408 @@ export interface DateSelectorProps {
|
|
|
37
65
|
style?: React.CSSProperties
|
|
38
66
|
/** Disabled flag / 無効化フラグ */
|
|
39
67
|
disabled?: boolean
|
|
40
|
-
/** Accessible label for previous date button / 前日ボタンのアクセシブル名 */
|
|
68
|
+
/** Accessible label for previous date button / 前日ボタンのアクセシブル名 (既定: "Previous day") */
|
|
41
69
|
prevDateAriaLabel?: string
|
|
42
|
-
/** Accessible label for next date button / 翌日ボタンのアクセシブル名 */
|
|
70
|
+
/** Accessible label for next date button / 翌日ボタンのアクセシブル名 (既定: "Next day") */
|
|
43
71
|
nextDateAriaLabel?: string
|
|
44
|
-
/** Accessible label for date select
|
|
72
|
+
/** Accessible label for the date select / input / 年月日セレクト・入力のアクセシブル名 (既定: "Date") */
|
|
45
73
|
selectAriaLabel?: string
|
|
74
|
+
/** Accessible label for the year select of variant="split-dropdown" / 年セレクトのアクセシブル名 (既定: "Year") */
|
|
75
|
+
yearAriaLabel?: string
|
|
76
|
+
/** Accessible label for the month select of variant="split-dropdown" / 月セレクトのアクセシブル名 (既定: "Month") */
|
|
77
|
+
monthAriaLabel?: string
|
|
78
|
+
/** Accessible label for the day select of variant="split-dropdown" / 日セレクトのアクセシブル名 (既定: "Day") */
|
|
79
|
+
dayAriaLabel?: string
|
|
80
|
+
/** BCP 47 language tag switching option text to Intl formatting; omitted keeps locale-independent ISO 8601, and the input value stays yyyy-MM-dd either way / 選択肢テキストを Intl 書式にする BCP 47 言語タグ (未指定時はロケール非依存の ISO 8601 表記・input の value は常に yyyy-MM-dd) */
|
|
81
|
+
locale?: string
|
|
82
|
+
/** Formatter for the year option text of variant="split-dropdown", taking priority over `locale` / variant="split-dropdown" の年選択肢テキスト用フォーマッタ (`locale` より優先) */
|
|
83
|
+
formatYearOption?: (year: number) => string
|
|
84
|
+
/** Formatter for the month option text of variant="split-dropdown", receiving a 1-12 month, taking priority over `locale` / variant="split-dropdown" の月選択肢テキスト用フォーマッタ (1-12 の月を受領・`locale` より優先) */
|
|
85
|
+
formatMonthOption?: (month: number) => string
|
|
86
|
+
/** Formatter for the day option text of variant="split-dropdown", receiving a 1-31 day, taking priority over `locale` / variant="split-dropdown" の日選択肢テキスト用フォーマッタ (1-31 の日を受領・`locale` より優先) */
|
|
87
|
+
formatDayOption?: (day: number) => string
|
|
46
88
|
}
|
|
47
89
|
|
|
48
90
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
91
|
+
* Copies a date and normalizes it to local midnight.
|
|
92
|
+
* 日付を複製し、ローカル 0 時に正規化する関数。
|
|
93
|
+
*
|
|
94
|
+
* @param date - Source date / 元日付
|
|
95
|
+
* @returns New Date pointing at 00:00:00.000 of the same calendar day / 同一暦日の 0 時を指す新しい Date
|
|
51
96
|
*/
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
97
|
+
const toStartOfDay = (date: Date): Date => {
|
|
98
|
+
const copy = new Date(date)
|
|
99
|
+
copy.setHours(0, 0, 0, 0)
|
|
100
|
+
return copy
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Asserts that a Date-typed prop carries a valid Date, failing fast otherwise.
|
|
105
|
+
* Date 型プロパティが有効な Date であることの表明。無効時は即時失敗。
|
|
106
|
+
*
|
|
107
|
+
* @param propName - Prop name reported in the error message / エラーメッセージに載せるプロパティ名
|
|
108
|
+
* @param date - Date to validate / 検証対象の日付
|
|
109
|
+
* @returns Nothing / 戻り値なし
|
|
110
|
+
* @throws RangeError when the date is an Invalid Date / Invalid Date の場合
|
|
111
|
+
*/
|
|
112
|
+
const assertValidDateProp = (propName: string, date: Date): void => {
|
|
113
|
+
if (Number.isNaN(date.getTime())) {
|
|
114
|
+
throw new RangeError(`DateSelector: "${propName}" must be a valid Date (received ${String(date)})`)
|
|
67
115
|
}
|
|
116
|
+
}
|
|
68
117
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
118
|
+
/**
|
|
119
|
+
* Asserts that a locale prop carries a language tag Intl.DateTimeFormat accepts, failing fast otherwise.
|
|
120
|
+
* locale プロパティが Intl.DateTimeFormat の受理する言語タグであることの表明。無効時は即時失敗。
|
|
121
|
+
*
|
|
122
|
+
* @param locale - Prop value to validate / 検証対象のプロパティ値
|
|
123
|
+
* @returns Nothing / 戻り値なし
|
|
124
|
+
* @throws RangeError when the tag is empty or Intl rejects it / 空文字列や Intl が拒否するタグの場合
|
|
125
|
+
*/
|
|
126
|
+
const assertLocaleProp = (locale: string | undefined): void => {
|
|
127
|
+
if (locale === undefined) return
|
|
128
|
+
try {
|
|
129
|
+
// 空文字列や構造的に不正なタグは Intl の送出する例外でしか判定できない。
|
|
130
|
+
// 自前の正規表現で代用すると ICU が受理するタグを取りこぼす
|
|
131
|
+
Intl.DateTimeFormat.supportedLocalesOf([locale])
|
|
132
|
+
} catch {
|
|
133
|
+
throw new RangeError(`DateSelector: "locale" must be a valid BCP 47 language tag (received ${JSON.stringify(locale)})`)
|
|
84
134
|
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Builds a local-midnight Date from calendar parts.
|
|
139
|
+
* 暦の構成要素からローカル 0 時の Date を生成する関数。
|
|
140
|
+
*
|
|
141
|
+
* @param year - Calendar year / 暦年
|
|
142
|
+
* @param month - Month in 1-12 / 月 (1-12)
|
|
143
|
+
* @param day - Day of month in 1-31 / 日 (1-31)
|
|
144
|
+
* @returns Date at 00:00:00.000 of that calendar day / その暦日の 0 時を指す Date
|
|
145
|
+
*/
|
|
146
|
+
const localDate = (year: number, month: number, day: number): Date => {
|
|
147
|
+
const d = new Date(year, month - 1, day)
|
|
148
|
+
// 0-99 年はコンストラクタが 1900 年代へ写像するため、明示的に年を上書きする
|
|
149
|
+
d.setFullYear(year)
|
|
150
|
+
d.setHours(0, 0, 0, 0)
|
|
151
|
+
return d
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Builds the local-midnight Date for January 1st of the given year.
|
|
156
|
+
* 指定年の 1 月 1 日 0 時を表す Date の生成。
|
|
157
|
+
*
|
|
158
|
+
* @param year - Calendar year / 暦年
|
|
159
|
+
* @returns Date at the first instant of that year / その年の最初の時刻を指す Date
|
|
160
|
+
*/
|
|
161
|
+
const startOfYear = (year: number): Date => localDate(year, 1, 1)
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Builds the local-midnight Date for December 31st of the given year.
|
|
165
|
+
* 指定年の 12 月 31 日 0 時を表す Date の生成。
|
|
166
|
+
*
|
|
167
|
+
* @param year - Calendar year / 暦年
|
|
168
|
+
* @returns Date at the last calendar day of that year / その年の最終暦日を指す Date
|
|
169
|
+
*/
|
|
170
|
+
const endOfYear = (year: number): Date => localDate(year, 12, 31)
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Calendar part a `variant="split-dropdown"` select is responsible for.
|
|
174
|
+
* `variant="split-dropdown"` の各セレクトが担当する暦の構成要素。
|
|
175
|
+
*/
|
|
176
|
+
type DatePart = "year" | "month" | "day"
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Intl option set each split-dropdown part is formatted with once a locale is supplied.
|
|
180
|
+
* ロケール指定時に分割セレクトの各部位を書式化する Intl オプション集合。
|
|
181
|
+
*/
|
|
182
|
+
const INTL_OPTIONS_BY_PART: Record<DatePart, Intl.DateTimeFormatOptions> = {
|
|
183
|
+
year: { year: "numeric" },
|
|
184
|
+
month: { month: "2-digit" },
|
|
185
|
+
day: { day: "2-digit" },
|
|
186
|
+
}
|
|
85
187
|
|
|
188
|
+
/** Zero-padded digit widths of the locale-independent ISO 8601 rendering of each part / 各部位の ISO 8601 表記における 0 埋め桁数 */
|
|
189
|
+
const ISO_DIGITS_BY_PART: Record<DatePart, number> = {
|
|
190
|
+
year: 4,
|
|
191
|
+
month: 2,
|
|
192
|
+
day: 2,
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Numeric readers of each calendar part / 各暦要素の数値読み出し関数 */
|
|
196
|
+
const PART_READERS: Record<DatePart, (date: Date) => number> = {
|
|
197
|
+
year: (date) => date.getFullYear(),
|
|
198
|
+
month: (date) => date.getMonth() + 1,
|
|
199
|
+
day: (date) => date.getDate(),
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Formatters already built, keyed by part and locale / 構築済みフォーマッタのキャッシュ (部位とロケールが鍵) */
|
|
203
|
+
const partFormatCache = new Map<string, Intl.DateTimeFormat>()
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Returns the Intl formatter for a calendar part and locale, building each combination once.
|
|
207
|
+
* 部位とロケールの組ごとに 1 度だけ構築した Intl フォーマッタの取得。
|
|
208
|
+
*
|
|
209
|
+
* @param part - Calendar part selecting the option set / オプション集合を選ぶ暦の構成要素
|
|
210
|
+
* @param locale - BCP 47 language tag / BCP 47 言語タグ
|
|
211
|
+
* @returns Formatter bound to the part and locale / 部位とロケールに束縛されたフォーマッタ
|
|
212
|
+
*/
|
|
213
|
+
const getPartFormat = (part: DatePart, locale: string): Intl.DateTimeFormat => {
|
|
214
|
+
// 言語タグに現れ得ない NUL を区切りに使い、部位とタグの境界が曖昧になる鍵衝突を避ける
|
|
215
|
+
const cacheKey = `${part}\u0000${locale}`
|
|
216
|
+
const cached = partFormatCache.get(cacheKey)
|
|
217
|
+
if (cached !== undefined) return cached
|
|
218
|
+
const formatter = new Intl.DateTimeFormat(locale, INTL_OPTIONS_BY_PART[part])
|
|
219
|
+
partFormatCache.set(cacheKey, formatter)
|
|
220
|
+
return formatter
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Renders one calendar part of a date as split-dropdown option text.
|
|
225
|
+
* 日付の 1 部位を分割セレクトの選択肢テキストへ整形する関数。
|
|
226
|
+
*
|
|
227
|
+
* An omitted locale yields zero-padded digits, which is the specification-defined output of this
|
|
228
|
+
* package rather than a convenience fallback: a built-in natural-language default would bake one
|
|
229
|
+
* language into a generic package, and Intl output drifts between ICU versions.
|
|
230
|
+
* locale 未指定時の 0 埋め数字は代替値ではなく本パッケージの仕様上の既定値
|
|
231
|
+
* (自然言語既定は汎用パッケージに 1 言語を焼き付け、Intl 出力は ICU 版で揺れるため)。
|
|
232
|
+
*
|
|
233
|
+
* @param part - Calendar part to render / 整形対象の暦要素
|
|
234
|
+
* @param anchor - Date carrying the part value, and the calendar context Intl needs / 対象値を保持し Intl が必要とする暦文脈も与える日付
|
|
235
|
+
* @param locale - BCP 47 language tag enabling Intl formatting / Intl 書式を有効化する BCP 47 言語タグ
|
|
236
|
+
* @returns Zero-padded digits, or the Intl rendering when a locale is supplied / 0 埋め数字 (locale 指定時は Intl 書式)
|
|
237
|
+
*/
|
|
238
|
+
const formatDatePart = (part: DatePart, anchor: Date, locale: string | undefined): string => {
|
|
239
|
+
if (locale === undefined) return String(PART_READERS[part](anchor)).padStart(ISO_DIGITS_BY_PART[part], "0")
|
|
240
|
+
return getPartFormat(part, locale).format(anchor)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Normalizes, deduplicates, sorts ascending and range-restricts the discrete allowed dates.
|
|
245
|
+
* 離散日付配列を正規化・重複排除・昇順ソートし、境界外要素を除外する関数。
|
|
246
|
+
*
|
|
247
|
+
* @param allowedDates - Caller supplied discrete dates / 呼び出し側が指定した離散日付
|
|
248
|
+
* @param minDateBound - Resolved lower bound, or null when unbounded / 解決済み下限 (無制限時は null)
|
|
249
|
+
* @param maxDateBound - Resolved upper bound, or null when unbounded / 解決済み上限 (無制限時は null)
|
|
250
|
+
* @returns Ascending, deduplicated dates inside the bounds (possibly empty) / 境界内の昇順・重複排除済み日付 (空の場合あり)
|
|
251
|
+
* @throws RangeError when any entry is an Invalid Date / 要素が Invalid Date の場合
|
|
252
|
+
*/
|
|
253
|
+
const resolveSelectableDates = (allowedDates: Date[], minDateBound: Date | null, maxDateBound: Date | null): Date[] => {
|
|
254
|
+
const minTime = minDateBound?.getTime() ?? null
|
|
255
|
+
const maxTime = maxDateBound?.getTime() ?? null
|
|
256
|
+
const byTime = new Map<number, Date>()
|
|
257
|
+
|
|
258
|
+
allowedDates.forEach((entry, index) => {
|
|
259
|
+
assertValidDateProp(`allowedDates[${index}]`, entry)
|
|
260
|
+
const normalized = toStartOfDay(entry)
|
|
261
|
+
const time = normalized.getTime()
|
|
262
|
+
if (minTime !== null && time < minTime) return
|
|
263
|
+
if (maxTime !== null && time > maxTime) return
|
|
264
|
+
if (!byTime.has(time)) {
|
|
265
|
+
byTime.set(time, normalized)
|
|
266
|
+
}
|
|
267
|
+
})
|
|
268
|
+
|
|
269
|
+
return Array.from(byTime.keys())
|
|
270
|
+
.sort((a, b) => a - b)
|
|
271
|
+
.map((time) => byTime.get(time) as Date)
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Expands a bounded range into every calendar day it contains, ascending.
|
|
276
|
+
* 境界で挟まれた範囲を、含まれる全暦日 (昇順) へ展開する関数。
|
|
277
|
+
*
|
|
278
|
+
* @param minDateBound - Resolved lower bound / 解決済み下限
|
|
279
|
+
* @param maxDateBound - Resolved upper bound / 解決済み上限
|
|
280
|
+
* @returns Ascending list of every day in [minDateBound, maxDateBound] / 範囲内全日の昇順配列
|
|
281
|
+
*/
|
|
282
|
+
const expandBoundedDays = (minDateBound: Date, maxDateBound: Date): Date[] => {
|
|
283
|
+
const list: Date[] = []
|
|
284
|
+
let current = toStartOfDay(minDateBound)
|
|
285
|
+
const endTime = toStartOfDay(maxDateBound).getTime()
|
|
286
|
+
while (current.getTime() <= endTime) {
|
|
287
|
+
list.push(current)
|
|
288
|
+
current = addDays(current, 1)
|
|
289
|
+
}
|
|
86
290
|
return list
|
|
87
291
|
}
|
|
88
292
|
|
|
89
293
|
/**
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
294
|
+
* Renders an ascending date list as the descending option list of the single select-box mode.
|
|
295
|
+
* 昇順日付配列を、単一セレクトボックス用の降順選択肢へ変換する関数。
|
|
296
|
+
*
|
|
297
|
+
* @param datesAscending - Ascending selectable dates / 昇順の選択可能日付
|
|
298
|
+
* @param locale - BCP 47 language tag enabling Intl label formatting / Intl ラベル書式を有効化する BCP 47 言語タグ
|
|
299
|
+
* @returns Descending options whose value is always yyyy-MM-dd / value が常に yyyy-MM-dd である降順選択肢
|
|
300
|
+
*/
|
|
301
|
+
const toDescendingOptions = (datesAscending: Date[], locale: string | undefined): { label: string; value: string }[] => {
|
|
302
|
+
const options: { label: string; value: string }[] = []
|
|
303
|
+
for (let i = datesAscending.length - 1; i >= 0; i--) {
|
|
304
|
+
// 表示は locale 次第で変わるが、value は change ハンドラが parseISO で解釈するため常に yyyy-MM-dd
|
|
305
|
+
options.push({ label: formatLabel("day", datesAscending[i], undefined, locale), value: format(datesAscending[i], "yyyy-MM-dd") })
|
|
306
|
+
}
|
|
307
|
+
return options
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Date (yyyy-MM-dd) selector component supporting bounded ranges, discrete allowed dates, and ±1 stepper buttons.
|
|
312
|
+
* 日付(年月日)セレクタ共通コンポーネント。
|
|
313
|
+
* ネイティブ日付入力・単一セレクトボックス・3 分割セレクトのいずれかと、両脇のステッパーボタン (◀▶) を描画。
|
|
314
|
+
* 選択可能集合は `allowedDates` (離散) または [minDate/minYear, maxDate/maxYear] (連続) が決定し、集合外の日付は emit されない。
|
|
315
|
+
* `value` / `initialDate` の欠落・不正値、バリアントが要求する境界の欠落・矛盾、`allowedDates` との組合せ違反はいずれも RangeError で即時失敗。
|
|
93
316
|
*/
|
|
94
317
|
const DateSelectorComponent = (props: DateSelectorProps): React.JSX.Element => {
|
|
95
|
-
const {
|
|
318
|
+
const {
|
|
319
|
+
value,
|
|
320
|
+
initialDate,
|
|
321
|
+
onChange,
|
|
322
|
+
children,
|
|
323
|
+
minYear,
|
|
324
|
+
maxYear,
|
|
325
|
+
minDate,
|
|
326
|
+
maxDate,
|
|
327
|
+
allowedDates,
|
|
328
|
+
variant = "date",
|
|
329
|
+
className = "",
|
|
330
|
+
style,
|
|
331
|
+
disabled = false,
|
|
332
|
+
prevDateAriaLabel = "Previous day",
|
|
333
|
+
nextDateAriaLabel = "Next day",
|
|
334
|
+
selectAriaLabel = "Date",
|
|
335
|
+
yearAriaLabel = "Year",
|
|
336
|
+
monthAriaLabel = "Month",
|
|
337
|
+
dayAriaLabel = "Day",
|
|
338
|
+
locale,
|
|
339
|
+
formatYearOption,
|
|
340
|
+
formatMonthOption,
|
|
341
|
+
formatDayOption,
|
|
342
|
+
} = props
|
|
343
|
+
|
|
344
|
+
assertLocaleProp(locale)
|
|
345
|
+
if (value !== undefined) assertValidDateProp("value", value)
|
|
346
|
+
if (initialDate !== undefined) assertValidDateProp("initialDate", initialDate)
|
|
347
|
+
if (minDate !== undefined) assertValidDateProp("minDate", minDate)
|
|
348
|
+
if (maxDate !== undefined) assertValidDateProp("maxDate", maxDate)
|
|
349
|
+
if (minYear !== undefined && !Number.isInteger(minYear)) {
|
|
350
|
+
throw new RangeError(`DateSelector: "minYear" must be an integer year (received ${String(minYear)})`)
|
|
351
|
+
}
|
|
352
|
+
if (maxYear !== undefined && !Number.isInteger(maxYear)) {
|
|
353
|
+
throw new RangeError(`DateSelector: "maxYear" must be an integer year (received ${String(maxYear)})`)
|
|
354
|
+
}
|
|
355
|
+
if (variant === "split-dropdown" && allowedDates !== undefined) {
|
|
356
|
+
throw new RangeError('DateSelector: variant="split-dropdown" cannot honour "allowedDates"; use variant="dropdown" or variant="date" for a discrete date set')
|
|
357
|
+
}
|
|
358
|
+
|
|
96
359
|
const effectiveDate = value ?? initialDate
|
|
360
|
+
if (effectiveDate === undefined) {
|
|
361
|
+
throw new RangeError("DateSelector: either `value` (controlled) or `initialDate` (uncontrolled) is required; the selected date is never inferred from the wall clock.")
|
|
362
|
+
}
|
|
97
363
|
|
|
98
|
-
const [currentDate, setCurrentDate] = useState<Date>(() =>
|
|
99
|
-
const d = new Date(effectiveDate ?? new Date())
|
|
100
|
-
d.setHours(0, 0, 0, 0)
|
|
101
|
-
return d
|
|
102
|
-
})
|
|
364
|
+
const [currentDate, setCurrentDate] = useState<Date>(() => toStartOfDay(effectiveDate))
|
|
103
365
|
|
|
104
366
|
const isControlled = value !== undefined
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
if (value && !Number.isNaN(value.getTime())) {
|
|
108
|
-
const d = new Date(value)
|
|
109
|
-
d.setHours(0, 0, 0, 0)
|
|
110
|
-
return d
|
|
111
|
-
}
|
|
112
|
-
const d = new Date()
|
|
113
|
-
d.setHours(0, 0, 0, 0)
|
|
114
|
-
return d
|
|
115
|
-
}
|
|
116
|
-
return currentDate
|
|
117
|
-
}, [isControlled, value?.getTime(), currentDate])
|
|
367
|
+
const valueTime = value?.getTime()
|
|
368
|
+
const activeDate = useMemo(() => (value !== undefined ? toStartOfDay(value) : currentDate), [valueTime, currentDate])
|
|
118
369
|
|
|
119
370
|
const minTime = minDate?.getTime()
|
|
120
371
|
const maxTime = maxDate?.getTime()
|
|
121
372
|
|
|
122
373
|
const minDateBound = useMemo(() => {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const d = new Date(minYear, 0, 1)
|
|
130
|
-
d.setFullYear(minYear)
|
|
131
|
-
d.setHours(0, 0, 0, 0)
|
|
132
|
-
return d
|
|
133
|
-
}
|
|
134
|
-
return null
|
|
374
|
+
const fromDate = minDate ? toStartOfDay(minDate) : null
|
|
375
|
+
const fromYear = minYear !== undefined ? startOfYear(minYear) : null
|
|
376
|
+
if (fromDate === null) return fromYear
|
|
377
|
+
if (fromYear === null) return fromDate
|
|
378
|
+
// 両方指定時に片方を捨てると宣言済みの下限が黙って失われるため、厳しい方 (遅い方) を採用する
|
|
379
|
+
return fromDate.getTime() >= fromYear.getTime() ? fromDate : fromYear
|
|
135
380
|
}, [minTime, minYear])
|
|
136
381
|
|
|
137
382
|
const maxDateBound = useMemo(() => {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
if (maxYear !== undefined) {
|
|
144
|
-
const d = new Date(maxYear, 11, 31)
|
|
145
|
-
d.setFullYear(maxYear)
|
|
146
|
-
d.setHours(0, 0, 0, 0)
|
|
147
|
-
return d
|
|
148
|
-
}
|
|
149
|
-
return null
|
|
383
|
+
const fromDate = maxDate ? toStartOfDay(maxDate) : null
|
|
384
|
+
const fromYear = maxYear !== undefined ? endOfYear(maxYear) : null
|
|
385
|
+
if (fromDate === null) return fromYear
|
|
386
|
+
if (fromYear === null) return fromDate
|
|
387
|
+
return fromDate.getTime() <= fromYear.getTime() ? fromDate : fromYear
|
|
150
388
|
}, [maxTime, maxYear])
|
|
151
389
|
|
|
152
|
-
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
390
|
+
const minBoundTime = minDateBound?.getTime() ?? null
|
|
391
|
+
const maxBoundTime = maxDateBound?.getTime() ?? null
|
|
392
|
+
|
|
393
|
+
if (minDateBound !== null && maxDateBound !== null && minDateBound.getTime() > maxDateBound.getTime()) {
|
|
394
|
+
throw new RangeError(`DateSelector: the resolved range is empty (min ${format(minDateBound, "yyyy-MM-dd")} is after max ${format(maxDateBound, "yyyy-MM-dd")})`)
|
|
395
|
+
}
|
|
396
|
+
if (variant !== "date" && allowedDates === undefined && (minDateBound === null || maxDateBound === null)) {
|
|
397
|
+
throw new RangeError(`DateSelector: variant="${variant}" needs a bounded range; supply minDate/minYear and maxDate/maxYear${variant === "dropdown" ? ' or supply "allowedDates"' : ""}`)
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const allowedKey = allowedDates === undefined ? null : allowedDates.map((d) => d.getTime()).join(",")
|
|
401
|
+
|
|
402
|
+
const selectableDates = useMemo<Date[] | null>(() => {
|
|
403
|
+
if (allowedDates === undefined) return null
|
|
404
|
+
return resolveSelectableDates(allowedDates, minDateBound, maxDateBound)
|
|
405
|
+
}, [allowedKey, minBoundTime, maxBoundTime])
|
|
406
|
+
|
|
407
|
+
const isSelectable = (candidate: Date): boolean => {
|
|
408
|
+
const time = candidate.getTime()
|
|
409
|
+
if (selectableDates !== null) {
|
|
410
|
+
return selectableDates.some((d) => d.getTime() === time)
|
|
159
411
|
}
|
|
412
|
+
if (minBoundTime !== null && time < minBoundTime) return false
|
|
413
|
+
if (maxBoundTime !== null && time > maxBoundTime) return false
|
|
414
|
+
return true
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// 外部からの initialDate 変更に追従 (制御時は value がそのまま真実の源)
|
|
418
|
+
const effectiveTime = effectiveDate.getTime()
|
|
419
|
+
useEffect(() => {
|
|
420
|
+
if (isControlled) return
|
|
421
|
+
setCurrentDate(toStartOfDay(effectiveDate))
|
|
160
422
|
}, [isControlled, effectiveTime])
|
|
161
423
|
|
|
162
424
|
const emit = (nextDate: Date) => {
|
|
163
425
|
if (disabled) return
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
if (minDateBound && clamped < minDateBound) {
|
|
168
|
-
clamped = new Date(minDateBound)
|
|
169
|
-
}
|
|
170
|
-
if (maxDateBound && clamped > maxDateBound) {
|
|
171
|
-
clamped = new Date(maxDateBound)
|
|
172
|
-
}
|
|
426
|
+
const normalized = toStartOfDay(nextDate)
|
|
427
|
+
// 選択可能集合の外は利用者の選択として成立しないため、近傍への差し替えをせず破棄する
|
|
428
|
+
if (!isSelectable(normalized)) return
|
|
173
429
|
|
|
174
430
|
if (!isControlled) {
|
|
175
|
-
setCurrentDate(
|
|
431
|
+
setCurrentDate(normalized)
|
|
176
432
|
}
|
|
177
|
-
onChange?.(
|
|
433
|
+
onChange?.(normalized)
|
|
178
434
|
}
|
|
179
435
|
|
|
180
|
-
const
|
|
181
|
-
const
|
|
436
|
+
const activeTime = activeDate.getTime()
|
|
437
|
+
const isActiveSelectable = isSelectable(activeDate)
|
|
182
438
|
|
|
183
|
-
const
|
|
184
|
-
|
|
439
|
+
const prevDate = useMemo<Date | null>(() => {
|
|
440
|
+
if (selectableDates !== null) {
|
|
441
|
+
for (let i = selectableDates.length - 1; i >= 0; i--) {
|
|
442
|
+
if (selectableDates[i].getTime() < activeTime) return selectableDates[i]
|
|
443
|
+
}
|
|
444
|
+
return null
|
|
445
|
+
}
|
|
446
|
+
const candidate = addDays(new Date(activeTime), -1)
|
|
447
|
+
return minBoundTime !== null && candidate.getTime() < minBoundTime ? null : candidate
|
|
448
|
+
}, [selectableDates, activeTime, minBoundTime])
|
|
449
|
+
|
|
450
|
+
const nextDate = useMemo<Date | null>(() => {
|
|
451
|
+
if (selectableDates !== null) {
|
|
452
|
+
for (const candidate of selectableDates) {
|
|
453
|
+
if (candidate.getTime() > activeTime) return candidate
|
|
454
|
+
}
|
|
455
|
+
return null
|
|
456
|
+
}
|
|
457
|
+
const candidate = addDays(new Date(activeTime), 1)
|
|
458
|
+
return maxBoundTime !== null && candidate.getTime() > maxBoundTime ? null : candidate
|
|
459
|
+
}, [selectableDates, activeTime, maxBoundTime])
|
|
185
460
|
|
|
186
|
-
const
|
|
187
|
-
const
|
|
461
|
+
const isPrevDisabled = disabled || prevDate === null
|
|
462
|
+
const isNextDisabled = disabled || nextDate === null
|
|
463
|
+
|
|
464
|
+
const handlePrevious = () => {
|
|
465
|
+
if (prevDate !== null) emit(prevDate)
|
|
466
|
+
}
|
|
467
|
+
const handleNext = () => {
|
|
468
|
+
if (nextDate !== null) emit(nextDate)
|
|
469
|
+
}
|
|
188
470
|
|
|
189
471
|
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
190
472
|
if (e.target.value) {
|
|
@@ -208,93 +490,80 @@ const DateSelectorComponent = (props: DateSelectorProps): React.JSX.Element => {
|
|
|
208
490
|
const currentMonth = activeDate.getMonth() + 1
|
|
209
491
|
const currentDay = activeDate.getDate()
|
|
210
492
|
|
|
211
|
-
const minBoundTime = minDateBound?.getTime() ?? null
|
|
212
|
-
const maxBoundTime = maxDateBound?.getTime() ?? null
|
|
213
|
-
const currTime = activeDate.getTime()
|
|
214
|
-
|
|
215
|
-
const optionsRangeKey = useMemo(() => {
|
|
216
|
-
if (allowedDates && allowedDates.length > 0) {
|
|
217
|
-
return allowedDates.map((d) => d.getTime()).join(",")
|
|
218
|
-
}
|
|
219
|
-
const startMs = minBoundTime ?? new Date(currentYear - 1, 0, 1).getTime()
|
|
220
|
-
const endMs = maxBoundTime ?? new Date(currentYear + 1, 11, 31).getTime()
|
|
221
|
-
const actualStartMs = Math.min(startMs, currTime)
|
|
222
|
-
const actualEndMs = Math.max(endMs, currTime)
|
|
223
|
-
return `${actualStartMs}_${actualEndMs}`
|
|
224
|
-
}, [allowedDates, minBoundTime, maxBoundTime, currTime, currentYear])
|
|
225
|
-
|
|
226
|
-
// --- Single Select-Box Options ---
|
|
227
493
|
const dateDropdownOptions = useMemo(() => {
|
|
228
494
|
if (variant !== "dropdown") return []
|
|
229
|
-
|
|
230
|
-
|
|
495
|
+
if (selectableDates !== null) return toDescendingOptions(selectableDates, locale)
|
|
496
|
+
if (minDateBound === null || maxDateBound === null) return []
|
|
497
|
+
return toDescendingOptions(expandBoundedDays(minDateBound, maxDateBound), locale)
|
|
498
|
+
}, [variant, selectableDates, minBoundTime, maxBoundTime, locale])
|
|
231
499
|
|
|
232
|
-
// --- Split Dropdown Options (Optional Backward Compatibility) ---
|
|
233
500
|
const yearOptions = useMemo(() => {
|
|
234
|
-
if (variant !== "split-dropdown") return []
|
|
235
|
-
const minY = minDateBound ? minDateBound.getFullYear() : currentYear - 10
|
|
236
|
-
const maxY = maxDateBound ? maxDateBound.getFullYear() : currentYear + 1
|
|
237
|
-
const start = Math.min(minY, currentYear)
|
|
238
|
-
const end = Math.max(maxY, currentYear)
|
|
501
|
+
if (variant !== "split-dropdown" || minDateBound === null || maxDateBound === null) return []
|
|
239
502
|
const list: number[] = []
|
|
240
|
-
for (let y =
|
|
503
|
+
for (let y = maxDateBound.getFullYear(); y >= minDateBound.getFullYear(); y--) {
|
|
241
504
|
list.push(y)
|
|
242
505
|
}
|
|
243
506
|
return list
|
|
244
|
-
}, [variant,
|
|
507
|
+
}, [variant, minBoundTime, maxBoundTime])
|
|
245
508
|
|
|
246
509
|
const monthOptions = useMemo(() => {
|
|
247
|
-
if (variant !== "split-dropdown") return []
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
minM = minDateBound.getMonth() + 1
|
|
253
|
-
}
|
|
254
|
-
if (maxDateBound && currentYear === maxDateBound.getFullYear()) {
|
|
255
|
-
maxM = maxDateBound.getMonth() + 1
|
|
256
|
-
}
|
|
510
|
+
if (variant !== "split-dropdown" || minDateBound === null || maxDateBound === null) return []
|
|
511
|
+
const lower = minDateBound
|
|
512
|
+
const upper = maxDateBound
|
|
513
|
+
const minM = currentYear === lower.getFullYear() ? lower.getMonth() + 1 : 1
|
|
514
|
+
const maxM = currentYear === upper.getFullYear() ? upper.getMonth() + 1 : 12
|
|
257
515
|
|
|
258
516
|
const list: number[] = []
|
|
259
517
|
for (let m = minM; m <= maxM; m++) {
|
|
260
518
|
list.push(m)
|
|
261
519
|
}
|
|
262
520
|
return list
|
|
263
|
-
}, [variant,
|
|
521
|
+
}, [variant, minBoundTime, maxBoundTime, currentYear])
|
|
264
522
|
|
|
265
523
|
const dayOptions = useMemo(() => {
|
|
266
|
-
if (variant !== "split-dropdown") return []
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
if (minDateBound && currentYear === minDateBound.getFullYear() && currentMonth === minDateBound.getMonth() + 1) {
|
|
272
|
-
minD = minDateBound.getDate()
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
if (maxDateBound && currentYear === maxDateBound.getFullYear() && currentMonth === maxDateBound.getMonth() + 1) {
|
|
276
|
-
maxD = maxDateBound.getDate()
|
|
277
|
-
}
|
|
524
|
+
if (variant !== "split-dropdown" || minDateBound === null || maxDateBound === null) return []
|
|
525
|
+
const lower = minDateBound
|
|
526
|
+
const upper = maxDateBound
|
|
527
|
+
const minD = currentYear === lower.getFullYear() && currentMonth === lower.getMonth() + 1 ? lower.getDate() : 1
|
|
528
|
+
const maxD = currentYear === upper.getFullYear() && currentMonth === upper.getMonth() + 1 ? upper.getDate() : getDaysInMonth(new Date(activeTime))
|
|
278
529
|
|
|
279
530
|
const list: number[] = []
|
|
280
531
|
for (let d = minD; d <= maxD; d++) {
|
|
281
532
|
list.push(d)
|
|
282
533
|
}
|
|
283
534
|
return list
|
|
284
|
-
}, [variant,
|
|
535
|
+
}, [variant, minBoundTime, maxBoundTime, activeTime, currentYear, currentMonth])
|
|
285
536
|
|
|
286
537
|
const handleSplitSelectChange = (newYear: number, newMonth: number, newDay: number) => {
|
|
287
|
-
|
|
288
|
-
|
|
538
|
+
// 短い月へ切り替えた際に存在しなくなる日は、月末日へ丸めると利用者が選んでいない日が親へ伝播するため破棄する
|
|
539
|
+
if (newDay > getDaysInMonth(localDate(newYear, newMonth, 1))) return
|
|
289
540
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
541
|
+
emit(localDate(newYear, newMonth, newDay))
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
const renderYearOption = (year: number): string => {
|
|
545
|
+
if (formatYearOption) return formatYearOption(year)
|
|
546
|
+
return formatDatePart("year", startOfYear(year), locale)
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
const renderMonthOption = (month: number): string => {
|
|
550
|
+
if (formatMonthOption) return formatMonthOption(month)
|
|
551
|
+
// Intl は暦法の解決に完全な日付を要求するため、表示中の年に属する 1 日を文脈として渡す
|
|
552
|
+
return formatDatePart("month", localDate(currentYear, month, 1), locale)
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
const renderDayOption = (day: number): string => {
|
|
556
|
+
if (formatDayOption) return formatDayOption(day)
|
|
557
|
+
return formatDatePart("day", localDate(currentYear, currentMonth, day), locale)
|
|
293
558
|
}
|
|
294
559
|
|
|
295
560
|
const formattedDate = format(activeDate, "yyyy-MM-dd")
|
|
561
|
+
const activeLabel = formatLabel("day", activeDate, undefined, locale)
|
|
296
562
|
const minFormatted = minDateBound ? format(minDateBound, "yyyy-MM-dd") : undefined
|
|
297
563
|
const maxFormatted = maxDateBound ? format(maxDateBound, "yyyy-MM-dd") : undefined
|
|
564
|
+
// 単一行 <select> は一致する選択肢が無いと先頭項目を表示してしまうため、集合外の現在値は選択不可の実値プレースホルダとして明示する
|
|
565
|
+
const selectValue = isActiveSelectable ? formattedDate : ""
|
|
566
|
+
const outOfRangeOptionClassName = "text-center font-mono text-gray-500 tabular-nums [text-align-last:center] [text-align:center] dark:bg-slate-700 dark:text-slate-400"
|
|
298
567
|
|
|
299
568
|
const containerWidthClass = variant === "split-dropdown" ? "w-auto" : "w-38"
|
|
300
569
|
|
|
@@ -302,7 +571,7 @@ const DateSelectorComponent = (props: DateSelectorProps): React.JSX.Element => {
|
|
|
302
571
|
"mx-1 my-auto w-28 appearance-none rounded-md border border-gray-300 bg-gray-100 px-1 py-0.5 text-center font-mono text-gray-900 text-xs tabular-nums [text-align-last:center] [text-align:center] focus:border-blue-500 focus:ring-blue-500 disabled:opacity-50 dark:border-slate-600 dark:bg-slate-700 dark:text-slate-100 dark:focus:border-blue-500 dark:focus:ring-blue-500"
|
|
303
572
|
|
|
304
573
|
return (
|
|
305
|
-
<div className={`inline-flex items-center ${containerWidthClass} ${className}`} style={style}>
|
|
574
|
+
<div className={`inline-flex items-center ${containerWidthClass} ${className}`} style={style} data-variant={variant} data-out-of-range={isActiveSelectable ? undefined : "true"}>
|
|
306
575
|
<button
|
|
307
576
|
type="button"
|
|
308
577
|
aria-label={prevDateAriaLabel}
|
|
@@ -313,7 +582,12 @@ const DateSelectorComponent = (props: DateSelectorProps): React.JSX.Element => {
|
|
|
313
582
|
</button>
|
|
314
583
|
|
|
315
584
|
{variant === "dropdown" ? (
|
|
316
|
-
<select aria-label={selectAriaLabel} disabled={disabled} className={singleSelectClassName} value={
|
|
585
|
+
<select aria-label={selectAriaLabel} disabled={disabled} className={singleSelectClassName} value={selectValue} onChange={handleDropdownChange}>
|
|
586
|
+
{isActiveSelectable ? null : (
|
|
587
|
+
<option value="" disabled className={outOfRangeOptionClassName}>
|
|
588
|
+
{activeLabel}
|
|
589
|
+
</option>
|
|
590
|
+
)}
|
|
317
591
|
{dateDropdownOptions.map((opt) => (
|
|
318
592
|
<option key={opt.value} value={opt.value} className="text-center font-mono text-gray-900 tabular-nums [text-align-last:center] [text-align:center] dark:bg-slate-700 dark:text-slate-100">
|
|
319
593
|
{opt.label}
|
|
@@ -323,38 +597,53 @@ const DateSelectorComponent = (props: DateSelectorProps): React.JSX.Element => {
|
|
|
323
597
|
) : variant === "split-dropdown" ? (
|
|
324
598
|
<div className="mx-1 my-auto inline-flex items-center gap-1">
|
|
325
599
|
<select
|
|
326
|
-
aria-label=
|
|
600
|
+
aria-label={yearAriaLabel}
|
|
327
601
|
disabled={disabled}
|
|
328
602
|
className="rounded-md border border-gray-300 bg-gray-50 px-1 py-0.5 text-center font-mono text-gray-900 text-xs tabular-nums focus:border-blue-500 focus:ring-blue-500 disabled:opacity-50 dark:border-slate-600 dark:bg-slate-700 dark:text-slate-100"
|
|
329
|
-
value={currentYear}
|
|
603
|
+
value={isActiveSelectable ? currentYear : ""}
|
|
330
604
|
onChange={(e) => handleSplitSelectChange(Number(e.target.value), currentMonth, currentDay)}>
|
|
605
|
+
{isActiveSelectable ? null : (
|
|
606
|
+
<option value="" disabled className={outOfRangeOptionClassName}>
|
|
607
|
+
{renderYearOption(currentYear)}
|
|
608
|
+
</option>
|
|
609
|
+
)}
|
|
331
610
|
{yearOptions.map((y) => (
|
|
332
611
|
<option key={y} value={y} className="dark:bg-slate-700 dark:text-slate-100">
|
|
333
|
-
{y}
|
|
612
|
+
{renderYearOption(y)}
|
|
334
613
|
</option>
|
|
335
614
|
))}
|
|
336
615
|
</select>
|
|
337
616
|
<select
|
|
338
|
-
aria-label=
|
|
617
|
+
aria-label={monthAriaLabel}
|
|
339
618
|
disabled={disabled}
|
|
340
619
|
className="rounded-md border border-gray-300 bg-gray-50 px-1 py-0.5 text-center font-mono text-gray-900 text-xs tabular-nums focus:border-blue-500 focus:ring-blue-500 disabled:opacity-50 dark:border-slate-600 dark:bg-slate-700 dark:text-slate-100"
|
|
341
|
-
value={currentMonth}
|
|
620
|
+
value={isActiveSelectable ? currentMonth : ""}
|
|
342
621
|
onChange={(e) => handleSplitSelectChange(currentYear, Number(e.target.value), currentDay)}>
|
|
622
|
+
{isActiveSelectable ? null : (
|
|
623
|
+
<option value="" disabled className={outOfRangeOptionClassName}>
|
|
624
|
+
{renderMonthOption(currentMonth)}
|
|
625
|
+
</option>
|
|
626
|
+
)}
|
|
343
627
|
{monthOptions.map((m) => (
|
|
344
628
|
<option key={m} value={m} className="dark:bg-slate-700 dark:text-slate-100">
|
|
345
|
-
{
|
|
629
|
+
{renderMonthOption(m)}
|
|
346
630
|
</option>
|
|
347
631
|
))}
|
|
348
632
|
</select>
|
|
349
633
|
<select
|
|
350
|
-
aria-label=
|
|
634
|
+
aria-label={dayAriaLabel}
|
|
351
635
|
disabled={disabled}
|
|
352
636
|
className="rounded-md border border-gray-300 bg-gray-50 px-1 py-0.5 text-center font-mono text-gray-900 text-xs tabular-nums focus:border-blue-500 focus:ring-blue-500 disabled:opacity-50 dark:border-slate-600 dark:bg-slate-700 dark:text-slate-100"
|
|
353
|
-
value={currentDay}
|
|
637
|
+
value={isActiveSelectable ? currentDay : ""}
|
|
354
638
|
onChange={(e) => handleSplitSelectChange(currentYear, currentMonth, Number(e.target.value))}>
|
|
639
|
+
{isActiveSelectable ? null : (
|
|
640
|
+
<option value="" disabled className={outOfRangeOptionClassName}>
|
|
641
|
+
{renderDayOption(currentDay)}
|
|
642
|
+
</option>
|
|
643
|
+
)}
|
|
355
644
|
{dayOptions.map((d) => (
|
|
356
645
|
<option key={d} value={d} className="dark:bg-slate-700 dark:text-slate-100">
|
|
357
|
-
{
|
|
646
|
+
{renderDayOption(d)}
|
|
358
647
|
</option>
|
|
359
648
|
))}
|
|
360
649
|
</select>
|
|
@@ -385,30 +674,42 @@ const DateSelectorComponent = (props: DateSelectorProps): React.JSX.Element => {
|
|
|
385
674
|
)
|
|
386
675
|
}
|
|
387
676
|
|
|
677
|
+
/**
|
|
678
|
+
* Day-granularity memo key for a Date prop.
|
|
679
|
+
* Date プロパティを日単位で比較するためのキー生成。
|
|
680
|
+
*
|
|
681
|
+
* @param date - Date prop value, or undefined when absent / 日付プロパティ値 (未指定時は undefined)
|
|
682
|
+
* @returns Midnight epoch milliseconds, NaN for an Invalid Date, or null when absent / 0 時のエポックミリ秒 (Invalid Date は NaN・未指定は null)
|
|
683
|
+
*/
|
|
684
|
+
const dayKey = (date: Date | undefined): number | null => (date === undefined ? null : toStartOfDay(date).getTime())
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* Day-resolution selector re-rendering only when a prop its output depends on changes.
|
|
688
|
+
* 描画結果が依存する Props の変化時のみ再描画する日単位セレクタ。
|
|
689
|
+
*/
|
|
388
690
|
export const DateSelector = memo(DateSelectorComponent, (prev, next) => {
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
const nextYmd = nextDate && !Number.isNaN(nextDate.getTime()) ? nextDate.getFullYear() * 10000 + (nextDate.getMonth() + 1) * 100 + nextDate.getDate() : null
|
|
393
|
-
if (prevYmd !== nextYmd) return false
|
|
691
|
+
if (dayKey(prev.value ?? prev.initialDate) !== dayKey(next.value ?? next.initialDate)) return false
|
|
692
|
+
if (dayKey(prev.minDate) !== dayKey(next.minDate)) return false
|
|
693
|
+
if (dayKey(prev.maxDate) !== dayKey(next.maxDate)) return false
|
|
394
694
|
if (prev.variant !== next.variant) return false
|
|
395
695
|
if (prev.disabled !== next.disabled) return false
|
|
396
696
|
if (prev.className !== next.className) return false
|
|
397
697
|
if (prev.style !== next.style) return false
|
|
398
698
|
if (prev.minYear !== next.minYear) return false
|
|
399
699
|
if (prev.maxYear !== next.maxYear) return false
|
|
400
|
-
const prevMinYmd = prev.minDate && !Number.isNaN(prev.minDate.getTime()) ? prev.minDate.getFullYear() * 10000 + (prev.minDate.getMonth() + 1) * 100 + prev.minDate.getDate() : null
|
|
401
|
-
const nextMinYmd = next.minDate && !Number.isNaN(next.minDate.getTime()) ? next.minDate.getFullYear() * 10000 + (next.minDate.getMonth() + 1) * 100 + next.minDate.getDate() : null
|
|
402
|
-
if (prevMinYmd !== nextMinYmd) return false
|
|
403
|
-
const prevMaxYmd = prev.maxDate && !Number.isNaN(prev.maxDate.getTime()) ? prev.maxDate.getFullYear() * 10000 + (prev.maxDate.getMonth() + 1) * 100 + prev.maxDate.getDate() : null
|
|
404
|
-
const nextMaxYmd = next.maxDate && !Number.isNaN(next.maxDate.getTime()) ? next.maxDate.getFullYear() * 10000 + (next.maxDate.getMonth() + 1) * 100 + next.maxDate.getDate() : null
|
|
405
|
-
if (prevMaxYmd !== nextMaxYmd) return false
|
|
406
700
|
if (prev.onChange !== next.onChange) return false
|
|
407
701
|
if (prev.allowedDates !== next.allowedDates) return false
|
|
408
702
|
if (prev.children !== next.children) return false
|
|
409
703
|
if (prev.prevDateAriaLabel !== next.prevDateAriaLabel) return false
|
|
410
704
|
if (prev.nextDateAriaLabel !== next.nextDateAriaLabel) return false
|
|
411
705
|
if (prev.selectAriaLabel !== next.selectAriaLabel) return false
|
|
706
|
+
if (prev.yearAriaLabel !== next.yearAriaLabel) return false
|
|
707
|
+
if (prev.monthAriaLabel !== next.monthAriaLabel) return false
|
|
708
|
+
if (prev.dayAriaLabel !== next.dayAriaLabel) return false
|
|
709
|
+
if (prev.locale !== next.locale) return false
|
|
710
|
+
if (prev.formatYearOption !== next.formatYearOption) return false
|
|
711
|
+
if (prev.formatMonthOption !== next.formatMonthOption) return false
|
|
712
|
+
if (prev.formatDayOption !== next.formatDayOption) return false
|
|
412
713
|
return true
|
|
413
714
|
})
|
|
414
715
|
DateSelector.displayName = "DateSelector"
|