@aiquants/period-slider 0.10.2 → 0.11.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 +41 -0
- package/README.md +34 -13
- package/dist/DateSelector.d.ts +1 -1
- package/dist/DateSelector.d.ts.map +1 -1
- package/dist/VirtualDateDropdown.d.ts +141 -0
- package/dist/VirtualDateDropdown.d.ts.map +1 -0
- package/dist/dateOptionDomain.d.ts +175 -0
- package/dist/dateOptionDomain.d.ts.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +1983 -1678
- package/dist/styles/period-slider.css +1 -1
- package/dist/styles/period-slider.standalone.css +1 -1
- package/package.json +5 -3
- package/src/DateSelector.tsx +72 -128
- package/src/VirtualDateDropdown.tsx +562 -0
- package/src/dateOptionDomain.ts +327 -0
- package/src/styles/period-slider.css +111 -0
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Virtualized combobox popup of the day-resolution dropdown.
|
|
3
|
+
*
|
|
4
|
+
* Presents a {@link DateOptionDomain} as a WAI-ARIA combobox whose `role="listbox"` popup renders
|
|
5
|
+
* only the visible rows through `@aiquants/virtualscroll`, so the number of DOM nodes is bounded by
|
|
6
|
+
* the popup height rather than by the option count. The control itself is a read-only text input,
|
|
7
|
+
* which keeps the accessible name / value surface of the native `<select>` it replaces while
|
|
8
|
+
* leaving the caret and the soft keyboard out of the interaction.
|
|
9
|
+
*
|
|
10
|
+
* 日単位ドロップダウンの仮想化コンボボックスポップアップ。
|
|
11
|
+
* {@link DateOptionDomain} を WAI-ARIA コンボボックスとして提示し、`role="listbox"` のポップアップは
|
|
12
|
+
* `@aiquants/virtualscroll` 経由で可視行のみを描画する (DOM ノード数は選択肢数ではなくポップアップの
|
|
13
|
+
* 高さで決まる)。操作要素は読み取り専用テキスト入力で、置き換え対象のネイティブ `<select>` の
|
|
14
|
+
* アクセシブル名・値の面を保ちながら、キャレットとソフトキーボードを操作から外す。
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { VirtualScroll, type VirtualScrollHandle, type VirtualScrollRange } from "@aiquants/virtualscroll"
|
|
18
|
+
import type React from "react"
|
|
19
|
+
import { useCallback, useEffect, useId, useLayoutEffect, useRef, useState } from "react"
|
|
20
|
+
import { createPortal } from "react-dom"
|
|
21
|
+
import type { DateOptionDomain } from "./dateOptionDomain"
|
|
22
|
+
import { fromDaySerial, NO_OPTION_INDEX, toDaySerial } from "./dateOptionDomain"
|
|
23
|
+
|
|
24
|
+
/** Row height in px, matching the package's WCAG 2.5.5 target size / 行高 (px)。本パッケージの WCAG 2.5.5 当たり判定と一致 */
|
|
25
|
+
export const DATE_OPTION_ROW_HEIGHT = 44
|
|
26
|
+
|
|
27
|
+
/** Rows the popup shows at once before it scrolls / ポップアップがスクロールせずに見せる行数 */
|
|
28
|
+
const VISIBLE_ROW_COUNT = 7
|
|
29
|
+
|
|
30
|
+
/** Gap in px kept between the popup and the viewport edge when it is flipped or clamped / 反転・クランプ時にポップアップと視野端の間に確保する余白 (px) */
|
|
31
|
+
const VIEWPORT_MARGIN = 8
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Total border thickness the popup's own frame takes out of its height, in px.
|
|
35
|
+
* ポップアップの枠線が自身の高さから差し引く合計の太さ (px)。
|
|
36
|
+
*
|
|
37
|
+
* The popup is a border-box, so the band the rows scroll in is `height - POPUP_BORDER_TOTAL`.
|
|
38
|
+
* Handing the outer height to the scroller instead makes every derived quantity (clip window, max
|
|
39
|
+
* scroll position, thumb mapping, rendered row count) that many pixels wrong, which the scroller
|
|
40
|
+
* reports as a warning and otherwise shows as a row that can never be scrolled fully into view.
|
|
41
|
+
* The stylesheet's own border width is the source of truth; a unit test pins the two together.
|
|
42
|
+
* ポップアップは border-box であり、行がスクロールする帯は `height - POPUP_BORDER_TOTAL`。
|
|
43
|
+
* 外側の高さをスクローラへ渡すと、クリップ窓・最大スクロール位置・つまみ写像・描画行数のすべてが
|
|
44
|
+
* その分ずれる (スクローラは警告を出すが、症状としては「最後まで送り込めない行」になる)。
|
|
45
|
+
* 真実の源はスタイルシートの枠線幅であり、両者は単体テストで固定する。
|
|
46
|
+
*/
|
|
47
|
+
export const POPUP_BORDER_TOTAL = 2
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Width in px the popup reserves for the scroller's own scrollbar.
|
|
51
|
+
* ポップアップがスクローラのスクロールバーのために確保する幅 (px)。
|
|
52
|
+
*
|
|
53
|
+
* Adding it to the control's width makes the row's text box exactly as wide as the control's, so a
|
|
54
|
+
* label that fits the control fits a row, in any host, without a chosen minimum width.
|
|
55
|
+
* 操作要素の幅へ加算することで、行のテキスト箱は操作要素のそれと同じ幅になる
|
|
56
|
+
* (最小幅を選ばずとも、操作要素に収まるラベルはどのホストでも行に収まる)。
|
|
57
|
+
*/
|
|
58
|
+
const POPUP_SCROLLBAR_WIDTH = 10
|
|
59
|
+
|
|
60
|
+
/** Milliseconds a type-ahead buffer survives without a new keystroke / 先頭一致入力のバッファが次の打鍵なしで保持されるミリ秒 */
|
|
61
|
+
const TYPE_AHEAD_RESET_MS = 1_000
|
|
62
|
+
|
|
63
|
+
/** Digits of a fully typed yyyyMMdd type-ahead buffer / 完全入力された yyyyMMdd 先頭一致バッファの桁数 */
|
|
64
|
+
const TYPE_AHEAD_FULL_DIGITS = 8
|
|
65
|
+
|
|
66
|
+
/** Digits of a yyyyMM type-ahead buffer / yyyyMM 先頭一致バッファの桁数 */
|
|
67
|
+
const TYPE_AHEAD_MONTH_DIGITS = 6
|
|
68
|
+
|
|
69
|
+
/** Digits of a yyyy type-ahead buffer / yyyy 先頭一致バッファの桁数 */
|
|
70
|
+
const TYPE_AHEAD_YEAR_DIGITS = 4
|
|
71
|
+
|
|
72
|
+
/** Months in a year, the upper bound a typed month field is validated against / 1 年の月数 (入力された月フィールドの上限) */
|
|
73
|
+
const MONTHS_PER_YEAR = 12
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Props of the virtualized day-resolution combobox.
|
|
77
|
+
* 仮想化された日単位コンボボックスの Props。
|
|
78
|
+
*/
|
|
79
|
+
export interface VirtualDateDropdownProps {
|
|
80
|
+
/** Index space of the selectable days, in descending order / 選択可能日の添字空間 (降順) */
|
|
81
|
+
domain: DateOptionDomain
|
|
82
|
+
/** Index of the held value inside the domain, or {@link NO_OPTION_INDEX} when it is outside the selectable set / 保持値の領域内添字 (選択可能集合の外では {@link NO_OPTION_INDEX}) */
|
|
83
|
+
selectedIndex: number
|
|
84
|
+
/** Option text of the held value, shown by the control whether or not it is selectable / 保持値の表示テキスト (選択可否に関わらず操作要素が表示) */
|
|
85
|
+
displayLabel: string
|
|
86
|
+
/**
|
|
87
|
+
* Machine-readable yyyy-MM-dd of the held value, published as `data-value`.
|
|
88
|
+
* 保持値の機械可読値 (yyyy-MM-dd)。`data-value` として公開。
|
|
89
|
+
*
|
|
90
|
+
* A native `<select>` carried the ISO value in `value` and the localised text in the selected
|
|
91
|
+
* option's label, so the two surfaces were separate. A text input has only one, which the user
|
|
92
|
+
* reads, so the machine surface has to be named explicitly instead of disappearing.
|
|
93
|
+
* ネイティブ `<select>` は ISO 値を `value`、地域化テキストを選択中 option のラベルに持ち、
|
|
94
|
+
* 2 つの面が分かれていた。テキスト入力は面が 1 つ (利用者が読むもの) しかないため、
|
|
95
|
+
* 機械可読の面は消えるのではなく明示的に名前を持つ必要がある。
|
|
96
|
+
*/
|
|
97
|
+
machineValue: string
|
|
98
|
+
/** Disabled flag / 無効化フラグ */
|
|
99
|
+
disabled: boolean
|
|
100
|
+
/** Accessible name of the combobox and of its popup / コンボボックスとポップアップのアクセシブル名 */
|
|
101
|
+
ariaLabel: string
|
|
102
|
+
/** Commits the date of an index the user picked / 利用者が選んだ添字の日付を確定する関数 */
|
|
103
|
+
onCommitIndex: (index: number) => void
|
|
104
|
+
/** Class name of the control element / 操作要素のクラス名 */
|
|
105
|
+
controlClassName: string
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Builds the day serial of the first day of a calendar month.
|
|
110
|
+
* 暦月の 1 日の日通番を組み立てる関数。
|
|
111
|
+
*
|
|
112
|
+
* @param year - Calendar year / 暦年
|
|
113
|
+
* @param month - Month in 1-12 / 月 (1-12)
|
|
114
|
+
* @returns Day serial of that month's first day / その月の 1 日の日通番
|
|
115
|
+
*/
|
|
116
|
+
const monthStartSerial = (year: number, month: number): number => {
|
|
117
|
+
const first = new Date(year, month - 1, 1)
|
|
118
|
+
// 0-99 年はコンストラクタが 1900 年代へ写像するため、明示的に年を上書きする
|
|
119
|
+
first.setFullYear(year)
|
|
120
|
+
return toDaySerial(first)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Resolves the index whose date best matches a partially typed date, without scanning the options.
|
|
125
|
+
* 部分入力された日付に最も合致する添字を、選択肢を走査せずに解決する関数。
|
|
126
|
+
*
|
|
127
|
+
* The digits of the buffer are read as `yyyy`, `yyyyMM`, `yyyyMMdd`, so the target date is computed
|
|
128
|
+
* and located through {@link DateOptionDomain.indexOf} instead of prefix-matching every label: a
|
|
129
|
+
* type-ahead over a century of days costs the same as over a month of days.
|
|
130
|
+
* バッファの数字を `yyyy` / `yyyyMM` / `yyyyMMdd` として読み、対象日付を算出して
|
|
131
|
+
* {@link DateOptionDomain.indexOf} で引く (全ラベルの前方一致照合を行わない)。
|
|
132
|
+
* 100 年分の日付に対する先頭一致入力の費用は 1 か月分と同じ。
|
|
133
|
+
*
|
|
134
|
+
* @param buffer - Digits typed so far, separators already stripped / これまでに入力された数字 (区切り文字は除去済み)
|
|
135
|
+
* @param domain - Option domain to locate the date in / 日付を引く対象の選択肢領域
|
|
136
|
+
* @returns Matching index, or {@link NO_OPTION_INDEX} when the buffer cannot name a day in the set / 合致する添字 (集合内の日を名指せない場合は {@link NO_OPTION_INDEX})
|
|
137
|
+
*/
|
|
138
|
+
export const resolveTypeAheadIndex = (buffer: string, domain: DateOptionDomain): number => {
|
|
139
|
+
if (buffer.length < TYPE_AHEAD_YEAR_DIGITS || domain.count === 0) return NO_OPTION_INDEX
|
|
140
|
+
const year = Number(buffer.slice(0, TYPE_AHEAD_YEAR_DIGITS))
|
|
141
|
+
if (!Number.isInteger(year)) return NO_OPTION_INDEX
|
|
142
|
+
const hasMonth = buffer.length >= TYPE_AHEAD_MONTH_DIGITS
|
|
143
|
+
const month = hasMonth ? Number(buffer.slice(TYPE_AHEAD_YEAR_DIGITS, TYPE_AHEAD_MONTH_DIGITS)) : MONTHS_PER_YEAR
|
|
144
|
+
if (month < 1 || month > MONTHS_PER_YEAR) return NO_OPTION_INDEX
|
|
145
|
+
const hasDay = buffer.length >= TYPE_AHEAD_FULL_DIGITS
|
|
146
|
+
const day = hasDay ? Number(buffer.slice(TYPE_AHEAD_MONTH_DIGITS, TYPE_AHEAD_FULL_DIGITS)) : 0
|
|
147
|
+
|
|
148
|
+
const firstSerialOfMonth = monthStartSerial(year, month)
|
|
149
|
+
const lastSerialOfMonth = (month === MONTHS_PER_YEAR ? monthStartSerial(year + 1, 1) : monthStartSerial(year, month + 1)) - 1
|
|
150
|
+
|
|
151
|
+
// 入力された桁数がそのまま「狙う範囲」を決める。範囲は最大 1 年 (366 日) で、選択肢の
|
|
152
|
+
// 総数には依存しない。降順一覧では範囲の末尾 (最も新しい日) が接頭辞の先頭行になる
|
|
153
|
+
const rangeStart = hasDay ? firstSerialOfMonth + day - 1 : hasMonth ? firstSerialOfMonth : monthStartSerial(year, 1)
|
|
154
|
+
const rangeEnd = hasDay ? rangeStart : hasMonth ? lastSerialOfMonth : monthStartSerial(year + 1, 1) - 1
|
|
155
|
+
if (hasDay && (day < 1 || rangeStart > lastSerialOfMonth)) return NO_OPTION_INDEX
|
|
156
|
+
|
|
157
|
+
for (let serial = rangeEnd; serial >= rangeStart; serial--) {
|
|
158
|
+
const index = domain.indexOf(fromDaySerial(serial))
|
|
159
|
+
if (index !== NO_OPTION_INDEX) return index
|
|
160
|
+
}
|
|
161
|
+
return NO_OPTION_INDEX
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Colour mode the popup paints itself in.
|
|
166
|
+
* ポップアップが自身を塗る配色モード。
|
|
167
|
+
*/
|
|
168
|
+
export type PopupColorMode = "light" | "dark"
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Resolves the colour mode that applies at an element's own place in the tree.
|
|
172
|
+
* 要素自身が木の中で置かれた位置に適用される配色モードを解決する関数。
|
|
173
|
+
*
|
|
174
|
+
* The popup is portaled to `document.body`, so an ancestor-path rule (`.dark .period-slider-date-popup`)
|
|
175
|
+
* only reaches it when `.dark` sits above `<body>`. A host that scopes `.dark` to a subtree — which this
|
|
176
|
+
* package's own `@custom-variant dark (&:where(.dark, .dark *))` explicitly permits — would otherwise get
|
|
177
|
+
* a light popup over a dark page. Reading the mode where the control actually stands, and carrying it on
|
|
178
|
+
* the popup as `data-color-mode`, makes the two agree wherever the host puts the class.
|
|
179
|
+
* ポップアップは `document.body` へポータルされるため、祖先経路の規則 (`.dark .period-slider-date-popup`)
|
|
180
|
+
* が届くのは `.dark` が `<body>` より上にある場合だけ。`.dark` を部分木へ限定するホスト
|
|
181
|
+
* (本パッケージの `@custom-variant dark (&:where(.dark, .dark *))` が明示的に許す形) では、
|
|
182
|
+
* 暗色のページの上に明色のポップアップが載る。操作要素が実際に立つ位置でモードを読み、
|
|
183
|
+
* `data-color-mode` としてポップアップへ運ぶことで、クラスの置き場所に関わらず両者が一致。
|
|
184
|
+
*
|
|
185
|
+
* @param element - Element whose place in the tree decides the mode / 木の中の位置がモードを決める要素
|
|
186
|
+
* @returns Colour mode applying at that element / その要素に適用される配色モード
|
|
187
|
+
*/
|
|
188
|
+
export const resolveColorMode = (element: Element): PopupColorMode => {
|
|
189
|
+
// クラスセレクタ例外 (AGENTS.md): `.dark` はホストが付ける契約クラスであり、
|
|
190
|
+
// 本パッケージの `@custom-variant dark` が読むのと同じ唯一の出典。ホストの DOM は
|
|
191
|
+
// 編集できないため data-* フックへ置き換える余地が無い
|
|
192
|
+
return element.closest(".dark") === null ? "light" : "dark"
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Geometry of the popup relative to the viewport.
|
|
197
|
+
* 視野に対するポップアップの配置情報。
|
|
198
|
+
*/
|
|
199
|
+
export interface PopupGeometry {
|
|
200
|
+
/** Distance from the viewport top in px / 視野上端からの距離 (px) */
|
|
201
|
+
top: number
|
|
202
|
+
/** Distance from the viewport left in px / 視野左端からの距離 (px) */
|
|
203
|
+
left: number
|
|
204
|
+
/** Popup width in px, the control's width plus the scrollbar gutter / ポップアップの幅 (px。操作要素の幅 + スクロールバー分) */
|
|
205
|
+
width: number
|
|
206
|
+
/** Popup height in px / ポップアップの高さ (px) */
|
|
207
|
+
height: number
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Places the popup below the control, flipping it above and clamping it when there is no room.
|
|
212
|
+
* ポップアップを操作要素の下に配置し、余地が無い場合は上へ反転・視野内へクランプする関数。
|
|
213
|
+
*
|
|
214
|
+
* A native `<select>` popup is placed by the browser and is always on screen; a DOM popup that only
|
|
215
|
+
* ever opens downward is unusable in the last row of a card, which is exactly where these selectors
|
|
216
|
+
* sit, so the placement is computed rather than declared in CSS.
|
|
217
|
+
* ネイティブ `<select>` のポップアップはブラウザが配置し常に画面内にあるが、下方向にしか開かない
|
|
218
|
+
* DOM ポップアップはカード最下段 (まさにこのセレクタが置かれる場所) では使えないため、
|
|
219
|
+
* 配置は CSS で宣言せず算出する。
|
|
220
|
+
*
|
|
221
|
+
* @param anchor - Bounding rect of the control / 操作要素の矩形
|
|
222
|
+
* @param desiredHeight - Height the popup asks for / ポップアップが要求する高さ
|
|
223
|
+
* @param viewportHeight - Height of the visual viewport / 視野の高さ
|
|
224
|
+
* @param viewportWidth - Width of the visual viewport / 視野の幅
|
|
225
|
+
* @returns Geometry to apply to the popup / ポップアップへ適用する配置情報
|
|
226
|
+
*/
|
|
227
|
+
export const resolvePopupGeometry = (anchor: { top: number; bottom: number; left: number; width: number }, desiredHeight: number, viewportHeight: number, viewportWidth: number): PopupGeometry => {
|
|
228
|
+
const roomBelow = viewportHeight - anchor.bottom - VIEWPORT_MARGIN
|
|
229
|
+
const roomAbove = anchor.top - VIEWPORT_MARGIN
|
|
230
|
+
const opensDown = roomBelow >= desiredHeight || roomBelow >= roomAbove
|
|
231
|
+
const available = opensDown ? roomBelow : roomAbove
|
|
232
|
+
const height = Math.max(DATE_OPTION_ROW_HEIGHT, Math.min(desiredHeight, available))
|
|
233
|
+
const top = opensDown ? anchor.bottom : anchor.top - height
|
|
234
|
+
const width = anchor.width + POPUP_SCROLLBAR_WIDTH
|
|
235
|
+
const left = Math.max(VIEWPORT_MARGIN, Math.min(anchor.left, viewportWidth - width - VIEWPORT_MARGIN))
|
|
236
|
+
return { top, left, width, height }
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Day-resolution combobox whose popup renders only the rows it shows.
|
|
241
|
+
* 表示行のみを描画するポップアップを持つ日単位コンボボックス。
|
|
242
|
+
*
|
|
243
|
+
* The popup is not mounted while closed, so a host that moves `minDate` / `maxDate` on every
|
|
244
|
+
* pointer move pays only the O(1) recomputation of the domain: there is no option list to rebuild
|
|
245
|
+
* and no `<option>` LayoutObject to re-lay out.
|
|
246
|
+
* 閉じている間ポップアップはマウントされないため、ポインタ移動ごとに `minDate` / `maxDate` を
|
|
247
|
+
* 動かすホストが払う費用は領域の O(1) 再計算のみ (作り直す選択肢配列も、再レイアウトされる
|
|
248
|
+
* `<option>` の LayoutObject も存在しない)。
|
|
249
|
+
*/
|
|
250
|
+
export const VirtualDateDropdown: React.FC<VirtualDateDropdownProps> = ({ domain, selectedIndex, displayLabel, machineValue, disabled, ariaLabel, onCommitIndex, controlClassName }) => {
|
|
251
|
+
const reactId = useId()
|
|
252
|
+
const listboxId = `period-slider-date-listbox-${reactId}`
|
|
253
|
+
const optionId = (index: number): string => `period-slider-date-option-${reactId}-${index}`
|
|
254
|
+
|
|
255
|
+
const [isOpen, setIsOpen] = useState(false)
|
|
256
|
+
const [activeIndex, setActiveIndex] = useState(NO_OPTION_INDEX)
|
|
257
|
+
const [isActiveRendered, setIsActiveRendered] = useState(true)
|
|
258
|
+
const [geometry, setGeometry] = useState<PopupGeometry | null>(null)
|
|
259
|
+
const [colorMode, setColorMode] = useState<PopupColorMode | null>(null)
|
|
260
|
+
|
|
261
|
+
const controlRef = useRef<HTMLInputElement>(null)
|
|
262
|
+
const popupRef = useRef<HTMLDivElement>(null)
|
|
263
|
+
const scrollRef = useRef<VirtualScrollHandle | null>(null)
|
|
264
|
+
const typeAheadRef = useRef<{ buffer: string; at: number }>({ buffer: "", at: 0 })
|
|
265
|
+
|
|
266
|
+
// domain は毎レンダー新しい object になり得るため、VirtualScroll へ渡す関数の identity を
|
|
267
|
+
// 安定させる目的で ref 越しに読む (関数 identity が変わると行の再構築が毎レンダー走る)
|
|
268
|
+
const domainRef = useRef(domain)
|
|
269
|
+
domainRef.current = domain
|
|
270
|
+
const count = domain.count
|
|
271
|
+
|
|
272
|
+
const desiredHeight = Math.min(count, VISIBLE_ROW_COUNT) * DATE_OPTION_ROW_HEIGHT
|
|
273
|
+
const visibleRowCount = Math.max(1, Math.min(count, VISIBLE_ROW_COUNT))
|
|
274
|
+
|
|
275
|
+
const measure = useCallback((): void => {
|
|
276
|
+
const control = controlRef.current
|
|
277
|
+
if (control === null) return
|
|
278
|
+
const rect = control.getBoundingClientRect()
|
|
279
|
+
setGeometry(resolvePopupGeometry(rect, desiredHeight, window.innerHeight, window.innerWidth))
|
|
280
|
+
}, [desiredHeight])
|
|
281
|
+
|
|
282
|
+
const close = useCallback((): void => {
|
|
283
|
+
setIsOpen(false)
|
|
284
|
+
setActiveIndex(NO_OPTION_INDEX)
|
|
285
|
+
typeAheadRef.current = { buffer: "", at: 0 }
|
|
286
|
+
}, [])
|
|
287
|
+
|
|
288
|
+
const open = useCallback(
|
|
289
|
+
(initialActive: number): void => {
|
|
290
|
+
if (disabled || count === 0) return
|
|
291
|
+
setActiveIndex(initialActive)
|
|
292
|
+
setIsOpen(true)
|
|
293
|
+
},
|
|
294
|
+
[disabled, count],
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
// 開いた時点の矩形で配置を決め、ページのスクロールとリサイズに追従する。
|
|
298
|
+
// ポップアップは body へポータルするため、祖先の overflow で切り取られない
|
|
299
|
+
useLayoutEffect(() => {
|
|
300
|
+
if (!isOpen) {
|
|
301
|
+
setGeometry(null)
|
|
302
|
+
setColorMode(null)
|
|
303
|
+
return
|
|
304
|
+
}
|
|
305
|
+
const control = controlRef.current
|
|
306
|
+
// 配色モードは操作要素の位置で読む。ポータル先 (body 直下) は `.dark` を部分木へ
|
|
307
|
+
// 限定するホストではその外側に立つため、祖先経路の規則だけでは明色のまま残る
|
|
308
|
+
if (control !== null) setColorMode(resolveColorMode(control))
|
|
309
|
+
measure()
|
|
310
|
+
window.addEventListener("scroll", measure, true)
|
|
311
|
+
window.addEventListener("resize", measure)
|
|
312
|
+
return () => {
|
|
313
|
+
window.removeEventListener("scroll", measure, true)
|
|
314
|
+
window.removeEventListener("resize", measure)
|
|
315
|
+
}
|
|
316
|
+
}, [isOpen, measure])
|
|
317
|
+
|
|
318
|
+
// ポップアップ外の押下で閉じる。click ではなく pointerdown で閉じるのは、押下位置が
|
|
319
|
+
// ポップアップの外だと判明した時点で閉じるのがネイティブの挙動と一致するため
|
|
320
|
+
useEffect(() => {
|
|
321
|
+
if (!isOpen) return
|
|
322
|
+
const onPointerDown = (event: PointerEvent): void => {
|
|
323
|
+
const target = event.target as Node | null
|
|
324
|
+
if (target === null) return
|
|
325
|
+
if (popupRef.current?.contains(target) === true) return
|
|
326
|
+
if (controlRef.current?.contains(target) === true) return
|
|
327
|
+
close()
|
|
328
|
+
}
|
|
329
|
+
document.addEventListener("pointerdown", onPointerDown, true)
|
|
330
|
+
return () => document.removeEventListener("pointerdown", onPointerDown, true)
|
|
331
|
+
}, [isOpen, close])
|
|
332
|
+
|
|
333
|
+
const activeIndexRef = useRef(activeIndex)
|
|
334
|
+
activeIndexRef.current = activeIndex
|
|
335
|
+
|
|
336
|
+
// 選択肢集合が入れ替わったまま開いていると、保持していたスクロール位置は別の日を指す。
|
|
337
|
+
// アクティブ行へ引き直さないと、開いたポップアップが無関係な位置を見せ続ける
|
|
338
|
+
useEffect(() => {
|
|
339
|
+
if (!isOpen) return
|
|
340
|
+
const index = activeIndexRef.current
|
|
341
|
+
if (index === NO_OPTION_INDEX) return
|
|
342
|
+
scrollRef.current?.scrollToIndex(Math.min(index, count - 1), { align: "center" })
|
|
343
|
+
}, [count, isOpen])
|
|
344
|
+
|
|
345
|
+
const moveActive = (next: number): void => {
|
|
346
|
+
if (count === 0) return
|
|
347
|
+
const clamped = Math.max(0, Math.min(next, count - 1))
|
|
348
|
+
setActiveIndex(clamped)
|
|
349
|
+
scrollRef.current?.scrollToIndex(clamped, { align: "center" })
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const commit = (index: number): void => {
|
|
353
|
+
if (index < 0 || index >= count) return
|
|
354
|
+
onCommitIndex(index)
|
|
355
|
+
close()
|
|
356
|
+
controlRef.current?.focus()
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>): void => {
|
|
360
|
+
if (disabled) return
|
|
361
|
+
const anchor = activeIndex !== NO_OPTION_INDEX ? activeIndex : selectedIndex !== NO_OPTION_INDEX ? selectedIndex : 0
|
|
362
|
+
|
|
363
|
+
switch (event.key) {
|
|
364
|
+
case "ArrowDown":
|
|
365
|
+
event.preventDefault()
|
|
366
|
+
if (!isOpen) {
|
|
367
|
+
open(anchor)
|
|
368
|
+
return
|
|
369
|
+
}
|
|
370
|
+
moveActive(anchor + 1)
|
|
371
|
+
return
|
|
372
|
+
case "ArrowUp":
|
|
373
|
+
event.preventDefault()
|
|
374
|
+
if (!isOpen) {
|
|
375
|
+
open(anchor)
|
|
376
|
+
return
|
|
377
|
+
}
|
|
378
|
+
moveActive(anchor - 1)
|
|
379
|
+
return
|
|
380
|
+
case "Home":
|
|
381
|
+
if (!isOpen) return
|
|
382
|
+
event.preventDefault()
|
|
383
|
+
moveActive(0)
|
|
384
|
+
return
|
|
385
|
+
case "End":
|
|
386
|
+
if (!isOpen) return
|
|
387
|
+
event.preventDefault()
|
|
388
|
+
moveActive(count - 1)
|
|
389
|
+
return
|
|
390
|
+
case "PageDown":
|
|
391
|
+
if (!isOpen) return
|
|
392
|
+
event.preventDefault()
|
|
393
|
+
moveActive(anchor + visibleRowCount)
|
|
394
|
+
return
|
|
395
|
+
case "PageUp":
|
|
396
|
+
if (!isOpen) return
|
|
397
|
+
event.preventDefault()
|
|
398
|
+
moveActive(anchor - visibleRowCount)
|
|
399
|
+
return
|
|
400
|
+
case "Enter":
|
|
401
|
+
event.preventDefault()
|
|
402
|
+
if (!isOpen) {
|
|
403
|
+
open(anchor)
|
|
404
|
+
return
|
|
405
|
+
}
|
|
406
|
+
commit(activeIndex !== NO_OPTION_INDEX ? activeIndex : anchor)
|
|
407
|
+
return
|
|
408
|
+
case "Escape":
|
|
409
|
+
if (!isOpen) return
|
|
410
|
+
event.preventDefault()
|
|
411
|
+
close()
|
|
412
|
+
return
|
|
413
|
+
case "Tab":
|
|
414
|
+
if (isOpen) close()
|
|
415
|
+
return
|
|
416
|
+
default:
|
|
417
|
+
break
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// 数字と日付の区切り文字だけを先頭一致入力として受け付ける。ISO でもロケール書式でも
|
|
421
|
+
// 区切りを捨てれば同じ数字列になるため、表示書式に依存せず同じキー入力で辿れる
|
|
422
|
+
if (event.key.length !== 1) return
|
|
423
|
+
const isDigit = event.key >= "0" && event.key <= "9"
|
|
424
|
+
const isSeparator = event.key === "-" || event.key === "/" || event.key === "."
|
|
425
|
+
if (!(isDigit || isSeparator)) return
|
|
426
|
+
event.preventDefault()
|
|
427
|
+
const now = Date.now()
|
|
428
|
+
const previous = now - typeAheadRef.current.at > TYPE_AHEAD_RESET_MS ? "" : typeAheadRef.current.buffer
|
|
429
|
+
const buffer = isDigit ? `${previous}${event.key}`.slice(0, TYPE_AHEAD_FULL_DIGITS) : previous
|
|
430
|
+
typeAheadRef.current = { buffer, at: now }
|
|
431
|
+
const target = resolveTypeAheadIndex(buffer, domainRef.current)
|
|
432
|
+
if (target === NO_OPTION_INDEX) return
|
|
433
|
+
if (!isOpen) {
|
|
434
|
+
open(target)
|
|
435
|
+
return
|
|
436
|
+
}
|
|
437
|
+
moveActive(target)
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
const getItem = useCallback((index: number) => index, [])
|
|
441
|
+
const getItemHeight = useCallback(() => DATE_OPTION_ROW_HEIGHT, [])
|
|
442
|
+
|
|
443
|
+
// aria-activedescendant は描画されている行しか指せない (WAI-ARIA)。描画範囲は ref 経由の
|
|
444
|
+
// 最新のアクティブ添字と突き合わせる。アクティブ行の移動は必ず scrollToIndex を伴い、
|
|
445
|
+
// 描画範囲の通知がその後に届くため、この 1 経路で常に最新の可否が得られる
|
|
446
|
+
const handleRangeChange = useCallback((range: VirtualScrollRange): void => {
|
|
447
|
+
const index = activeIndexRef.current
|
|
448
|
+
setIsActiveRendered(index === NO_OPTION_INDEX || (index >= range.renderingStartIndex && index <= range.renderingEndIndex))
|
|
449
|
+
}, [])
|
|
450
|
+
|
|
451
|
+
// 配置と配色モードはどちらも操作要素の実測から来る。片方だけで描くと、最初のフレームだけ
|
|
452
|
+
// 明色のポップアップが暗色のページに載る
|
|
453
|
+
const popup =
|
|
454
|
+
isOpen && geometry !== null && colorMode !== null && count > 0 ? (
|
|
455
|
+
// biome-ignore lint/a11y/noStaticElementInteractions: ポップアップの外枠が拾うのは「フォーカスを奪わせない」ための mousedown 抑止だけで、操作機能は持たない。操作は内側の role="option" と操作要素が担う
|
|
456
|
+
<div
|
|
457
|
+
ref={popupRef}
|
|
458
|
+
className="period-slider-date-popup"
|
|
459
|
+
data-testid="period-slider-date-popup"
|
|
460
|
+
data-color-mode={colorMode}
|
|
461
|
+
style={{ top: geometry.top, left: geometry.left, width: geometry.width, height: geometry.height }}
|
|
462
|
+
onMouseDown={(event) => {
|
|
463
|
+
// 行や余白を掴んでも操作要素からフォーカスを奪わない (コンボボックスは DOM
|
|
464
|
+
// フォーカスを操作要素に保持し続ける契約)。pointer 系は抑止しないため
|
|
465
|
+
// スクロールバーのドラッグは従来どおり機能する
|
|
466
|
+
event.preventDefault()
|
|
467
|
+
}}>
|
|
468
|
+
<VirtualScroll
|
|
469
|
+
ref={scrollRef}
|
|
470
|
+
itemCount={count}
|
|
471
|
+
getItem={getItem}
|
|
472
|
+
getItemHeight={getItemHeight}
|
|
473
|
+
viewportSize={geometry.height - POPUP_BORDER_TOTAL}
|
|
474
|
+
initialScrollIndex={activeIndex !== NO_OPTION_INDEX ? activeIndex : undefined}
|
|
475
|
+
onRangeChange={handleRangeChange}
|
|
476
|
+
className="h-full w-full"
|
|
477
|
+
testId="period-slider-date-listbox-pane"
|
|
478
|
+
contentProps={{
|
|
479
|
+
id: listboxId,
|
|
480
|
+
role: "listbox",
|
|
481
|
+
"aria-label": ariaLabel,
|
|
482
|
+
}}
|
|
483
|
+
scrollBarOptions={{
|
|
484
|
+
width: POPUP_SCROLLBAR_WIDTH,
|
|
485
|
+
// ポップアップとその子孫はページのタブ順から除外する (APG)。
|
|
486
|
+
// 未指定だと矢印ボタンが tabIndex=0 のままポップアップのタブ順へ露出する
|
|
487
|
+
enableArrowButtons: false,
|
|
488
|
+
enableThumbDrag: true,
|
|
489
|
+
enableTrackClick: true,
|
|
490
|
+
}}
|
|
491
|
+
behaviorOptions={{
|
|
492
|
+
// 必ず false。true だと各行ラッパーに tabIndex=-1 が付いてポップアップが
|
|
493
|
+
// タブ順に入り、pointerdown で行へフォーカスが移って操作要素から奪う。
|
|
494
|
+
// キーボード操作は aria-activedescendant 方式で本コンポーネントが担う
|
|
495
|
+
enableKeyboardNavigation: false,
|
|
496
|
+
// 必ず "contain"。端に達してもホイールをページへ連鎖させない (ネイティブ
|
|
497
|
+
// <select> と同じ)。既定 "auto" では末尾の次のノッチでページ本体が動き、
|
|
498
|
+
// 固定配置のポップアップが操作要素から離れて見える
|
|
499
|
+
overscrollBehavior: "contain",
|
|
500
|
+
}}>
|
|
501
|
+
{(index: number) => {
|
|
502
|
+
const isSelected = index === selectedIndex
|
|
503
|
+
const isActive = index === activeIndex
|
|
504
|
+
return (
|
|
505
|
+
// biome-ignore lint/a11y/useKeyWithClickEvents: キーボード操作はコンボボックス本体 (操作要素) が aria-activedescendant 方式で担う。行は APG どおりフォーカスを持たない
|
|
506
|
+
// biome-ignore lint/a11y/useFocusableInteractive: 同上。role="option" の行にフォーカスを与えると DOM フォーカスが操作要素から離れる
|
|
507
|
+
<div
|
|
508
|
+
id={optionId(index)}
|
|
509
|
+
role="option"
|
|
510
|
+
aria-selected={isSelected}
|
|
511
|
+
aria-posinset={index + 1}
|
|
512
|
+
aria-setsize={count}
|
|
513
|
+
data-testid="period-slider-date-option"
|
|
514
|
+
data-active={isActive ? "true" : undefined}
|
|
515
|
+
className="period-slider-date-option"
|
|
516
|
+
style={{ height: DATE_OPTION_ROW_HEIGHT }}
|
|
517
|
+
onClick={() => commit(index)}>
|
|
518
|
+
{domainRef.current.labelAt(index)}
|
|
519
|
+
</div>
|
|
520
|
+
)
|
|
521
|
+
}}
|
|
522
|
+
</VirtualScroll>
|
|
523
|
+
</div>
|
|
524
|
+
) : null
|
|
525
|
+
|
|
526
|
+
return (
|
|
527
|
+
<>
|
|
528
|
+
<input
|
|
529
|
+
ref={controlRef}
|
|
530
|
+
type="text"
|
|
531
|
+
role="combobox"
|
|
532
|
+
readOnly
|
|
533
|
+
aria-label={ariaLabel}
|
|
534
|
+
aria-expanded={isOpen}
|
|
535
|
+
aria-controls={isOpen ? listboxId : undefined}
|
|
536
|
+
aria-haspopup="listbox"
|
|
537
|
+
aria-autocomplete="none"
|
|
538
|
+
aria-activedescendant={isOpen && activeIndex !== NO_OPTION_INDEX && isActiveRendered ? optionId(activeIndex) : undefined}
|
|
539
|
+
disabled={disabled}
|
|
540
|
+
value={displayLabel}
|
|
541
|
+
className={controlClassName}
|
|
542
|
+
data-testid="period-slider-date-combobox"
|
|
543
|
+
data-value={machineValue}
|
|
544
|
+
onKeyDown={handleKeyDown}
|
|
545
|
+
onMouseDown={(event) => {
|
|
546
|
+
if (disabled) return
|
|
547
|
+
// 押下で開閉を切り替える (ネイティブ <select> と同じ)。既定動作を止めるのは
|
|
548
|
+
// 読み取り専用入力のテキスト選択を始めさせないため
|
|
549
|
+
event.preventDefault()
|
|
550
|
+
controlRef.current?.focus()
|
|
551
|
+
if (isOpen) close()
|
|
552
|
+
else open(selectedIndex !== NO_OPTION_INDEX ? selectedIndex : 0)
|
|
553
|
+
}}
|
|
554
|
+
onBlur={() => {
|
|
555
|
+
if (isOpen) close()
|
|
556
|
+
}}
|
|
557
|
+
/>
|
|
558
|
+
{popup === null ? null : createPortal(popup, document.body)}
|
|
559
|
+
</>
|
|
560
|
+
)
|
|
561
|
+
}
|
|
562
|
+
VirtualDateDropdown.displayName = "VirtualDateDropdown"
|