ButtonBar.tsx 4.91 KB
Newer Older
Oleksii Savran's avatar
Oleksii Savran committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import * as React from 'react';

import { withStyles } from '@material-ui/core/styles';
import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import MenuItem from '@material-ui/core/MenuItem';
import Menu from '@material-ui/core/Menu';
import ListItemIcon from '@material-ui/core/ListItemIcon';
import ReorderIcon from '@material-ui/icons/Reorder';
import Button from '@material-ui/core/Button';

const styles = (theme) => ({
  root: {
    width: '100%',
    display: 'flex' as 'flex',
    flexFlow: 'row wrap' as 'row wrap',
    justifyContent: 'flex-start' as 'flex-start',
    [theme.breakpoints.down('md')]: {
      justifyContent: 'flex-end' as 'flex-end',
    },
  },
  /* tslint:disable */
23
  barText: {
Oleksii Savran's avatar
Oleksii Savran committed
24
25
26
27
28
29
30
31
    '& > *:first-child': {
      display: 'block',
      [theme.breakpoints.down('xs')]: {
        display: 'none',
      },
    },
  },
  menuItem: {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
32
    padding: '0',
Maxym Borodenko's avatar
Maxym Borodenko committed
33
34
35
36
    '& > div, & > span': {
      width: '100%',
      margin: '0 !important',
    },
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
37
    height: '100%',
38
    '& button': {
Oleksii Savran's avatar
Oleksii Savran committed
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
      width: '100%',
      height: '100%',
      '& > span': {
        justifyContent: 'flex-start' as 'flex-start',
      }
    },
    '& > a': {
      width: '100%',
      '& > button': {
        width: '100%',
        '& > span': {
          justifyContent: 'flex-start' as 'flex-start',
        }
      },
    },
    '& > span': {
      width: '100%',
      '& > button': {
        width: '100%',
        margin: '0',
        '& > span': {
          justifyContent: 'flex-start' as 'flex-start',
        }
      },
    }
  },
  mobile: {
    display: 'none',
Oleksii Savran's avatar
Oleksii Savran committed
67
    [theme.breakpoints.down(1512)]: {
Oleksii Savran's avatar
Oleksii Savran committed
68
69
70
71
72
73
74
      display: 'block',
    },
  },
  desktop: {
    display: 'block',
    '& > *': {
      display: 'inline-flex',
Oleksii Savran's avatar
Oleksii Savran committed
75
76
77
78
79
80
      'html[dir="ltr"] &': {
        marginRight: '10px',
      },
      'html[dir="rtl"] &': {
        marginLeft: '10px',
      },
Oleksii Savran's avatar
Oleksii Savran committed
81
    },
Oleksii Savran's avatar
Oleksii Savran committed
82
    [theme.breakpoints.down(1512)]: {
Oleksii Savran's avatar
Oleksii Savran committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
      display: 'none',
    },
  },

  icon: {
    margin: '5px',
    [theme.breakpoints.down('xs')]: {
      margin: '10px',
    }
  },
  list: {
    '& > div > ul': {
      padding: '0',
      '& button': {
        boxShadow: 'none',
98
        backgroundColor: 'transparent !important',
Oleksii Savran's avatar
Oleksii Savran committed
99
100
101
102
103
104
105
106
107
108
109
      }
    },
  },
  firstAction: {
    '& > span': {
      color: 'rgba(0, 0, 0, 0.87)',
      '& > button': {
        boxShadow: 'none',
        backgroundColor: 'transparent',
      }
    },
Oleksii Savran's avatar
Oleksii Savran committed
110
111
112
  },
  hidden: {
    visibility: 'hidden' as 'hidden',
Oleksii Savran's avatar
Oleksii Savran committed
113
114
115
116
117
118
119
  }
  /* tslint:enable */
});

interface IButtonBarProps extends React.Props<any> {
  classes?: any;
  children?: any[];
120
  barLabelText?: any;
Oleksii Savran's avatar
Oleksii Savran committed
121
122
123
124
125
126
127
}

class ButtonBar extends React.Component<IButtonBarProps, any> {
  constructor(props: any) {
    super(props);
    this.state = {
      anchorEl: null,
Oleksii Savran's avatar
Oleksii Savran committed
128
      hidden: true,
Oleksii Savran's avatar
Oleksii Savran committed
129
130
131
132
133
    };
  }

  private handleClickListItem = (event) => {
    this.setState({ anchorEl: event.currentTarget });
Oleksii Savran's avatar
Oleksii Savran committed
134
    this.setState({ hidden: false });
Oleksii Savran's avatar
Oleksii Savran committed
135
136
137
  }

  private handleMenuItemClick = (event, index) => {
Oleksii Savran's avatar
Oleksii Savran committed
138
    this.setState({ anchorEl: null });
Oleksii Savran's avatar
Oleksii Savran committed
139
    this.setState({ hidden: true });
Oleksii Savran's avatar
Oleksii Savran committed
140
141
142
  }

  private handleClose = () => {
Oleksii Savran's avatar
Oleksii Savran committed
143
    this.setState({ anchorEl: null });
Oleksii Savran's avatar
Oleksii Savran committed
144
    this.setState({ hidden: true });
Oleksii Savran's avatar
Oleksii Savran committed
145
146
147
  }

  public render() {
148
    const { classes, children, barLabelText } = this.props;
Oleksii Savran's avatar
Oleksii Savran committed
149
    const { anchorEl, hidden } = this.state;
150
151
    const childrenArr = React.Children.toArray(children) as any;

Oleksii Savran's avatar
Oleksii Savran committed
152
153
154
155
156
157

    return (
      <div className={ classes.root }>
          <div className={ classes.mobile }>
            <List className={ `p-0` }>
              <ListItem
158
                className={ `p-0 ${ barLabelText ? classes.barText : '' }` }
Oleksii Savran's avatar
Oleksii Savran committed
159
160
161
162
163
                button
                aria-haspopup="true"
                aria-controls="button-menu"
                onClick={ this.handleClickListItem }
              >
164
                { barLabelText && <Button disabled className={ classes.firstAction }>{ barLabelText }</Button> }
Oleksii Savran's avatar
Oleksii Savran committed
165
166
167
168
169
170
171
172
173
174
                <ListItemIcon className={ `${classes.icon}` }>
                  <ReorderIcon />
                </ListItemIcon>
              </ListItem>
            </List>
            <Menu
              id="button-menu"
              anchorEl={ anchorEl }
              open={ Boolean(anchorEl) }
              onClose={ this.handleClose }
Oleksii Savran's avatar
Oleksii Savran committed
175
              className={ `${classes.list} ${hidden ? classes.hidden : ''}` }
Oleksii Savran's avatar
Oleksii Savran committed
176
177
            >
              { childrenArr.map((button, index) => (
178
                button && !(button.props && button.props.children === false) && (
Oleksii Savran's avatar
Oleksii Savran committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
                  <MenuItem
                    key={ index }
                    selected={ null }
                    onClick={ (event) => this.handleMenuItemClick(event, index) }
                    className={ classes.menuItem }
                  >
                    { button }
                  </MenuItem>
                )
              )) }
            </Menu>
          </div>
          <div className={ classes.desktop }>
            { children }
          </div>
      </div>
    );
  }
}

export default withStyles(styles)(ButtonBar);