@aiquants/period-slider 0.9.0 → 0.10.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.
@@ -7,18 +7,21 @@
7
7
  *
8
8
  * Human-facing option text is locale-independent ISO 8601 until a `locale` tag opts into Intl
9
9
  * formatting or a per-part formatter overrides it, while every machine-read value stays yyyy-MM-dd.
10
+ * The previous / next buttons are drawn by `StepperButton`.
10
11
  *
11
12
  * 年月日セレクタコンポーネントの提供モジュール。
12
13
  * ネイティブ `input[type="date"]`、yyyy-MM-dd の単一セレクトボックス、
13
14
  * 年・月・日の 3 分割セレクトのいずれかを、前日/翌日ステッパーで挟んで描画。
14
15
  * 表示テキストは既定でロケール非依存の ISO 8601 表記 (`locale` 指定時は Intl 書式、
15
16
  * 部位別フォーマッタ指定時はそれが最優先)、機械可読値は常に yyyy-MM-dd。
17
+ * 前日/翌日ボタンは `StepperButton` が描く。
16
18
  */
17
19
 
18
20
  import { addDays, format, getDaysInMonth, parseISO } from "date-fns"
19
21
  import type React from "react"
20
22
  import { memo, useEffect, useMemo, useState } from "react"
21
23
  import { formatLabel } from "./PeriodSlider"
24
+ import { STEPPER_ROW_GAP_CLASS, StepperButton } from "./StepperButton"
22
25
 
23
26
  /**
24
27
  * Props of the day-resolution selector.
@@ -32,9 +35,9 @@ import { formatLabel } from "./PeriodSlider"
32
35
  * 部位別フォーマッタを通じた利用側の選択。
33
36
  */
34
37
  export interface DateSelectorProps {
35
- /** Controlled selected date; required unless `initialDate` is supplied; must be a valid Date / 選択日付(外部制御用・`initialDate` 未指定時は必須・Invalid Date 不可) */
38
+ /** Controlled selected date; required unless `initialDate` is supplied; must be a valid Date / 選択日付 (外部制御用・`initialDate` 未指定時は必須・Invalid Date 不可) */
36
39
  value?: Date
37
- /** Initial selected date; required unless `value` is supplied; must be a valid Date / 初期選択日付(非制御用・`value` 未指定時は必須・Invalid Date 不可) */
40
+ /** Initial selected date; required unless `value` is supplied; must be a valid Date / 初期選択日付 (非制御用・`value` 未指定時は必須・Invalid Date 不可) */
38
41
  initialDate?: Date
39
42
  /** Callback triggered when a selectable date is chosen / 選択可能な日付が選ばれた時のコールバック */
40
43
  onChange?(date: Date): void
@@ -310,7 +313,7 @@ const toDescendingOptions = (datesAscending: Date[], locale: string | undefined)
310
313
  * Every control the component presents as enabled is emittable: a stepper whose target date falls
311
314
  * outside the selectable set is disabled, and so is a `variant="split-dropdown"` year or month
312
315
  * whose combination with the other two parts would be discarded.
313
- * 日付(年月日)セレクタ共通コンポーネント。
316
+ * 日付 (年月日) セレクタ共通コンポーネント。
314
317
  * ネイティブ日付入力・単一セレクトボックス・3 分割セレクトのいずれかと、両脇のステッパーボタン (◀▶) を描画。
315
318
  * 選択可能集合は `allowedDates` (離散) または [minDate/minYear, maxDate/maxYear] (連続) が決定し、集合外の日付は emit されない。
316
319
  * 発行できない日へ進むステッパーは無効化し、`variant="split-dropdown"` では他の 2 部位との組合せが
@@ -464,9 +467,6 @@ const DateSelectorComponent = (props: DateSelectorProps): React.JSX.Element => {
464
467
  return isSelectable(candidate) ? candidate : null
465
468
  }, [selectableDates, activeTime, minBoundTime, maxBoundTime])
