2017-09-09 19:39:10 -06:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React, { Component } from 'react';
|
2016-12-07 15:44:07 -02:00
|
|
|
import Autosuggest from 'react-autosuggest';
|
2017-10-18 11:02:16 -06:00
|
|
|
import uuid from 'uuid/v4';
|
2016-12-29 17:18:24 -02:00
|
|
|
import { Map } from 'immutable';
|
2016-12-07 15:44:07 -02:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { debounce } from 'lodash';
|
|
|
|
import { Loader } from '../../components/UI/index';
|
|
|
|
import { query, clearSearch } from '../../actions/search';
|
|
|
|
|
|
|
|
|
|
|
|
function escapeRegexCharacters(str) {
|
|
|
|
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
|
|
}
|
|
|
|
|
|
|
|
class RelationControl extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
onChange: PropTypes.func.isRequired,
|
2017-01-19 14:26:49 -02:00
|
|
|
forID: PropTypes.string.isRequired,
|
2016-12-07 15:44:07 -02:00
|
|
|
value: PropTypes.node,
|
|
|
|
field: PropTypes.node,
|
2016-12-29 17:18:24 -02:00
|
|
|
isFetching: PropTypes.node,
|
2016-12-07 15:44:07 -02:00
|
|
|
query: PropTypes.func.isRequired,
|
|
|
|
clearSearch: PropTypes.func.isRequired,
|
2016-12-29 17:18:24 -02:00
|
|
|
queryHits: PropTypes.oneOfType([
|
|
|
|
PropTypes.array,
|
|
|
|
PropTypes.object,
|
|
|
|
]),
|
2016-12-07 15:44:07 -02:00
|
|
|
};
|
|
|
|
|
2016-12-29 17:18:24 -02:00
|
|
|
constructor(props, ctx) {
|
|
|
|
super(props, ctx);
|
2017-10-18 11:02:16 -06:00
|
|
|
this.controlID = uuid();
|
2016-12-29 17:18:24 -02:00
|
|
|
this.didInitialSearch = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const { value, field } = this.props;
|
|
|
|
if (value) {
|
|
|
|
const collection = field.get('collection');
|
2017-01-19 15:50:26 -02:00
|
|
|
const searchFields = field.get('searchFields').toJS();
|
2016-12-29 17:18:24 -02:00
|
|
|
this.props.query(this.controlID, collection, searchFields, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (this.didInitialSearch) return;
|
|
|
|
if (nextProps.queryHits !== this.props.queryHits && nextProps.queryHits.get && nextProps.queryHits.get(this.controlID)) {
|
|
|
|
this.didInitialSearch = true;
|
|
|
|
const suggestion = nextProps.queryHits.get(this.controlID);
|
|
|
|
if (suggestion && suggestion.length === 1) {
|
|
|
|
const val = this.getSuggestionValue(suggestion[0]);
|
|
|
|
this.props.onChange(val, { [nextProps.field.get('collection')]: { [val]: suggestion[0].data } });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 15:44:07 -02:00
|
|
|
onChange = (event, { newValue }) => {
|
|
|
|
this.props.onChange(newValue);
|
|
|
|
};
|
|
|
|
|
2016-12-29 17:18:24 -02:00
|
|
|
onSuggestionSelected = (event, { suggestion }) => {
|
|
|
|
const value = this.getSuggestionValue(suggestion);
|
|
|
|
this.props.onChange(value, { [this.props.field.get('collection')]: { [value]: suggestion.data } });
|
|
|
|
};
|
|
|
|
|
2016-12-07 15:44:07 -02:00
|
|
|
onSuggestionsFetchRequested = debounce(({ value }) => {
|
2016-12-29 17:18:24 -02:00
|
|
|
if (value.length < 2) return;
|
2016-12-07 15:44:07 -02:00
|
|
|
const { field } = this.props;
|
|
|
|
const collection = field.get('collection');
|
2017-01-19 15:50:26 -02:00
|
|
|
const searchFields = field.get('searchFields').toJS();
|
2016-12-29 17:18:24 -02:00
|
|
|
this.props.query(this.controlID, collection, searchFields, value);
|
2017-06-30 17:58:26 -04:00
|
|
|
}, 500);
|
2016-12-07 15:44:07 -02:00
|
|
|
|
|
|
|
onSuggestionsClearRequested = () => {
|
|
|
|
this.props.clearSearch();
|
|
|
|
};
|
|
|
|
|
|
|
|
getSuggestionValue = (suggestion) => {
|
|
|
|
const { field } = this.props;
|
|
|
|
const valueField = field.get('valueField');
|
|
|
|
return suggestion.data[valueField];
|
|
|
|
};
|
|
|
|
|
|
|
|
renderSuggestion = (suggestion) => {
|
|
|
|
const { field } = this.props;
|
|
|
|
const valueField = field.get('valueField');
|
|
|
|
return <span>{suggestion.data[valueField]}</span>;
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2017-01-19 14:26:49 -02:00
|
|
|
const { value, isFetching, forID, queryHits } = this.props;
|
2016-12-07 15:44:07 -02:00
|
|
|
|
|
|
|
const inputProps = {
|
|
|
|
placeholder: '',
|
|
|
|
value: value || '',
|
|
|
|
onChange: this.onChange,
|
2017-01-19 14:26:49 -02:00
|
|
|
id: forID,
|
2016-12-07 15:44:07 -02:00
|
|
|
};
|
|
|
|
|
2016-12-29 17:18:24 -02:00
|
|
|
const suggestions = (queryHits.get) ? queryHits.get(this.controlID, []) : [];
|
|
|
|
|
2016-12-07 15:44:07 -02:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Autosuggest
|
2016-12-29 17:18:24 -02:00
|
|
|
suggestions={suggestions}
|
|
|
|
onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
|
|
|
|
onSuggestionsClearRequested={this.onSuggestionsClearRequested}
|
|
|
|
onSuggestionSelected={this.onSuggestionSelected}
|
2016-12-07 15:44:07 -02:00
|
|
|
getSuggestionValue={this.getSuggestionValue}
|
|
|
|
renderSuggestion={this.renderSuggestion}
|
|
|
|
inputProps={inputProps}
|
|
|
|
/>
|
2016-12-29 17:18:24 -02:00
|
|
|
<Loader active={isFetching === this.controlID} />
|
2016-12-07 15:44:07 -02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapStateToProps(state) {
|
|
|
|
const isFetching = state.search.get('isFetching');
|
|
|
|
const queryHits = state.search.get('queryHits');
|
|
|
|
return { isFetching, queryHits };
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(
|
|
|
|
mapStateToProps,
|
|
|
|
{
|
|
|
|
query,
|
|
|
|
clearSearch,
|
2017-11-11 20:57:45 -05:00
|
|
|
},
|
|
|
|
null,
|
|
|
|
{
|
|
|
|
withRef: true,
|
2016-12-07 15:44:07 -02:00
|
|
|
}
|
|
|
|
)(RelationControl);
|