Skip to content

Commit

Permalink
[SDESK-7062] improve: Use includes instead of startsWith for Autocomp…
Browse files Browse the repository at this point in the history
…lete suggestions (#1867)
  • Loading branch information
MarkLark86 authored Oct 19, 2023
1 parent 83064ae commit b6c4bdd
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion client/components/fields/editor/base/text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class EditorFieldText extends React.Component<IEditorFieldTextProps, ISta
super(props);

this.onChange = this.onChange.bind(this);
this.searchSuggestions = this.searchSuggestions.bind(this);
this.node = React.createRef();

this.state = {
Expand Down Expand Up @@ -81,7 +82,7 @@ export class EditorFieldText extends React.Component<IEditorFieldTextProps, ISta
this.getInputElement()?.focus();
}

fetchSuggestions(field, language) {
fetchSuggestions(field: string, language: string): Promise<Array<string>> {
const {httpRequestJsonLocal} = superdeskApi;

return httpRequestJsonLocal<IRestApiResponse<{value: string}>>({
Expand All @@ -92,10 +93,22 @@ export class EditorFieldText extends React.Component<IEditorFieldTextProps, ISta
(response) => response._items.map((_item) => _item.value).filter((value) => !!value),
(reason) => {
console.warn(reason);
return [];
}
);
}

searchSuggestions(searchString: string, callback: (result: Array<any>) => void) {
const currentValue = get(this.props.item, this.props.field);

callback(this.state.suggestions.filter(
(name) => name.toLowerCase().includes(searchString) && name.toLowerCase() !== currentValue
));

// eslint-disable-next-line no-empty-function
return {cancel: () => {}};
}

render() {
const field = this.props.field;
const value = get(this.props.item, field, this.props.defaultValue);
Expand Down Expand Up @@ -134,6 +147,7 @@ export class EditorFieldText extends React.Component<IEditorFieldTextProps, ISta
error={this.props.showErrors ? error : undefined}
onChange={this.onChange}
items={this.state.suggestions}
search={this.searchSuggestions}
/>
)}
</Row>
Expand Down

0 comments on commit b6c4bdd

Please sign in to comment.