react-toolbox/spec/components/autocomplete.js

71 lines
1.9 KiB
JavaScript
Raw Normal View History

import React from 'react';
2015-09-21 11:01:52 +03:00
import Autocomplete from '../../components/autocomplete';
2015-10-22 02:31:17 +03:00
class AutocompleteTest extends React.Component {
state = {
2015-11-08 18:49:00 +03:00
simple: 'Spain',
simpleShowAll: 'England',
2016-07-28 05:53:09 +03:00
multiple: ['ES-es', 'TH-th'],
countriesArray: ['Spain', 'England', 'USA', 'Thailand', 'Tongo', 'Slovenia'],
countriesObject: {'ES-es': 'Spain', 'TH-th': 'Thailand', 'EN-gb': 'England',
'EN-en': 'United States of America', 'EN-nz': 'New Zealand'}
};
2015-09-21 11:01:52 +03:00
2015-11-08 18:49:00 +03:00
handleMultipleChange = (value) => {
2016-07-28 05:53:09 +03:00
this.setState({
multiple: value,
countriesObject: {
...this.state.countriesObject,
...!this.state.countriesObject[value[0]] ? {[value[0]]: value[0]} : {}
}
});
2015-10-22 02:31:17 +03:00
};
2015-09-21 11:01:52 +03:00
2015-11-08 18:49:00 +03:00
handleSimpleChange = (value) => {
this.setState({simple: value});
};
2015-09-21 11:01:52 +03:00
handleSimpleShowAllChange = (value) => {
this.setState({simpleShowAll: value});
};
2015-11-08 18:49:00 +03:00
render () {
2015-09-21 11:01:52 +03:00
return (
<section>
2015-10-08 17:35:34 +03:00
<h5>Autocomplete</h5>
<p>You can have a multiple or simple autocomplete.</p>
2015-09-21 11:01:52 +03:00
<Autocomplete
2015-11-08 18:49:00 +03:00
onChange={this.handleMultipleChange}
2015-12-20 19:39:10 +03:00
label="Pick multiple elements..."
2016-07-28 05:53:09 +03:00
source={this.state.countriesObject}
2015-11-08 18:49:00 +03:00
value={this.state.multiple}
suggestionMatch="anywhere"
2016-07-28 05:53:09 +03:00
create={true}
2015-11-08 18:49:00 +03:00
/>
2015-09-21 11:01:52 +03:00
<Autocomplete
2015-11-08 18:49:00 +03:00
label="Choose a country"
2016-03-25 03:31:36 +03:00
hint="Elements up to you..."
2015-09-21 11:01:52 +03:00
multiple={false}
2015-11-08 18:49:00 +03:00
onChange={this.handleSimpleChange}
2016-07-28 05:53:09 +03:00
source={this.state.countriesArray}
2015-11-08 18:49:00 +03:00
value={this.state.simple}
/>
<Autocomplete
label="Choose a country (showing all suggestions)"
hint="Elements up to you..."
multiple={false}
onChange={this.handleSimpleShowAllChange}
2016-07-28 05:53:09 +03:00
source={this.state.countriesArray}
value={this.state.simpleShowAll}
showSuggestionsWhenValueIsSet
2015-11-08 18:49:00 +03:00
/>
2015-09-21 11:01:52 +03:00
</section>
);
}
2015-10-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
export default AutocompleteTest;