@aiquants/period-slider 0.2.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/LICENSE +21 -0
- package/README.md +389 -0
- package/dist/DateSelector.d.ts +45 -0
- package/dist/DateSelector.d.ts.map +1 -0
- package/dist/MonthSelector.d.ts +43 -0
- package/dist/MonthSelector.d.ts.map +1 -0
- package/dist/PeriodSlider.d.ts +384 -0
- package/dist/PeriodSlider.d.ts.map +1 -0
- package/dist/YearSelector.d.ts +36 -0
- package/dist/YearSelector.d.ts.map +1 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1770 -0
- package/dist/period-slider.css +1 -0
- package/dist/styles/period-slider.css +2 -0
- package/dist/styles/period-slider.standalone.css +3 -0
- package/dist/usePeriodSlider.d.ts +213 -0
- package/dist/usePeriodSlider.d.ts.map +1 -0
- package/package.json +98 -0
- package/src/DateSelector.tsx +414 -0
- package/src/MonthSelector.tsx +303 -0
- package/src/PeriodSlider.tsx +2557 -0
- package/src/YearSelector.tsx +168 -0
- package/src/index.ts +67 -0
- package/src/styles/components.entry.css +10 -0
- package/src/styles/period-slider.css +910 -0
- package/src/styles/standalone.entry.css +10 -0
- package/src/usePeriodSlider.ts +692 -0
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import type React from "react"
|
|
2
|
+
import { memo, useEffect, useMemo, useState } from "react"
|
|
3
|
+
import type { PeriodSliderUnit } from "./PeriodSlider"
|
|
4
|
+
|
|
5
|
+
export interface MonthSelectorProps {
|
|
6
|
+
/** Controlled selected date / 選択日付(外部制御用) */
|
|
7
|
+
value?: Date
|
|
8
|
+
/** Initial selected date (defaults to current month) / 選択日付(未指定時は現在月) */
|
|
9
|
+
initialDate?: Date
|
|
10
|
+
/** Time resolution unit (ignored by selector, accepted for hook interoperability) / 時間解像度単位 (フック相互運用用) */
|
|
11
|
+
unit?: PeriodSliderUnit
|
|
12
|
+
/** Callback triggered when month changes / 月変更時コールバック */
|
|
13
|
+
onChange?(date: Date, yyyymm: number): void
|
|
14
|
+
/** Child elements / 子要素 */
|
|
15
|
+
children?: React.ReactNode
|
|
16
|
+
/**
|
|
17
|
+
* UI variant: "date" (input[type="month"]) or "dropdown" (select[yyyy-mm]). Defaults to "date".
|
|
18
|
+
* 表示バリエーション: "date" (input[type="month"]) または "dropdown" (select[yyyy-mm])。既定値 "date"
|
|
19
|
+
*/
|
|
20
|
+
variant?: "date" | "dropdown"
|
|
21
|
+
/** Minimum year for options / 選択肢・可能範囲の最小年 */
|
|
22
|
+
minYear?: number
|
|
23
|
+
/** Maximum year for options / 選択肢・可能範囲の最大年 */
|
|
24
|
+
maxYear?: number
|
|
25
|
+
/** Minimum date for options / 選択肢・可能範囲の最小日付 */
|
|
26
|
+
minDate?: Date
|
|
27
|
+
/** Maximum date for options / 選択肢・可能範囲の最大日付 */
|
|
28
|
+
maxDate?: Date
|
|
29
|
+
/** Warning style flag / 警告枠スタイル適用フラグ */
|
|
30
|
+
isWarning?: boolean
|
|
31
|
+
/** Additional CSS class name / 追加の CSS クラス名 */
|
|
32
|
+
className?: string
|
|
33
|
+
/** Custom styles / スタイル */
|
|
34
|
+
style?: React.CSSProperties
|
|
35
|
+
/** Disabled flag / 無効化フラグ */
|
|
36
|
+
disabled?: boolean
|
|
37
|
+
/** Accessible label for previous month button / 前月ボタンのアクセシブル名 */
|
|
38
|
+
prevMonthAriaLabel?: string
|
|
39
|
+
/** Accessible label for next month button / 翌月ボタンのアクセシブル名 */
|
|
40
|
+
nextMonthAriaLabel?: string
|
|
41
|
+
/** Accessible label for month select combobox / 年月セレクトのアクセシブル名 */
|
|
42
|
+
selectAriaLabel?: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Generate yyyy-mm option list for dropdown mode.
|
|
47
|
+
* ドロップダウン表示用の yyyy-mm 選択肢リストを生成する。
|
|
48
|
+
*/
|
|
49
|
+
function generateDropdownOptions(currentDate: Date, minYear?: number, maxYear?: number, minDate?: Date, maxDate?: Date): { label: string; value: string }[] {
|
|
50
|
+
const currentY = currentDate.getFullYear()
|
|
51
|
+
const lowerY = minYear ?? (minDate ? minDate.getFullYear() : currentY - 5)
|
|
52
|
+
const upperY = maxYear ?? (maxDate ? maxDate.getFullYear() : currentY + 5)
|
|
53
|
+
|
|
54
|
+
const startY = Math.min(lowerY, currentY)
|
|
55
|
+
const endY = Math.max(upperY, currentY)
|
|
56
|
+
|
|
57
|
+
const minTime = minDate ? new Date(minDate.getFullYear(), minDate.getMonth(), 1).getTime() : null
|
|
58
|
+
const maxTime = maxDate ? new Date(maxDate.getFullYear(), maxDate.getMonth(), 1).getTime() : null
|
|
59
|
+
|
|
60
|
+
const list: { label: string; value: string }[] = []
|
|
61
|
+
|
|
62
|
+
for (let y = endY; y >= startY; y--) {
|
|
63
|
+
for (let m = 12; m >= 1; m--) {
|
|
64
|
+
const d = new Date(y, m - 1, 1)
|
|
65
|
+
d.setFullYear(y)
|
|
66
|
+
const t = d.getTime()
|
|
67
|
+
|
|
68
|
+
if (minTime !== null && t < minTime) continue
|
|
69
|
+
if (maxTime !== null && t > maxTime) continue
|
|
70
|
+
|
|
71
|
+
const monthStr = String(m).padStart(2, "0")
|
|
72
|
+
const valStr = `${y}-${monthStr}`
|
|
73
|
+
list.push({ label: valStr, value: valStr })
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return list
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Month selector component supporting both input[type="month"] and dropdown (yyyy-mm) modes.
|
|
82
|
+
* 月セレクタ共通コンポーネント。
|
|
83
|
+
* `<input type="month">` または `<select>` ドロップダウンと両脇のステッパーボタン (◀▶) で ±1 ヶ月を変更可能。
|
|
84
|
+
*/
|
|
85
|
+
const MonthSelectorComponent = (props: MonthSelectorProps): React.JSX.Element => {
|
|
86
|
+
const { value, initialDate, onChange, children, variant = "date", minYear, maxYear, minDate, maxDate, isWarning = false, className = "", style, disabled = false, prevMonthAriaLabel = "前月", nextMonthAriaLabel = "翌月", selectAriaLabel = "年月" } = props
|
|
87
|
+
const effectiveDate = value ?? initialDate
|
|
88
|
+
|
|
89
|
+
const [currentDate, setCurrentDate] = useState<Date>(() => {
|
|
90
|
+
const base = effectiveDate ?? new Date()
|
|
91
|
+
const d = new Date(base.getFullYear(), base.getMonth(), 1)
|
|
92
|
+
d.setFullYear(base.getFullYear())
|
|
93
|
+
return d
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
const isControlled = value !== undefined
|
|
97
|
+
const activeDate = useMemo(() => {
|
|
98
|
+
if (isControlled) {
|
|
99
|
+
if (value && !Number.isNaN(value.getTime())) {
|
|
100
|
+
const d = new Date(value.getFullYear(), value.getMonth(), 1)
|
|
101
|
+
d.setFullYear(value.getFullYear())
|
|
102
|
+
return d
|
|
103
|
+
}
|
|
104
|
+
return new Date()
|
|
105
|
+
}
|
|
106
|
+
return currentDate
|
|
107
|
+
}, [isControlled, value?.getTime(), currentDate])
|
|
108
|
+
|
|
109
|
+
const minTime = minDate?.getTime()
|
|
110
|
+
const maxTime = maxDate?.getTime()
|
|
111
|
+
|
|
112
|
+
const minDateBound = useMemo(() => {
|
|
113
|
+
if (minDate) {
|
|
114
|
+
const d = new Date(minDate.getFullYear(), minDate.getMonth(), 1)
|
|
115
|
+
d.setFullYear(minDate.getFullYear())
|
|
116
|
+
return d
|
|
117
|
+
}
|
|
118
|
+
if (minYear !== undefined) {
|
|
119
|
+
const d = new Date(minYear, 0, 1)
|
|
120
|
+
d.setFullYear(minYear)
|
|
121
|
+
return d
|
|
122
|
+
}
|
|
123
|
+
return null
|
|
124
|
+
}, [minTime, minYear])
|
|
125
|
+
|
|
126
|
+
const maxDateBound = useMemo(() => {
|
|
127
|
+
if (maxDate) {
|
|
128
|
+
const d = new Date(maxDate.getFullYear(), maxDate.getMonth(), 1)
|
|
129
|
+
d.setFullYear(maxDate.getFullYear())
|
|
130
|
+
return d
|
|
131
|
+
}
|
|
132
|
+
if (maxYear !== undefined) {
|
|
133
|
+
const d = new Date(maxYear, 11, 1)
|
|
134
|
+
d.setFullYear(maxYear)
|
|
135
|
+
return d
|
|
136
|
+
}
|
|
137
|
+
return null
|
|
138
|
+
}, [maxTime, maxYear])
|
|
139
|
+
|
|
140
|
+
// 外部からの value または initialDate 変更に追従
|
|
141
|
+
const effectiveTime = effectiveDate?.getTime()
|
|
142
|
+
useEffect(() => {
|
|
143
|
+
if (!isControlled && effectiveDate && !Number.isNaN(effectiveDate.getTime())) {
|
|
144
|
+
const next = new Date(effectiveDate.getFullYear(), effectiveDate.getMonth(), 1)
|
|
145
|
+
next.setFullYear(effectiveDate.getFullYear())
|
|
146
|
+
setCurrentDate(next)
|
|
147
|
+
}
|
|
148
|
+
}, [isControlled, effectiveTime])
|
|
149
|
+
|
|
150
|
+
const emit = (nextDate: Date) => {
|
|
151
|
+
if (disabled) return
|
|
152
|
+
let clamped = nextDate
|
|
153
|
+
if (minDateBound && clamped < minDateBound) {
|
|
154
|
+
clamped = new Date(minDateBound)
|
|
155
|
+
}
|
|
156
|
+
if (maxDateBound && clamped > maxDateBound) {
|
|
157
|
+
clamped = new Date(maxDateBound)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (!isControlled) {
|
|
161
|
+
setCurrentDate(clamped)
|
|
162
|
+
}
|
|
163
|
+
const yyyymm = clamped.getFullYear() * 100 + (clamped.getMonth() + 1)
|
|
164
|
+
onChange?.(clamped, yyyymm)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const prevMonthDate = useMemo(() => {
|
|
168
|
+
const d = new Date(activeDate.getFullYear(), activeDate.getMonth() - 1, 1)
|
|
169
|
+
d.setFullYear(activeDate.getMonth() === 0 ? activeDate.getFullYear() - 1 : activeDate.getFullYear())
|
|
170
|
+
return d
|
|
171
|
+
}, [activeDate])
|
|
172
|
+
|
|
173
|
+
const nextMonthDate = useMemo(() => {
|
|
174
|
+
const d = new Date(activeDate.getFullYear(), activeDate.getMonth() + 1, 1)
|
|
175
|
+
d.setFullYear(activeDate.getMonth() === 11 ? activeDate.getFullYear() + 1 : activeDate.getFullYear())
|
|
176
|
+
return d
|
|
177
|
+
}, [activeDate])
|
|
178
|
+
|
|
179
|
+
const isPrevDisabled = disabled || (minDateBound !== null && prevMonthDate < minDateBound)
|
|
180
|
+
const isNextDisabled = disabled || (maxDateBound !== null && nextMonthDate > maxDateBound)
|
|
181
|
+
|
|
182
|
+
const handlePrevious = () => {
|
|
183
|
+
emit(prevMonthDate)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const handleNext = () => {
|
|
187
|
+
emit(nextMonthDate)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
191
|
+
const val = e.target.value
|
|
192
|
+
if (!val) return
|
|
193
|
+
const [yearStr, monthStr] = val.split("-")
|
|
194
|
+
const year = Number(yearStr)
|
|
195
|
+
const month = Number(monthStr) - 1
|
|
196
|
+
if (!(Number.isNaN(year) || Number.isNaN(month))) {
|
|
197
|
+
const next = new Date(year, month, 1)
|
|
198
|
+
next.setFullYear(year)
|
|
199
|
+
emit(next)
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const handleSelectChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
|
|
204
|
+
const val = e.target.value
|
|
205
|
+
if (!val) return
|
|
206
|
+
const [yearStr, monthStr] = val.split("-")
|
|
207
|
+
const year = Number(yearStr)
|
|
208
|
+
const month = Number(monthStr) - 1
|
|
209
|
+
if (!(Number.isNaN(year) || Number.isNaN(month))) {
|
|
210
|
+
const next = new Date(year, month, 1)
|
|
211
|
+
next.setFullYear(year)
|
|
212
|
+
emit(next)
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const formattedValue = `${activeDate.getFullYear()}-${String(activeDate.getMonth() + 1).padStart(2, "0")}`
|
|
217
|
+
|
|
218
|
+
const minFormatted = minDateBound ? `${minDateBound.getFullYear()}-${String(minDateBound.getMonth() + 1).padStart(2, "0")}` : undefined
|
|
219
|
+
|
|
220
|
+
const maxFormatted = maxDateBound ? `${maxDateBound.getFullYear()}-${String(maxDateBound.getMonth() + 1).padStart(2, "0")}` : undefined
|
|
221
|
+
|
|
222
|
+
const currentY = activeDate.getFullYear()
|
|
223
|
+
const lowerY = minYear ?? (minDate ? minDate.getFullYear() : currentY - 5)
|
|
224
|
+
const upperY = maxYear ?? (maxDate ? maxDate.getFullYear() : currentY + 5)
|
|
225
|
+
const startY = Math.min(lowerY, currentY)
|
|
226
|
+
const endY = Math.max(upperY, currentY)
|
|
227
|
+
|
|
228
|
+
const monthOptionsKey = `${startY}_${endY}_${minTime ?? "none"}_${maxTime ?? "none"}`
|
|
229
|
+
|
|
230
|
+
const dropdownOptions = useMemo(() => {
|
|
231
|
+
if (variant !== "dropdown") return []
|
|
232
|
+
return generateDropdownOptions(activeDate, minYear, maxYear, minDate, maxDate)
|
|
233
|
+
}, [variant, monthOptionsKey, activeDate])
|
|
234
|
+
|
|
235
|
+
const warningClassName = isWarning
|
|
236
|
+
? "border-amber-300 bg-amber-50 text-amber-900 focus:border-amber-500 focus:ring-amber-500 dark:border-amber-900/50 dark:bg-amber-950/20 dark:text-amber-200"
|
|
237
|
+
: "border-gray-300 bg-gray-100 text-gray-900 focus:border-blue-500 focus:ring-blue-500 dark:border-slate-600 dark:bg-slate-700 dark:text-slate-100 dark:focus:border-blue-500 dark:focus:ring-blue-500"
|
|
238
|
+
|
|
239
|
+
const inputClassName = `mx-1 my-auto w-28 rounded-md border py-0.5 px-1 text-xs dark:placeholder:text-gray-400 disabled:opacity-50 ${warningClassName}`
|
|
240
|
+
|
|
241
|
+
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] focus:border-blue-500 focus:ring-blue-500 disabled:opacity-50 ${warningClassName}`
|
|
242
|
+
|
|
243
|
+
return (
|
|
244
|
+
<div className={`inline-flex w-38 items-center ${className}`} style={style}>
|
|
245
|
+
<button
|
|
246
|
+
type="button"
|
|
247
|
+
aria-label={prevMonthAriaLabel}
|
|
248
|
+
disabled={isPrevDisabled}
|
|
249
|
+
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"
|
|
250
|
+
onClick={handlePrevious}>
|
|
251
|
+
<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" />
|
|
252
|
+
</button>
|
|
253
|
+
{variant === "dropdown" ? (
|
|
254
|
+
<select aria-label={selectAriaLabel} disabled={disabled} className={selectClassName} value={formattedValue} onChange={handleSelectChange}>
|
|
255
|
+
{dropdownOptions.map((opt) => (
|
|
256
|
+
<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">
|
|
257
|
+
{opt.label}
|
|
258
|
+
</option>
|
|
259
|
+
))}
|
|
260
|
+
</select>
|
|
261
|
+
) : (
|
|
262
|
+
<input aria-label={selectAriaLabel} disabled={disabled} className={inputClassName} value={formattedValue} min={minFormatted} max={maxFormatted} onChange={handleInputChange} type="month" />
|
|
263
|
+
)}
|
|
264
|
+
<button
|
|
265
|
+
type="button"
|
|
266
|
+
aria-label={nextMonthAriaLabel}
|
|
267
|
+
disabled={isNextDisabled}
|
|
268
|
+
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"
|
|
269
|
+
onClick={handleNext}>
|
|
270
|
+
<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" />
|
|
271
|
+
</button>
|
|
272
|
+
{children}
|
|
273
|
+
</div>
|
|
274
|
+
)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export const MonthSelector = memo(MonthSelectorComponent, (prev, next) => {
|
|
278
|
+
const prevD = prev.value ?? prev.initialDate
|
|
279
|
+
const nextD = next.value ?? next.initialDate
|
|
280
|
+
const prevYm = prevD && !Number.isNaN(prevD.getTime()) ? prevD.getFullYear() * 12 + prevD.getMonth() : null
|
|
281
|
+
const nextYm = nextD && !Number.isNaN(nextD.getTime()) ? nextD.getFullYear() * 12 + nextD.getMonth() : null
|
|
282
|
+
if (prevYm !== nextYm) return false
|
|
283
|
+
if (prev.variant !== next.variant) return false
|
|
284
|
+
if (prev.disabled !== next.disabled) return false
|
|
285
|
+
if (prev.className !== next.className) return false
|
|
286
|
+
if (prev.style !== next.style) return false
|
|
287
|
+
if (prev.isWarning !== next.isWarning) return false
|
|
288
|
+
if (prev.minYear !== next.minYear) return false
|
|
289
|
+
if (prev.maxYear !== next.maxYear) return false
|
|
290
|
+
const prevMinYm = prev.minDate ? prev.minDate.getFullYear() * 12 + prev.minDate.getMonth() : null
|
|
291
|
+
const nextMinYm = next.minDate ? next.minDate.getFullYear() * 12 + next.minDate.getMonth() : null
|
|
292
|
+
if (prevMinYm !== nextMinYm) return false
|
|
293
|
+
const prevMaxYm = prev.maxDate ? prev.maxDate.getFullYear() * 12 + prev.maxDate.getMonth() : null
|
|
294
|
+
const nextMaxYm = next.maxDate ? next.maxDate.getFullYear() * 12 + next.maxDate.getMonth() : null
|
|
295
|
+
if (prevMaxYm !== nextMaxYm) return false
|
|
296
|
+
if (prev.onChange !== next.onChange) return false
|
|
297
|
+
if (prev.children !== next.children) return false
|
|
298
|
+
if (prev.prevMonthAriaLabel !== next.prevMonthAriaLabel) return false
|
|
299
|
+
if (prev.nextMonthAriaLabel !== next.nextMonthAriaLabel) return false
|
|
300
|
+
if (prev.selectAriaLabel !== next.selectAriaLabel) return false
|
|
301
|
+
return true
|
|
302
|
+
})
|
|
303
|
+
MonthSelector.displayName = "MonthSelector"
|