Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

flatpicker refactor

kruzhambusa17ed85e9248
diff --git a/src/pages/Project/View/ViewProject.tsx b/src/pages/Project/View/ViewProject.tsx
index 6981e7789..2c15bb9ed 100644
--- a/src/pages/Project/View/ViewProject.tsx
+++ b/src/pages/Project/View/ViewProject.tsx
@@ -1691,6 +1691,7 @@ const ViewProject = ({
}}
value={isActiveCompare ? dateRangeCompare || [] : dateRange || []}
maxDateMonths={MAX_MONTHS_IN_PAST}
+ maxRange={10}
/>
</div>
</div>
diff --git a/src/ui/Flatpicker.tsx b/src/ui/Flatpicker.tsx
index 2fe746ec1..febe5367c 100644
--- a/src/ui/Flatpicker.tsx
+++ b/src/ui/Flatpicker.tsx
@@ -21,18 +21,38 @@ interface FlatPickerProps {
value?: Date[],
maxDateMonths?: number,
options?: any,
+ maxRange?: number,
}
-class FlatPicker extends React.Component<FlatPickerProps> {
+class FlatPicker extends React.Component<FlatPickerProps, {
+ maxDate: string,
+ minDate: Date | string,
+}> {
private calendar = createRef<Flatpickr>()
constructor(props: FlatPickerProps) {
super(props)
this.setCustomDate = this.setCustomDate.bind(this)
+ this.state = {
+ maxDate: 'today',
+ minDate: this.removeMonths(new Date(), props?.maxDateMonths || 24),
+ }
}
private setCustomDate(dates: Date[]) {
- const { onChange } = this.props
+ const { onChange, maxRange } = this.props
+
+ // set max date to the first date selected using maxRange
+ if (maxRange && _size(dates) === 1) {
+ const maxDate = new Date(dates[0])
+ const minDate = new Date(dates[0])
+ maxDate.setDate(maxDate.getDate() + maxRange)
+ minDate.setDate(minDate.getDate() - maxRange)
+ this.setState({
+ maxDate: maxDate.toISOString().split('T')[0],
+ minDate: minDate.toISOString().split('T')[0],
+ })
+ }
if (_size(dates) === 2) {
onChange?.(dates)
@@ -56,6 +76,7 @@ class FlatPicker extends React.Component<FlatPickerProps> {
public render() {
const { value = [], maxDateMonths = MAX_MONTHS_IN_PAST, options } = this.props
+ const { maxDate, minDate } = this.state
if (options) {
return (
@@ -88,8 +109,8 @@ class FlatPicker extends React.Component<FlatPickerProps> {
value={value}
options={{
mode: 'range',
- maxDate: 'today',
- minDate: this.removeMonths(new Date(), maxDateMonths),
+ maxDate,
+ minDate,
showMonths: 1,
static: true,
animate: true,
@@ -119,6 +140,7 @@ FlatPicker.defaultProps = {
value: [],
maxDateMonths: MAX_MONTHS_IN_PAST,
options: null,
+ maxRange: 0,
}
export default memo(FlatPicker)