466
469
 
467
- const isPrevDisabled = disabled || prevDate === null
468
- const isNextDisabled = disabled || nextDate === null
469
-
470
470
  const handlePrevious = () => {
471
471
  if (prevDate !== null) emit(prevDate)
472
472
  }
@@ -578,21 +578,12 @@ const DateSelectorComponent = (props: DateSelectorProps): React.JSX.Element => {
578
578
  const selectValue = isActiveSelectable ? formattedDate : ""
579
579
  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"
580
580
 
581
- const containerWidthClass = variant === "split-dropdown" ? "w-auto" : "w-38"
582
-
583
581
  const singleSelectClassName =
584
- "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"
582
+ "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"
585
583
 
586
584
  return (
587
- <div className={`inline-flex items-center ${containerWidthClass} ${className}`} style={style} data-variant={variant} data-out-of-range={isActiveSelectable ? undefined : "true"}>
588
- <button
589
- type="button"
590
- aria-label={prevDateAriaLabel}
591
- disabled={isPrevDisabled}
592
- className="relative my-auto inline-flex h-5 w-4 cursor-pointer items-center justify-center rounded p-0 after:absolute after:inset-[-14px] after:content-[''] focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-600 disabled:opacity-40"
593
- onClick={handlePrevious}>
594
- <span className="h-0 w-0 border-y-[6px] border-y-transparent border-r-[6px] border-r-slate-500 border-solid hover:border-r-slate-700 dark:border-r-slate-400 dark:hover:border-r-slate-200" />
595
- </button>
585
+ <div className={`inline-flex items-center ${STEPPER_ROW_GAP_CLASS} ${className}`} style={style} data-variant={variant} data-out-of-range={isActiveSelectable ? undefined : "true"}>
586
+ <StepperButton neighbour="control" direction="left" disabled={disabled} canStep={prevDate !== null} onStep={handlePrevious} ariaLabel={prevDateAriaLabel} />
596
587
 
597
588
  {variant === "dropdown" ? (
598
589
  <select aria-label={selectAriaLabel} disabled={disabled} className={singleSelectClassName} value={selectValue} onChange={handleDropdownChange}>
@@ -608,7 +599,7 @@ const DateSelectorComponent = (props: DateSelectorProps): React.JSX.Element => {
608
599
  ))}
609
600
  </select>
610
601
  ) : variant === "split-dropdown" ? (
611
- <div className="mx-1 my-auto inline-flex items-center gap-1">
602
+ <div className="inline-flex items-center gap-1">
612
603
  <select
613
604
  aria-label={yearAriaLabel}
614
605
  disabled={disabled}
@@ -665,7 +656,7 @@ const DateSelectorComponent = (props: DateSelectorProps): React.JSX.Element => {
665
656
  <input
666
657
  aria-label={selectAriaLabel}
667
658
  disabled={disabled}
668
- className="period-slider-native-date-field mx-1 my-auto w-28 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 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 dark:placeholder:text-gray-400"
659
+ className="period-slider-native-date-field w-28 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 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 dark:placeholder:text-gray-400"
669
660
  value={formattedDate}
670
661
  min={minFormatted}
671
662
  max={maxFormatted}
@@ -674,14 +665,7 @@ const DateSelectorComponent = (props: DateSelectorProps): React.JSX.Element => {
674
665
  />
675
666
  )}
676
667
 
677
- <button
678
- type="button"
679
- aria-label={nextDateAriaLabel}
680
- disabled={isNextDisabled}
681
- className="relative my-auto inline-flex h-5 w-4 cursor-pointer items-center justify-center rounded p-0 after:absolute after:inset-[-14px] after:content-[''] focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-600 disabled:opacity-40"
682
- onClick={handleNext}>
683
- <span className="h-0 w-0 border-y-[6px] border-y-transparent border-l-[6px] border-l-slate-500 border-solid hover:border-l-slate-700 dark:border-l-slate-400 dark:hover:border-l-slate-200" />
684
- </button>
668
+ <StepperButton neighbour="control" direction="right" disabled={disabled} canStep={nextDate !== null} onStep={handleNext} ariaLabel={nextDateAriaLabel} />
685
669
  {children}
686
670
  </div>
687
671
  )
@@ -8,18 +8,19 @@
8
8
  * date bounds, or the discrete `allowedMonths` intersected with those bounds; missing and invalid
9
9
  * inputs fail fast rather than being substituted, while a month outside the set is neither clamped
10
10
  * nor emitted. Option text is locale-independent ISO 8601 until a `locale` tag opts into Intl
11
- * formatting, while every machine-read value stays yyyy-MM.
11
+ * formatting, while every machine-read value stays yyyy-MM. The ◀▶ buttons are drawn by `StepperButton`.
12
12
  *
13
13
  * ネイティブ `input[type="month"]` または yyyy-MM ドロップダウンと、隣接する選択可能月へ移動する
14
14
  * ステッパーによる年月セレクタ。選択可能集合は年指定と日付指定の厳しい方で挟んだ全月、または
15
15
  * その境界と積を取った離散集合 `allowedMonths` のいずれか。欠落値・不正値は代替せず即時エラー、
16
16
  * 集合外の月はクランプも発行もせず破棄。選択肢テキストは既定でロケール非依存の ISO 8601 表記
17
- * (`locale` 指定時のみ Intl 書式)、機械可読値は常に yyyy-MM。
17
+ * (`locale` 指定時のみ Intl 書式)、機械可読値は常に yyyy-MM。◀▶ は `StepperButton` が描く。
18
18
  */
19
19
 
20
20
  import type React from "react"
21
21
  import { memo, useEffect, useMemo, useState } from "react"
22
22
  import { formatLabel } from "./PeriodSlider"
23
+ import { STEPPER_ROW_GAP_CLASS, StepperButton } from "./StepperButton"
23
24
 
24
25
  /**
25
26
  * Props of the month-resolution selector.
@@ -33,9 +34,9 @@ import { formatLabel } from "./PeriodSlider"
33
34
  * 通じた利用側の選択。
34
35
  */
35
36
  export interface MonthSelectorProps {
36
- /** Controlled selected date; required unless `initialDate` is supplied / 選択日付(外部制御用・`initialDate` 未指定時は必須) */
37
+ /** Controlled selected date; required unless `initialDate` is supplied / 選択日付 (外部制御用・`initialDate` 未指定時は必須) */
37
38
  value?: Date
38
- /** Initial selected date; required unless `value` is supplied / 選択日付(非制御用・`value` 未指定時は必須) */
39
+ /** Initial selected date; required unless `value` is supplied / 選択日付 (非制御用・`value` 未指定時は必須) */
39
40
  initialDate?: Date
40
41
  /** Callback triggered when a selectable month is chosen / 選択可能な月が選ばれた時のコールバック */
41
42
  onChange?(date: Date, yyyymm: number): void
@@ -375,9 +376,6 @@ const MonthSelectorComponent = (props: MonthSelectorProps): React.JSX.Element =>
375
376
  return isSelectable(candidate) ? candidate : null
376
377
  }, [selectableMonths, activeTime, minBoundTime, maxBoundTime])
