fix(widget-relation): append initial value search to options (#4026)

This commit is contained in:
Erez Rokah 2020-07-15 16:10:14 +03:00 committed by GitHub
parent 64aa845c6e
commit 12c388eff3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -60,6 +60,10 @@ function getSelectedValue({ value, options, isMultiple }) {
export default class RelationControl extends React.Component { export default class RelationControl extends React.Component {
didInitialSearch = false; didInitialSearch = false;
state = {
initialOptions: [],
};
static propTypes = { static propTypes = {
onChange: PropTypes.func.isRequired, onChange: PropTypes.func.isRequired,
forID: PropTypes.string.isRequired, forID: PropTypes.string.isRequired,
@ -176,20 +180,36 @@ export default class RelationControl extends React.Component {
const collection = field.get('collection'); const collection = field.get('collection');
const searchFields = field.get('searchFields'); const searchFields = field.get('searchFields');
const optionsLength = field.get('optionsLength') || 20; const optionsLength = field.get('optionsLength') || 20;
let searchFieldsArray = List.isList(searchFields) ? searchFields.toJS() : [searchFields]; const searchFieldsArray = List.isList(searchFields) ? searchFields.toJS() : [searchFields];
const file = field.get('file'); const file = field.get('file');
// if the field has a previous value perform the initial search based on the value field // if the field has a previous value perform an initial search based on the value field
// this is needed since search results are limited to optionsLength // and display it as the first option.
// this is required since each search is limited by optionsLength so the selected value
// might not show up on the first search
let initialSearchPromise = Promise.resolve([]);
if (!this.didInitialSearch && value && !term) { if (!this.didInitialSearch && value && !term) {
searchFieldsArray = [field.get('valueField')]; initialSearchPromise = query(
term = value; forID,
collection,
[field.get('valueField')],
value,
file,
1,
).then(({ payload }) => {
const hits = payload.response?.hits || [];
const options = this.parseHitOptions(hits);
return options;
});
} }
query(forID, collection, searchFieldsArray, term, file, optionsLength).then(({ payload }) => { initialSearchPromise.then(initialOptions => {
const hits = payload.response?.hits || []; this.setState({ initialOptions });
const options = this.parseHitOptions(hits); query(forID, collection, searchFieldsArray, term, file, optionsLength).then(({ payload }) => {
callback(options); const hits = payload.response?.hits || [];
const options = this.parseHitOptions(hits);
callback(initialOptions.concat(options));
});
}); });
}, 500); }, 500);
@ -209,7 +229,7 @@ export default class RelationControl extends React.Component {
const hits = queryHits.get(forID, []); const hits = queryHits.get(forID, []);
const options = this.parseHitOptions(hits); const options = this.parseHitOptions(hits);
const selectedValue = getSelectedValue({ const selectedValue = getSelectedValue({
options, options: this.state.initialOptions.concat(options),
value, value,
isMultiple, isMultiple,
}); });