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: focus next range on defined range click #3

Merged
merged 4 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ inputRanges(`DefinedRange`, `DateRangePicker`) | Array | [default i
ariaLabels | Object | {} | inserts aria-label to inner elements
dayContentRenderer | Function | null | Function to customize the rendering of Calendar Day. given a date is supposed to return what to render.
preventScrollToFocusedMonth | Boolean | false | When two or more months are open, prevent the shift of the focused month to the left.
focusNextRangeOnDefinedRangeClick | Boolean | false | When a defined range is clicked, the next range will be focused

#### Type DateRange:
```ts
Expand Down
12 changes: 6 additions & 6 deletions src/components/DateRangePicker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ export default function DateRangePicker({
monthDisplayFormat,
months,
moveRangeOnFirstSelection,
preventScrollToFocusedMonth
preventScrollToFocusedMonth,
focusNextRangeOnDefinedRangeClick
}: DateRangePickerProps) {

const refs = React.useRef({
styles: generateStyles([Styles, classNames])
});
Expand All @@ -64,7 +64,6 @@ export default function DateRangePicker({
rangePreview: undefined
});


return (
<div className={classnames(refs.current.styles.dateRangePickerWrapper, className)}>
<DefinedRange
Expand All @@ -75,9 +74,11 @@ export default function DateRangePicker({
renderStaticRangeLabel={renderStaticRangeLabel}
headerContent={headerContent}
footerContent={footerContent}
focusNextRangeOnDefinedRangeClick={focusNextRangeOnDefinedRangeClick}
onRangeFocusChange={(focusedRange) => setState((s) => ({ ...s, focusedRange }))}
/>
<DateRange
onRangeFocusChange={focusedRange => setState(s => ({...s, focusedRange}))}
onRangeFocusChange={(focusedRange) => setState((s) => ({ ...s, focusedRange }))}
focusedRange={focusedRange || state.focusedRange}
weekStartsOn={weekStartsOn}
weekdayDisplayFormat={weekdayDisplayFormat}
Expand Down Expand Up @@ -120,6 +121,5 @@ export default function DateRangePicker({
preventScrollToFocusedMonth={preventScrollToFocusedMonth}
/>
</div>
)

);
}
12 changes: 11 additions & 1 deletion src/components/DefinedRange/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import styles from '../../styles';
import { defaultInputRanges, defaultStaticRanges } from '../../defaultRanges';
import { DateRange } from '../DayCell';
import InputRangeField from '../InputRangeField';
import { findNextRangeIndex } from '../../utils';

export type DefinedRangeProps = {
inputRanges?: {label: string, range: (value: number) => DateRange, getCurrentValue: (range: DateRange) => number | "-" | "∞"}[],
Expand All @@ -14,9 +15,11 @@ export type DefinedRangeProps = {
footerContent?: ReactElement,
focusedRange?: number[],
rangeColors?: string[],
focusNextRangeOnDefinedRangeClick?: boolean,
onPreviewChange?: (value?: DateRange) => void,
onChange?: (value: {[x: string]: DateRange}) => void,
renderStaticRangeLabel?: (staticRange: DefinedRangeProps["staticRanges"][number]) => ReactElement
onRangeFocusChange?: (range: number[]) => void,
};

export default function DefinedRange({
Expand All @@ -28,9 +31,11 @@ export default function DefinedRange({
rangeColors = ['#3d91ff', '#3ecf8e', '#fed14c'],
ranges = [],
focusedRange = [0, 0],
focusNextRangeOnDefinedRangeClick,
onChange,
onPreviewChange,
renderStaticRangeLabel
renderStaticRangeLabel,
onRangeFocusChange
}: DefinedRangeProps) {

const [state, setState] = React.useState({rangeOffset: 0, focusedInput: -1});
Expand Down Expand Up @@ -59,6 +64,11 @@ export default function DefinedRange({
onChange({
[selectedRange.key || `range${focusedRange[0] + 1}`]: { ...selectedRange, ...range },
});

if (focusNextRangeOnDefinedRangeClick) {
const nextFocusRange = [findNextRangeIndex(ranges, focusedRange[0]), 0];
onRangeFocusChange?.(nextFocusRange);
}
}

const getRangeOptionValue = (option: DefinedRangeProps['inputRanges'][number]) => {
Expand Down