Skip to content

Commit

Permalink
[SelectField] Fix all linting errors and warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ctavan committed Jul 18, 2017
1 parent 6e58e9b commit 6ddcf59
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 74 deletions.
70 changes: 56 additions & 14 deletions docs/src/pages/component-demos/select-field/FilteredSelectField.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,54 @@ import { MenuItem } from 'material-ui/Menu';
import { ListItem } from 'material-ui/List';
import Card, { CardHeader, CardContent } from 'material-ui/Card';

const US_PRESIDENTS = ["George Washington","John Adams","Thomas Jefferson","James Madison","James Monroe","John Quincy Adams","Andrew Jackson","Martin Van Buren","William Henry Harrison","John Tyler","James K. Polk","Zachary Taylor","Millard Fillmore","Franklin Pierce","James Buchanan","Abraham Lincoln","Andrew Johnson","Ulysses S. Grant","Rutherford B. Hayes","James A. Garfield","Chester A. Arthur","Grover Cleveland","Benjamin Harrison","Grover Cleveland","William McKinley","Theodore Roosevelt","William Howard Taft","Woodrow Wilson","Warren G. Harding","Calvin Coolidge","Herbert Hoover","Franklin D. Roosevelt","Harry S. Truman","Dwight D. Eisenhower","John F. Kennedy","Lyndon B. Johnson","Richard Nixon","Gerald Ford","Jimmy Carter","Ronald Reagan","George H. W. Bush","Bill Clinton","George W. Bush","Barack Obama","Donald Trump"];
const US_PRESIDENTS = [
'Abraham Lincoln',
'Andrew Jackson',
'Andrew Johnson',
'Barack Obama',
'Benjamin Harrison',
'Bill Clinton',
'Calvin Coolidge',
'Chester A. Arthur',
'Donald Trump',
'Dwight D. Eisenhower',
'Franklin D. Roosevelt',
'Franklin Pierce',
'George H. W. Bush',
'George W. Bush',
'George Washington',
'Gerald Ford',
'Grover Cleveland',
'Harry S. Truman',
'Herbert Hoover',
'James A. Garfield',
'James Buchanan',
'James K. Polk',
'James Madison',
'James Monroe',
'Jimmy Carter',
'John Adams',
'John F. Kennedy',
'John Quincy Adams',
'John Tyler',
'Lyndon B. Johnson',
'Martin Van Buren',
'Millard Fillmore',
'Richard Nixon',
'Ronald Reagan',
'Rutherford B. Hayes',
'Theodore Roosevelt',
'Thomas Jefferson',
'Ulysses S. Grant',
'Warren G. Harding',
'William Henry Harrison',
'William Howard Taft',
'William McKinley',
'Woodrow Wilson',
'Zachary Taylor',
];

