Skip to content

Commit

Permalink
Prevent DateInput component from triggering form submissions (#2772)
Browse files Browse the repository at this point in the history
  • Loading branch information
connor-baer authored Nov 12, 2024
1 parent eb08159 commit d91ddee
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-kings-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sumup-oss/circuit-ui': patch
---

Added `type="button"` to all `button` elements in the Calendar and DateInput components to prevent them from triggering form submissions.
3 changes: 0 additions & 3 deletions packages/circuit-ui/components/Calendar/Calendar.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@
align-items: center;
justify-content: center;
aspect-ratio: 1 / 1;
font-size: var(--cui-body-m-font-size);
font-weight: var(--cui-font-weight-bold);
line-height: var(--cui-body-m-line-height);
}

.day {
Expand Down
13 changes: 11 additions & 2 deletions packages/circuit-ui/components/Calendar/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
import { applyMultipleRefs } from '../../util/refs.js';
import { useSwipe } from '../../hooks/useSwipe/useSwipe.js';
import { last } from '../../util/helpers.js';
import { Body } from '../Body/Body.js';

import {
CalendarActionType,
Expand Down Expand Up @@ -287,6 +288,7 @@ export const Calendar = forwardRef<HTMLDivElement, CalendarProps>(
<div className={classes.header}>
<div className={classes.prev}>
<IconButton
type="button"
icon={ArrowLeft}
size="s"
variant="tertiary"
Expand All @@ -300,6 +302,7 @@ export const Calendar = forwardRef<HTMLDivElement, CalendarProps>(
</div>
<div className={classes.next}>
<IconButton
type="button"
icon={ArrowRight}
size="s"
variant="tertiary"
Expand Down Expand Up @@ -410,9 +413,14 @@ function Month({
{weekdays.map((weekday) => (
<th key={weekday.long} scope="col">
<span className={utilClasses.hideVisually}>{weekday.long}</span>
<span aria-hidden="true" className={classes.weekday}>
<Body
as="span"
weight="semibold"
aria-hidden="true"
className={classes.weekday}
>
{weekday.narrow}
</span>
</Body>
</th>
))}
</tr>
Expand Down Expand Up @@ -466,6 +474,7 @@ function Month({
return (
<td key={isoDate}>
<button
type="button"
data-date={isoDate}
onClick={handleClick}
onMouseEnter={handleMouseEnter}
Expand Down
3 changes: 3 additions & 0 deletions packages/circuit-ui/components/DateInput/DateInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ describe('DateInput', () => {
const openCalendarButton = screen.getByRole('button', {
name: /change date/i,
});

expect(openCalendarButton).toHaveAttribute('type', 'button');

await userEvent.click(openCalendarButton);

const calendarDialog = screen.getByRole('dialog');
Expand Down
8 changes: 7 additions & 1 deletion packages/circuit-ui/components/DateInput/DateInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ export const DateInput = forwardRef<HTMLInputElement, DateInputProps>(
})}
</div>
<IconButton
type="button"
icon={CalendarIcon}
variant="secondary"
onClick={openCalendar}
Expand Down Expand Up @@ -515,11 +516,16 @@ export const DateInput = forwardRef<HTMLInputElement, DateInputProps>(
{(!required || isMobile) && (
<div className={classes.buttons}>
{!required && (
<Button variant="tertiary" onClick={handleClear}>
<Button
type="button"
variant="tertiary"
onClick={handleClear}
>
{clearDateButtonLabel}
</Button>
)}
<Button
type="button"
variant="primary"
onClick={handleApply}
className={classes.apply}
Expand Down

0 comments on commit d91ddee

Please sign in to comment.