|
@@ -5,26 +5,21 @@ import { cn } from "../../lib/utils";
|
|
|
import { FilterIcon } from "./filter-select";
|
|
import { FilterIcon } from "./filter-select";
|
|
|
import "../../styles/month-filter.css";
|
|
import "../../styles/month-filter.css";
|
|
|
|
|
|
|
|
-export function MonthFilter({ value, onChange, maxMonth, allowAll = true, availableMonths, disabled = false }: {
|
|
|
|
|
|
|
+export function MonthFilter({ value, onChange, maxMonth, allowAll = true }: {
|
|
|
value: string;
|
|
value: string;
|
|
|
onChange: (value: string) => void;
|
|
onChange: (value: string) => void;
|
|
|
/** 可选上界(YYYY-MM),默认本月。人工补跑传最近一个已结束的自然月。 */
|
|
/** 可选上界(YYYY-MM),默认本月。人工补跑传最近一个已结束的自然月。 */
|
|
|
maxMonth?: string;
|
|
maxMonth?: string;
|
|
|
/** 是否给「全部自然月」这一项;必填期次的场景(人工补跑)传 false。 */
|
|
/** 是否给「全部自然月」这一项;必填期次的场景(人工补跑)传 false。 */
|
|
|
allowAll?: boolean;
|
|
allowAll?: boolean;
|
|
|
- /** 可选的服务端账期清单,传入时仅允许选择清单内的月份。 */
|
|
|
|
|
- availableMonths?: readonly string[];
|
|
|
|
|
- disabled?: boolean;
|
|
|
|
|
}) {
|
|
}) {
|
|
|
const id = useId();
|
|
const id = useId();
|
|
|
const popup = useRef<HTMLDivElement>(null);
|
|
const popup = useRef<HTMLDivElement>(null);
|
|
|
const trigger = useRef<HTMLButtonElement>(null);
|
|
const trigger = useRef<HTMLButtonElement>(null);
|
|
|
const limit = maxMonth ?? businessToday().slice(0, 7);
|
|
const limit = maxMonth ?? businessToday().slice(0, 7);
|
|
|
- const availableYears = availableMonths?.map((month) => Number(month.slice(0, 4)));
|
|
|
|
|
- const maxYear = availableYears?.length ? Math.max(...availableYears) : Number(limit.slice(0, 4));
|
|
|
|
|
- const minYear = availableYears ? Math.min(maxYear, ...availableYears) : 1;
|
|
|
|
|
|
|
+ const maxYear = Number(limit.slice(0, 4));
|
|
|
const selectedYear = Number(value.slice(0, 4));
|
|
const selectedYear = Number(value.slice(0, 4));
|
|
|
- const initialYear = selectedYear > 0 ? Math.max(minYear, Math.min(selectedYear, maxYear)) : maxYear;
|
|
|
|
|
|
|
+ const initialYear = selectedYear > 0 ? Math.min(selectedYear, maxYear) : maxYear;
|
|
|
const [year, setYear] = useState(initialYear);
|
|
const [year, setYear] = useState(initialYear);
|
|
|
const label = value || "全部自然月";
|
|
const label = value || "全部自然月";
|
|
|
const limitHint = maxMonth ? "只能选已结束的月份" : "只能选择本月及之前的月份";
|
|
const limitHint = maxMonth ? "只能选已结束的月份" : "只能选择本月及之前的月份";
|
|
@@ -37,13 +32,13 @@ export function MonthFilter({ value, onChange, maxMonth, allowAll = true, availa
|
|
|
|
|
|
|
|
return <>
|
|
return <>
|
|
|
<button type="button" ref={trigger} className="filter-select month-filter-trigger" popoverTarget={id}
|
|
<button type="button" ref={trigger} className="filter-select month-filter-trigger" popoverTarget={id}
|
|
|
- aria-label={`自然月:${label}`} aria-haspopup="dialog" disabled={disabled} onClick={() => setYear(initialYear)}>
|
|
|
|
|
|
|
+ aria-label={`自然月:${label}`} aria-haspopup="dialog" onClick={() => setYear(initialYear)}>
|
|
|
<FilterIcon kind="month" />
|
|
<FilterIcon kind="month" />
|
|
|
<span className={cn("filter-select-value", !value && "is-placeholder")}>{label}</span>
|
|
<span className={cn("filter-select-value", !value && "is-placeholder")}>{label}</span>
|
|
|
</button>
|
|
</button>
|
|
|
<div id={id} ref={popup} popover="auto" role="dialog" aria-label="选择自然月" className="month-filter-popup">
|
|
<div id={id} ref={popup} popover="auto" role="dialog" aria-label="选择自然月" className="month-filter-popup">
|
|
|
<div className="month-filter-heading">
|
|
<div className="month-filter-heading">
|
|
|
- <button type="button" aria-label="上一年" disabled={year <= minYear} onClick={() => setYear(year - 1)}><ChevronLeft size={16} aria-hidden="true" /></button>
|
|
|
|
|
|
|
+ <button type="button" aria-label="上一年" disabled={year === 1} onClick={() => setYear(year - 1)}><ChevronLeft size={16} aria-hidden="true" /></button>
|
|
|
<strong aria-live="polite">{year} 年</strong>
|
|
<strong aria-live="polite">{year} 年</strong>
|
|
|
<button type="button" aria-label="下一年"
|
|
<button type="button" aria-label="下一年"
|
|
|
disabled={year >= maxYear}
|
|
disabled={year >= maxYear}
|
|
@@ -52,13 +47,13 @@ export function MonthFilter({ value, onChange, maxMonth, allowAll = true, availa
|
|
|
<div className="month-filter-grid">
|
|
<div className="month-filter-grid">
|
|
|
{Array.from({ length: 12 }, (_, index) => {
|
|
{Array.from({ length: 12 }, (_, index) => {
|
|
|
const month = `${String(year).padStart(4, "0")}-${String(index + 1).padStart(2, "0")}`;
|
|
const month = `${String(year).padStart(4, "0")}-${String(index + 1).padStart(2, "0")}`;
|
|
|
- const unavailable = availableMonths ? !availableMonths.includes(month) : month > limit;
|
|
|
|
|
- return <button type="button" key={month} aria-pressed={month === value} disabled={disabled || unavailable}
|
|
|
|
|
- title={unavailable ? (availableMonths ? "本方暂无此账期" : limitHint) : undefined}
|
|
|
|
|
|
|
+ const beyond = month > limit;
|
|
|
|
|
+ return <button type="button" key={month} aria-pressed={month === value} disabled={beyond}
|
|
|
|
|
+ title={beyond ? limitHint : undefined}
|
|
|
onClick={() => choose(month)}>{index + 1} 月</button>;
|
|
onClick={() => choose(month)}>{index + 1} 月</button>;
|
|
|
})}
|
|
})}
|
|
|
</div>
|
|
</div>
|
|
|
- {allowAll && <button type="button" className="month-filter-all" aria-pressed={!value} disabled={disabled} onClick={() => choose("")}>全部自然月</button>}
|
|
|
|
|
|
|
+ {allowAll && <button type="button" className="month-filter-all" aria-pressed={!value} onClick={() => choose("")}>全部自然月</button>}
|
|
|
</div>
|
|
</div>
|
|
|
</>;
|
|
</>;
|
|
|
}
|
|
}
|