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.

People picker selection disappears

See original GitHub issue

Category

[ ] Enhancement

[x] Bug

[ ] Question

Version

Please specify what version of the library you are using: [3.9.0 + spfx 1.15 ]

Expected / Desired Behavior / Question

Using the PeoplePicker component should be controllable (mean getting and settings values from the state).

Observed Behavior

The people picker allows gettings its value from the state, and trigger changes with the new selection. However, the text input is cleared after a very small delay, even if the state actually contains the correct value

chrome-capture-2022-6-15

Steps to Reproduce

Bootstrap a new spfx webpart project using standard yeoman generator (choose webpart and react framework) Replace the HelloWorld component with :

import { BaseComponentContext } from '@microsoft/sp-component-base';
import { PeoplePicker, PrincipalType } from "@pnp/spfx-controls-react/lib/PeoplePicker";
import { Label, Stack } from 'office-ui-fabric-react';
import * as React from 'react';

export interface helloWorldProps {
  context: BaseComponentContext
}

export const HelloWorld = ({ context }: helloWorldProps): JSX.Element => {

  const [selectedUserId, setSelectUserId] = React.useState<string[]>([]);

  return (
    <Stack>
      <PeoplePicker
        context={context}
        defaultSelectedUsers={selectedUserId}
        titleText="People Picker"
        personSelectionLimit={3}
        showtooltip={true}
        required={true}

        onChange={(items) => {
          setSelectUserId(items.map(item => item.id));
        }}

        principalTypes={[PrincipalType.User]}
 />

      <Label>Selected ({selectedUserId.length})</Label>
      <ul>
        {selectedUserId.map(user => (
          <li key={user}>{user}</li>
        ))}
        
      </ul>
    </Stack>
  );

}

In the HelloworldWebPart.tsx file, change the render method to :

  public render(): void {
    const element: React.ReactElement<helloWorldProps> = React.createElement(
      HelloWorld,
      {
        context : this.context
      }
    );

    ReactDom.render(element, this.domElement);
  }

Drop the webpart on any client side page

Reproduction : https://github.com/stevebeauge/spfx-react-control-sandbox/tree/repro/issue%231265

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
omarelaniscommented, Sep 9, 2022

@stevebeauge happened to come across this issue you raised, it’s the same problem you have in the taxonomy bug you raised, you should not be updating the default or initial values otherwise it will stop the components from working correctly, so in your first code this line:

    defaultSelectedUsers={selectedUserId}

should be a constant value that never changes, you however are updating it in the onChange method using the setState, the onChange method should only be used to store the value in a separate state item which is not linked back to the control, the people picker control will handle the selected values itself.

I.e

PnPControl state -> your component state, not the other way around

You will find this is a common theme with the PnP control so avoid trying to update any readonly properties within the controls

0reactions
joelfmrodriguescommented, Nov 13, 2022

@stevebeauge I see your point, but unfortunately the control can not be fully controlled. I will close this bug based on the response from @omarelanis, but if you are willing to update the component to be fully controlled, we would be happy to review the PR 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

People picker value disappear when a dropdown selects a ...
i have an infopath form which starts with 2 people picker at the top. then there is a dropdown contains choices A,B,C etc....
Read more >
Name Disappears After Selecting in People Picker with ...
It may appear to be working in some cases – you can search for something, it shows you the list of matches, you...
Read more >
[BUG] people picker selected person remove button is hidden ...
When the people picker control is too narrow to fit the name of a selected person, the (x) button disappears and can't be...
Read more >
SharePoint 2013 People Picker Browse Button Missing?
With massive changes from SharePoint 2010 to SharePoint 2013, People picker also has changed! In its previous versions, SharePoint 2010 and ...
Read more >
People Picker icon is not showing inside the built-in New ...
In Sharepoint 2013, the person or group field no longer uses the old directory people picker. They have switched over to the autocomplete ......
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