日付の入力
生年月日など分かっている日付を、年・月・日の 3 つの欄に直接入れる部品です。
見本で種類と状態を確かめる
欄の幅は年 96、月 64、日 64 です。欄と「年」「月」「日」の間は 8、組と組の間は 24 です。
入力前
エラー
コードを写す
HTML
<fieldset class="mz-field mz-date-fields" aria-describedby="birth-light-help birth-light-error" data-date>
<legend class="mz-field__label">生年月日 <span class="mz-field__req">※必須</span></legend>
<p class="mz-field__help" id="birth-light-help">西暦で入力してください。例:1988年4月12日</p>
<div class="mz-date-fields__row">
<span class="mz-date-fields__part" data-part="year"><input class="mz-input" id="by-light" name="year" type="text" inputmode="numeric" autocomplete="bday-year"><label for="by-light">年</label></span>
<span class="mz-date-fields__part" data-part="month"><input class="mz-input" id="bm-light" name="month" type="text" inputmode="numeric" autocomplete="bday-month"><label for="bm-light">月</label></span>
<span class="mz-date-fields__part" data-part="day"><input class="mz-input" id="bd-light" name="day" type="text" inputmode="numeric" autocomplete="bday-day"><label for="bd-light">日</label></span>
</div>
<p class="mz-field__error" id="birth-light-error" hidden></p>
</fieldset>React の呼び出し方
const [birth, setBirth] = useState<DateValue>({ year: '1988', month: '4', day: '12' });
return (
<DateFields
legend="生年月日"
requirement="required"
help="西暦で入力してください。例:1988年4月12日"
value={birth}
onChange={setBirth}
autoCompleteSection="bday"
/>
);実装を見る(react/DateFields.tsx)
React の実装(react/DateFields.tsx)
/**
* DateFields — 日付の直接入力(docs/spec/components.md「date-fields」・css/components/date-fields.css)
* 年・月・日の 3 つの欄。生年月日など、分かっている日付はこれで入力させる。
* <input type="date"> は予約の日付の選択などに限る。
* 全角の数字・先頭の 0 は送信時に揃える(toHalfWidth と、親の確かめる処理で)。
* 3 つの欄の id は `${id}-year`・`${id}-month`・`${id}-day`(id を渡さないときは useId の値)。
* ErrorSummary の宛先には、誤りのある欄のこの id を渡す。
*/
import { forwardRef, useId, type FieldsetHTMLAttributes, type ReactNode } from 'react';
import { requirementText, type FieldRequirement } from './Field';
export interface DateValue {
year: string;
month: string;
day: string;
}
export interface DateFieldsProps
extends Omit<FieldsetHTMLAttributes<HTMLFieldSetElement>, 'onChange' | 'disabled' | 'children'> {
legend: ReactNode;
requirement: FieldRequirement;
help?: ReactNode;
/** 送信時に見つかった誤り。先頭の「*」は部品が付ける */
error?: ReactNode;
/** 誤りのある欄(既定は error があれば 3 つとも) */
invalidParts?: (keyof DateValue)[];
value: DateValue;
onChange: (value: DateValue) => void;
/** 生年月日なら 'bday'(autocomplete の bday-year などになる) */
autoCompleteSection?: 'bday';
}
const parts: { key: keyof DateValue; unit: string; ac: string }[] = [
{ key: 'year', unit: '年', ac: 'year' },
{ key: 'month', unit: '月', ac: 'month' },
{ key: 'day', unit: '日', ac: 'day' },
];
export const DateFields = forwardRef<HTMLFieldSetElement, DateFieldsProps>(function DateFields(
{ legend, requirement, help, error, invalidParts, value, onChange, autoCompleteSection, className, id, ...rest },
ref,
) {
const auto = useId();
const base = id ?? auto;
const described = [help ? `${base}-help` : '', error ? `${base}-error` : ''].filter(Boolean).join(' ') || undefined;
const invalid = new Set(error ? (invalidParts ?? parts.map((p) => p.key)) : []);
return (
<fieldset
ref={ref}
id={id}
className={className ? `mz-field mz-date-fields ${className}` : 'mz-field mz-date-fields'}
aria-describedby={described}
{...rest}
>
<legend className="mz-field__label">
{legend} <span className="mz-field__req">{requirementText[requirement]}</span>
</legend>
{help ? (
<p className="mz-field__help" id={`${base}-help`}>
{help}
</p>
) : null}
<div className="mz-date-fields__row">
{parts.map((p) => {
const inputId = `${base}-${p.key}`;
return (
<span key={p.key} className="mz-date-fields__part" data-part={p.key}>
<input
id={inputId}
className="mz-input"
type="text"
inputMode="numeric"
autoComplete={autoCompleteSection ? `${autoCompleteSection}-${p.ac}` : undefined}
aria-invalid={invalid.has(p.key) || undefined}
value={value[p.key]}
onChange={(e) => onChange({ ...value, [p.key]: e.target.value })}
/>
<label htmlFor={inputId}>{p.unit}</label>
</span>
);
})}
</div>
{error ? (
<p className="mz-field__error" id={`${base}-error`}>
*{error}
</p>
) : null}
</fieldset>
);
});いつ使うか
- 生年月日・契約の開始日など、利用者がすでに知っている日付に使います。カレンダーから探すより、打つほうが早いためです。
- 予約の日など、カレンダーを見ながら選ぶ日付には
<input type="date">を使ってよいです。 - 全角の数字や「04」のような 0 埋めは、エラーにせず受け取って揃えます。
読み上げとキーボードにどう応えるか
- 全体を
<fieldset>と<legend>で包み、各欄は「年」「月」「日」の<label>と結びます。「生年月日、年」のように読まれます。 inputmode="numeric"で、スマホに数字のキーボードを出します。欄のtypeはtextのままにします。
よい例と悪い例
更新日