Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "office-ui-fabric-react",
"comment": "Modify the ExtendedPeoplePicker so that the input props, spellcheck and autocorrect, are always set to false and off, respectively.",
"type": "minor"
}
],
"packageName": "office-ui-fabric-react",
"email": "adameury@outlook.com"
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ class BaseExtendedPicker<T, P extends IBaseExtendedPickerProps<T>> extends BaseC
// (undocumented)
readonly inputElement: HTMLInputElement | null;
// (undocumented)
protected inputProps: () => IInputProps;
// (undocumented)
readonly items: any;
// (undocumented)
protected onBackspace: (ev: React.KeyboardEvent<HTMLElement>) => void;
Expand Down Expand Up @@ -1149,6 +1151,8 @@ enum ExpandingCardMode {

// @public (undocumented)
class ExtendedPeoplePicker extends BaseExtendedPeoplePicker {
// (undocumented)
protected inputProps: () => IInputProps;
}

// @public (undocumented)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class BaseExtendedPicker<T, P extends IBaseExtendedPickerProps<T>> extend
}

public render(): JSX.Element {
const { className, inputProps, disabled, focusZoneProps } = this.props;
const { className, disabled, focusZoneProps } = this.props;
const activeDescendant =
this.floatingPicker.current && this.floatingPicker.current.currentSelectedSuggestionIndex !== -1
? 'sug-' + this.floatingPicker.current.currentSelectedSuggestionIndex
Expand All @@ -115,7 +115,7 @@ export class BaseExtendedPicker<T, P extends IBaseExtendedPickerProps<T>> extend
{this.renderSelectedItemsList()}
{this.canAddItems() && (
<Autofill
{...inputProps as IInputProps}
{...this.inputProps()}
className={css('ms-BasePicker-input', styles.pickerInput)}
ref={this.input}
onFocus={this.onInputFocus}
Expand Down Expand Up @@ -172,6 +172,10 @@ export class BaseExtendedPicker<T, P extends IBaseExtendedPickerProps<T>> extend
});
}

protected inputProps = (): IInputProps => {
return this.props.inputProps as IInputProps;
};

