Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoComplete: shows "[object Object]" if value object instance is not one of suggestions #1392

Closed
VsevolodGolovanov opened this issue Jun 4, 2020 · 8 comments · Fixed by #2766
Assignees
Labels
Type: Bug Issue contains a defect related to a specific component.
Milestone

Comments

@VsevolodGolovanov
Copy link

I'm submitting a ... (check one with "x")

[x] bug report
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://forum.primefaces.org/viewforum.php?f=57

Codesandbox Case (Bug Reports)
https://codesandbox.io/s/primereact-test-yus1p

Current behavior
Initial value object is shown as "[object Object]" in the autocomplete field.
Selected value object is shown as "[object Object]" in the autocomplete field.

Expected behavior
Value objects should be shown using the provided selectedItemTemplate function.

Minimal reproduction of the problem with instructions
Open the showcase. See "[object Object]" in the field.
Type something into the field, select one of the suggestions. See "[object Object]" in the field.

Please tell us about your environment:

  • React version:
    16.3.1

  • PrimeReact version:
    4.2.1

  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
    Firefox 77.0.1

  • Language: [all | TypeScript X.X | ES6/7 | ES5]

@VsevolodGolovanov
Copy link
Author

VsevolodGolovanov commented Jun 4, 2020

Likely a bug introduced by the #1296 fix.
This worked fine in PR 4.1.2: https://codesandbox.io/s/primereact-test-h50nn
Of course you had to work around #1296 with a typeof check in the selectedItemTemplate.

@VsevolodGolovanov VsevolodGolovanov changed the title AutoComplete: says "[object Object]" if value object instance is not one of suggestions AutoComplete: shows "[object Object]" if value object instance is not one of suggestions Jun 4, 2020
@kashifshamaz21
Copy link

kashifshamaz21 commented Oct 27, 2020

@VsevolodGolovanov @aloker Any luck with this issue and a fix for it? I'm running into this issue too.
In my case, I have a different condition leading to this issue:
When my backend API fails for any reason, I need to show an error message. The UX is to show this error message looking like a dropdown suggestion.
So here's what I've done to achieve this UX :

searchAPI({ query }).then((response: any) => {
            const searchResults = response.queryResult;
            this.setState({ suggestions: [...searchResults], apiError: false });
        }, (error: any) => {
            console.log('SearchEntity: API Error while searching for: ', entityType);
            this.setState({ 
                suggestions: [{ id: -1, name: ''}],
                apiError: true
            });
        });

itemTemplate(item) {
        return (
            this.state.apiError ? <div className={styles.searchError}>{this.errorTemplate()}</div> :
            <div className={styles.itemSuggestion}>
                <span className={styles.itemSuggestionName}>{item.name}</span>
                <span className={styles.itemSuggestionId}>({item.id})</span>
            </div>
        )
}

selectedItemTemplate(item) {
        return item.name;
}

errorTemplate() {
        const entityType = this.props.entityType;
        return `Error while searching for ${entityType.toLowerCase()}`
}

With the above setup, whenever API fails, an error message shows in the autocomplete dropdown. But, the problem is, if the user clicks on the Error message, the autocomplete renders "[object Object]" in the input element.

@VsevolodGolovanov
Copy link
Author

@kashifshamaz21 , in your case I'd try working around the issue by preventing the error value from being selected. In your onChange or onSelect, if the new value is an error, then either leave the current value unchanged or set to null/undefined.

@VsevolodGolovanov
Copy link
Author

This is not some obscure edge case btw. Showing projections for text search results, and only turning a projection into a full fledged object when user actually selects it - this is the most normal thing in the world.

@VsevolodGolovanov
Copy link
Author

If there are no plans to fix this, consider rolling back the #1296 fix? At least that issue could be worked around trivially.

@mcandu mcandu added the Status: Pending Review Issue or pull request is being reviewed by Core Team label Aug 17, 2021
@mcandu mcandu self-assigned this Aug 17, 2021
melloware added a commit to melloware/primereact that referenced this issue Apr 18, 2022
@melloware melloware assigned melloware and unassigned mcandu Apr 18, 2022
@melloware melloware added Type: Bug Issue contains a defect related to a specific component. and removed Status: Pending Review Issue or pull request is being reviewed by Core Team labels Apr 18, 2022
@melloware melloware added this to the 8.0.0-rc.3 milestone Apr 18, 2022
@melloware melloware linked a pull request Apr 18, 2022 that will close this issue
@melloware
Copy link
Member

If you guys don't mind reviewing my PR I believe I have fixed both issues #1392 and the original #1296

@mertsincan
Copy link
Member

I don't think it is correct to directly return the 'value' based on the type of 'value'. Users can define value differently and return a different value from selectedItemTemplate. My personal opinion for this issue is to assign the value given as value to the suggestion object. For your example;

constructor(props) {
    super(props);
    this.state = {
      query: "",
      value: { id: 1, objectName: "Number one" },
      suggestions: [{ id: 1, objectName: "Number one" }]
    };
    this.suggest = this.suggest.bind(this);
  }

You can directly change the suggestion in any query. It totally depends on your implementation.

@mertsincan mertsincan removed this from the 8.0.0-rc.3 milestone Apr 20, 2022
@mertsincan mertsincan added the Status: Discussion Issue or pull request needs to be discussed by Core Team label Apr 20, 2022
@mertsincan mertsincan self-assigned this Apr 20, 2022
@VsevolodGolovanov
Copy link
Author

VsevolodGolovanov commented Apr 20, 2022

1. A suggestion object often contains stuff like matched string and info for highlighting. Those are not a part of the domain object represented by the suggestion.

A suggestion object could contain the corresponding domain object. In fact that's how it is in my actual code:

export class SearchResult {
	readonly matchedString: MatchedString;
	readonly value: SuperClassRef;
}

export class MatchedString {
	readonly value: string;
	readonly matches: MatchInfo[];
}

export class MatchInfo {
	readonly start: number,
	readonly end: number
}

I think this must be supported, the API just wouldn't make sense otherwise.

2. The next level would be to support values not coming from suggestions at all, but created in onSelect.

Would be great if this were supported, but it's not as quintessential as 1.

@melloware melloware added this to the 8.1.0 milestone Apr 26, 2022
@mertsincan mertsincan modified the milestones: 8.2.0, 9.0.0 Jul 6, 2022
@mertsincan mertsincan modified the milestones: 9.0.0, 8.5.0 Aug 31, 2022
@mertsincan mertsincan removed the Status: Discussion Issue or pull request needs to be discussed by Core Team label Aug 31, 2022
@mertsincan mertsincan removed their assignment Aug 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Issue contains a defect related to a specific component.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants