Files
Iris/src/js/components/Fields/SearchForm.js

87 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-09-05 15:36:11 +12:00
import React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import * as helpers from '../../helpers';
import * as uiActions from '../../services/ui/actions';
class SearchForm extends React.Component {
2019-09-05 15:36:11 +12:00
constructor(props) {
super(props);
this.state = {
term: this.props.term,
pristine: true,
};
}
componentWillReceiveProps(nextProps) {
if (this.state.pristine && this.state.term == '' && this.state.term !== nextProps.term) {
this.setState({ term: nextProps.term, pristine: false });
}
}
handleBlur(e) {
this.setState({ pristine: false });
if (this.props.onBlur) {
this.props.onBlur(this.state.term);
}
}
handleFocus(e) {
this.setState({ pristine: false });
}
handleSubmit(e) {
e.preventDefault();
// check for uri type matching
switch (helpers.uriType(this.state.term)) {
2019-09-05 15:36:11 +12:00
case 'album':
this.props.history.push(`/album/${encodeURIComponent(this.state.term)}`);
break;
case 'artist':
this.props.history.push(`/artist/${encodeURIComponent(this.state.term)}`);
break;
case 'playlist':
this.props.history.push(`/playlist/${encodeURIComponent(this.state.term)}`);
break;
case 'track':
this.props.history.push(`/track/${encodeURIComponent(this.state.term)}`);
break;
default:
this.props.onSubmit(this.state.term);
break;
}
return false;
}
render() {
return (
<form className="search-form" onSubmit={(e) => this.handleSubmit(e)}>
<label>
<input
type="text"
placeholder="Search..."
onChange={(e) => this.setState({ term: e.target.value, pristine: false })}
onBlur={(e) => this.handleBlur}
onFocus={(e) => this.handleFocus}
value={this.state.term}
2019-09-05 15:36:11 +12:00
/>
</label>
</form>
);
}
2016-11-03 08:57:16 +13:00
}
2019-09-05 15:36:11 +12:00
const mapDispatchToProps = (dispatch) => ({
uiActions: bindActionCreators(uiActions, dispatch),
});
2016-11-03 08:57:16 +13:00
export default connect(mapDispatchToProps)(SearchForm);