377
378
 
378
- const isPrevDisabled = disabled || prevMonthDate === null
379
- const isNextDisabled = disabled || nextMonthDate === null
380
-
381
379
  const handlePrevious = () => {
382
380
  if (prevMonthDate !== null) emit(prevMonthDate)
383
381
  }
@@ -431,20 +429,13 @@ const MonthSelectorComponent = (props: MonthSelectorProps): React.JSX.Element =>
431
429
  // ネイティブ入力の既定書体はプロポーショナルかつ左寄せで、同じ値を示すドロップダウンと並ぶと別物に見える。
432
430
  // `appearance-none` だけは与えない (`<select>` では矢印を消すためのものであり、日付入力ではカレンダー起動子まで消えかねない)。
433
431
  // text-center は preflight 下の編集領域には届かないため、中央寄せは period-slider-native-date-field が担う
434
- const inputClassName = `period-slider-native-date-field mx-1 my-auto w-28 rounded-md border py-0.5 px-1 text-center font-mono text-xs tabular-nums dark:placeholder:text-gray-400 disabled:opacity-50 ${warningClassName}`
432
+ const inputClassName = `period-slider-native-date-field w-28 rounded-md border py-0.5 px-1 text-center font-mono text-xs tabular-nums dark:placeholder:text-gray-400 disabled:opacity-50 ${warningClassName}`
435
433
 
