@aiquants/period-slider 0.18.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +209 -0
- package/README.md +26 -7
- package/dist/DateSelector.d.ts +9 -9
- package/dist/DateSelector.d.ts.map +1 -1
- package/dist/MonthSelector.d.ts +5 -4
- package/dist/MonthSelector.d.ts.map +1 -1
- package/dist/PeriodSlider.d.ts +23 -4
- package/dist/PeriodSlider.d.ts.map +1 -1
- package/dist/VirtualDateDropdown.d.ts +25 -10
- package/dist/VirtualDateDropdown.d.ts.map +1 -1
- package/dist/YearSelector.d.ts +7 -4
- package/dist/YearSelector.d.ts.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +2859 -2394
- package/dist/optionText.d.ts +1 -1
- package/dist/optionText.d.ts.map +1 -1
- package/dist/renderScope.d.ts +486 -0
- package/dist/renderScope.d.ts.map +1 -0
- package/dist/selectorMemo.d.ts +42 -22
- package/dist/selectorMemo.d.ts.map +1 -1
- package/dist/useCommittedRef.d.ts +64 -71
- package/dist/useCommittedRef.d.ts.map +1 -1
- package/dist/usePeriodSlider.d.ts +56 -12
- package/dist/usePeriodSlider.d.ts.map +1 -1
- package/dist/useUncontrolledSelection.d.ts +7 -7
- package/dist/useUncontrolledSelection.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/DateSelector.tsx +45 -20
- package/src/MonthSelector.tsx +41 -19
- package/src/PeriodSlider.tsx +39 -14
- package/src/VirtualDateDropdown.tsx +163 -92
- package/src/YearSelector.tsx +26 -27
- package/src/optionText.ts +6 -4
- package/src/renderScope.ts +766 -0
- package/src/selectorMemo.ts +53 -25
- package/src/useCommittedRef.ts +51 -155
- package/src/usePeriodSlider.ts +316 -138
- package/src/useUncontrolledSelection.ts +8 -9
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* アクセシブル名・値の面を保ちながら、キャレットとソフトキーボードを操作から外す。
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import { VirtualScroll, type VirtualScrollHandle
|
|
17
|
+
import { VirtualScroll, type VirtualScrollHandle } from "@aiquants/virtualscroll"
|
|
18
18
|
import type React from "react"
|
|
19
19
|
import { useCallback, useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "react"
|
|
20
20
|
import { createPortal } from "react-dom"
|
|
@@ -285,18 +285,33 @@ const clampActiveIndex = (index: number, count: number): number => (index < coun
|
|
|
285
285
|
*
|
|
286
286
|
* Rows take the domain, the held value's index and the active index from the render that made the row renderer, which is
|
|
287
287
|
* memoized on them, so a render that changes none of them (the popup following a page scroll) re-renders no row, and a row
|
|
288
|
-
* the scroller renders on its own reads the same render as the rest of the popup. Functions that outlive their render (
|
|
289
|
-
*
|
|
290
|
-
*
|
|
291
|
-
*
|
|
292
|
-
*
|
|
293
|
-
*
|
|
288
|
+
* the scroller renders on its own reads the same render as the rest of the popup. Functions that outlive their render (a row's
|
|
289
|
+
* click) and the effect that re-anchors the popup read the last committed render through useCommittedRef; the handlers React
|
|
290
|
+
* attaches as props, and the active row's ref, read their own render, which is the committed one. No render writes a ref or
|
|
291
|
+
* the state. An active index the domain no longer contains is drawn on the last row by the render that shrinks the domain, and
|
|
292
|
+
* the commit that drew it records it as the held index from a layout effect, leaving alone a move queued after that render
|
|
293
|
+
* read the state. The control's two IDREFs name only elements in the DOM: `aria-controls` names the listbox under the same
|
|
294
|
+
* condition that renders the popup, and `aria-activedescendant` is not a prop — the active row's ref writes it in the commit
|
|
295
|
+
* that attaches the row, and the cleanup it returns, holding the control it wrote to, removes it in the commit that detaches
|
|
296
|
+
* the row, including when Activity or Suspense hides the subtree.
|
|
294
297
|
* 行は、行の描画関数を作った描画の領域・保持値の添字・アクティブ添字を読み、描画関数はそれらでメモ化する。どれも変えない描画
|
|
295
298
|
* (ページのスクロールに追従するポップアップ) は行を 1 つも描き直さず、スクローラが自分で描く行もポップアップの他の部分と同じ描画を読む。
|
|
296
|
-
* 描画より長く生きる関数 (
|
|
297
|
-
* React が Props
|
|
298
|
-
*
|
|
299
|
-
* (その描画がステートを読んだ後に積まれた移動は上書きしない)
|
|
299
|
+
* 描画より長く生きる関数 (行の押下) と引き直しのエフェクトは useCommittedRef を通して最後に確定した描画を読み、
|
|
300
|
+
* React が Props として付ける処理関数とアクティブ行の ref は自身の描画 (確定した描画) を読む。どの描画も ref にもステートにも書かない。
|
|
301
|
+
* 領域が含まなくなったアクティブ添字は、領域を縮めた描画が末尾の行として描き、それを描いた確定がレイアウト副作用で保持する添字へ記録する
|
|
302
|
+
* (その描画がステートを読んだ後に積まれた移動は上書きしない)。操作要素の 2 つの IDREF は DOM に在る要素だけを指す。`aria-controls` は
|
|
303
|
+
* ポップアップを描くのと同じ条件で listbox を名乗り、`aria-activedescendant` は Props ではなく、アクティブ行の ref が行を付けた確定で書き、
|
|
304
|
+
* 書いた操作要素を持つ後始末が行を外す確定 (Activity・Suspense による退避を含む) で消す。
|
|
305
|
+
*
|
|
306
|
+
* Hiding the owner (a hidden Activity, or Suspense showing its fallback) closes the popup. React hides only the top-most host
|
|
307
|
+
* nodes of the hidden boundary and never reaches a portal below one, so the popup root's ref cleanup, which React runs in the
|
|
308
|
+
* hide commit, takes the root off screen with the same inline `display: none !important`, and the cleanup of a layout effect
|
|
309
|
+
* React runs on hide calls the close path while the open flag is set. open and close write that flag when they are called and
|
|
310
|
+
* every commit copies it, so an open queued in the same batch as the hide is closed too. The reveal shows a closed combobox.
|
|
311
|
+
* 所有側が隠れると (隠れた Activity、フォールバックを出した Suspense) ポップアップは閉じる。React が隠すのは境界の中の最上位のホスト要素だけで
|
|
312
|
+
* その下のポータルへは届かないため、隠す確定で React が走らせるポップアップの根の ref の後始末が同じインラインの `display: none !important` で
|
|
313
|
+
* 根を画面から外し、隠すときに React が走らせるレイアウト副作用の後始末が、開いている旗が立っていれば閉じる経路を呼ぶ。旗は open と close が
|
|
314
|
+
* 呼ばれた時点で書き、確定のたびに写すため、隠す更新と同じ一括で積まれた開く更新も閉じる。表示し直した操作要素は閉じている。
|
|
300
315
|
*/
|
|
301
316
|
export const VirtualDateDropdown: React.FC<VirtualDateDropdownProps> = ({ domain, selectedIndex, openAnchorIndex, displayLabel, machineValue, disabled, ariaLabel, locale, labels, onCommitIndex, controlClassName }) => {
|
|
302
317
|
const reactId = useId()
|
|
@@ -307,7 +322,6 @@ export const VirtualDateDropdown: React.FC<VirtualDateDropdownProps> = ({ domain
|
|
|
307
322
|
|
|
308
323
|
const [isOpen, setIsOpen] = useState(false)
|
|
309
324
|
const [heldActiveIndex, setActiveIndex] = useState(NO_OPTION_INDEX)
|
|
310
|
-
const [isActiveRendered, setIsActiveRendered] = useState(true)
|
|
311
325
|
const [geometry, setGeometry] = useState<PopupGeometry | null>(null)
|
|
312
326
|
const [colorMode, setColorMode] = useState<PopupColorMode | null>(null)
|
|
313
327
|
|
|
@@ -317,8 +331,8 @@ export const VirtualDateDropdown: React.FC<VirtualDateDropdownProps> = ({ domain
|
|
|
317
331
|
const typeAheadRef = useRef<{ buffer: string; at: number }>({ buffer: "", at: 0 })
|
|
318
332
|
|
|
319
333
|
const count = domain.count
|
|
320
|
-
//
|
|
321
|
-
//
|
|
334
|
+
// 件数が縮むと、保持していたアクティブ添字が範囲外に取り残される。確定後に寄せるまで待つと、その間の確定は
|
|
335
|
+
// アクティブ行を描かず (名指しも無く)、Enter が境界検査に弾かれる。描く添字はこの描画の件数で寄せて導き、
|
|
322
336
|
// 状態への記録はそれを描いた確定のレイアウト副作用に回す。描画中に記録すると、確定しない描画 (Suspense が保留した移行) の
|
|
323
337
|
// 寄せた値を React が同値判定の基準に残し、利用者がその添字へ動かす打鍵が描画を起こさずに捨てられる
|
|
324
338
|
const activeIndex = clampActiveIndex(heldActiveIndex, count)
|
|
@@ -340,19 +354,34 @@ export const VirtualDateDropdown: React.FC<VirtualDateDropdownProps> = ({ domain
|
|
|
340
354
|
setGeometry(resolvePopupGeometry(rect, desiredHeight, window.innerHeight, window.innerWidth))
|
|
341
355
|
}, [desiredHeight])
|
|
342
356
|
|
|
357
|
+
// 開いているかは確定した描画が写すだけでなく、open と close が呼ばれた時点でも書く。確定した値だけを読むと、所有側を隠す更新と
|
|
358
|
+
// 同じ一括で積まれた開く更新を隠す確定が見落とし、隠れた木の描画がそれを開いたまま描いて、表示し直すと一覧が開いて出る
|
|
359
|
+
const isOpenRef = useCommittedRef(isOpen)
|
|
343
360
|
const close = useCallback((): void => {
|
|
344
361
|
setIsOpen(false)
|
|
345
362
|
setActiveIndex(NO_OPTION_INDEX)
|
|
346
363
|
typeAheadRef.current = { buffer: "", at: 0 }
|
|
347
|
-
|
|
364
|
+
isOpenRef.current = false
|
|
365
|
+
}, [isOpenRef])
|
|
366
|
+
|
|
367
|
+
// 隠れた操作要素はフォーカスを保てず (ブラウザは次の描画更新で外す)、表示し直しても React はフォーカスを戻さない。開いたまま戻すと、キーボードの届かない一覧が
|
|
368
|
+
// aria-expanded="true" のまま残る (ネイティブの <select> も描かれなくなると一覧を閉じ、自分からは開き直さない)。依存配列は変わらないため、
|
|
369
|
+
// 後始末が走るのは隠す確定 (Activity・Suspense)、アンマウント、StrictMode の付け外し (マウント時と表示し直した時) だけ。閉じたまま隠しても更新を積まない
|
|
370
|
+
useLayoutEffect(
|
|
371
|
+
() => () => {
|
|
372
|
+
if (isOpenRef.current) close()
|
|
373
|
+
},
|
|
374
|
+
[isOpenRef, close],
|
|
375
|
+
)
|
|
348
376
|
|
|
349
377
|
const open = useCallback(
|
|
350
378
|
(initialActive: number): void => {
|
|
351
379
|
if (disabled || count === 0) return
|
|
352
380
|
setActiveIndex(initialActive)
|
|
353
381
|
setIsOpen(true)
|
|
382
|
+
isOpenRef.current = true
|
|
354
383
|
},
|
|
355
|
-
[disabled, count],
|
|
384
|
+
[disabled, count, isOpenRef],
|
|
356
385
|
)
|
|
357
386
|
|
|
358
387
|
// 開いた時点の矩形で配置を決め、ページのスクロールとリサイズに追従する。
|
|
@@ -391,9 +420,8 @@ export const VirtualDateDropdown: React.FC<VirtualDateDropdownProps> = ({ domain
|
|
|
391
420
|
return () => document.removeEventListener("pointerdown", onPointerDown, true)
|
|
392
421
|
}, [isOpen, close])
|
|
393
422
|
|
|
394
|
-
//
|
|
395
|
-
//
|
|
396
|
-
// aria-controls / aria-activedescendant が存在しない要素を指したまま残り、読み上げが嘘をつく
|
|
423
|
+
// open() が開ける条件を見るのは入口だけ。開いたまま境界が狭まって選択肢が 0 になるとポップアップは消えるのに
|
|
424
|
+
// aria-expanded="true" が残り、disabled になると無効な操作要素の下でポップアップが操作できるまま残る
|
|
397
425
|
useEffect(() => {
|
|
398
426
|
if (isOpen && (disabled || count === 0)) close()
|
|
399
427
|
}, [isOpen, disabled, count, close])
|
|
@@ -425,8 +453,18 @@ export const VirtualDateDropdown: React.FC<VirtualDateDropdownProps> = ({ domain
|
|
|
425
453
|
// 閉包の確定経路を呼ぶと、行を描いた後に所有側が渡し直したコールバックを読み落とす
|
|
426
454
|
const commitRef = useCommittedRef(commit)
|
|
427
455
|
|
|
456
|
+
/**
|
|
457
|
+
* Handles a key pressed on the combobox control; does nothing while the control is disabled or while its owner hides it (the control's ref is detached).
|
|
458
|
+
* 操作要素で押されたキーの処理。無効な間と、所有側が隠して操作要素の ref が外れている間は何もしない関数。
|
|
459
|
+
*
|
|
460
|
+
* @param event - Key event React dispatched to the control / React が操作要素へ配った打鍵の事象
|
|
461
|
+
*/
|
|
428
462
|
const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>): void => {
|
|
429
|
-
|
|
463
|
+
// 所有側 (Activity・Suspense) が隠している間、React は操作要素の ref を外している。ブラウザが隠れた要素からフォーカスを外すのは
|
|
464
|
+
// 次の描画更新のときで、それまでの入力は隠れた操作要素へ届く。受けると隠れた木で一覧を開き直し (Activity では配置を片付ける
|
|
465
|
+
// 副作用が走らず、隠す前の配置でポップアップが画面に出る)、Enter で onChange を呼び、表示し直すと開いたまま現れる。
|
|
466
|
+
// ref は確定のレイアウトの段階で付くため、画面に在る操作要素への入力は捨てない
|
|
467
|
+
if (disabled || controlRef.current === null) return
|
|
430
468
|
// 集合の外の保持値でも先頭行から始めない。開く位置は保持値の近さで決まる
|
|
431
469
|
const anchor = activeIndex !== NO_OPTION_INDEX ? activeIndex : selectedIndex !== NO_OPTION_INDEX ? selectedIndex : openAnchorIndex
|
|
432
470
|
|
|
@@ -510,16 +548,29 @@ export const VirtualDateDropdown: React.FC<VirtualDateDropdownProps> = ({ domain
|
|
|
510
548
|
const getItem = useCallback((index: number) => index, [])
|
|
511
549
|
const getItemHeight = useCallback(() => DATE_OPTION_ROW_HEIGHT, [])
|
|
512
550
|
|
|
513
|
-
// aria-activedescendant
|
|
514
|
-
//
|
|
515
|
-
//
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
551
|
+
// aria-activedescendant は DOM に在る行しか指せない (WAI-ARIA)。どの行が DOM に在るかはスクローラが自分の状態で決め、描画範囲の通知は
|
|
552
|
+
// 確定より後のアニメーションフレームで届くため、通知から導くと窓を動かした確定が消えた行を指し、戻した確定が名指しを欠く。そこで属性は
|
|
553
|
+
// Props として描かず、アクティブ行の ref が行を付けた確定で書き、行を外す確定で返した後始末が消す。後始末は付けた時点の操作要素を閉包に持つ。
|
|
554
|
+
// 隠れた Activity・Suspense が退避するときは操作要素の ref が行より先に外れ、外す時点で ref を読むと消し損ねて、隠すと閉じる
|
|
555
|
+
// ポップアップの消えた行を表示後も指し続ける。外す処理 (変更の段階) は付ける処理 (レイアウトの段階) より先に走り、消すのは自分が書いた値の
|
|
556
|
+
// ときだけなので、次のアクティブ行の名指しは消さない
|
|
557
|
+
const activeOptionId = activeIndex === NO_OPTION_INDEX ? null : optionId(activeIndex)
|
|
558
|
+
/**
|
|
559
|
+
* Ref of the active row: names it on the combobox control while the row is attached.
|
|
560
|
+
* アクティブ行の ref。行が付いている間だけ操作要素からその行を名指す関数。
|
|
561
|
+
*
|
|
562
|
+
* @returns Cleanup React runs when the row detaches (deleted, or hidden by Activity / Suspense); removes the name only while it is still this row's / 行が外れるとき (削除、または Activity / Suspense による退避) に React が呼ぶ後始末。名指しがまだこの行のときだけ消す
|
|
563
|
+
* @throws Error when the row attaches without the control or an active option id / 操作要素かアクティブ添字が無いまま行が付いた場合
|
|
564
|
+
*/
|
|
565
|
+
const nameActiveRow = useCallback((): (() => void) => {
|
|
566
|
+
const control = controlRef.current
|
|
567
|
+
// この ref はアクティブ行にだけ付き、操作要素はポータルより前の兄弟として先に付く。満たされないのは不変条件の破れ
|
|
568
|
+
if (control === null || activeOptionId === null) throw new Error("VirtualDateDropdown: the active row attached without its combobox control or an active option id, so aria-activedescendant has nothing to name it on")
|
|
569
|
+
control.setAttribute("aria-activedescendant", activeOptionId)
|
|
570
|
+
return () => {
|
|
571
|
+
if (control.getAttribute("aria-activedescendant") === activeOptionId) control.removeAttribute("aria-activedescendant")
|
|
572
|
+
}
|
|
573
|
+
}, [activeOptionId])
|
|
523
574
|
|
|
524
575
|
// 行は描画の値を閉包から読む。スクローラが自分の状態だけで行を描き足すときに呼ぶのは確定した描画の関数であり、
|
|
525
576
|
// 描き足した行も aria-setsize や他の行と同じ領域を読む。依存配列は行の見た目を決める値に限り、領域は所有側が
|
|
@@ -539,6 +590,7 @@ export const VirtualDateDropdown: React.FC<VirtualDateDropdownProps> = ({ domain
|
|
|
539
590
|
aria-setsize={domain.count}
|
|
540
591
|
data-testid="period-slider-date-option"
|
|
541
592
|
data-active={isActive ? "true" : undefined}
|
|
593
|
+
ref={isActive ? nameActiveRow : undefined}
|
|
542
594
|
className="period-slider-date-option"
|
|
543
595
|
style={{ height: DATE_OPTION_ROW_HEIGHT }}
|
|
544
596
|
onClick={() => commitRef.current(index)}>
|
|
@@ -546,69 +598,88 @@ export const VirtualDateDropdown: React.FC<VirtualDateDropdownProps> = ({ domain
|
|
|
546
598
|
</div>
|
|
547
599
|
)
|
|
548
600
|
},
|
|
549
|
-
[domain, selectedIndex, activeIndex, optionId, commitRef],
|
|
601
|
+
[domain, selectedIndex, activeIndex, optionId, commitRef, nameActiveRow],
|
|
550
602
|
)
|
|
551
603
|
|
|
604
|
+
/**
|
|
605
|
+
* Ref of the popup root: keeps the root off screen while it is detached, the way React hides the control.
|
|
606
|
+
* ポップアップの根の ref。根が外れている間、React が操作要素を隠すのと同じ方法で根を画面から外す関数。
|
|
607
|
+
*
|
|
608
|
+
* @param popup - Popup root React attaches / React が付けるポップアップの根
|
|
609
|
+
* @returns Cleanup React runs when the root detaches (deleted, or hidden by Activity / Suspense); hides it with inline `display: none !important` / 根が外れるとき (削除、または Activity / Suspense による退避) に React が呼ぶ後始末。インラインの `display: none !important` で隠す
|
|
610
|
+
*/
|
|
611
|
+
const attachPopup = useCallback((popup: HTMLDivElement): (() => void) => {
|
|
612
|
+
popupRef.current = popup
|
|
613
|
+
// StrictMode の付け外しで、外した時の非表示を戻す。表示し直す前に必ず閉じるため、表示し直しで付け直すことは無い。ポップアップの style Props は display を持たない
|
|
614
|
+
popup.style.removeProperty("display")
|
|
615
|
+
return () => {
|
|
616
|
+
popupRef.current = null
|
|
617
|
+
// React が隠すのは境界の中の最上位のホスト要素だけで、その下のポータルは辿らない。閉じる更新は隠れた木の中で後から描かれ
|
|
618
|
+
// (Suspense では表示し直す確定まで描かれない)、それまでの確定でポップアップが操作要素なしに画面へ残らないよう、同じ指定で隠す
|
|
619
|
+
popup.style.setProperty("display", "none", "important")
|
|
620
|
+
}
|
|
621
|
+
}, [])
|
|
622
|
+
|
|
552
623
|
// 配置と配色モードはどちらも操作要素の実測から来る。片方だけで描くと、最初のフレームだけ
|
|
553
|
-
//
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
624
|
+
// 明色のポップアップが暗色のページに載る。listbox を描く条件はこの 1 つで、aria-controls も同じ条件で名乗る。
|
|
625
|
+
// 開いている (isOpen) だけで名乗ると、配置の実測前の最初の確定と、件数 0 で閉じる前の確定で、存在しない listbox を指す
|
|
626
|
+
const isPopupShown = isOpen && geometry !== null && colorMode !== null && count > 0
|
|
627
|
+
const popup = isPopupShown ? (
|
|
628
|
+
// biome-ignore lint/a11y/noStaticElementInteractions: ポップアップの外枠が拾うのは「フォーカスを奪わせない」ための mousedown 抑止だけで、操作機能は持たない。操作は内側の role="option" と操作要素が担う
|
|
629
|
+
<div
|
|
630
|
+
ref={attachPopup}
|
|
631
|
+
className="period-slider-date-popup"
|
|
632
|
+
data-testid="period-slider-date-popup"
|
|
633
|
+
data-color-mode={colorMode}
|
|
634
|
+
style={{ top: geometry.top, left: geometry.left, width: geometry.width, height: geometry.height }}
|
|
635
|
+
onMouseDown={(event) => {
|
|
636
|
+
// 行や余白を掴んでも操作要素からフォーカスを奪わない (コンボボックスは DOM
|
|
637
|
+
// フォーカスを操作要素に保持し続ける契約)。pointer 系は抑止しないため、
|
|
638
|
+
// スクロールバーのドラッグは妨げない
|
|
639
|
+
event.preventDefault()
|
|
640
|
+
}}>
|
|
641
|
+
<VirtualScroll
|
|
642
|
+
ref={scrollRef}
|
|
643
|
+
itemCount={count}
|
|
644
|
+
getItem={getItem}
|
|
645
|
+
getItemHeight={getItemHeight}
|
|
646
|
+
viewportSize={geometry.height - POPUP_BORDER_TOTAL}
|
|
647
|
+
initialScrollIndex={activeIndex !== NO_OPTION_INDEX ? activeIndex : undefined}
|
|
648
|
+
className="h-full w-full"
|
|
649
|
+
testId="period-slider-date-listbox-pane"
|
|
650
|
+
locale={locale}
|
|
651
|
+
labels={engineLabels}
|
|
652
|
+
contentProps={{
|
|
653
|
+
id: listboxId,
|
|
654
|
+
role: "listbox",
|
|
655
|
+
"aria-label": ariaLabel,
|
|
656
|
+
}}
|
|
657
|
+
scrollBarOptions={{
|
|
658
|
+
width: POPUP_SCROLLBAR_WIDTH,
|
|
659
|
+
// ポップアップとその子孫はページのタブ順から除外する (APG)。
|
|
660
|
+
// 未指定だと矢印ボタンが tabIndex=0 のままポップアップのタブ順へ露出する
|
|
661
|
+
enableArrowButtons: false,
|
|
662
|
+
enableThumbDrag: true,
|
|
663
|
+
enableTrackClick: true,
|
|
664
|
+
// 必ず false。スクローラの既定は有効で、直径 40px の円がポップアップの上へ重なる。
|
|
665
|
+
// 大きなスクロール面を親指で送るための部品であり、操作要素の幅に合わせた
|
|
666
|
+
// 1 列のリストでは行を覆うだけで、ネイティブのポップアップにも無い
|
|
667
|
+
tapScrollCircleOptions: { enabled: false },
|
|
668
|
+
}}
|
|
669
|
+
behaviorOptions={{
|
|
670
|
+
// 必ず false。true だと各行ラッパーに tabIndex=-1 が付いてポップアップが
|
|
671
|
+
// タブ順に入り、pointerdown で行へフォーカスが移って操作要素から奪う。
|
|
672
|
+
// キーボード操作は aria-activedescendant 方式で本コンポーネントが担う
|
|
673
|
+
enableKeyboardNavigation: false,
|
|
674
|
+
// 必ず "contain"。端に達してもホイールをページへ連鎖させない (ネイティブ
|
|
675
|
+
// <select> と同じ)。既定 "auto" では末尾の次のノッチでページ本体が動き、
|
|
676
|
+
// 固定配置のポップアップが操作要素から離れて見える
|
|
677
|
+
overscrollBehavior: "contain",
|
|
568
678
|
}}>
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
getItemHeight={getItemHeight}
|
|
574
|
-
viewportSize={geometry.height - POPUP_BORDER_TOTAL}
|
|
575
|
-
initialScrollIndex={activeIndex !== NO_OPTION_INDEX ? activeIndex : undefined}
|
|
576
|
-
onRangeChange={handleRangeChange}
|
|
577
|
-
className="h-full w-full"
|
|
578
|
-
testId="period-slider-date-listbox-pane"
|
|
579
|
-
locale={locale}
|
|
580
|
-
labels={engineLabels}
|
|
581
|
-
contentProps={{
|
|
582
|
-
id: listboxId,
|
|
583
|
-
role: "listbox",
|
|
584
|
-
"aria-label": ariaLabel,
|
|
585
|
-
}}
|
|
586
|
-
scrollBarOptions={{
|
|
587
|
-
width: POPUP_SCROLLBAR_WIDTH,
|
|
588
|
-
// ポップアップとその子孫はページのタブ順から除外する (APG)。
|
|
589
|
-
// 未指定だと矢印ボタンが tabIndex=0 のままポップアップのタブ順へ露出する
|
|
590
|
-
enableArrowButtons: false,
|
|
591
|
-
enableThumbDrag: true,
|
|
592
|
-
enableTrackClick: true,
|
|
593
|
-
// 必ず false。スクローラの既定は有効で、直径 40px の円がポップアップの上へ重なる。
|
|
594
|
-
// 大きなスクロール面を親指で送るための部品であり、操作要素の幅に合わせた
|
|
595
|
-
// 1 列のリストでは行を覆うだけで、ネイティブのポップアップにも無い
|
|
596
|
-
tapScrollCircleOptions: { enabled: false },
|
|
597
|
-
}}
|
|
598
|
-
behaviorOptions={{
|
|
599
|
-
// 必ず false。true だと各行ラッパーに tabIndex=-1 が付いてポップアップが
|
|
600
|
-
// タブ順に入り、pointerdown で行へフォーカスが移って操作要素から奪う。
|
|
601
|
-
// キーボード操作は aria-activedescendant 方式で本コンポーネントが担う
|
|
602
|
-
enableKeyboardNavigation: false,
|
|
603
|
-
// 必ず "contain"。端に達してもホイールをページへ連鎖させない (ネイティブ
|
|
604
|
-
// <select> と同じ)。既定 "auto" では末尾の次のノッチでページ本体が動き、
|
|
605
|
-
// 固定配置のポップアップが操作要素から離れて見える
|
|
606
|
-
overscrollBehavior: "contain",
|
|
607
|
-
}}>
|
|
608
|
-
{renderOption}
|
|
609
|
-
</VirtualScroll>
|
|
610
|
-
</div>
|
|
611
|
-
) : null
|
|
679
|
+
{renderOption}
|
|
680
|
+
</VirtualScroll>
|
|
681
|
+
</div>
|
|
682
|
+
) : null
|
|
612
683
|
|
|
613
684
|
return (
|
|
614
685
|
<>
|
|
@@ -619,10 +690,9 @@ export const VirtualDateDropdown: React.FC<VirtualDateDropdownProps> = ({ domain
|
|
|
619
690
|
readOnly
|
|
620
691
|
aria-label={ariaLabel}
|
|
621
692
|
aria-expanded={isOpen}
|
|
622
|
-
aria-controls={
|
|
693
|
+
aria-controls={isPopupShown ? listboxId : undefined}
|
|
623
694
|
aria-haspopup="listbox"
|
|
624
695
|
aria-autocomplete="none"
|
|
625
|
-
aria-activedescendant={isOpen && activeIndex !== NO_OPTION_INDEX && isActiveRendered ? optionId(activeIndex) : undefined}
|
|
626
696
|
disabled={disabled}
|
|
627
697
|
value={displayLabel}
|
|
628
698
|
className={controlClassName}
|
|
@@ -630,7 +700,8 @@ export const VirtualDateDropdown: React.FC<VirtualDateDropdownProps> = ({ domain
|
|
|
630
700
|
data-value={machineValue}
|
|
631
701
|
onKeyDown={handleKeyDown}
|
|
632
702
|
onMouseDown={(event) => {
|
|
633
|
-
|
|
703
|
+
// 隠れている間の押下を捨てる理由は handleKeyDown と同じ
|
|
704
|
+
if (disabled || controlRef.current === null) return
|
|
634
705
|
// 押下で開閉を切り替える (ネイティブ <select> と同じ)。既定動作を止めるのは
|
|
635
706
|
// 読み取り専用入力のテキスト選択を始めさせないため
|
|
636
707
|
event.preventDefault()
|
package/src/YearSelector.tsx
CHANGED
|
@@ -22,9 +22,9 @@ import type React from "react"
|
|
|
22
22
|
import { memo, useMemo } from "react"
|
|
23
23
|
import { assertCandidateListInput, describeValue, isValidDate } from "./inputValue"
|
|
24
24
|
import { areLabelOverridesEqual, type PeriodSliderLabelOverrides, type PeriodSliderLocale } from "./labels"
|
|
25
|
-
import { assertLocaleTag, encodeInsideDateRange, formatLabel, isYearStartMonth, type PeriodOptionFormatter, resolveYearStartMonth, sentinelBoundRemedy } from "./PeriodSlider"
|
|
25
|
+
import { assertLocaleTag, encodeInsideDateRange, formatLabel, isYearStartMonth, type PeriodOptionFormatter, resolveYearStartMonth, sentinelBoundRemedy, yearKeyOfCivil } from "./PeriodSlider"
|
|
26
26
|
import { STEPPER_ROW_GAP_CLASS, StepperButton } from "./StepperButton"
|
|
27
|
-
import { areSelectorDatePropsEqual } from "./selectorMemo"
|
|
27
|
+
import { areSelectorDatePropsEqual, selectorUnitKey } from "./selectorMemo"
|
|
28
28
|
import { assertSelectorStepper, type SelectorStepper } from "./selectorStep"
|
|
29
29
|
import { useResolvedLabels } from "./useResolvedLabels"
|
|
30
30
|
import { useUncontrolledSelection } from "./useUncontrolledSelection"
|
|
@@ -114,29 +114,22 @@ function yearStart(year: number, yearStartMonth: number): Date {
|
|
|
114
114
|
* Resolves the year a date belongs to when a year starts in the given calendar month.
|
|
115
115
|
* 年の開始月を指定したときに、日付が属する年を解決する関数。
|
|
116
116
|
*
|
|
117
|
-
* The rule is the package's single definition of a shifted year, shared with `normalizeDateByUnit`
|
|
118
|
-
* and `encode`: a year is named by the calendar year its first month falls in.
|
|
119
|
-
*
|
|
120
|
-
*
|
|
117
|
+
* The rule is the package's single definition of a shifted year (`yearKeyOfCivil`), shared with `normalizeDateByUnit`
|
|
118
|
+
* and `encode`: a year is named by the calendar year its first month falls in. The date is read as `normalizeDateByUnit`
|
|
119
|
+
* reads it, by its time value in the system time zone (a plain copy), so the drawn year is the year the validation
|
|
120
|
+
* (`encodeInsideDateRange`) and the memo key (`selectorUnitKey`) judge, a Date subclass whose getters report another zone included.
|
|
121
|
+
* ずらした年の定義はパッケージで 1 つだけ (`yearKeyOfCivil`。`normalizeDateByUnit` / `encode` と同じ規約)。
|
|
122
|
+
* 年は先頭月が属する暦年で名付ける。日付は `normalizeDateByUnit` と同じく時刻値の複製をシステムのタイムゾーンで読むため、描く年は
|
|
123
|
+
* 検証 (`encodeInsideDateRange`) とメモのキー (`selectorUnitKey`) が判定する年と一致する (ゲッターが別のタイムゾーンを名乗る Date の派生クラスも)。
|
|
121
124
|
*
|
|
122
|
-
* @param date - Date to classify / 判定対象の日付
|
|
125
|
+
* @param date - Date to classify, read by its time value / 判定対象の日付 (時刻値で読む)
|
|
123
126
|
* @param yearStartMonth - Calendar month a year starts in, 0-based / 1 年が始まる暦月 (0 始まり)
|
|
124
127
|
* @returns Year number, named by the calendar year its first month falls in / 先頭月が属する暦年で名付けた年番号
|
|
125
128
|
*/
|
|
126
129
|
function yearOf(date: Date, yearStartMonth: number): number {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Resolves the first day of the year a date belongs to when a year starts in the given calendar month.
|
|
132
|
-
* 年の開始月を指定したときに、日付が属する年の初日を解決する関数。
|
|
133
|
-
*
|
|
134
|
-
* @param date - Valid Date to classify / 判定対象の有効な日付
|
|
135
|
-
* @param yearStartMonth - Calendar month a year starts in, 0-based / 1 年が始まる暦月 (0 始まり)
|
|
136
|
-
* @returns First day of that year, or an Invalid Date when it starts outside the Date range / その年の初日 (始まりが表現範囲外なら Invalid Date)
|
|
137
|
-
*/
|
|
138
|
-
function yearStartOf(date: Date, yearStartMonth: number): Date {
|
|
139
|
-
return yearStart(yearOf(date, yearStartMonth), yearStartMonth)
|
|
130
|
+
// 派生クラスのゲッターは別のタイムゾーンの年月を名乗る。検証と同じく複製から読まないと、検証が受け付けた年と別の年を描く
|
|
131
|
+
const plain = new Date(date.getTime())
|
|
132
|
+
return yearKeyOfCivil(plain.getFullYear(), plain.getMonth(), yearStartMonth)
|
|
140
133
|
}
|
|
141
134
|
|
|
142
135
|
/**
|
|
@@ -353,11 +346,12 @@ const YearSelectorComponent = (props: YearSelectorProps): React.JSX.Element => {
|
|
|
353
346
|
const resolvedLabels = useResolvedLabels(locale, labels)
|
|
354
347
|
|
|
355
348
|
const isControlled = value !== undefined
|
|
356
|
-
// 年の区切りは yearStartMonth
|
|
349
|
+
// 年の区切りは yearStartMonth が決めるため、キーは年番号と開始月の対にする。区切りを変えた描画は、同じ initialDate で年番号が変わらなくても
|
|
350
|
+
// キーが変わり、その描画で取り込み直す
|
|
357
351
|
const [year, setYear] = useUncontrolledSelection(
|
|
358
352
|
effectiveDate,
|
|
359
353
|
isControlled,
|
|
360
|
-
(date) =>
|
|
354
|
+
(date) => selectorUnitKey(date, "year", yearStartMonth),
|
|
361
355
|
(date) => yearOf(date, yearStartMonth),
|
|
362
356
|
)
|
|
363
357
|
|
|
@@ -462,10 +456,13 @@ const YearSelectorComponent = (props: YearSelectorProps): React.JSX.Element => {
|
|
|
462
456
|
* Memo comparator of `YearSelector`: true only when every prop its output depends on is unchanged, never for a prop the render rejects.
|
|
463
457
|
* `YearSelector` のメモ比較器 (描画結果が依存する Props がすべて不変のときだけ true。描画が拒否する Props では常に false)。
|
|
464
458
|
*
|
|
465
|
-
* Date props are compared
|
|
466
|
-
*
|
|
467
|
-
*
|
|
468
|
-
*
|
|
459
|
+
* Date props are compared through `areSelectorDatePropsEqual` by `selectorUnitKey` at unit year under the render's yearStartMonth,
|
|
460
|
+
* the key the validation's own function computes, so an invalid one on a re-render reaches the render-time validation and throws the
|
|
461
|
+
* same named RangeError as on the first mount. The comparator itself never throws: yearStartMonth is compared first, and an invalid
|
|
462
|
+
* one never reaches the key.
|
|
463
|
+
* Date 型 Props は `areSelectorDatePropsEqual` で、今回の描画の yearStartMonth のもとの年の `selectorUnitKey` (検証と同じ関数が求めるキー) により比べるため、
|
|
464
|
+
* 再描画で渡した不正な値も描画時の検証へ届き、初回のマウントと同じく Props 名を名指す RangeError になる。比較器自身は例外を投げない
|
|
465
|
+
* (yearStartMonth を先に比べ、不正な値はキーへ届かない)。
|
|
469
466
|
*
|
|
470
467
|
* @param prev - Props of the previous render / 前回の描画の Props
|
|
471
468
|
* @param next - Props of the next render / 今回の描画の Props
|
|
@@ -476,7 +473,9 @@ export const areYearSelectorPropsEqual = (prev: YearSelectorProps, next: YearSel
|
|
|
476
473
|
// 制御値の変化が「等しい」と判定されて描画ごと捨てられる。比較器は本体と同じ年定義を読む
|
|
477
474
|
const startMonth = yearStartMonthMemoKey(next.yearStartMonth)
|
|
478
475
|
if (yearStartMonthMemoKey(prev.yearStartMonth) !== startMonth) return false
|
|
479
|
-
|
|
476
|
+
// keyInsideDateRange は不正な yearStartMonth で例外を投げる。上の比較は不正な開始月を NaN として必ず不一致にするため、
|
|
477
|
+
// ここへ届くのは有効な開始月だけで、比較器は例外を投げない
|
|
478
|
+
if (!areSelectorDatePropsEqual(prev, next, (date) => selectorUnitKey(date, "year", startMonth))) return false
|
|
480
479
|
if (prev.disabled !== next.disabled) return false
|
|
481
480
|
if (prev.className !== next.className) return false
|
|
482
481
|
if (prev.style !== next.style) return false
|
package/src/optionText.ts
CHANGED
|
@@ -83,16 +83,18 @@ const getPartFormat = (part: PeriodSliderUnit, formatLocale: string): Intl.DateT
|
|
|
83
83
|
* (自然言語既定は汎用パッケージに 1 言語を焼き付け、Intl 出力は ICU 版で揺れるため)。
|
|
84
84
|
*
|
|
85
85
|
* @param part - Calendar part to render / 整形対象の暦要素
|
|
86
|
-
* @param anchor - Date carrying the part value, and the calendar context Intl needs / 対象値を保持し Intl が必要とする暦文脈も与える日付
|
|
86
|
+
* @param anchor - Date carrying the part value, and the calendar context Intl needs; both tiers read it by its time value in the system time zone / 対象値を保持し Intl が必要とする暦文脈も与える日付 (どちらの段も時刻値をシステムのタイムゾーンで読む)
|
|
87
87
|
* @param formatLocale - BCP 47 language tag enabling Intl formatting / Intl 書式を有効化する BCP 47 言語タグ
|
|
88
88
|
* @returns Zero-padded digits, or the Intl rendering when a format locale is supplied / 0 埋め数字 (formatLocale 指定時は Intl 書式)
|
|
89
89
|
*/
|
|
90
90
|
const formatDatePart = (part: PeriodSliderUnit, anchor: Date, formatLocale: string | undefined): string => {
|
|
91
91
|
if (formatLocale !== undefined) return getPartFormat(part, formatLocale).format(anchor)
|
|
92
|
+
// Intl の段は時刻値をシステムのタイムゾーンで読む。派生クラスのゲッターを読むと、formatLocale の有無で別の部位を名乗る
|
|
93
|
+
const plain = new Date(anchor.getTime())
|
|
92
94
|
// 年だけは 0 埋め桁数が固定ではない (0 年以下は ISO 8601 の拡張表記)。ここで桁数を書き直すと
|
|
93
95
|
// 同じ 1 日がウィジェットの中で 2 通りの ISO 表記を持つ
|
|
94
|
-
if (part === "year") return formatIsoYear(PART_READERS.year(
|
|
95
|
-
return String(PART_READERS[part](
|
|
96
|
+
if (part === "year") return formatIsoYear(PART_READERS.year(plain))
|
|
97
|
+
return String(PART_READERS[part](plain)).padStart(ISO_DIGITS_BY_PART[part], "0")
|
|
96
98
|
}
|
|
97
99
|
|
|
98
100
|
/**
|
|
@@ -107,7 +109,7 @@ const formatDatePart = (part: PeriodSliderUnit, anchor: Date, formatLocale: stri
|
|
|
107
109
|
* a public entry point whose arguments can arrive from JavaScript, where no union is enforced.
|
|
108
110
|
* フォーマッタ Props は型検査を受けるが、本関数は公開の入口であり JavaScript からも引数が届くため検証する。
|
|
109
111
|
*
|
|
110
|
-
* @param date - Date to render / 描画対象の日付
|
|
112
|
+
* @param date - Date to render; both built-in tiers read it by its time value in the system time zone / 描画対象の日付 (組み込みのどちらの段も時刻値をシステムのタイムゾーンで読む)
|
|
111
113
|
* @param unit - Precision to name / 名乗る精度
|
|
112
114
|
* @param scope - Scope selecting the built-in option set / 組み込みオプション集合を選ぶ区分
|
|
113
115
|
* @param formatLocale - BCP 47 language tag enabling Intl formatting (the `formatLocale` prop, not the UI chrome `locale`) / Intl 書式を有効化する BCP 47 言語タグ (UI クロームの `locale` ではなく `formatLocale` prop)
|