Skip to content

Commit

Permalink
[select] fix: mark items arrays as readonly where appropriate (#5171)
Browse files Browse the repository at this point in the history
  • Loading branch information
maclockard authored Mar 17, 2022
1 parent c1b1153 commit dc11929
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/select/src/common/listItemsProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface IListItemsProps<T> extends Props {
activeItem?: T | ICreateNewItem | 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
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 `IListItemsProps`.
*/
export type ItemListPredicate<T> = (query: string, items: T[]) => T[];
export type ItemListPredicate<T> = (query: string, items: T[]) => readonly T[];

/**
* A custom predicate for filtering items based on the provided query.
Expand Down
9 changes: 5 additions & 4 deletions packages/select/src/components/query-list/queryList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export interface IQueryListState<T> {
createNewItem: 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 @@ -218,7 +218,8 @@ export class QueryList<T> extends AbstractComponent2<QueryListProps<T>, IQueryLi
handleQueryChange: this.handleInputQueryChange,
itemList: itemListRenderer({
...spreadableState,
items,
filteredItems: spreadableState.filteredItems.slice(),
items: items.slice(),
itemsParentRef: this.refHandlers.itemsParent,
renderCreateItem: this.renderCreateItemMenuItem,
renderItem: this.renderItem,
Expand Down Expand Up @@ -609,7 +610,7 @@ function getMatchingItem<T>(query: string, { items, itemPredicate }: QueryListPr
function getFilteredItems<T>(query: string, { items, itemPredicate, itemListPredicate }: QueryListProps<T>) {
if (Utils.isFunction(itemListPredicate)) {
// note that implementations can reorder the items here
return itemListPredicate(query, items);
return itemListPredicate(query, items.slice());
} else if (Utils.isFunction(itemPredicate)) {
return items.filter((item, index) => itemPredicate(query, item, index));
}
Expand Down Expand Up @@ -645,7 +646,7 @@ function isItemDisabled<T>(item: T | null, index: number, itemDisabled?: IListIt
* @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
2 changes: 1 addition & 1 deletion packages/select/src/components/select/multiSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export interface IMultiSelectProps<T> extends IListItemsProps<T> {
popoverProps?: Partial<IPopoverProps> & object;

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

/** Props to spread to `TagInput`. Use `query` and `onQueryChange` to control the input. */
// eslint-disable-next-line @typescript-eslint/ban-types
Expand Down

1 comment on commit dc11929

@blueprint-bot
Copy link

Choose a reason for hiding this comment

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

[select] fix: mark items arrays as readonly where appropriate (#5171)

Previews: documentation | landing | table | modern colors demo

Please sign in to comment.