436
- const selectClassName = `mx-1 my-auto w-28 appearance-none rounded-md border px-1 py-0.5 text-center font-mono text-xs tabular-nums [text-align-last:center] [text-align:center] disabled:opacity-50 ${warningClassName}`
434
+ const selectClassName = `w-28 appearance-none rounded-md border px-1 py-0.5 text-center font-mono text-xs tabular-nums [text-align-last:center] [text-align:center] disabled:opacity-50 ${warningClassName}`
437
435
 
438
436
  return (
439
- <div className={`inline-flex w-38 items-center ${className}`} style={style} data-out-of-range={isActiveSelectable ? undefined : "true"}>
440
- <button
441
- type="button"
442
- aria-label={prevMonthAriaLabel}
443
- disabled={isPrevDisabled}
444
- className="relative my-auto inline-flex h-5 w-4 cursor-pointer items-center justify-center rounded p-0 after:absolute after:inset-[-14px] after:content-[''] focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-600 disabled:opacity-40"
445
- onClick={handlePrevious}>
446
- <span className="h-0 w-0 border-y-[6px] border-y-transparent border-r-[6px] border-r-slate-500 border-solid hover:border-r-slate-700 dark:border-r-slate-400 dark:hover:border-r-slate-200" />
447
- </button>
437
+ <div className={`inline-flex items-center ${STEPPER_ROW_GAP_CLASS} ${className}`} style={style} data-out-of-range={isActiveSelectable ? undefined : "true"}>
438
+ <StepperButton neighbour="control" direction="left" disabled={disabled} canStep={prevMonthDate !== null} onStep={handlePrevious} ariaLabel={prevMonthAriaLabel} />
448
439
  {variant === "dropdown" ? (
449
440
  <select aria-label={selectAriaLabel} disabled={disabled} className={selectClassName} value={selectValue} onChange={handleSelectChange}>
450
441
  {isActiveSelectable ? null : (
@@ -461,14 +452,7 @@ const MonthSelectorComponent = (props: MonthSelectorProps): React.JSX.Element =>
461
452
  ) : (
462
453
  <input aria-label={selectAriaLabel} disabled={disabled} className={inputClassName} value={formattedValue} min={minFormatted} max={maxFormatted} onChange={handleInputChange} type="month" />
463
454
  )}
464
- <button
465
- type="button"
466
- aria-label={nextMonthAriaLabel}
467
- disabled={isNextDisabled}
468
- className="relative my-auto inline-flex h-5 w-4 cursor-pointer items-center justify-center rounded p-0 after:absolute after:inset-[-14px] after:content-[''] focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-600 disabled:opacity-40"
469
- onClick={handleNext}>
470
- <span className="h-0 w-0 border-y-[6px] border-y-transparent border-l-[6px] border-l-slate-500 border-solid hover:border-l-slate-700 dark:border-l-slate-400 dark:hover:border-l-slate-200" />
471
- </button>
455
+ <StepperButton neighbour="control" direction="right" disabled={disabled} canStep={nextMonthDate !== null} onStep={handleNext} ariaLabel={nextMonthAriaLabel} />
472
456
  {children}
473
457
  </div>
474
458
  )