🐛(react) fix DatePicker submit button

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
This commit is contained in:
Nathan Vasse
2024-02-02 15:06:35 +01:00
committed by NathanVss
parent 009714a89d
commit e79768c7ce
4 changed files with 29 additions and 13 deletions

View File

@@ -0,0 +1,5 @@
---
"@openfun/cunningham-react": patch
---
fix DatePicker submit button

View File

@@ -233,6 +233,7 @@ const CalendarAux = forwardRef(
}} }}
disabled={isPrevButtonDisabled} disabled={isPrevButtonDisabled}
onClick={() => state.focusPreviousSection()} onClick={() => state.focusPreviousSection()}
type="button"
/> />
<Button <Button
className="c__calendar__wrapper__header__actions__dropdown" className="c__calendar__wrapper__header__actions__dropdown"
@@ -240,6 +241,7 @@ const CalendarAux = forwardRef(
size="small" size="small"
iconPosition="right" iconPosition="right"
icon={<span className="material-icons">arrow_drop_down</span>} icon={<span className="material-icons">arrow_drop_down</span>}
type="button"
{...getToggleButtonProps("month", monthItems, downshiftMonth)} {...getToggleButtonProps("month", monthItems, downshiftMonth)}
> >
{selectedMonthItemFormatter.format( {selectedMonthItemFormatter.format(
@@ -250,6 +252,7 @@ const CalendarAux = forwardRef(
color="tertiary-text" color="tertiary-text"
size="small" size="small"
icon={<span className="material-icons">navigate_next</span>} icon={<span className="material-icons">navigate_next</span>}
type="button"
{...{ {...{
...nextButtonOtherProps, ...nextButtonOtherProps,
"aria-label": t( "aria-label": t(
@@ -274,6 +277,7 @@ const CalendarAux = forwardRef(
aria-label={t( aria-label={t(
"components.forms.date_picker.previous_year_button_aria_label", "components.forms.date_picker.previous_year_button_aria_label",
)} )}
type="button"
/> />
<Button <Button
className="c__calendar__wrapper__header__actions__dropdown" className="c__calendar__wrapper__header__actions__dropdown"
@@ -281,6 +285,7 @@ const CalendarAux = forwardRef(
size="small" size="small"
iconPosition="right" iconPosition="right"
icon={<span className="material-icons">arrow_drop_down</span>} icon={<span className="material-icons">arrow_drop_down</span>}
type="button"
{...getToggleButtonProps("year", yearItems, downshiftYear)} {...getToggleButtonProps("year", yearItems, downshiftYear)}
> >
{yearItemsFormatter.format( {yearItemsFormatter.format(
@@ -299,6 +304,7 @@ const CalendarAux = forwardRef(
aria-label={t( aria-label={t(
"components.forms.date_picker.next_year_button_aria_label", "components.forms.date_picker.next_year_button_aria_label",
)} )}
type="button"
/> />
</div> </div>
</div> </div>

View File

@@ -78,6 +78,7 @@ export const CalendarCell = ({ state, date }: CalendarCellProps) => {
), ),
}, },
)} )}
type="button"
disabled={isDisabled} disabled={isDisabled}
{...buttonProps} {...buttonProps}
// The keyboard's ENTER event triggers the button twice. // The keyboard's ENTER event triggers the button twice.

View File

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