protected onInputChange = (value: string): void => {
this.setState({ queryString: value });
if (this.floatingPicker.current) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* tslint:disable:no-unused-variable */
import * as React from 'react';
/* tslint:enable:no-unused-variable */
import * as renderer from 'react-test-renderer';

import { Autofill } from '../../Autofill/index';
import { IPersonaProps } from '../../Persona/index';
import { people } from '../examples/PeopleExampleData';
import { ExtendedPeoplePicker } from './ExtendedPeoplePicker';
import { SuggestionsStore, FloatingPeoplePicker, IBaseFloatingPickerProps } from '../../FloatingPicker/index';
import { ISelectedItemProps, IBaseSelectedItemsListProps, IExtendedPersonaProps, SelectedPeopleList } from '../../SelectedItemsList/index';

function onResolveSuggestions(text: string): IPersonaProps[] {
const peopleList: IPersonaProps[] = people;
return peopleList.filter(p => p.text && p.text.includes(text));
}

const floatingPickerProps = {
onResolveSuggestions: onResolveSuggestions,
suggestionsStore: new SuggestionsStore<IPersonaProps>()
};

const basicItemRenderer = (props: ISelectedItemProps<IPersonaProps>) => {
return <div>{props.item.text}</div>;
};

const selectedItemsListProps: IBaseSelectedItemsListProps<IPersonaProps> = {
onRenderItem: basicItemRenderer
};

const onRenderFloatingPicker = (props: IBaseFloatingPickerProps<IPersonaProps>): JSX.Element => {
return <FloatingPeoplePicker {...props} />;
};

const onRenderSelectedItems = (props: IBaseSelectedItemsListProps<IExtendedPersonaProps>): JSX.Element => {
return <SelectedPeopleList {...props} />;
};

describe('Pickers', () => {
describe('ExtendedPeoplePicker', () => {
it('sets the Autofill spellCheck is set to false', () => {
const root = document.createElement('div');
document.body.appendChild(root);

const pickerRenderer = renderer.create(
<ExtendedPeoplePicker
floatingPickerProps={floatingPickerProps}
selectedItemsListProps={selectedItemsListProps}
onRenderFloatingPicker={onRenderFloatingPicker}
onRenderSelectedItems={onRenderSelectedItems}
/>
);
const pickerInstance = pickerRenderer.root;

expect(pickerInstance.findByType(Autofill).props.spellCheck).toBe(false);
});

it('sets the Autofill autoCorrect prop to off', () => {
const root = document.createElement('div');
document.body.appendChild(root);

const pickerRenderer = renderer.create(
<ExtendedPeoplePicker
floatingPickerProps={floatingPickerProps}
selectedItemsListProps={selectedItemsListProps}
onRenderFloatingPicker={onRenderFloatingPicker}
onRenderSelectedItems={onRenderSelectedItems}
/>
);
const pickerInstance = pickerRenderer.root;

expect(pickerInstance.findByType(Autofill).props.autoCorrect).toBe('off');
});

it('overwrites spellCheck', () => {
const root = document.createElement('div');
document.body.appendChild(root);

const pickerRenderer = renderer.create(
<ExtendedPeoplePicker
floatingPickerProps={floatingPickerProps}
selectedItemsListProps={selectedItemsListProps}
onRenderFloatingPicker={onRenderFloatingPicker}
onRenderSelectedItems={onRenderSelectedItems}
inputProps={{ spellCheck: true }}
/>
);
const pickerInstance = pickerRenderer.root;

expect(pickerInstance.findByType(Autofill).props.spellCheck).toBe(false);
});

it('overwrites autoCorrect', () => {
const root = document.createElement('div');
document.body.appendChild(root);

const pickerRenderer = renderer.create(
<ExtendedPeoplePicker
floatingPickerProps={floatingPickerProps}
selectedItemsListProps={selectedItemsListProps}
onRenderFloatingPicker={onRenderFloatingPicker}
onRenderSelectedItems={onRenderSelectedItems}
inputProps={{ autoCorrect: 'on' }}
/>
);
const pickerInstance = pickerRenderer.root;

expect(pickerInstance.findByType(Autofill).props.autoCorrect).toBe('off');
});
});
});
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
/* tslint:disable */
import { IPickerItemProps } from '../../../Pickers';
import { IPickerItemProps, IInputProps } from '../../../Pickers';
/* tslint:enable */

import { IExtendedPersonaProps } from '../../../SelectedItemsList';
import { IPersonaProps } from '../../../Persona';
import './ExtendedPeoplePicker.scss';
import { BaseExtendedPicker } from '../BaseExtendedPicker';
import { IBaseExtendedPickerProps } from '../BaseExtendedPicker.types';
import { assign } from '@uifabric/utilities';

export interface IPeoplePickerItemProps extends IPickerItemProps<IExtendedPersonaProps> {}

export interface IExtendedPeoplePickerProps extends IBaseExtendedPickerProps<IPersonaProps> {}

export class BaseExtendedPeoplePicker extends BaseExtendedPicker<IPersonaProps, IExtendedPeoplePickerProps> {}

export class ExtendedPeoplePicker extends BaseExtendedPeoplePicker {}
export class ExtendedPeoplePicker extends BaseExtendedPeoplePicker {

@joschect joschect Nov 30, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this is the right way to achieve this. Instead you could do something like

export class ExtendedPeoplePicker extends BaseExtendedPeoplePicker {
  protected _skipComponentRefResolution = true;
  public render() {
    return <BaseExtendedPeoplePicker 
    {...this.props} 
    inputProps={{...this.props.inputProps, ...overwrittenInputProps}}
    />
  }
}

That way you don't need to mess with your BaseExtendedPicker, you don't need to have a method called, and you can use the component ref stuff to ensure you won't lose out on the interface methods you might call.

For example of how some of this works, check out the various buttons. Although I'm not 100% we want to continue that pattern. @dzearing or @micahgodbolt what are your thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JasonGore may have some thoughts on how to best override component props from the base class here when he has a moment.

@JasonGore JasonGore Dec 3, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The best way (and official React way) is not to use inheritance at all.

In this case, I think the sanest approach is just to do:

class ExtendedPeoplePicker extends React.Component {
    render() {
        return <BaseExtendedPeoplePicker {...this.props} inputProps={{...this.props.inputProps, ...overwrittenInputProps} />;
    }
}

If there is a reason not to use composition, then at the very least @joschect's advice should be taken as it doesn't require the base class to be modified at all and inputProps member can be removed.

[EDIT] Code corrected per my latest comments below.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Changed to extends BaseComponent to make sure refs still work.)

@aleury aleury Dec 4, 2018

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the input @joschect and @JasonGore! My initial thought was to go with something similar to what @joschect suggested, but I thought it was a bit weird to render the component that it's also extending. I was aware of the official recommendation of not using component inheritance, but I didn't want to make that assumption for this project and remove it without further input.

I like @JasonGore's suggestion, but am happy to go with @joschect if desired.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I was wrong about my comment. Extending React.Component should be sufficient. Similar to our other HOC wrappers (styled, etc.) the refs will just get forwarded in the props and should continue to work fine.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aleury Go with Jason's suggestion, it looks more correct.

protected inputProps = (): IInputProps => {
const overwrittenInputProps: IInputProps = {
autoCorrect: 'off',
spellCheck: false
};
return assign({}, this.props.inputProps, overwrittenInputProps);
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ exports[`Component Examples renders ExtendedPeoplePicker.Basic.Example.tsx corre
onKeyDown={[Function]}
onPaste={[Function]}
role="combobox"
spellCheck={false}
value=""
/>
</div>
Expand Down