Skip to content

ISuggestionModel misused for PeoplePickers? #4195

@Markionium

Description

@Markionium

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>;
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions