Skip to content

Commit

Permalink
Merge pull request #163 from salockhart/master
Browse files Browse the repository at this point in the history
[New Component] Auto Complete
  • Loading branch information
Ryan B. Blakeley authored Nov 21, 2016
2 parents 79bd81d + 1aa874c commit 517e9f3
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ import FormsySelect from 'formsy-material-ui/lib/FormsySelect';
import FormsyText from 'formsy-material-ui/lib/FormsyText';
import FormsyTime from 'formsy-material-ui/lib/FormsyTime';
import FormsyToggle from 'formsy-material-ui/lib/FormsyToggle';
import FormsyAutoComplete from 'formsy-material-ui/lib/FormsyAutoComplete';
```

OR:

```js
import { FormsyCheckbox, FormsyDate, FormsyRadio, FormsyRadioGroup,
FormsySelect, FormsyText, FormsyTime, FormsyToggle } from 'formsy-material-ui/lib';
FormsySelect, FormsyText, FormsyTime, FormsyToggle, FormsyAutoComplete } from 'formsy-material-ui/lib';
```

### Events
Expand Down
12 changes: 11 additions & 1 deletion examples/webpack-example/src/app/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Paper from 'material-ui/Paper';
import RaisedButton from 'material-ui/RaisedButton';
import MenuItem from 'material-ui/MenuItem';
import { FormsyCheckbox, FormsyDate, FormsyRadio, FormsyRadioGroup,
FormsySelect, FormsyText, FormsyTime, FormsyToggle } from 'formsy-material-ui/lib';
FormsySelect, FormsyText, FormsyTime, FormsyToggle, FormsyAutoComplete } from 'formsy-material-ui/lib';

const Main = React.createClass({

Expand Down Expand Up @@ -103,6 +103,16 @@ const Main = React.createClass({
<MenuItem value={'nightly'} primaryText="Every Night" />
<MenuItem value={'weeknights'} primaryText="Weeknights" />
</FormsySelect>
<FormsyAutoComplete
name="frequency-auto-complete"
required
floatingLabelText="How often do you?"
dataSource={[
'Never',
'Every Night',
'Weeknights'
]}
/>
<FormsyCheckbox
name="agree"
label="Do you agree to disagree?"
Expand Down
71 changes: 71 additions & 0 deletions src/FormsyAutoComplete.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React from 'react';
import keycode from 'keycode';
import Formsy from 'formsy-react';
import AutoComplete from 'material-ui/AutoComplete';
import { setMuiComponentAndMaybeFocus } from 'formsy-react/src/utils';

const FormsyAutoComplete = React.createClass({

propTypes: {
defaultValue: React.PropTypes.any,
name: React.PropTypes.string.isRequired,
onBlur: React.PropTypes.func,
onChange: React.PropTypes.func,
onFocus: React.PropTypes.func,
onKeyDown: React.PropTypes.func,
value: React.PropTypes.any,
},

mixins: [Formsy.Mixin],

getInitialState() {
return {
value: this.props.defaultValue || this.props.value || '',
};
},

componentWillMount() {
this.setValue(this.props.defaultValue || this.props.value || '');
},

handleBlur: function handleBlur(event) {
this.setValue(event.currentTarget.value);
if (this.props.onBlur) this.props.onBlur(event);
},

handleChange: function handleChange(event) {
this.setState({
value: event.currentTarget.value,
});
if (this.props.onChange) this.props.onChange(event);
},

handleKeyDown: function handleKeyDown(event) {
if (keycode(event) === 'enter') this.setValue(event.currentTarget.value);
if (this.props.onKeyDown) this.props.onKeyDown(event, event.currentTarget.value);
},

setMuiComponentAndMaybeFocus: setMuiComponentAndMaybeFocus,

render() {
const {
defaultValue, // eslint-disable-line no-unused-vars
onFocus,
value, // eslint-disable-line no-unused-vars
...rest } = this.props;
return (
<AutoComplete
{...rest}
errorText={this.getErrorMessage()}
onBlur={this.handleBlur}
onChange={this.handleChange}
onFocus={onFocus}
onKeyDown={this.handleKeyDown}
ref={this.setMuiComponentAndMaybeFocus}
value={this.state.value}
/>
);
},
});

export default FormsyAutoComplete;
1 change: 1 addition & 0 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export FormsySelect from './FormsySelect';
export FormsyText from './FormsyText';
export FormsyTime from './FormsyTime';
export FormsyToggle from './FormsyToggle';
export FormsyAutoComplete from './FormsyAutoComplete';

0 comments on commit 517e9f3

Please sign in to comment.