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

Marks Select arrays as readonly #6999

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
Expand Up @@ -251,7 +251,9 @@ export class MultiSelectExample extends React.PureComponent<ExampleProps, MultiS
);
}

private renderCustomTarget = (selectedItems: Film[]) => <MultiSelectCustomTarget count={selectedItems.length} />;
private renderCustomTarget = (selectedItems: readonly Film[]) => (
<MultiSelectCustomTarget count={selectedItems.length} />
);

private renderTag = (film: Film) => film.title;

Expand Down Expand Up @@ -287,7 +289,7 @@ export class MultiSelectExample extends React.PureComponent<ExampleProps, MultiS
this.selectFilms([film]);
}

private selectFilms(filmsToSelect: Film[]) {
private selectFilms(filmsToSelect: readonly Film[]) {
this.setState(({ createdItems, films, items }) => {
let nextCreatedItems = createdItems.slice();
let nextFilms = films.slice();
Expand Down Expand Up @@ -336,7 +338,7 @@ export class MultiSelectExample extends React.PureComponent<ExampleProps, MultiS
}
};

private handleFilmsPaste = (films: Film[]) => {
private handleFilmsPaste = (films: readonly Film[]) => {
// On paste, don't bother with deselecting already selected values, just
// add the new ones.
this.selectFilms(films);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class SelectExample extends React.PureComponent<ExampleProps, SelectExamp
return /[0-9]/.test(firstLetter) ? "0-9" : firstLetter;
}

private getGroupedItems = (filteredItems: Film[]) => {
private getGroupedItems = (filteredItems: readonly Film[]) => {
return filteredItems.reduce<Array<{ group: string; index: number; items: Film[]; key: number }>>(
(acc, item, index) => {
const group = this.getGroup(item);
Expand All @@ -193,7 +193,7 @@ export class SelectExample extends React.PureComponent<ExampleProps, SelectExamp
) : undefined;
};

private groupedItemListPredicate = (query: string, items: Film[]) => {
private groupedItemListPredicate = (query: string, items: readonly Film[]) => {
return items
.filter((item, index) => filterFilm(query, item, index))
.sort((a, b) => this.getGroup(a).localeCompare(this.getGroup(b)));
Expand Down
2 changes: 1 addition & 1 deletion packages/docs-theme/src/components/navigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class Navigator extends React.PureComponent<NavigatorProps> {
}

private filterMatches: ItemListPredicate<NavigationSection> = (query, items) =>
filter(items, query, {
filter(items.slice(), query, {
key: "route",
maxInners: items.length / 5,
maxResults: 10,
Expand Down
4 changes: 2 additions & 2 deletions packages/select/src/common/itemListRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ export interface ItemListRendererProps<T> {
* map each item in this array through `renderItem`, with support for
* optional `noResults` and `initialContent` states.
*/
filteredItems: T[];
filteredItems: readonly T[];

/**
* Array of all items in the list.
* See `filteredItems` for a filtered array based on `query` and predicate props.
*/
items: T[];
items: readonly T[];

/**
* The current query string.
Expand Down
4 changes: 2 additions & 2 deletions packages/select/src/common/listItemsProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface ListItemsProps<T> extends Props {
activeItem?: T | CreateNewItem | null;

/** Array of items in the list. */
items: T[];
items: readonly T[];

/**
* Specifies how to test if two items are equal. By default, simple strict
Expand Down Expand Up @@ -157,7 +157,7 @@ export interface ListItemsProps<T> extends Props {
/**
* Callback invoked when multiple items are selected at once via pasting.
*/
onItemsPaste?: (items: T[]) => void;
onItemsPaste?: (items: readonly T[]) => void;

/**
* Callback invoked when the query string changes.
Expand Down
2 changes: 1 addition & 1 deletion packages/select/src/common/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* A custom predicate for returning an entirely new `items` array based on the provided query.
* See usage sites in `ListItemsProps`.
*/
export type ItemListPredicate<T> = (query: string, items: T[]) => T[];
export type ItemListPredicate<T> = (query: string, items: readonly T[]) => readonly T[];

/**
* A custom predicate for filtering items based on the provided query.
Expand Down
4 changes: 2 additions & 2 deletions packages/select/src/components/multi-select/multiSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface MultiSelectProps<T> extends ListItemsProps<T>, SelectPopoverPro
* Element which triggers the multiselect popover. Providing this prop will replace the default TagInput
* target thats rendered and move the search functionality to within the Popover.
*/
customTarget?: (selectedItems: T[], isOpen: boolean) => React.ReactNode;
customTarget?: (selectedItems: readonly T[], isOpen: boolean) => React.ReactNode;

/**
* Whether the component is non-interactive.
Expand Down Expand Up @@ -104,7 +104,7 @@ export interface MultiSelectProps<T> extends ListItemsProps<T>, SelectPopoverPro
placeholder?: string;

/** Controlled selected values. */
selectedItems: T[];
selectedItems: readonly T[];

/**
* Props to pass to the [TagInput component](##core/components/tag-input).
Expand Down
4 changes: 2 additions & 2 deletions packages/select/src/components/query-list/queryList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export interface QueryListState<T> {
createNewItem: T | T[] | undefined;

/** The original `items` array filtered by `itemListPredicate` or `itemPredicate`. */
filteredItems: T[];
filteredItems: readonly T[];

/** The current query string. */
query: string;
Expand Down Expand Up @@ -676,7 +676,7 @@ function isItemDisabled<T>(item: T | null, index: number, itemDisabled?: ListIte
* @param startIndex which index to begin moving from
*/
export function getFirstEnabledItem<T>(
items: T[],
items: readonly T[],
itemDisabled?: keyof T | ((item: T, index: number) => boolean),
direction = 1,
startIndex = items.length - 1,
Expand Down
4 changes: 3 additions & 1 deletion packages/select/test/queryListTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ describe("<QueryList>", () => {
});

it("itemListPredicate filters entire list by query", () => {
const predicate = sinon.spy((query: string, films: Film[]) => films.filter(f => f.year === +query));
const predicate = sinon.spy((query: string, films: readonly Film[]) =>
films.filter(f => f.year === +query),
);
shallow(<QueryList<Film> {...testProps} itemListPredicate={predicate} query="1994" />);

assert.equal(predicate.callCount, 1, "called once for entire list");
Expand Down