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

Adding IColumn properties supported by tubular to the main model. #646

Merged
merged 1 commit into from
Oct 8, 2024
Merged
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
6 changes: 0 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,4 @@ module.exports = {
automock: false,
setupFilesAfterEnv: ['./jest-setup.ts'],
setupFiles: ['@testing-library/react/dont-cleanup-after-each'],
globals: {
'ts-jest': {
diagnostics: false,
tsconfig: 'tsconfig.json',
},
},
};
9 changes: 4 additions & 5 deletions sample/src/ColumnsDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import { ITbColumnProxy } from '../../src/interfaces/ITbColumn';
export const columns: ITbColumnProxy[] = [
{
name: 'Actions',
minWidth: 50,
isComputed: true,
dataType: ColumnDataType.String,
},
{
name: 'OrderID',
label: 'Order ID',
minWidth: 100,
maxWidth: 110,
minWidth: 80,
maxWidth: 90,
isKey: true,
dataType: ColumnDataType.Numeric,
sortable: true,
Expand Down Expand Up @@ -42,8 +41,8 @@ export const columns: ITbColumnProxy[] = [
{
name: 'Amount',
label: 'Amount',
minWidth: 100,
maxWidth: 110,
minWidth: 70,
maxWidth: 90,
dataType: ColumnDataType.Numeric,
sortable: true,
},
Expand Down
4 changes: 3 additions & 1 deletion src/interfaces/ITbColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ export interface ITbColumn extends IColumn {
tb?: Partial<ColumnModel>;
}

export interface ITbColumnProxy extends Partial<ColumnModel>, Pick<IColumn, 'minWidth' | 'maxWidth'> {}
export type TbSupportedIColumnProps = Partial<Pick<IColumn, 'minWidth' | 'maxWidth'>>;

export interface ITbColumnProxy extends Partial<ColumnModel>, TbSupportedIColumnProps {}
22 changes: 19 additions & 3 deletions src/useTbFabric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
columnHasFilter,
} from 'tubular-common/dist/Models';
import { IColumn } from '@fluentui/react';
import { ITbColumnProxy } from './interfaces/ITbColumn';
import { ITbColumnProxy, TbSupportedIColumnProps } from './interfaces/ITbColumn';
import { ITbFabricInstance } from './interfaces/ITbFabricInstance';
import { ITbFabricApi } from './interfaces';

Expand Down Expand Up @@ -40,10 +40,14 @@
}),
);

const mapToFabricColumns = (tbColumns: ColumnModel[]): Partial<IColumn>[] =>
const mapToFabricColumns = (
tbColumns: ColumnModel[],
fabricColumnProps: { [key: string]: Partial<TbSupportedIColumnProps> },
): Partial<IColumn>[] =>
tbColumns.map((column) => {
const fabricProps = fabricColumnProps[column.name];
return {
key: column.name!,

Check warning on line 50 in src/useTbFabric.ts

View workflow job for this annotation

GitHub Actions / build / eslint

Forbidden non-null assertion
name: column.label,
isFiltered: columnHasFilter(column),
isSorted: column.sortDirection !== ColumnSortDirection.None,
Expand All @@ -51,15 +55,23 @@
column.sortDirection !== ColumnSortDirection.None
? column.sortDirection === ColumnSortDirection.Descending
: false,
...fabricProps,
};
});

const useTbFabric = (
initColumns: ITbColumnProxy[],
source: string | Request | TubularHttpClientAbstract | any[],

Check warning on line 64 in src/useTbFabric.ts

View workflow job for this annotation

GitHub Actions / build / eslint

Unexpected any. Specify a different type
tubularOptions?: Partial<ITbOptions>,
): ITbFabricInstance => {
const { deps, ...rest } = tubularOptions;
const fabricColumnProps: { [key: string]: Partial<TbSupportedIColumnProps> } = {};
initColumns.forEach((element) => {
fabricColumnProps[element.name] = {
maxWidth: element.maxWidth,
minWidth: element.minWidth,
};
});
const tbColumns = React.useMemo(() => createInitialTbColumns(initColumns), [initColumns]);
const { state: tbState, api: tbApi } = useTubular(tbColumns, source, rest);
const [list, setListState] = React.useState({
Expand All @@ -71,13 +83,13 @@

const isCtrlKeyPressed = React.useRef<boolean>(false);

const handleKeyDown = (event: any) => {

Check warning on line 86 in src/useTbFabric.ts

View workflow job for this annotation

GitHub Actions / build / eslint

Unexpected any. Specify a different type
if (event.key === 'Control' && !isCtrlKeyPressed.current) {
isCtrlKeyPressed.current = true;
}
};

const handleKeyUp = (event: any) => {

Check warning on line 92 in src/useTbFabric.ts

View workflow job for this annotation

GitHub Actions / build / eslint

Unexpected any. Specify a different type
if (event.key === 'Control' && isCtrlKeyPressed) {
isCtrlKeyPressed.current = false;
}
Expand Down Expand Up @@ -177,7 +189,7 @@
const clearFilter = (columnName: string) => applyOrResetFilter(columnName, null);

const fabricColumnsMapper = (item) => {
const mapped: any = {};

Check warning on line 192 in src/useTbFabric.ts

View workflow job for this annotation

GitHub Actions / build / eslint

Unexpected any. Specify a different type
const tbColumns = tbState.columns;

tbColumns.forEach((col) => {
Expand Down Expand Up @@ -224,13 +236,13 @@
items: newItems,
};
});
}, [tbState.data, tbState.error]);

Check warning on line 239 in src/useTbFabric.ts

View workflow job for this annotation

GitHub Actions / build / eslint

React Hook React.useEffect has missing dependencies: 'fabricColumnsMapper' and 'tbState.filteredRecordCount'. Either include them or remove the dependency array

const listDeps = deps || [];

React.useEffect(() => {
resetList();
}, listDeps);

Check warning on line 245 in src/useTbFabric.ts

View workflow job for this annotation

GitHub Actions / build / eslint

React Hook React.useEffect was passed a dependency list that is not an array literal. This means we can't statically verify whether you've passed the correct dependencies

const api: ITbFabricApi = {
...tbApi,
Expand All @@ -248,8 +260,12 @@
};

const filteredFabricColumns = React.useMemo(
() => mapToFabricColumns(tbState.columns.filter((c) => c.visible)),
() =>
mapToFabricColumns(
tbState.columns.filter((c) => c.visible),
fabricColumnProps,
),
[tbState.columns],

Check warning on line 268 in src/useTbFabric.ts

View workflow job for this annotation

GitHub Actions / build / eslint

React Hook React.useMemo has a missing dependency: 'fabricColumnProps'. Either include it or remove the dependency array
);

const state = {
Expand Down
Loading