summaryrefslogtreecommitdiffstats
path: root/front/odiparpack/app/containers/Forms/demos/DateInput.js
blob: fc3397c96d12a694671d6a5451151a1b0ed1ad28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import React, { Fragment, PureComponent } from 'react';
import PropTypes from 'prop-types';
import { DatePicker, KeyboardDatePicker, MuiPickersUtilsProvider } from '@material-ui/pickers';
import MomentUtils from '@date-io/moment';
import DateFnsUtils from '@date-io/date-fns';
import { withStyles } from '@material-ui/core/styles';

import frLocale from 'date-fns/locale/fr';
import ruLocale from 'date-fns/locale/ru';
import enLocale from 'date-fns/locale/en-US';

import { MenuItem, Menu, Icon, IconButton, Typography, Grid } from '@material-ui/core';

const localeMap = {
  en: enLocale,
  fr: frLocale,
  ru: ruLocale,
};

const styles = theme => ({
  demo: {
    height: 240,
  },
  divider: {
    display: 'block',
    margin: `${theme.spacing(3)}px 0`,
  },
  picker: {
    margin: `${theme.spacing(3)}px 5px`,
  }
});

class DateInput extends PureComponent {
  state = {
    selectedDate: new Date(),
    anchorEl: null,
    currentLocale: 'fr',
  }

  handleDateChange = (date) => {
    this.setState({ selectedDate: date });
  }

  handleMenuOpen = (event) => {
    event.stopPropagation();
    this.setState({ anchorEl: event.currentTarget });
  }

  handleMenuClose = () => {
    this.setState({ anchorEl: null });
  };

  selectLocale = (selectedLocale) => {
    this.setState({
      currentLocale: selectedLocale,
      anchorEl: null,
    });
  }

  render() {
    const { selectedDate, currentLocale, anchorEl } = this.state;
    const { classes } = this.props;
    const locale = localeMap[currentLocale];
    return (
      <Fragment>
        <Grid
          container
          alignItems="center"
          justify="space-around"
          direction="row"
        >
          <Grid
            item
            md={4}
            className={classes.demo}
          >
            <Typography variant="button" className={classes.divider}>Basic usage</Typography>
            <div className={classes.picker}>
              <MuiPickersUtilsProvider utils={MomentUtils}>
                <DatePicker
                  label="Basic example"
                  value={selectedDate}
                  onChange={this.handleDateChange}
                  animateYearScrolling={false}
                />
              </MuiPickersUtilsProvider>
            </div>

            <div className={classes.picker}>
              <MuiPickersUtilsProvider utils={MomentUtils}>
                <DatePicker
                  label="Clearable"
                  clearable
                  disableFuture
                  maxDateMessage="Date must be less than today"
                  value={selectedDate}
                  onChange={this.handleDateChange}
                  animateYearScrolling={false}
                />
              </MuiPickersUtilsProvider>
            </div>
          </Grid>
          <Grid
            item
            md={4}
            className={classes.demo}
          >
            <Typography variant="button" className={classes.divider}>Keyboard Input</Typography>
            <div className={classes.picker}>
              <MuiPickersUtilsProvider utils={MomentUtils}>
                <KeyboardDatePicker
                  clearable
                  label="Uncontrolled input"
                  value={selectedDate}
                  onChange={this.handleDateChange}
                  animateYearScrolling={false}
                />
              </MuiPickersUtilsProvider>
            </div>

            <div className={classes.picker}>
              <MuiPickersUtilsProvider utils={MomentUtils}>
                <KeyboardDatePicker
                  label="Masked input"
                  format="DD/MM/YYYY"
                  placeholder="10/10/2018"
                  mask={[/\d/, /\d/, '/', /\d/, /\d/, '/', /\d/, /\d/, /\d/, /\d/]}
                  value={selectedDate}
                  onChange={this.handleDateChange}
                  animateYearScrolling={false}
                />
              </MuiPickersUtilsProvider>
            </div>
          </Grid>
          <Grid
            item
            md={4}
            className={classes.demo}
          >
            <Typography variant="button" className={classes.divider}>Localization</Typography>
            <div className={classes.picker}>
              <div className={classes.divider}>
                <MuiPickersUtilsProvider utils={DateFnsUtils} locale={localeMap[currentLocale]}>
                  <DatePicker
                    value={selectedDate}
                    onChange={this.handleDateChange}
                    InputProps={{
                      endAdornment: (
                        <IconButton
                          aria-label="Select locale"
                          aria-owns={anchorEl ? 'locale-menu' : null}
                          onClick={this.handleMenuOpen}
                        >
                          <Icon> more_vert </Icon>
                        </IconButton>
                      ),
                    }}
                  />
                </MuiPickersUtilsProvider>
              </div>

              <Menu
                id="locale-menu"
                anchorEl={anchorEl}
                open={Boolean(anchorEl)}
                onClose={this.handleMenuClose}
              >
                {
                  Object.keys(localeMap).map(localeItem => (
                    <MenuItem
                      key={localeItem}
                      selected={localeItem === locale}
                      onClick={() => this.selectLocale(localeItem)}
                    >
                      {localeItem}
                    </MenuItem>
                  ))
                }
              </Menu>
            </div>
          </Grid>
        </Grid>
      </Fragment>
    );
  }
}


DateInput.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(DateInput);