react-toolbox/spec/components/autocomplete.js

127 lines
3.3 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',
multipleArray: ['ES-es', 'TH-th'],
2017-01-26 20:05:32 +03:00
multipleObject: { 'ES-es': 'Spain', 'TH-th': 'Thailand' },
2016-07-28 05:53:09 +03:00
countriesArray: ['Spain', 'England', 'USA', 'Thailand', 'Tongo', 'Slovenia'],
countriesObject: {
'EN-gb': 'England',
2017-01-26 20:05:32 +03:00
'EN-en': 'United States of America',
'EN-nz': 'New Zealand',
},
simpleMonth: 0,
monthsObject: {
0: 'January',
1: 'February',
2: 'March',
3: 'April',
4: 'May',
5: 'June',
6: 'July',
7: 'August',
8: 'September',
9: 'October',
10: 'November',
11: 'December',
}
};
2015-09-21 11:01:52 +03:00
2017-01-04 17:14:26 +03:00
handleFocus = (event) => {
console.log('This is focused');
console.log(event);
};
handleMultipleArrayChange = (value) => {
2016-07-28 05:53:09 +03:00
this.setState({
multipleArray: value,
2016-07-28 05:53:09 +03:00
countriesObject: {
...this.state.countriesObject,
2017-01-26 20:05:32 +03:00
...(value[0] && !this.state.countriesObject[value[0]]) ? { [value[0]]: value[0] } : {},
},
2016-07-28 05:53:09 +03:00
});
2015-10-22 02:31:17 +03:00
};
2015-09-21 11:01:52 +03:00
handleMultipleObjectChange = (value) => {
this.setState({
2017-01-26 20:05:32 +03:00
multipleObject: value,
});
};
2015-11-08 18:49:00 +03:00
handleSimpleChange = (value) => {
2017-01-26 20:05:32 +03:00
this.setState({ simple: value });
2015-11-08 18:49:00 +03:00
};
2015-09-21 11:01:52 +03:00
handleSimpleShowAllChange = (value) => {
2017-01-26 20:05:32 +03:00
this.setState({ simpleShowAll: value });
};
handleSimpleMonthChange = (value) => {
this.setState({ simpleMonth: parseInt(value, 10) });
};
2017-01-26 20:05:32 +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
allowCreate
2016-11-18 20:47:39 +03:00
keepFocusOnChange
2015-12-20 19:39:10 +03:00
label="Pick multiple elements..."
2017-01-04 17:14:26 +03:00
onFocus={this.handleFocus}
onChange={this.handleMultipleArrayChange}
source={this.state.countriesObject}
suggestionMatch="anywhere"
value={this.state.multipleArray}
/>
<Autocomplete
allowCreate
2016-11-30 18:05:52 +03:00
label="Pick multiple elements with object value..."
onChange={this.handleMultipleObjectChange}
showSelectedWhenNotInSource
2016-07-28 05:53:09 +03:00
source={this.state.countriesObject}
suggestionMatch="anywhere"
value={this.state.multipleObject}
2015-11-08 18:49:00 +03:00
/>
2015-09-21 11:01:52 +03:00
<Autocomplete
2016-03-25 03:31:36 +03:00
hint="Elements up to you..."
label="Choose a country"
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
hint="Elements up to you..."
label="Choose a country (showing all suggestions)"
multiple={false}
onChange={this.handleSimpleShowAllChange}
showSuggestionsWhenValueIsSet
2016-07-28 05:53:09 +03:00
source={this.state.countriesArray}
value={this.state.simpleShowAll}
2015-11-08 18:49:00 +03:00
/>
<Autocomplete
hint="Months (Falsy object key example)"
label="Months (Falsy object key example)"
multiple={false}
onChange={this.handleSimpleMonthChange}
showSuggestionsWhenValueIsSet
source={this.state.monthsObject}
value={this.state.simpleMonth}
/>
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;