DateFilter.tsx 2.28 KB
Newer Older
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
1
import * as React from 'react';
2
import { DateTime } from 'luxon';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { createUseStyles } from 'react-jss';
import { useTranslation } from 'react-i18next';
import { Field, FieldProps, useForm } from 'react-final-form';

// ui
import { DateField } from '@gringlobal-ce/client/ui/common/form/DateField';

const useStyles = createUseStyles((theme) => ({
  fieldsContainer: {
    'display': 'flex',
    '& > *': {
      flexBasis: '50%',
    },
    '& > div:nth-child(even)': {
      marginLeft: 8,
Maksym Tishchenko's avatar
Maksym Tishchenko committed
18
19
20
21
      'html[dir="rtl"] &' : {
        marginLeft: 0,
        marginRight: 8,
      }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
22
23
24
    },
    '& > div:nth-child(odd)': {
      marginRight: 8,
Maksym Tishchenko's avatar
Maksym Tishchenko committed
25
26
27
28
      'html[dir="rtl"] &' : {
        marginLeft: 8,
        marginRight: 0,
      }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
29
30
31
32
33
34
35
36
37
38
39
40
    },
  },
}));


interface IDateFilter extends FieldProps<any, any> {
  name: string;
  label: string;
  placeholder?: string;
  endAdornment?: any;
}

41
const validateDateFilter = (val) => !val || DateTime.fromISO(val).isValid ? undefined : 'Must be a valid date';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55

const DateFilter = ({ name, label, endAdornment, ...other }: IDateFilter) => {

  // util
  const classes = useStyles();
  const { t } = useTranslation();
  const { getFieldState, change } = useForm();

  // callback
  const swapIfNeeded = React.useCallback((ev) => {
    const valueGe = getFieldState(`${name}.ge`).value;
    const valueLe = getFieldState(`${name}.le`).value;

    // console.log(`DateFilter, valueGe: ${valueGe}, valueLe: ${valueLe}`)
56
    if (valueGe && valueLe && DateTime.fromISO(valueGe) > (DateTime.fromISO(valueLe))) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
57
58
59
60
      // console.log('DateFilter, swapping')
      change(`${name}.le`, valueGe);
      change(`${name}.ge`, valueLe);
    }
Matija Obreza's avatar
Matija Obreza committed
61
  }, [ change, name, getFieldState ]);
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

  return (
    <div>
      <div className={ classes.fieldsContainer } onBlur={ swapIfNeeded }>
        <Field
          name={ `${name}.ge` }
          type="date"
          component={ DateField }
          validate={ validateDateFilter }
          label={ `${t(label)} ${t('common:label.fromValue')}` }
          shrink
        />
        <Field
          name={ `${name}.le` }
          type="date"
          component={ DateField }
          validate={ validateDateFilter }
          label={ `${t(label)} ${t('common:label.toValue')}` }
          shrink
        />
      </div>
    </div>
  )
};

export default DateFilter;