Skip to content

Commit

Permalink
feat: use ReadonlyArray in props
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiangmoe committed Jan 27, 2021
1 parent 4cf7ec0 commit 1b7b4d6
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 41 deletions.
4 changes: 3 additions & 1 deletion src/Body/BodyRow.tsx
Expand Up @@ -24,7 +24,9 @@ export interface BodyRowProps<RecordType> {
childrenColumnName: string;
}

function BodyRow<RecordType extends { children?: RecordType[] }>(props: BodyRowProps<RecordType>) {
function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
props: BodyRowProps<RecordType>,
) {
const {
className,
style,
Expand Down
2 changes: 1 addition & 1 deletion src/Body/index.tsx
Expand Up @@ -9,7 +9,7 @@ import ResizeContext from '../context/ResizeContext';
import MeasureCell from './MeasureCell';

export interface BodyProps<RecordType> {
data: RecordType[];
data: readonly RecordType[];
getRowKey: GetRowKey<RecordType>;
measureColumnWidth: boolean;
expandedKeys: Set<Key>;
Expand Down
4 changes: 2 additions & 2 deletions src/ColGroup.tsx
Expand Up @@ -3,8 +3,8 @@ import { ColumnType } from './interface';
import { INTERNAL_COL_DEFINE } from './utils/legacyUtil';

export interface ColGroupProps<RecordType> {
colWidths: (number | string)[];
columns?: ColumnType<RecordType>[];
colWidths: readonly (number | string)[];
columns?: readonly ColumnType<RecordType>[];
columCount?: number;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Header/FixedHeader.tsx
Expand Up @@ -7,7 +7,7 @@ import ColGroup from '../ColGroup';
import { ColumnsType, ColumnType } from '../interface';
import TableContext from '../context/TableContext';

function useColumnWidth(colWidths: number[], columCount: number) {
function useColumnWidth(colWidths: readonly number[], columCount: number) {
return useMemo(() => {
const cloneColumns: number[] = [];
for (let i = 0; i < columCount; i += 1) {
Expand All @@ -24,7 +24,7 @@ function useColumnWidth(colWidths: number[], columCount: number) {

export interface FixedHeaderProps<RecordType> extends HeaderProps<RecordType> {
noData: boolean;
colWidths: number[];
colWidths: readonly number[];
columCount: number;
direction: 'ltr' | 'rtl';
fixHeader: boolean;
Expand Down Expand Up @@ -92,7 +92,7 @@ const FixedHeader = React.forwardRef<HTMLDivElement, FixedHeaderProps<unknown>>(
[combinationScrollBarSize, columns],
);

const flattenColumnsWithScrollbar = useMemo<ColumnType<unknown>[]>(
const flattenColumnsWithScrollbar = useMemo(
() => (combinationScrollBarSize ? [...flattenColumns, ScrollBarColumn] : flattenColumns),
[combinationScrollBarSize, flattenColumns],
);
Expand Down
4 changes: 2 additions & 2 deletions src/Header/Header.tsx
Expand Up @@ -83,9 +83,9 @@ function parseHeaderRows<RecordType>(

export interface HeaderProps<RecordType> {
columns: ColumnsType<RecordType>;
flattenColumns: ColumnType<RecordType>[];
flattenColumns: readonly ColumnType<RecordType>[];
stickyOffsets: StickyOffsets;
onHeaderRow: GetComponentProps<ColumnType<RecordType>[]>;
onHeaderRow: GetComponentProps<readonly ColumnType<RecordType>[]>;
}

function Header<RecordType>({
Expand Down
6 changes: 3 additions & 3 deletions src/Header/HeaderRow.tsx
Expand Up @@ -12,12 +12,12 @@ import { getCellFixedInfo } from '../utils/fixUtil';
import { getColumnsKey } from '../utils/valueUtil';

export interface RowProps<RecordType> {
cells: CellType<RecordType>[];
cells: readonly CellType<RecordType>[];
stickyOffsets: StickyOffsets;
flattenColumns: ColumnType<RecordType>[];
flattenColumns: readonly ColumnType<RecordType>[];
rowComponent: CustomizeComponent;
cellComponent: CustomizeComponent;
onHeaderRow: GetComponentProps<ColumnType<RecordType>[]>;
onHeaderRow: GetComponentProps<readonly ColumnType<RecordType>[]>;
index: number;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Table.tsx
Expand Up @@ -106,7 +106,7 @@ export interface TableProps<RecordType = unknown> extends LegacyExpandableProps<
className?: string;
style?: React.CSSProperties;
children?: React.ReactNode;
data?: RecordType[];
data?: readonly RecordType[];
columns?: ColumnsType<RecordType>;
rowKey?: string | GetRowKey<RecordType>;
tableLayout?: TableLayout;
Expand All @@ -123,14 +123,14 @@ export interface TableProps<RecordType = unknown> extends LegacyExpandableProps<
// Additional Part
title?: PanelRender<RecordType>;
footer?: PanelRender<RecordType>;
summary?: (data: RecordType[]) => React.ReactNode;
summary?: (data: readonly RecordType[]) => React.ReactNode;

// Customize
id?: string;
showHeader?: boolean;
components?: TableComponents<RecordType>;
onRow?: GetComponentProps<RecordType>;
onHeaderRow?: GetComponentProps<ColumnType<RecordType>[]>;
onHeaderRow?: GetComponentProps<readonly ColumnType<RecordType>[]>;
emptyText?: React.ReactNode | (() => React.ReactNode);

direction?: 'ltr' | 'rtl';
Expand Down Expand Up @@ -303,7 +303,7 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
return false;
}, [!!expandedRowRender, mergedData]);

const [innerExpandedKeys, setInnerExpandedKeys] = React.useState<Key[]>(() => {
const [innerExpandedKeys, setInnerExpandedKeys] = React.useState(() => {
if (defaultExpandedRowKeys) {
return defaultExpandedRowKeys;
}
Expand Down
2 changes: 1 addition & 1 deletion src/context/BodyContext.tsx
Expand Up @@ -16,7 +16,7 @@ export interface BodyContextProps<RecordType = DefaultRecordType> {
expandedRowClassName: RowClassName<RecordType>;

columns: ColumnsType<RecordType>;
flattenColumns: ColumnType<RecordType>[];
flattenColumns: readonly ColumnType<RecordType>[];

componentWidth: number;
tableLayout: TableLayout;
Expand Down
2 changes: 1 addition & 1 deletion src/context/TableContext.tsx
Expand Up @@ -12,7 +12,7 @@ export interface TableContextProps {

direction: 'ltr' | 'rtl';

fixedInfoList: FixedInfo[];
fixedInfoList: readonly FixedInfo[];

isSticky: boolean;
}
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useColumns.tsx
Expand Up @@ -60,7 +60,7 @@ function flatColumns<RecordType>(columns: ColumnsType<RecordType>): ColumnType<R
}, []);
}

function warningFixed(flattenColumns: { fixed?: FixedType }[]) {
function warningFixed(flattenColumns: readonly { fixed?: FixedType }[]) {
let allFixLeft = true;
for (let i = 0; i < flattenColumns.length; i += 1) {
const col = flattenColumns[i];
Expand Down Expand Up @@ -136,7 +136,7 @@ function useColumns<RecordType>(
columnWidth?: number | string;
},
transformColumns: (columns: ColumnsType<RecordType>) => ColumnsType<RecordType>,
): [ColumnsType<RecordType>, ColumnType<RecordType>[]] {
): [ColumnsType<RecordType>, readonly ColumnType<RecordType>[]] {
const baseColumns = React.useMemo<ColumnsType<RecordType>>(
() => columns || convertChildrenToColumns(children),
[columns, children],
Expand Down
23 changes: 10 additions & 13 deletions src/interface.ts
Expand Up @@ -53,7 +53,7 @@ export interface RenderedCell<RecordType> {
children?: React.ReactNode;
}

export type DataIndex = string | number | (string | number)[];
export type DataIndex = string | number | readonly (string | number)[];

export type CellEllipsisType = { showTitle?: boolean } | boolean;

Expand Down Expand Up @@ -89,17 +89,14 @@ export interface ColumnType<RecordType> extends ColumnSharedType<RecordType> {
onCellClick?: (record: RecordType, e: React.MouseEvent<HTMLElement>) => void;
}

export type ColumnsType<RecordType = unknown> = (
| ColumnGroupType<RecordType>
| ColumnType<RecordType>
)[];
export type ColumnsType<RecordType = unknown> = readonly (ColumnGroupType<RecordType> | ColumnType<RecordType>)[];

export type GetRowKey<RecordType> = (record: RecordType, index?: number) => Key;

// ================= Fix Column =================
export interface StickyOffsets {
left: number[];
right: number[];
left: readonly number[];
right: readonly number[];
isSticky?: boolean;
}

Expand All @@ -118,7 +115,7 @@ type Component<P> =
export type CustomizeComponent = Component<any>;

export type CustomizeScrollBody<RecordType> = (
data: RecordType[],
data: readonly RecordType[],
info: {
scrollbarSize: number;
ref: React.Ref<{ scrollLeft: number }>;
Expand All @@ -143,7 +140,7 @@ export interface TableComponents<RecordType> {
}

export type GetComponent = (
path: string[],
path: readonly string[],
defaultComponent?: CustomizeComponent,
) => CustomizeComponent;

Expand Down Expand Up @@ -197,13 +194,13 @@ export type RenderExpandIcon<RecordType> = (
) => React.ReactNode;

export interface ExpandableConfig<RecordType> {
expandedRowKeys?: Key[];
defaultExpandedRowKeys?: Key[];
expandedRowKeys?: readonly Key[];
defaultExpandedRowKeys?: readonly Key[];
expandedRowRender?: ExpandedRowRender<RecordType>;
expandRowByClick?: boolean;
expandIcon?: RenderExpandIcon<RecordType>;
onExpand?: (expanded: boolean, record: RecordType) => void;
onExpandedRowsChange?: (expandedKeys: Key[]) => void;
onExpandedRowsChange?: (expandedKeys: readonly Key[]) => void;
defaultExpandAllRows?: boolean;
indentSize?: number;
expandIconColumnIndex?: number;
Expand All @@ -214,7 +211,7 @@ export interface ExpandableConfig<RecordType> {
}

// =================== Render ===================
export type PanelRender<RecordType> = (data: RecordType[]) => React.ReactNode;
export type PanelRender<RecordType> = (data: readonly RecordType[]) => React.ReactNode;

// =================== Events ===================
export type TriggerEventHandler<RecordType> = (
Expand Down
2 changes: 1 addition & 1 deletion src/sugar/ColumnGroup.tsx
Expand Up @@ -5,7 +5,7 @@ import { ColumnType } from '../interface';
export interface ColumnGroupProps<RecordType> extends Omit<ColumnType<RecordType>, 'children'> {
children:
| React.ReactElement<ColumnProps<RecordType>>
| React.ReactElement<ColumnProps<RecordType>>[];
| readonly React.ReactElement<ColumnProps<RecordType>>[];
}

/* istanbul ignore next */
Expand Down
4 changes: 2 additions & 2 deletions src/utils/expandUtil.tsx
Expand Up @@ -32,13 +32,13 @@ export function renderExpandIcon<RecordType>({
}

export function findAllChildrenKeys<RecordType>(
data: RecordType[],
data: readonly RecordType[],
getRowKey: GetRowKey<RecordType>,
childrenColumnName: string,
): Key[] {
const keys: Key[] = [];

function dig(list: RecordType[]) {
function dig(list: readonly RecordType[]) {
(list || []).forEach((item, index) => {
keys.push(getRowKey(item, index));

Expand Down
2 changes: 1 addition & 1 deletion src/utils/fixUtil.ts
Expand Up @@ -16,7 +16,7 @@ export interface FixedInfo {
export function getCellFixedInfo(
colStart: number,
colEnd: number,
columns: { fixed?: FixedType }[],
columns: readonly { fixed?: FixedType }[],
stickyOffsets: StickyOffsets,
direction: 'ltr' | 'rtl',
): FixedInfo {
Expand Down
7 changes: 3 additions & 4 deletions src/utils/valueUtil.tsx
Expand Up @@ -2,12 +2,11 @@ import { Key, DataIndex } from '../interface';

const INTERNAL_KEY_PREFIX = 'RC_TABLE_KEY';

function toArray<T>(arr: T | T[]): T[] {
function toArray<T>(arr: T | readonly T[]): T[] {
if (arr === undefined || arr === null) {
return [];
}

return Array.isArray(arr) ? arr : [arr];
return (Array.isArray(arr) ? arr : [arr]) as T[];
}

export function getPathValue<ValueType, ObjectType extends object>(
Expand Down Expand Up @@ -40,7 +39,7 @@ interface GetColumnKeyColumn {
dataIndex?: DataIndex;
}

export function getColumnsKey(columns: GetColumnKeyColumn[]) {
export function getColumnsKey(columns: readonly GetColumnKeyColumn[]) {
const columnKeys: React.Key[] = [];
const keys: Record<React.Key, boolean> = {};

Expand Down

0 comments on commit 1b7b4d6

Please sign in to comment.