const styleSheet = createStyleSheet('FilteredSelectField', (theme) => {
const styleSheet = createStyleSheet('FilteredSelectField', theme => {
const { typography } = theme;
return {
filter: {
Expand Down Expand Up @@ -45,14 +90,14 @@ export default class FilteredSelectField extends Component {

handleChange = (event, index, value) => this.setState({ value });

handleFilter = (event) => {
handleFilter = event => {
const { filter: oldFilter } = this.state;
const filter = event.target.value;
const searchChoices = filter.length > oldFilter.length ? this.state.choices : US_PRESIDENTS;
const pattern = new RegExp(filter, 'i');
this.setState({
filter,
choices: searchChoices.filter(c => pattern.test(c))
choices: searchChoices.filter(c => pattern.test(c)),
});
};

Expand All @@ -61,9 +106,7 @@ export default class FilteredSelectField extends Component {

return (
<Card>
<CardHeader
title="Filtered Select Field"
/>
<CardHeader title="Filtered Select Field" />
<CardContent>
<div className={classes.row}>
<SelectField
Expand All @@ -72,19 +115,18 @@ export default class FilteredSelectField extends Component {
value={this.state.value}
onChange={this.handleChange}
>
<ListItem
tabIndex="-1"
className={classes.filter}
>
<ListItem tabIndex="-1" className={classes.filter}>
<Input
placeholder="Filter..."
value={this.state.filter}
onChange={this.handleFilter}
/>
</ListItem>
{this.state.choices.map((choice, i) => (
<MenuItem value={choice} key={i}>{choice}</MenuItem>
))}
{this.state.choices.map(choice =>
<MenuItem value={choice} key={choice}>
{choice}
</MenuItem>,
)}
</SelectField>
</div>
</CardContent>
Expand Down
22 changes: 4 additions & 18 deletions docs/src/pages/component-demos/select-field/FocusSelectField.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import TextField from 'material-ui/TextField';
import MenuItem from 'material-ui/Menu/MenuItem';
import Card, { CardHeader, CardContent } from 'material-ui/Card';


const styleSheet = createStyleSheet('FocusSelectField', () => ({
row: {
display: 'flex',
Expand Down Expand Up @@ -49,15 +48,10 @@ export default class FocusSelectField extends Component {

return (
<Card>
<CardHeader
title="Focus Select Field"
/>
<CardHeader title="Focus Select Field" />
<CardContent>
<div className={classes.row}>
<TextField
className={classes.column}
label="Focus Here"
/>
<TextField className={classes.column} label="Focus Here" />
<SelectField
className={classes.column}
label="Tab Here"
Expand All @@ -78,21 +72,13 @@ export default class FocusSelectField extends Component {
</div>
<div className={classes.row}>
<FormControlLabel
control={
<Checkbox
checked={this.state.selectFocused}
/>
}
control={<Checkbox checked={this.state.selectFocused} />}
label="Is Focused"
className={classes.column}
disabled
/>
<FormControlLabel
control={
<Checkbox
checked={this.state.selectClean}
/>
}
control={<Checkbox checked={this.state.selectClean} />}
label="Is Clean"
className={classes.column}
disabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ export default class SimpleSelectField extends Component {

return (
<Card>
<CardHeader
title="Basic Select Field"
/>
<CardHeader title="Basic Select Field" />
<CardContent>
<SelectField
className={classes.selectBox}
Expand Down
42 changes: 19 additions & 23 deletions src/SelectField/SelectField.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,41 +118,39 @@ class SelectField extends Component {
selectedIndex: undefined,
};

handleMouseDown = (event) => event.preventDefault();
handleMouseDown = event => event.preventDefault();

handleKeyDown = (event) => {
handleKeyDown = event => {
if (OPEN_MENU_KEYS.includes(keycode(event))) {
event.preventDefault();
this.setState({ open: true, anchorEl: event.currentTarget });
}
};

handleClick = (event) => {
handleClick = event => {
event.currentTarget.focus();
this.setState({ open: true, anchorEl: event.currentTarget });
};

handleRequestClose = () => this.setState({ open: false });

handleSelectFocus = (event) => {
handleSelectFocus = event => {
if (this.state.ignoreFocusOnce) {
event.stopPropagation();
this.setState({ ignoreFocusOnce: false });
}
};

handleSelectBlur = (event) => {
handleSelectBlur = event => {
if (this.state.open) {
event.stopPropagation();
}
};

handleItemClick = (event, index, value) => {
event.persist();
this.setState({
open: false,
ignoreFocusOnce: true,
}, () => {

this.setState({ open: false, ignoreFocusOnce: true }, () => {
if (this.props.onChange) {
this.props.onChange(event, index, value);
}
Expand Down Expand Up @@ -192,17 +190,12 @@ class SelectField extends Component {
};

return (
<FormControl
className={className}
error={error}
required={required}
{...other}
>
{label && !(hideLabel && value) && (
<FormControl className={className} error={error} required={required} {...other}>
{label &&
!(hideLabel && value) &&
<InputLabel className={labelClassName} shrink={initialShrink}>
{label}
</InputLabel>
)}
</InputLabel>}
<Input
className={inputClassName}
value={value}
Expand All @@ -221,12 +214,15 @@ class SelectField extends Component {
onRequestClose={this.handleRequestClose}
{...menuProps}
>
{React.Children.map(children, (child, index) =>
(typeof child.props.value === 'undefined' ? child : React.cloneElement(child, {
{React.Children.map(children, (child, index) => {
if (typeof child.props.value === 'undefined') {
return child;
}
return React.cloneElement(child, {
selected: compareFunction(value, child.props.value),
onClick: (event) => this.handleItemClick(event, index, child.props.value),
})),
)}
onClick: event => this.handleItemClick(event, index, child.props.value),
});
})}
</Menu>
</FormControl>
);
Expand Down
20 changes: 10 additions & 10 deletions src/SelectField/SelectFieldInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import classNames from 'classnames';
import customPropTypes from '../utils/customPropTypes';
import ArrowDropDownIcon from '../svg-icons/arrow-drop-down';

const styleSheet = createStyleSheet('MuiSelectFieldInput', (theme) => {
const styleSheet = createStyleSheet('MuiSelectFieldInput', theme => {
return {
container: {
position: 'relative',
Expand Down Expand Up @@ -55,24 +55,22 @@ const SelectFieldInput = (props, context) => {
);

return (
<div
onFocus={onFocus}
onBlur={onBlur}
className={classes.container}
>
<div onFocus={onFocus} onBlur={onBlur} className={classes.container}>
<select
className={selectClassName}
onFocus={onSelectFocus}
onBlur={onSelectBlur}
{...inputProps}
>
{/* Need this option for proper select sizing */}
<option className={classes.labelHolder}>{label}</option>
{React.Children.map(options, (option, index) => (
<option className={classes.labelHolder}>
{label}
</option>
{React.Children.map(options, (option, index) =>
<option key={index} value={option.props.value}>
{option.props.value && option.props.children}
</option>
))}
</option>,
)}
</select>
<ArrowDropDownIcon className={classes.icon} />
</div>
Expand Down Expand Up @@ -100,6 +98,8 @@ SelectFieldInput.propTypes = {
* Select options.
*/
options: PropTypes.arrayOf(PropTypes.node),
/** @ignore */
rowsMax: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
};

SelectFieldInput.contextTypes = {
Expand Down
5 changes: 2 additions & 3 deletions src/SelectField/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable flowtype/require-valid-file-annotation */

export default from './SelectField';
export SelectField from './SelectField';
export SelectFieldInput from './SelectFieldInput';
export { default } from './SelectField';
export { default as SelectFieldInput } from './SelectFieldInput';
5 changes: 2 additions & 3 deletions src/svg-icons/arrow-drop-down.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import React from 'react';
import pure from 'recompose/pure';
import SvgIcon from '../SvgIcon';

let ArrowDropDown = (props) => (
let ArrowDropDown = props =>
<SvgIcon {...props}>
<path d="M7,10L12,15L17,10H7Z" />
</SvgIcon>
);
</SvgIcon>;
ArrowDropDown = pure(ArrowDropDown);
ArrowDropDown.muiName = 'SvgIcon';

Expand Down

0 comments on commit 6ddcf59

Please sign in to comment.