Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/popover with dialog #2838

Draft
wants to merge 12 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilled-coins-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sumup-oss/circuit-ui": minor
---

Refactored the DateInput and Toggletip components to use the Popover component under the hood.
5 changes: 5 additions & 0 deletions .changeset/tasty-radios-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sumup-oss/circuit-ui": minor
---

Updated the popover component to allow any content inside the popover by adding a `children` prop. Under the hood, the component now uses the dialog element instead of the portal component.
sirineJ marked this conversation as resolved.
Show resolved Hide resolved
40 changes: 26 additions & 14 deletions packages/circuit-ui/components/DateInput/DateInput.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,21 @@
z-index: calc(var(--cui-z-index-absolute) + 1);
}

.content {
color: var(--cui-fg-normal);
background-color: var(--cui-bg-elevated);
border: var(--cui-border-width-kilo) solid var(--cui-border-subtle);
border-radius: var(--cui-border-radius-byte);
outline: 0;
box-shadow: 0 2px 6px 0 rgb(0 0 0 / 8%);
}

@media (max-width: 479px) {
.content {
padding: 0;
border: none;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
box-shadow: none;
}
}

.header {
display: flex;
align-items: center;
justify-content: space-between;
padding: var(--cui-spacings-giga) var(--cui-spacings-mega)
var(--cui-spacings-byte) var(--cui-spacings-mega);
padding: var(--cui-spacings-byte) 0;
}

@media (min-width: 480px) {
Expand All @@ -151,16 +144,35 @@
}

.calendar {
padding: var(--cui-spacings-mega);
margin-bottom: var(--cui-spacings-mega);
}

@media (max-width: 479px) {
.calendar {
margin: var(--cui-spacings-mega) 0;
}
}

.divider {
width: calc(100% + var(--cui-spacings-tera));
margin-left: calc(-1 * var(--cui-spacings-mega));
}

.buttons {
display: flex;
flex-wrap: wrap;
gap: var(--cui-spacings-kilo);
justify-content: space-between;
padding: var(--cui-spacings-mega);
border-top: var(--cui-border-width-kilo) solid var(--cui-border-divider);
}

.popover {
max-width: min(410px, 100vw);
border-color: var(--cui-border-subtle);
box-shadow: none;
}

.popover::after {
visibility: hidden;
}

.apply {
Expand Down
41 changes: 35 additions & 6 deletions packages/circuit-ui/components/DateInput/DateInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,50 @@
* limitations under the License.
*/

import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
import {
beforeEach,
describe,
expect,
it,
vi,
type Mock,
afterEach,
} from 'vitest';
import { createRef } from 'react';
import MockDate from 'mockdate';

import { render, screen, axe, userEvent } from '../../util/test-utils.js';
import { render, screen, axe, userEvent, act } from '../../util/test-utils.js';
import { useMedia } from '../../hooks/useMedia/useMedia.js';
import { ANIMATION_DURATION } from '../Modal/Modal.js';

import { DateInput } from './DateInput.js';

vi.mock('../../hooks/useMedia/useMedia.js');
vi.mock('../../hooks/useScrollLock/useScrollLock.js');

describe('DateInput', () => {
const props = {
onChange: vi.fn(),
label: 'Date of birth',
};
let originalHTMLDialogElement: typeof window.HTMLDialogElement;

beforeEach(() => {
originalHTMLDialogElement = window.HTMLDialogElement;
MockDate.set('2000-01-01');
(useMedia as Mock).mockReturnValue(false);
vi.clearAllMocks();
vi.useFakeTimers({ shouldAdvanceTime: true });
});

afterEach(() => {
vi.runOnlyPendingTimers();
vi.useRealTimers();
vi.resetAllMocks();
Object.defineProperty(window, 'HTMLDialogElement', {
writable: true,
value: originalHTMLDialogElement,
});
});

it('should forward a ref', () => {
Expand Down Expand Up @@ -228,7 +252,7 @@ describe('DateInput', () => {
it('should focus the first input when clicking the label', async () => {
render(<DateInput {...props} />);

await userEvent.click(screen.getByText('Date of birth'));
await userEvent.click(screen.getAllByText('Date of birth')[0]);

expect(screen.getAllByRole('spinbutton')[0]).toHaveFocus();
});
Expand Down Expand Up @@ -319,7 +343,6 @@ describe('DateInput', () => {

expect(ref.current).toHaveValue('2000-01-12');
expect(onChange).toHaveBeenCalled();
expect(openCalendarButton).toHaveFocus();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀

});

it('should allow users to clear the date', async () => {
Expand Down Expand Up @@ -348,7 +371,11 @@ describe('DateInput', () => {

expect(ref.current).toHaveValue('');
expect(onChange).toHaveBeenCalled();
expect(openCalendarButton).toHaveFocus();
expect(
screen.getByRole('button', {
name: /change date/i,
}),
).toHaveFocus();
});

it('should close calendar on outside click', async () => {
Expand Down Expand Up @@ -452,7 +479,9 @@ describe('DateInput', () => {

const closeButton = screen.getByRole('button', { name: /close/i });
await userEvent.click(closeButton);

act(() => {
vi.advanceTimersByTime(ANIMATION_DURATION);
});
expect(calendarDialog).not.toBeVisible();
expect(ref.current).toHaveValue('2000-01-12');
expect(onChange).not.toHaveBeenCalled();
Expand Down
14 changes: 13 additions & 1 deletion packages/circuit-ui/components/DateInput/DateInput.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

import { useState } from 'react';
import { userEvent, within } from '@storybook/test';

import { Stack } from '../../../../.storybook/components/index.js';

Expand Down Expand Up @@ -94,13 +95,24 @@ export const Validations = (args: DateInputProps) => (

Validations.args = baseArgs;

const openCalendar = async ({
canvasElement,
}: {
canvasElement: HTMLCanvasElement;
}) => {
const canvas = within(canvasElement);
const referenceEl = canvas.getAllByRole('button');

await userEvent.click(referenceEl[0]);
};

export const Optional = (args: DateInputProps) => <DateInput {...args} />;

Optional.args = {
...baseArgs,
optionalLabel: 'optional',
};

Optional.play = openCalendar;
export const Readonly = (args: DateInputProps) => <DateInput {...args} />;

Readonly.args = {
Expand Down
Loading
Loading