Skip to content

Commit

Permalink
🐛(react) fix DatePicker submit button
Browse files Browse the repository at this point in the history
As the default type of button is "submit", when including a DatePicker
inside a form, clicking on any of its button was triggering form
submission.

Fixes #245
  • Loading branch information
NathanVss committed Feb 12, 2024
1 parent 009714a commit e79768c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-monkeys-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@openfun/cunningham-react": patch
---

fix DatePicker submit button
6 changes: 6 additions & 0 deletions packages/react/src/components/Forms/DatePicker/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,15 @@ const CalendarAux = forwardRef(
}}
disabled={isPrevButtonDisabled}
onClick={() => state.focusPreviousSection()}
type="button"
/>
<Button
className="c__calendar__wrapper__header__actions__dropdown"
color="tertiary-text"
size="small"
iconPosition="right"
icon={<span className="material-icons">arrow_drop_down</span>}
type="button"
{...getToggleButtonProps("month", monthItems, downshiftMonth)}
>
{selectedMonthItemFormatter.format(
Expand All @@ -250,6 +252,7 @@ const CalendarAux = forwardRef(
color="tertiary-text"
size="small"
icon={<span className="material-icons">navigate_next</span>}
type="button"
{...{
...nextButtonOtherProps,
"aria-label": t(
Expand All @@ -274,13 +277,15 @@ const CalendarAux = forwardRef(
aria-label={t(
"components.forms.date_picker.previous_year_button_aria_label",
)}
type="button"
/>
<Button
className="c__calendar__wrapper__header__actions__dropdown"
color="tertiary-text"
size="small"
iconPosition="right"
icon={<span className="material-icons">arrow_drop_down</span>}
type="button"
{...getToggleButtonProps("year", yearItems, downshiftYear)}
>
{yearItemsFormatter.format(
Expand All @@ -299,6 +304,7 @@ const CalendarAux = forwardRef(
aria-label={t(
"components.forms.date_picker.next_year_button_aria_label",
)}
type="button"
/>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export const CalendarCell = ({ state, date }: CalendarCellProps) => {
),
},
)}
type="button"
disabled={isDisabled}
{...buttonProps}
// The keyboard's ENTER event triggers the button twice.
Expand Down
30 changes: 17 additions & 13 deletions packages/react/src/components/Forms/DatePicker/DatePickerAux.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ const DatePickerAux = forwardRef(
[pickerState.validationState, props.state],
);

// onPress props don't exist on the <Button /> component.
// Remove it to avoid any warning.
const { onPress: onPressToggleButton, ...otherButtonProps } =
pickerProps.buttonProps;

return (
<I18nProvider locale={locale || currentLocale}>
<Field {...props}>
Expand Down Expand Up @@ -141,18 +136,21 @@ const DatePickerAux = forwardRef(
)}
<div className="c__date-picker__wrapper__icon">
<Button
{...{
...otherButtonProps,
"aria-label": t(
pickerState.isOpen
? "components.forms.date_picker.toggle_button_aria_label_close"
: "components.forms.date_picker.toggle_button_aria_label_open",
),
type="button"
onKeyDown={(e) => {
if (e.key === "Enter") {
pickerState.toggle();
}
}}
onClick={pickerState.toggle}
aria-label={t(
pickerState.isOpen
? "components.forms.date_picker.toggle_button_aria_label_close"
: "components.forms.date_picker.toggle_button_aria_label_open",
)}
color="tertiary-text"
size="small"
className="c__date-picker__wrapper__toggle"
onClick={pickerState.toggle}
icon={
<span className="material-icons icon">calendar_today</span>
}
Expand All @@ -170,10 +168,16 @@ const DatePickerAux = forwardRef(
size="nano"
icon={<span className="material-icons">close</span>}
onClick={onClear}
onKeyDown={(e) => {
if (e.key === "Enter") {
onClear();
}
}}
aria-label={t(
"components.forms.date_picker.clear_button_aria_label",
)}
disabled={disabled}
type="button"
/>
</div>
{pickerState.isOpen && (
Expand Down

0 comments on commit e79768c

Please sign in to comment.