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

ISuggestionModel misused for PeoplePickers? #4195

Closed
Markionium opened this issue Mar 6, 2018 · 5 comments
Closed

ISuggestionModel misused for PeoplePickers? #4195

Markionium opened this issue Mar 6, 2018 · 5 comments

Comments

@Markionium
Copy link
Member

When trying to add some additional types i ran into the following.

I tried adding a typedef to the defaultProps in the following snippet. I ran into the problem that the createGenericItem does not use the same signature as the one expected by the BasePickerProps

/**
 * Standard People Picker.
 */
export class NormalPeoplePicker extends BasePeoplePicker {
  public static defaultProps = {
    onRenderItem: (props: IPeoplePickerItemProps) => <SelectedItemDefault {...props} />,
    onRenderSuggestionsItem: (props: IPersonaProps, itemProps?: IBasePickerSuggestionsProps) => SuggestionItemNormal({ ...props }, { ...itemProps }),
    createGenericItem: createGenericItem
  };
}

The expected signature for the createGenericItem is the following.

/**
   * Function that specifies how arbitrary text entered into the well is handled.
   */
  createGenericItem?: (input: string, ValidationState: ValidationState) => ISuggestionModel<T>;

The returned object should be an ISuggestionModel which has the following interface.

export interface ISuggestionModel<T> {
  item: T;
  selected: boolean;
  ariaLabel?: string;
}

However the function createGenericItem returns something wildly different, that does not have any properties in common with the expected ISuggestionModel.

export function createGenericItem(name: string, currentValidationState: ValidationState) {
  const personaToConvert = {
    key: name,
    primaryText: name,
    imageInitials: '!',
    ValidationState: currentValidationState
  };

  if (currentValidationState !== ValidationState.warning) {
    personaToConvert.imageInitials = getInitials(name, getRTL());
  }

  return personaToConvert;
}

It seems we do some juggling to make sure that the item use in the end is of the correct interface, but it seems we could do this somewhat cleaner.

@autobind
  private _ensureSuggestionModel(
    suggestion: ISuggestionModel<T> | T
  ): ISuggestionModel<T> {
    if (this._isSuggestionModel(suggestion)) {
      return suggestion as ISuggestionModel<T>;
    } else {
      return {
        item: suggestion,
        selected: false,
        ariaLabel: (<any>suggestion).name || (<any>suggestion).primaryText
      } as ISuggestionModel<T>;
    }
  }
@Jahnp
Copy link
Member

Jahnp commented Mar 8, 2018

@joschect for FYI as Pickers codeowner, @dzearing for FYI as we were just discussing the future of decorator usage, per @Markionium's suggested design.

@joschect
Copy link
Contributor

joschect commented Mar 9, 2018

I like the suggestion. I'm pretty on board with anything that makes pickers cleaner.

@Jahnp
Copy link
Member

Jahnp commented Mar 9, 2018

Rock on. @Markionium would you be willing to open a PR?

@stale
Copy link

stale bot commented Apr 11, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions to Fabric React!

@michaelangotti
Copy link
Collaborator

PR merged (#4569 ). Please open new issue if you are still having issues.

@microsoft microsoft locked as resolved and limited conversation to collaborators Aug 31, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants