Skip to content

Commit

Permalink
deprecation message for string selectors (#770)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbetancur authored Feb 15, 2021
1 parent 0ab8555 commit ed6068f
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 281 deletions.
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-data-table-component",
"version": "7.0.0-alpha-5",
"version": "7.0.0-alpha-6",
"description": "A declarative react based data table",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
Expand Down Expand Up @@ -39,14 +39,14 @@
"build-storybook": "build-storybook"
},
"devDependencies": {
"@material-ui/core": "^4.11.2",
"@material-ui/core": "^4.11.3",
"@material-ui/icons": "^4.11.2",
"@storybook/addon-a11y": "^6.1.14",
"@storybook/addon-actions": "^6.1.14",
"@storybook/addon-a11y": "^6.1.15",
"@storybook/addon-actions": "^6.1.15",
"@storybook/addon-console": "^1.2.2",
"@storybook/addon-links": "^6.1.14",
"@storybook/addon-storysource": "^6.1.14",
"@storybook/react": "^6.1.14",
"@storybook/addon-links": "^6.1.15",
"@storybook/addon-storysource": "^6.1.15",
"@storybook/react": "^6.1.15",
"@testing-library/react": "^11.2.3",
"@types/faker": "^5.1.5",
"@types/jest": "^26.0.20",
Expand All @@ -56,8 +56,8 @@
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@types/styled-components": "^5.1.7",
"@typescript-eslint/eslint-plugin": "^4.14.0",
"@typescript-eslint/parser": "^4.14.0",
"@typescript-eslint/eslint-plugin": "^4.14.1",
"@typescript-eslint/parser": "^4.14.1",
"axios": "^0.21.1",
"babel-eslint": "^10.1.0",
"codecov": "^3.8.1",
Expand All @@ -82,7 +82,7 @@
"react-app-polyfill": "^2.0.0",
"react-dom": "^17.0.1",
"rimraf": "^3.0.2",
"rollup": "^2.37.0",
"rollup": "^2.38.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^7.0.2",
Expand Down
9 changes: 9 additions & 0 deletions src/DataTable/TableCol.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ function TableCol<T>({
selectableRowsVisibleOnly,
onSort,
}: TableColProps<T>): JSX.Element | null {
React.useEffect(() => {
if (typeof column.selector === 'string') {
console.error(
`Warning: ${column.selector} is a string based column selector which has been deprecated as of v7 and will be removed in v8. Instead, use a selector function e.g. row => row[field]...`,
);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

if (column.omit) {
return null;
}
Expand Down
4 changes: 4 additions & 0 deletions src/DataTable/__tests__/DataTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ const dataMock = (colProps?: any) => {
};
};

beforeEach(() => {
console.error = jest.fn();
});

test('should render and empty table correctly', () => {
const { container } = render(<DataTable data={[]} columns={[]} />);

Expand Down
2 changes: 2 additions & 0 deletions src/DataTable/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export function getProperty<T extends Record<string, any>>(
throw new Error('selector must be a . delimited string eg (my.property) or function (e.g. row => row.field');
}

// format will override how the selector is displayed but the original dataset is used for sorting
if (format && typeof format === 'function') {
return format(row, rowIndex);
}
Expand All @@ -61,6 +62,7 @@ export function getProperty<T extends Record<string, any>>(
return selector(row, rowIndex);
}

// string based selectors will be removed in v8
return selector.split('.').reduce((acc, part) => {
// O(n2) when querying for an array (e.g. items[0].name)
// Likely, the object depth will be reasonable enough that performance is not a concern
Expand Down
Loading

0 comments on commit ed6068f

Please sign in to comment.