Skip to content

Commit 27df9d4

Browse files
author
Robert Stires
committed
Add ToggleBox.
1 parent 85b186a commit 27df9d4

File tree

2 files changed

+103
-83
lines changed

2 files changed

+103
-83
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import * as React from 'react';
2+
import { compose } from 'redux';
3+
4+
import {
5+
withStyles,
6+
Theme,
7+
WithStyles,
8+
StyleRulesCallback,
9+
} from 'material-ui/styles';
10+
import Button from 'material-ui/Button';
11+
import ViewList from 'material-ui-icons/ViewList';
12+
import ViewModule from 'material-ui-icons/ViewModule';
13+
14+
type CSSClasses =
15+
'root'
16+
| 'button'
17+
| 'buttonActive'
18+
| 'buttonLeft'
19+
| 'buttonRight'
20+
| 'icon';
21+
22+
const styles: StyleRulesCallback<CSSClasses> = (theme: Theme) => ({
23+
root: {
24+
marginBottom: theme.spacing.unit * 2,
25+
},
26+
button: {
27+
border: '1px solid #333',
28+
},
29+
buttonActive: {
30+
backgroundColor: '#333',
31+
color: '#f3f3f3',
32+
'&:hover': {
33+
backgroundColor: '#333',
34+
},
35+
},
36+
buttonLeft: {
37+
borderWidth: '1px 0 1px 1px',
38+
borderRadius: '5px 0 0 5px',
39+
},
40+
buttonRight: {
41+
borderWidth: '1px 1px 1px 0',
42+
borderRadius: '0 5px 5px 0',
43+
},
44+
icon: {
45+
marginRight: theme.spacing.unit,
46+
},
47+
});
48+
49+
interface Props {
50+
handleClick: (v: 'grid' | 'list') => void;
51+
status: 'grid' | 'list';
52+
}
53+
54+
const styled = withStyles(styles, { withTheme: true });
55+
56+
type CombinedProps = Props & WithStyles<CSSClasses>;
57+
58+
export const ToggleBox: React.StatelessComponent<CombinedProps> = (props) => {
59+
const {
60+
classes,
61+
handleClick,
62+
status,
63+
} = props;
64+
65+
return (
66+
<div className={classes.root}>
67+
<Button
68+
onClick={() => handleClick('list')}
69+
className={`
70+
${!status || status === 'list' && classes.buttonActive}
71+
${classes.button}
72+
${classes.buttonLeft}`
73+
}
74+
>
75+
<ViewList className={classes.icon} />
76+
List
77+
</Button>
78+
<Button
79+
onClick={() => handleClick('grid')}
80+
className={`
81+
${status === 'grid' && classes.buttonActive}
82+
${classes.button}
83+
${classes.buttonRight}`
84+
}
85+
>
86+
<ViewModule className={classes.icon} />
87+
Grid
88+
</Button>
89+
</div>
90+
);
91+
};
92+
93+
export default compose(
94+
styled,
95+
)(ToggleBox);

src/features/linodes/ListLinodes/index.tsx

Lines changed: 8 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -7,68 +7,22 @@ import {
77
import { compose } from 'redux';
88
import { pathOr } from 'ramda';
99

10-
import {
11-
withStyles,
12-
Theme,
13-
WithStyles,
14-
StyleRulesCallback,
15-
} from 'material-ui/styles';
16-
import Button from 'material-ui/Button';
17-
10+
import WithDocumentation from 'src/components/WithDocumentation';
1811

1912
import LinodesListView from './LinodesListView';
2013
import LinodesGridView from './LinodesGridView';
21-
import ViewList from 'material-ui-icons/ViewList';
22-
import ViewModule from 'material-ui-icons/ViewModule';
23-
24-
import WithDocumentation from 'src/components/WithDocumentation';
25-
2614
import ListLinodesEmptyState from './ListLinodesEmptyState';
15+
import ToggleBox from './ToggleBox';
2716

2817
import './linodes.css';
2918

30-
type CSSClasses =
31-
'toggleBox'
32-
| 'toggleButton'
33-
| 'toggleButtonActive'
34-
| 'toggleButtonLeft'
35-
| 'toggleButtonRight'
36-
| 'icon';
37-
38-
const styles: StyleRulesCallback<CSSClasses> = (theme: Theme) => ({
39-
toggleBox: {
40-
marginBottom: theme.spacing.unit * 2,
41-
},
42-
toggleButton: {
43-
border: '1px solid #333',
44-
},
45-
toggleButtonActive: {
46-
backgroundColor: '#333',
47-
color: '#f3f3f3',
48-
'&:hover': {
49-
backgroundColor: '#333',
50-
},
51-
},
52-
toggleButtonLeft: {
53-
borderWidth: '1px 0 1px 1px',
54-
borderRadius: '5px 0 0 5px',
55-
},
56-
toggleButtonRight: {
57-
borderWidth: '1px 1px 1px 0',
58-
borderRadius: '0 5px 5px 0',
59-
},
60-
icon: {
61-
marginRight: theme.spacing.unit,
62-
},
63-
});
64-
6519
interface Props {
6620
linodes: Linode.Linode[];
6721
images: Linode.Image[];
6822
types: Linode.LinodeType[];
6923
}
7024

71-
type CombinedProps = Props & WithStyles<CSSClasses> & RouteComponentProps<{}>;
25+
type CombinedProps = Props & RouteComponentProps<{}>;
7226

7327
class ListLinodes extends React.Component<CombinedProps> {
7428
static defaultProps = {
@@ -102,48 +56,20 @@ class ListLinodes extends React.Component<CombinedProps> {
10256
},
10357
];
10458

105-
changeViewStyle(style: string) {
59+
changeViewStyle = (style: string) => {
10660
const { history } = this.props;
10761
history.push(`#${style}`);
10862
}
10963

110-
toggleBox() {
111-
const { classes, location: { hash } } = this.props;
112-
113-
return (
114-
<div className={classes.toggleBox}>
115-
<Button
116-
onClick={() => this.changeViewStyle('list')}
117-
className={`
118-
${hash !== '#grid' && classes.toggleButtonActive}
119-
${classes.toggleButton}
120-
${classes.toggleButtonLeft}`
121-
}
122-
>
123-
<ViewList className={classes.icon} />
124-
List
125-
</Button>
126-
<Button
127-
onClick={() => this.changeViewStyle('grid')}
128-
className={`
129-
${hash === '#grid' && classes.toggleButtonActive}
130-
${classes.toggleButton}
131-
${classes.toggleButtonRight}`
132-
}
133-
>
134-
<ViewModule className={classes.icon} />
135-
Grid
136-
</Button>
137-
</div>
138-
);
139-
}
140-
14164
listLinodes() {
14265
const { location: { hash } } = this.props;
14366

14467
return (
14568
<React.Fragment>
146-
{this.toggleBox()}
69+
<ToggleBox
70+
handleClick={this.changeViewStyle}
71+
status={hash === '#grid' ? 'grid' : 'list'}
72+
/>
14773
{hash === '#grid'
14874
? <LinodesGridView
14975
linodes={this.props.linodes}
@@ -183,6 +109,5 @@ const mapStateToProps = (state: any) => ({
183109

184110
export default compose(
185111
connect<Props>(mapStateToProps),
186-
withStyles(styles, { withTheme: true }),
187112
withRouter,
188113
)(ListLinodes);

0 commit comments

Comments
 (0)