-
-
Notifications
You must be signed in to change notification settings - Fork 921
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(DateRangePicker): supports placing predefined ranges on the left (…
- Loading branch information
Showing
10 changed files
with
369 additions
and
131 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
140 changes: 122 additions & 18 deletions
140
docs/pages/components/date-range-picker/fragments/custom-shortcut-options.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import React, { useCallback, useState } from 'react'; | ||
import Button from '../Button'; | ||
import Stack, { StackProps } from '../Stack'; | ||
import { useUpdateEffect } from '../utils'; | ||
import { getDefaultRanges, getRanges } from './utils'; | ||
import { InnerRange, RangeType } from './types'; | ||
import { CalendarLocale } from '../locales'; | ||
|
||
export interface PredefinedRangesProps<T = any, Shortcut = T> extends StackProps { | ||
ranges?: RangeType<Shortcut>[]; | ||
calendarDate: T; | ||
locale: CalendarLocale; | ||
disabledShortcut?: (value: T) => boolean; | ||
onClickShortcut?: (value: Shortcut, closeOverlay: boolean, event: React.MouseEvent) => void; | ||
} | ||
|
||
const PredefinedRanges = React.forwardRef<HTMLDivElement, PredefinedRangesProps>((props, ref) => { | ||
const { | ||
className, | ||
disabledShortcut, | ||
onClickShortcut, | ||
calendarDate, | ||
ranges: rangesProp, | ||
locale, | ||
...rest | ||
} = props; | ||
const [ranges, setRanges] = useState<InnerRange<any>[]>(getRanges(props)); | ||
|
||
useUpdateEffect(() => { | ||
setRanges(getRanges({ ranges: rangesProp, calendarDate })); | ||
}, [calendarDate, rangesProp]); | ||
|
||
const hasLocaleKey = useCallback( | ||
(key: React.ReactNode) => getDefaultRanges(calendarDate).some(item => item.label === key), | ||
[calendarDate] | ||
); | ||
|
||
return ( | ||
<Stack className={className} ref={ref} alignItems="flex-start" spacing={4} {...rest}> | ||
{ranges.map(({ value, closeOverlay, label, ...rest }, index: number) => { | ||
const disabled = disabledShortcut?.(value); | ||
|
||
const handleClickShortcut = (event: React.MouseEvent) => { | ||
if (disabled) { | ||
return; | ||
} | ||
onClickShortcut?.(value, closeOverlay !== false ? true : false, event); | ||
}; | ||
|
||
return ( | ||
<Button | ||
appearance="link" | ||
size="sm" | ||
key={index} | ||
disabled={disabled} | ||
onClick={handleClickShortcut} | ||
{...rest} | ||
> | ||
{hasLocaleKey(label) && typeof label === 'string' ? locale?.[label] : label} | ||
</Button> | ||
); | ||
})} | ||
</Stack> | ||
); | ||
}); | ||
|
||
export default PredefinedRanges; |
Oops, something went wrong.
8df4a61
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.
Successfully deployed to the following URLs:
rsuite-nextjs – ./docs
rsuite-nextjs-rsuite.vercel.app
rsuite.vercel.app
next.rsuitejs.com
rsuitejs.com
rsuite-nextjs-git-main-rsuite.vercel.app