question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ListItemPicker: DefaultSelectedItems for ListItemPicker not working with Dynamic Data

See original GitHub issue

Bug: defaultSelectedItems for ListItemPicker not working with React dynamic data

I’ve installed @pnp/spfx-controls-react of version 1.16.0. I am facing challenge in setting default selected item for ListitemPicker dynamically. If we set defaultSelectedItems using any static data it is working properly. However if we try to set defaultSelectedItems dynamically using react state variable then it does not set default selected items.

Expected / Desired Behavior / Question

We should be able to set defaultSelectedItems dynamically using react state or any other way.

Observed Behavior

If I set static DefaultSelectedItem for ListItemPicker it works properly.

<ListItemPicker listId='653741e7-84c2-4d78-81a6-ac77dcc6bd24'  
                                            columnInternalName='Title'  
                                            keyColumnInternalName='Id'  
                                            itemLimit={1}  
                                            onSelectedItem={this.onSelectedItem}  
                                            context={this.props.context}   
                                            suggestionsHeaderText = "Please select Location"   
                                            defaultSelectedItems={[{key: 12297, name: "A1R1C1S1"}]} 

Steps to Reproduce

If we trying to set DefaultSelectedItem for ListItemPicker dynamically then it does not work. Please find following example in which I am trying to set default selected items using react state.

  public componentDidMount() {
    sp.setup({
        spfxContext: this.props.context
    });
    this.GetLookupData("Record Location", "Title").then(ChoiceItemCollection => {
        this.setState({
            RecordLocations: ChoiceItemCollection,
            SelectedRecordLocation: ChoiceItemCollection.filter(item => item.key === 12297),
        });
    });

}
  public render(): React.ReactElement < IListPickerProps > {
  
    return(
      <div className = { styles.listPicker } >
            <div className={styles.container}>
                <div className={styles.row}>
                    <div className={styles.column}>

                        <ListItemPicker listId='653741e7-84c2-4d78-81a6-ac77dcc6bd24'
                            columnInternalName='Title'
                            keyColumnInternalName='Id'
                            itemLimit={1}
                            onSelectedItem={this.onSelectedItem}
                            context={this.props.context}
                            suggestionsHeaderText="Please select Location"
                            defaultSelectedItems={this.state.SelectedRecordLocation}

                        />
                    </div>
                </div>
            </div>
      </div>
    );
  }

  private GetLookupData(SPListName: string, SPFieldName: string): Promise < any > {
    let spfields = [SPFieldName, "Id"];

    return sp.web.lists.getByTitle(SPListName).items.select(...spfields).top(5000)
        .get()
        .then((data) => {

            if (data && data.length > 0) {
                let ChoicesCollection: any[] = [];
                data.forEach((item, index) => {
                    ChoicesCollection.push({
                        key: parseInt(item.Id),
                        // text: item[SPFieldName],
                        // title:item[SPFieldName],
                        name: item[SPFieldName],
                    });
                });
                return ChoicesCollection;
            }

        }).catch((e) => console.log("Error in GetLookupData() function. " + e));
}

In above example I am getting data from “Record Location” list using async function GetLookupData and trying to set default value using SelectedRecordLocation state variable.

Control re renders when we set state in componentDidMount however it is not setting default value for ListItemPicker control.

Please let me know if this is bug /any solution is there for this issue

Any help will be appreciated.

Thanks!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
AJIXuMuKcommented, Mar 1, 2020

Another option here could be to display Spinner until loading the items.

{this.state.defaultItems && <ListItemPicker ... />}
{!this.state.defaultItems && <Spinner ... />}
1reaction
Macro21commented, Feb 28, 2020

Hi! I have the same problem. I solved it with a method and the react state…

image

image

Update: The bug its on Fabric UI TagPicker. ListItemPicker use TagPicker 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

DefaultSelectedItems for ListItemPicker not working ... - GitHub
I am facing challenge in setting default selected item for ListitemPicker dynamically. If we set defaultSelectedItems using any static data it ...
Read more >
How to set selected values on ComboBoxListItemPicker ...
I managed to solve this by using a workaround...I force the component to be recreated by setting a new key to the component...
Read more >
Release notes - @pnp/spfx-controls-react
LivePersona : Fix LivePersona not showing card on hover #1241 ... 702; ListItemPicker : When use defaultSelectedItems, ListItemPicker allows you to select ...
Read more >
Not able to set defaultselecteditems combobox | SharePoint List
Hi, I am facing issue with sharepoint list custom form where combobox not showing defaultselecteditems in Edit Mode.
Read more >
Source - GitHub
3.8.1 ### Fixes - `LivePersona`: Fix LivePersona not showing card on hover ... `ListItemPicker`: When use defaultSelectedItems, ListItemPicker allows you to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found