-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LG-3839: Date picker switch selects (#2178)
* wip, allow flipping of selects * add const * add changeset * only checks for iso * fix test * remove only --------- Co-authored-by: Brooke Scarlett Yalof <[email protected]>
- Loading branch information
Showing
7 changed files
with
216 additions
and
82 deletions.
There are no files selected for viewing
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,5 @@ | ||
--- | ||
'@leafygreen-ui/date-picker': patch | ||
--- | ||
|
||
Rearranges the placement of the year select to come before the month select when the `locale` is `iso8601`. [LG-3839](https://jira.mongodb.org/browse/LG-3839) |
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
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
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
70 changes: 70 additions & 0 deletions
70
...e-picker/src/DatePicker/DatePickerMenu/DatePickerMenuSelect/DatePickerMenuSelectMonth.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,70 @@ | ||
import React, { useCallback } from 'react'; | ||
|
||
import { getLocaleMonths, setUTCMonth } from '@leafygreen-ui/date-utils'; | ||
import { cx } from '@leafygreen-ui/emotion'; | ||
import { Option, Select } from '@leafygreen-ui/select'; | ||
|
||
import { selectElementProps } from '../../../shared/constants'; | ||
import { useSharedDatePickerContext } from '../../../shared/context'; | ||
import { useDatePickerContext } from '../../DatePickerContext'; | ||
import { | ||
selectInputWidthStyles, | ||
selectTruncateStyles, | ||
} from '../DatePickerMenu.styles'; | ||
import { shouldMonthBeEnabled } from '../DatePickerMenuHeader/utils'; | ||
|
||
interface DatePickerMenuSelectMonthProps { | ||
updateMonth: (newMonth: Date) => void; | ||
} | ||
|
||
/** | ||
* Month Select | ||
* @internal | ||
*/ | ||
export const DatePickerMenuSelectMonth = ({ | ||
updateMonth, | ||
}: DatePickerMenuSelectMonthProps) => { | ||
const { setIsSelectOpen, locale, min, max } = useSharedDatePickerContext(); | ||
const { month } = useDatePickerContext(); | ||
const monthOptions = getLocaleMonths(locale); | ||
|
||
/** Returns whether the provided month should be enabled */ | ||
const isMonthEnabled = useCallback( | ||
(monthName: string) => | ||
shouldMonthBeEnabled(monthName, { month, min, max, locale }), | ||
[locale, max, min, month], | ||
); | ||
|
||
const handleMonthOnChange = (value: string) => { | ||
const newMonth = setUTCMonth(month, Number(value)); | ||
updateMonth(newMonth); | ||
}; | ||
|
||
return ( | ||
<Select | ||
{...selectElementProps} | ||
aria-label={`Select month - ${ | ||
monthOptions[month.getUTCMonth()].long | ||
} selected`} | ||
value={month.getUTCMonth().toString()} | ||
onChange={handleMonthOnChange} | ||
className={cx(selectTruncateStyles, selectInputWidthStyles)} | ||
onEntered={() => setIsSelectOpen(true)} | ||
onExited={() => setIsSelectOpen(false)} | ||
placeholder={monthOptions[month.getUTCMonth()].short} | ||
> | ||
{monthOptions.map((m, i) => ( | ||
<Option | ||
disabled={!isMonthEnabled(m.long)} | ||
value={i.toString()} | ||
key={m.short} | ||
aria-label={m.long} | ||
> | ||
{m.short} | ||
</Option> | ||
))} | ||
</Select> | ||
); | ||
}; | ||
|
||
DatePickerMenuSelectMonth.displayName = 'DatePickerMenuSelectMonth'; |
Oops, something went wrong.