-
Notifications
You must be signed in to change notification settings - Fork 0
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: add departure date selector #27
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cb5dfac
feat: add departure date selector
adriansberg dbf8c77
fix: remove outdated comment
adriansberg 6357721
fix: add onChange for state
adriansberg f827bbe
test: add tests for departure date selector
adriansberg 424bbde
refactor: clean up api
adriansberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
139 changes: 139 additions & 0 deletions
139
src/components/departure-date-selector/__tests__/departure-date-selector.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { cleanup, render, fireEvent } from '@testing-library/react'; | ||
import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
|
||
import DepartureDateSelector, { | ||
DepartureDateState, | ||
} from '@atb/components/departure-date-selector'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
afterEach(function () { | ||
cleanup(); | ||
}); | ||
|
||
describe('departure date selector', function () { | ||
it('should default to "Now"', async () => { | ||
const output = render(<DepartureDateSelector onChange={() => {}} />); | ||
|
||
expect( | ||
output.getByRole('radio', { | ||
name: 'Nå', | ||
}), | ||
).toBeChecked(); | ||
}); | ||
|
||
it('should not show date and time selector when "Now" is selected', async () => { | ||
const output = render(<DepartureDateSelector onChange={() => {}} />); | ||
|
||
expect(output.queryByText('Dato')).not.toBeInTheDocument(); | ||
expect(output.queryByText('Tid')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('should use initialState for default selected', async () => { | ||
const output = render( | ||
<DepartureDateSelector | ||
initialState={{ type: DepartureDateState.Arrival, dateTime: 0 }} | ||
onChange={() => {}} | ||
/>, | ||
); | ||
|
||
output.getByRole('radio', { | ||
name: 'Ankomst', | ||
}); | ||
}); | ||
|
||
it('should show date and time selector when "Arrival" is selected', async () => { | ||
const output = render( | ||
<DepartureDateSelector | ||
initialState={{ type: DepartureDateState.Arrival, dateTime: 0 }} | ||
onChange={() => {}} | ||
/>, | ||
); | ||
|
||
expect(output.queryByText('Dato')).toBeInTheDocument(); | ||
expect(output.queryByText('Tid')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should show date and time selector when "Departure" is selected', async () => { | ||
const output = render( | ||
<DepartureDateSelector | ||
initialState={{ type: DepartureDateState.Departure, dateTime: 0 }} | ||
onChange={() => {}} | ||
/>, | ||
); | ||
|
||
expect(output.queryByText('Dato')).toBeInTheDocument(); | ||
expect(output.queryByText('Tid')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should change selection with keyboard', async () => { | ||
const output = render(<DepartureDateSelector onChange={() => {}} />); | ||
|
||
const radio = output.getByRole('radio', { name: 'Nå' }); | ||
radio.focus(); | ||
expect(radio).toHaveFocus(); | ||
|
||
// {ArrowRight} does not work | ||
await userEvent.type(radio, '{ArrowDown}'); | ||
|
||
expect( | ||
output.getByRole('radio', { | ||
name: 'Ankomst', | ||
}), | ||
).toBeChecked(); | ||
}); | ||
|
||
it('should change selection when clicked', async () => { | ||
const output = render(<DepartureDateSelector onChange={() => {}} />); | ||
|
||
const radio = output.getByRole('radio', { | ||
name: 'Ankomst', | ||
}); | ||
|
||
expect(radio).not.toBeChecked(); | ||
|
||
await userEvent.click(radio); | ||
|
||
expect(radio).toBeChecked(); | ||
}); | ||
|
||
it('should call onStateChange', async () => { | ||
const onStateChange = vi.fn(); | ||
const output = render(<DepartureDateSelector onChange={onStateChange} />); | ||
|
||
output.getByRole('radio', { name: 'Ankomst' }).click(); | ||
|
||
expect(onStateChange).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call onDateChange', async () => { | ||
const onChange = vi.fn(); | ||
const output = render( | ||
<DepartureDateSelector | ||
initialState={{ type: DepartureDateState.Arrival, dateTime: 0 }} | ||
onChange={onChange} | ||
/>, | ||
); | ||
|
||
const date = output.getByLabelText('Dato'); | ||
|
||
fireEvent.change(date, { target: { value: '2100-01-01' } }); | ||
|
||
expect(onChange).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call onTimeChange', async () => { | ||
const onChange = vi.fn(); | ||
const output = render( | ||
<DepartureDateSelector | ||
initialState={{ type: DepartureDateState.Arrival, dateTime: 0 }} | ||
onChange={onChange} | ||
/>, | ||
); | ||
|
||
const time = output.getByLabelText('Tid'); | ||
|
||
fireEvent.change(time, { target: { value: '00:00' } }); | ||
|
||
expect(onChange).toHaveBeenCalled(); | ||
}); | ||
}); |
113 changes: 113 additions & 0 deletions
113
src/components/departure-date-selector/departure-date-selector.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
.departureDateSelector { | ||
display: flex; | ||
flex-direction: column; | ||
/* TODO: Spacing not in variables. Use variable instead? */ | ||
gap: .375rem; | ||
} | ||
|
||
.options { | ||
--container-height: 2.75rem; | ||
--option-height: 2.25rem; | ||
--container-border-radius: 0.75rem; | ||
--option-border-radius: var(--border-radius-regular); | ||
|
||
display: grid; | ||
grid-template-columns: repeat(3, minmax(0, 1fr)); | ||
padding: var(--spacings-xSmall); | ||
background: var(--static-background-background_0-background); | ||
width: fit-content; | ||
border-radius: var(--container-border-radius); | ||
height: var(--container-height); | ||
align-items: center; | ||
} | ||
|
||
.options:focus-within { | ||
border-radius: var(--border-radius-regular); | ||
outline: 0; | ||
box-shadow: inset 0 0 0 var(--border-width-medium) var(--interactive-interactive_2-outline-background); | ||
} | ||
|
||
.option label span { | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
cursor: pointer; | ||
padding: var(--spacings-small); | ||
border-radius: var(--option-border-radius); | ||
height: var(--option-height); | ||
} | ||
|
||
.option input { | ||
clip: rect(0 0 0 0); | ||
clip-path: inset(100%); | ||
height: 1px; | ||
overflow: hidden; | ||
position: absolute; | ||
white-space: nowrap; | ||
width: 1px; | ||
display: block; | ||
} | ||
|
||
.option label input:checked + span { | ||
background: var(--interactive-interactive_1-default-background); | ||
color: var(--interactive-interactive_1-default-text); | ||
} | ||
|
||
|
||
.dateAndTimeSelectors { | ||
display: flex; | ||
gap: var(--spacings-medium); | ||
} | ||
|
||
.dateSelector, .timeSelector { | ||
--height: 2.75rem; | ||
border-radius: 0.75rem; | ||
overflow: hidden; | ||
background-color: var(--static-background-background_0-background); | ||
display: flex; | ||
} | ||
|
||
.dateSelector label, | ||
.timeSelector label { | ||
padding: var(--spacings-medium); | ||
padding-right: 0; | ||
min-width: 3rem; | ||
} | ||
|
||
.dateSelector input[type="date"], | ||
.timeSelector input[type="time"] { | ||
height: var(--height); | ||
padding: var(--spacings-medium); | ||
border: none; | ||
background-color: transparent; | ||
color: var(--static-background-background_0-text); | ||
flex: 1; | ||
border-top-right-radius: 0.75rem; | ||
border-bottom-right-radius: 0.75rem; | ||
} | ||
|
||
.dateSelector input[type="date"]:focus, | ||
.timeSelector input[type="time"]:focus { | ||
outline: 0; | ||
} | ||
|
||
.dateSelector:focus-within, | ||
.timeSelector:focus-within { | ||
box-shadow: inset 0 0 0 var(--border-width-medium) var(--interactive-interactive_2-outline-background); | ||
} | ||
|
||
:global(.dark) .dateSelector input[type="date"]::-webkit-calendar-picker-indicator, | ||
:global(.dark) .timeSelector input[type="time"]::-webkit-calendar-picker-indicator { | ||
filter: invert(1); | ||
} | ||
|
||
@media (max-width: 650px) { | ||
.dateAndTimeSelectors { | ||
flex-direction: column; | ||
gap: .375rem; | ||
} | ||
|
||
.options { | ||
width: 100%; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What should we do with values like these that are not specified in the theme? Should the design be revised, should we add the value(s) or should we just use them as is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The design should be revised to follow the correct spacing I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, I'd prefer that over adding new values. Though a bit strange (maybe not the best word) that a spacing of 1rem is not in the set of spacings? Same with 0.75rem for border-radius.
Should we just keep this values for now until we have a design review, or similar to update the spacings, or use the spacings we have for now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think 16px is ever used in spacing in the webshop or app 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably not since it is not a spacing.
The ones we are using at the moment are:
6px / 0.375rem (spacing between date/time inputs and type)
16px / 1rem (margin-bottom for search box asks for this, have used --spacings-medium)
12px / 0.75rem (border-radius)