Skip to content

Commit

Permalink
ref: datepicker multi fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Grafikart committed Oct 30, 2023
1 parent 357c85c commit e638ce9
Show file tree
Hide file tree
Showing 9 changed files with 453 additions and 94 deletions.
58 changes: 58 additions & 0 deletions src/components/datepicker/html/DatepickerField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import classNames from 'classnames';
import type { ChangeEventHandler } from 'react';

type Props = {
id: string;
label: string;
description: string;
max?: number;
onChange: (value: number) => void;
value?: number;
readOnly?: boolean;
disabled?: boolean;
};

export function DatepickerField({
label,
id,
description,
onChange,
value,
max,
readOnly,
disabled,
}: Props) {
const handleChange: ChangeEventHandler<HTMLInputElement> = (e) => {
if (
!Number.isNaN(e.target.valueAsNumber) &&
((max && e.target.valueAsNumber > max) || e.target.valueAsNumber < 1)
) {
return;
}
onChange(e.target.valueAsNumber);
};
return (
<div
className={classNames(
'lunaticDatepickerField',
!max && 'lunaticDatepickerFieldLarge'
)}
>
<label htmlFor={id}>
{label}
<span className="lunaticDatepickerHint">{description}</span>
</label>
<input
id={id}
type="number"
min={1}
max={max}
readOnly={readOnly}
disabled={disabled}
value={Number.isNaN(value) ? '' : value}
onChange={handleChange}
onFocus={(e) => e.target.select()}
/>
</div>
);
}
226 changes: 209 additions & 17 deletions src/components/datepicker/html/__snapshots__/datepicker.spec.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,34 +1,226 @@
// Vitest Snapshot v1

exports[`Datepicker > renders without crashing 1`] = `
exports[`Datepicker > should render properly with format YYYY 1`] = `
<div>
<div
class="lunatic-input"
>
<input
aria-labelledby="lunatic-datepicker-datepicker"
class="lunatic-datepicker"
id="datepicker"
type="date"
value="1999-01-01"
/>
<div
class="lunaticDatepickerFields"
>
<div
class="lunaticDatepickerField"
>
<label
for="datepickerday"
>
Jour
<span
class="lunaticDatepickerHint"
>
Exemple: 14
</span>
</label>
<input
id="datepickerday"
max="31"
min="1"
type="number"
value="1"
/>
</div>
<div
class="lunaticDatepickerField"
>
<label
for="datepickermonth"
>
Mois
<span
class="lunaticDatepickerHint"
>
Exemple: 07
</span>
</label>
<input
id="datepickermonth"
max="12"
min="1"
type="number"
value="1"
/>
</div>
<div
class="lunaticDatepickerField lunaticDatepickerFieldLarge"
>
<label
for="datepickeryear"
>
Année
<span
class="lunaticDatepickerHint"
>
Exemple: 2023
</span>
</label>
<input
id="datepickeryear"
min="1"
type="number"
value="1999"
/>
</div>
</div>
</div>
</div>
`;

exports[`Datepicker > should handle readOnly 1`] = `
exports[`Datepicker > should render properly with format YYYY-MM 1`] = `
<div>
<div
class="lunatic-input"
>
<input
aria-labelledby="lunatic-datepicker-Datepicker"
class="lunatic-datepicker readOnly"
id="Datepicker"
readonly=""
type="date"
value="1980-01-19"
/>
<div
class="lunaticDatepickerFields"
>
<div
class="lunaticDatepickerField"
>
<label
for="datepickerday"
>
Jour
<span
class="lunaticDatepickerHint"
>
Exemple: 14
</span>
</label>
<input
id="datepickerday"
max="31"
min="1"
type="number"
value="1"
/>
</div>
<div
class="lunaticDatepickerField"
>
<label
for="datepickermonth"
>
Mois
<span
class="lunaticDatepickerHint"
>
Exemple: 07
</span>
</label>
<input
id="datepickermonth"
max="12"
min="1"
type="number"
value="1"
/>
</div>
<div
class="lunaticDatepickerField lunaticDatepickerFieldLarge"
>
<label
for="datepickeryear"
>
Année
<span
class="lunaticDatepickerHint"
>
Exemple: 2023
</span>
</label>
<input
id="datepickeryear"
min="1"
type="number"
value="1999"
/>
</div>
</div>
</div>
</div>
`;

exports[`Datepicker > should render properly with format YYYY-MM-DD 1`] = `
<div>
<div
class="lunatic-input"
>
<div
class="lunaticDatepickerFields"
>
<div
class="lunaticDatepickerField"
>
<label
for="datepickerday"
>
Jour
<span
class="lunaticDatepickerHint"
>
Exemple: 14
</span>
</label>
<input
id="datepickerday"
max="31"
min="1"
type="number"
value="1"
/>
</div>
<div
class="lunaticDatepickerField"
>
<label
for="datepickermonth"
>
Mois
<span
class="lunaticDatepickerHint"
>
Exemple: 07
</span>
</label>
<input
id="datepickermonth"
max="12"
min="1"
type="number"
value="1"
/>
</div>
<div
class="lunaticDatepickerField lunaticDatepickerFieldLarge"
>
<label
for="datepickeryear"
>
Année
<span
class="lunaticDatepickerHint"
>
Exemple: 2023
</span>
</label>
<input
id="datepickeryear"
min="1"
type="number"
value="1999"
/>
</div>
</div>
</div>
</div>
`;
7 changes: 0 additions & 7 deletions src/components/datepicker/html/datepicker-container.tsx

This file was deleted.

41 changes: 0 additions & 41 deletions src/components/datepicker/html/datepicker-input.tsx

This file was deleted.

20 changes: 19 additions & 1 deletion src/components/datepicker/html/datepicker.scss
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
// see input.scss
.lunaticDatepickerFields {
display: flex;
align-items: flex-end;
gap: 1rem;
}

.lunaticDatepickerField input {
width: 2.5em;
}

.lunaticDatepickerFieldLarge input {
width: 4.5em;
}

.lunaticDatepickerHint {
display: block;
font-weight: 400;
font-size: .9em;
}
Loading

0 comments on commit e638ce9

Please sign in to comment.