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: selectedItemTemplate displays [Object object] #6633

Closed
xAl3xFx opened this issue May 16, 2024 · 3 comments · Fixed by #7353
Closed

AutoComplete: selectedItemTemplate displays [Object object] #6633

xAl3xFx opened this issue May 16, 2024 · 3 comments · Fixed by #7353
Assignees
Labels
Type: Bug Issue contains a defect related to a specific component.
Milestone

Comments

@xAl3xFx
Copy link

xAl3xFx commented May 16, 2024

Describe the bug

Having AutoComplete component with selectedItemTemplate which returns React.Node the displayed value is [Object object]

Reproducer

https://stackblitz.com/edit/k4e3ju?file=src%2FApp.jsx

PrimeReact version

10.6.5

React version

18.x

Language

TypeScript

Build / Runtime

Create React App (CRA)

Browser(s)

No response

Steps to reproduce the behavior

No response

Expected behavior

No response

@xAl3xFx xAl3xFx added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label May 16, 2024
@LunaticPoring
Copy link

+1 here.
It looks like the selectedItemTemplate accepts a string but not a react node

This works:
selectedItemTemplate={(product: Product) => product.title}
This doesn't work
selectedItemTemplate={(product: Product) => <div>product.title</div>}

So can't apply specific formatting to the selectedItem

@melloware
Copy link
Member

hmmm the code tries to get it as a JSX element.

 const formatValue = (value) => {
            if (ObjectUtils.isNotEmpty(value)) {
                if (typeof value === 'string') {
                    return value;
                } else if (props.selectedItemTemplate) {
                    const resolvedFieldData = ObjectUtils.getJSXElement(props.selectedItemTemplate, value);

                    return resolvedFieldData ? resolvedFieldData : value;
                } else if (props.field) {
                    const resolvedFieldData = ObjectUtils.resolveFieldData(value, props.field);

                    return resolvedFieldData !== null && resolvedFieldData !== undefined ? resolvedFieldData : value;
                }

                return value;
            }

            return '';
        };

@melloware
Copy link
Member

oh duh i see the issue

melloware added a commit to melloware/primereact that referenced this issue Oct 17, 2024
@melloware melloware self-assigned this Oct 17, 2024
@melloware melloware added Type: Bug Issue contains a defect related to a specific component. and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Oct 17, 2024
@melloware melloware added this to the 10.8.5 milestone Oct 17, 2024
melloware added a commit to melloware/primereact that referenced this issue Oct 17, 2024
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.

3 participants