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

Add possibility to navigate year picker in calendar #3571

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export const Calendar = forwardRef(
ref: RefObject<HTMLDivElement>,
) => {
const [showYearPicker, setShowYearPicker] = useState(false)
const [yearPickerPage, setYearPickerPage] = useState(0)

const { locale } = useLocale()
const calendarState = useCalendarState({
...props,
Expand Down Expand Up @@ -74,6 +76,7 @@ export const Calendar = forwardRef(
nextMonthDisabled={nextButtonProps.isDisabled}
setShowYearPicker={setShowYearPicker}
showYearPicker={showYearPicker}
setYearPickerPage={setYearPickerPage}
/>
)}
</Popover.Header>
Expand All @@ -82,6 +85,7 @@ export const Calendar = forwardRef(
state={calendarState}
setShowYearPicker={setShowYearPicker}
showYearPicker={showYearPicker}
yearPickerPage={yearPickerPage}
/>
</Popover.Content>
{Footer && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ export function CalendarGrid({
state,
showYearPicker,
setShowYearPicker,
yearPickerPage,
...props
}: {
state: CalendarState | RangeCalendarState
showYearPicker: boolean
setShowYearPicker: (showYearPicker: boolean) => void
yearPickerPage: number
} & AriaCalendarGridProps) {
const { locale } = useLocale()
const { gridProps, headerProps, weekDays } = useCalendarGrid(
Expand All @@ -35,6 +37,7 @@ export function CalendarGrid({
state.setFocusedDate(state.focusedDate.set({ year }))
setShowYearPicker(false)
}}
yearPickerPage={yearPickerPage}
/>
) : (
<table {...gridProps} style={{ borderSpacing: '0px' }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '@equinor/eds-icons'
import { CalendarDate } from '@internationalized/date'
import { tokens } from '@equinor/eds-tokens'
import { Dispatch, SetStateAction } from 'react'

const HeaderWrapper = styled.div`
display: flex;
Expand Down Expand Up @@ -63,22 +64,28 @@ export function CalendarHeader({
nextMonthDisabled,
showYearPicker,
setShowYearPicker,
setYearPickerPage,
}: {
state: CalendarState | RangeCalendarState
title: string
previousMonthDisabled?: boolean
nextMonthDisabled?: boolean
showYearPicker: boolean
setShowYearPicker: (showYearPicker: boolean) => void
setYearPickerPage?: Dispatch<SetStateAction<number>>
}) {
return (
<HeaderWrapper>
<HeaderActions>
<Button
variant={'ghost_icon'}
aria-label={'Previous month'}
disabled={previousMonthDisabled || showYearPicker}
onClick={() => state.focusPreviousPage()}
disabled={previousMonthDisabled}
onClick={() =>
showYearPicker
? setYearPickerPage((page) => page - 1)
: state.focusPreviousPage()
}
>
<Icon data={chevron_left} />
</Button>
Expand All @@ -103,8 +110,12 @@ export function CalendarHeader({
<span style={{ flex: '1 1 auto' }}></span>
<Button
variant={'ghost_icon'}
onClick={() => state.focusNextPage()}
disabled={nextMonthDisabled || showYearPicker}
onClick={() =>
showYearPicker
? setYearPickerPage((page) => page + 1)
: state.focusNextPage()
}
disabled={nextMonthDisabled}
aria-label={'Next month'}
>
<Icon data={chevron_right} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export const RangeCalendar = forwardRef(
ref: RefObject<HTMLDivElement>,
) => {
const [showYearPicker, setShowYearPicker] = useState(false)
const [yearPickerPage, setYearPickerPage] = useState(0)

const { locale } = useLocale()
const state = useRangeCalendarState({
...props,
Expand Down Expand Up @@ -64,6 +66,7 @@ export const RangeCalendar = forwardRef(
title={title}
setShowYearPicker={setShowYearPicker}
showYearPicker={showYearPicker}
setYearPickerPage={setYearPickerPage}
/>
)}
</Popover.Header>
Expand All @@ -72,6 +75,7 @@ export const RangeCalendar = forwardRef(
state={state}
setShowYearPicker={setShowYearPicker}
showYearPicker={showYearPicker}
yearPickerPage={yearPickerPage}
/>
</Popover.Content>
{Footer && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ const GridColumn = styled.button<{ $active: boolean }>`
}
`

const TOTAL_VISIBLE_YEARS = 36
const RANGE_OFFSET = 30 / 2

const GridFocusManager = ({
year: selectedYear,
setFocusedYear,
yearPickerPage,
}: {
setFocusedYear: (year: number) => void
year: number
yearPickerPage?: number
}) => {
const focusManager = useFocusManager()
const onKeyDown = (e: KeyboardEvent<HTMLButtonElement>) => {
Expand Down Expand Up @@ -74,9 +79,12 @@ const GridFocusManager = ({
}
}
}

const page = yearPickerPage * TOTAL_VISIBLE_YEARS

const years = Array.from(
{ length: 36 },
(_, i) => i + (selectedYear - 30 / 2),
{ length: TOTAL_VISIBLE_YEARS },
(_, i) => i + (selectedYear + page - RANGE_OFFSET),
)
return years.map((year) => (
<GridColumn
Expand All @@ -95,14 +103,20 @@ const GridFocusManager = ({
export const YearGrid = ({
setFocusedYear,
year: selectedYear,
yearPickerPage,
}: {
setFocusedYear: (year: number) => void
year: number
yearPickerPage: number
}) => {
return (
<Grid>
<FocusScope contain restoreFocus autoFocus>
Copy link
Collaborator

Choose a reason for hiding this comment

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

With the new interactive buttons in the header, this focusScope stuff probably needs to be changed, otherwise the page changer buttons are inaccessible with keyboard. Not sure how. Perhaps FocusScope can be moved out to encompass the entire popover

<GridFocusManager year={selectedYear} setFocusedYear={setFocusedYear} />
<GridFocusManager
year={selectedYear}
setFocusedYear={setFocusedYear}
yearPickerPage={yearPickerPage}
/>
</FocusScope>
</Grid>
)
Expand Down
Loading