Skip to content

Commit baadf6c

Browse files
authored
Merge pull request #51 from easeq/master
Issue fixes and improvements
2 parents 870720e + 1838123 commit baadf6c

File tree

10 files changed

+59
-31
lines changed

10 files changed

+59
-31
lines changed

demo/src/schema/basic.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default {
2323
},
2424
layout: [
2525
['Editable'],
26-
['MassActions', 'SimpleButton', 'ResetFilters', 'Spacer', 'Print', 'Columns'],
26+
['MassActions', 'SimpleButton', 'ExternalCustomButton', 'ResetFilters', 'Spacer', 'Print', 'Columns'],
2727
['Limiter', 'Spacer', 'ResultCount', 'Spacer', 'Pages'],
2828
[{ id: 'Table', layout: [
2929
['Header'],
@@ -60,6 +60,12 @@ export default {
6060
// last: { backgroundColor: 'purple' },
6161
// }
6262
// },
63+
ExternalCustomButton: {
64+
renderer: (props) => {
65+
console.log(props);
66+
return <button>nothing</button>
67+
}
68+
},
6369
Editable: {
6470
type: 'editable',
6571
labels: {

demo/src/schema/normalized.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default {
5656
},
5757
layout: [
5858
['Editable'],
59-
['MassActions', 'SimpleButton', 'ResetFilters', 'Spacer', 'Print', 'Columns'],
59+
['MassActions', 'SimpleButton', 'ExternalCustomButton', 'ResetFilters', 'Spacer', 'Print', 'Columns'],
6060
['Limiter', 'Spacer', 'ResultCount', 'Spacer', 'Pages'],
6161
[{ id: 'Table', layout: [
6262
['Header'],
@@ -84,6 +84,12 @@ export default {
8484
justifyContent: 'center'
8585
}
8686
},
87+
ExternalCustomButton: {
88+
renderer: (props) => {
89+
console.log(props);
90+
return <button>nothing</button>
91+
}
92+
},
8793
// Pages: {
8894
// styles: {
8995
// first: { backgroundColor: 'red' },

package-lock.json

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@flipbyte/redux-datatable",
3-
"version": "0.7.8",
3+
"version": "0.7.9",
44
"description": "React-Redux data table",
55
"main": "lib/index.js",
66
"module": "es/index.js",
@@ -33,6 +33,7 @@
3333
"redux-thunk": "^2.3.0"
3434
},
3535
"dependencies": {
36+
"element-resize-event": "^3.0.3",
3637
"normalizr": "^3.3.0",
3738
"object-assign-deep": "^0.4.0",
3839
"query-string": "^6.1.0",

src/components/Body.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ import React, { useContext, useEffect } from 'react';
33
import { useSelector } from 'react-redux';
44
import { Tbody, Tr, Td, Div } from '../styled-components';
55
import { Body as Renderers } from './Renderer';
6-
import { getStyles, getRenderer } from '../utils';
7-
import { MODIFY_DATA, SET_BODY_INNER_WIDTH } from '../actions';
6+
import { getStyles, getRenderer, getInitialVisibleColumns } from '../utils';
7+
import { MODIFY_DATA, SET_BODY_INNER_WIDTH, SET_TABLE_WIDTH, SET_VISIBLE_COLUMN_IDS, SET_COLUMN_WIDTHS } from '../actions';
88
import { withScrollSpy } from '../hoc';
99
import ConfigContext from '../context';
1010
import { createSelector } from 'reselect';
1111

12+
const addElementResizeEventListener = require('element-resize-event');
13+
const removeElementResizeEventListener = require('element-resize-event').unbind;
14+
1215
const renderCol = (rowIndex, primaryKey, schema, styles, column, index) => {
1316
const { textAlign, name, type } = column;
1417
const ColRenderer = getRenderer(column, Renderers);
@@ -70,16 +73,27 @@ const Body = React.forwardRef(({ top: startTop = 0 }, ref) => {
7073

7174
const updateTableDimensions = () => {
7275
action(SET_BODY_INNER_WIDTH)({
73-
clientWidth: ref.current ? ref.current.clientWidth : minWidth,
74-
});
76+
clientWidth: ref.current ? ref.current.parentElement.clientWidth : minWidth,
77+
})
7578
};
7679

7780
useEffect(() => {
78-
window.addEventListener('resize', updateTableDimensions);
79-
return () => window.removeEventListener('resize', updateTableDimensions);
80-
}, [ updateTableDimensions ]);
81+
action(SET_VISIBLE_COLUMN_IDS)({ ids: getInitialVisibleColumns(columns) });
82+
action(SET_TABLE_WIDTH)({ width: minWidth, widthAdjustment: 1 });
83+
action(SET_COLUMN_WIDTHS)(columns.reduce((acc, column) => {
84+
if (column.visible !== false) {
85+
acc.push(column.width);
86+
}
87+
return acc;
88+
}, []));
89+
}, []);
90+
91+
useEffect(() => {
92+
addElementResizeEventListener(ref.current, updateTableDimensions);
93+
return () => removeElementResizeEventListener(ref.current)
94+
}, []);
8195

82-
useEffect(() => updateTableDimensions(), [ ref.current ]);
96+
useEffect(() => updateTableDimensions(), [ ref.current.clientWidth ]);
8397

8498
const totalHeight = rowHeight * itemCount;
8599
const visibleHeight = minHeight || totalHeight;

src/components/Columns.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _ from 'lodash';
12
import React, { useContext } from 'react';
23
import { useSelector } from 'react-redux';
34
import { withDropdown } from '../hoc';
@@ -30,16 +31,19 @@ const Columns = ({
3031
className="rdt-toolbar-item"
3132
padding="0.25rem 0.75rem"
3233
styles={ styles.dropdownItem }
34+
htmlFor={ `rdt-columns-${name}-${index}` }
3335
>
34-
<input name={ name }
36+
<input
37+
id={ `rdt-columns-${name}-${index}` }
38+
name={ name }
3539
type="checkbox"
3640
style={{ margin: 5 }}
3741
checked={ -1 !== visibleColumnIds.indexOf(index) }
3842
onChange={(event) => (
3943
action(SET_VISIBLE_COLUMN_IDS)({ index, checked: event.target.checked, width })
4044
)}
4145
/>
42-
<label htmlFor={ name }>{ label }</label>
46+
<label>{ label }</label>
4347
</Dropdown.Item>
4448
))}
4549
</Dropdown.Menu>

src/createTable.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,14 @@ import {
1212
createActionCreator,
1313
isObject,
1414
calculateWidth,
15-
getInitialVisibleColumns,
1615
toPascalCase,
1716
prepareActionPayload
1817
} from './utils';
1918
import {
2019
SET_PAGE,
2120
SET_SORT,
2221
SET_LIMIT,
23-
SET_IS_EDITING,
24-
SET_VISIBLE_COLUMN_IDS,
25-
SET_TABLE_WIDTH,
26-
SET_COLUMN_WIDTHS
22+
SET_IS_EDITING
2723
} from './actions';
2824

2925
const getVisibleColumns = (columns) => _.memoize((visibleColumnIds) => (
@@ -57,14 +53,6 @@ const ReduxDatatable = ( props ) => {
5753
action(SET_LIMIT)({ limit: Limiter.default || 10 });
5854
action(SET_SORT)({ dir: 'desc' });
5955
action(SET_IS_EDITING)({ value: config.editing });
60-
action(SET_VISIBLE_COLUMN_IDS)({ ids: getInitialVisibleColumns(columns) });
61-
action(SET_TABLE_WIDTH)({ width: minWidth, widthAdjustment: 1 });
62-
action(SET_COLUMN_WIDTHS)(columns.reduce((acc, column) => {
63-
if (column.visible !== false) {
64-
acc.push(column.width);
65-
}
66-
return acc;
67-
}, []));
6856
}, [ dispatch ]);
6957

7058
const tableConfig = {
@@ -96,7 +84,10 @@ const ReduxDatatable = ( props ) => {
9684

9785
const componentConfig = _.get(components, id, false);
9886
if (componentConfig !== false && !!componentConfig.renderer === true) {
99-
return componentConfig.renderer({ componentConfig });
87+
return {
88+
Component: componentConfig.renderer,
89+
componentConfig
90+
};
10091
}
10192

10293
const type = toPascalCase(componentConfig.type || id);

src/hoc/withScrollSpy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export let scrollerMap = {};
99
const withScrollSpy = WrappedComponent => (props) => {
1010
const { name } = props;
1111
const id = _.uniqueId(name);
12-
const ref = useRef(null);
12+
const ref = useRef({});
1313
const [ scrollData, setScrollData ] = useState({ top: 0, pointerEvents: 'none' });
1414
const { getData, minWidth, config: { name: tableName, height } } = useContext(ConfigContext);
1515

src/reducer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ let initialTableState = {
3636
const calculateTableWidth = (columnWidths, clientWidth) => {
3737
const totalColWidth = columnWidths.reduce((a, b) => a + b, 0);
3838
const innerWidth = totalColWidth > clientWidth ? totalColWidth : clientWidth;
39-
const widthAdjustment = innerWidth / totalColWidth;
39+
const widthAdjustment = innerWidth / (totalColWidth || innerWidth);
4040
return {
4141
width: totalColWidth * widthAdjustment,
4242
widthAdjustment

src/styled-components/Dropdown.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ export const Menu = styled.div `
3737
`};
3838
`;
3939

40-
export const Item = styled.div `
40+
export const Item = styled.label `
41+
display: block;
4142
clear: both;
4243
text-align: inherit;
4344
white-space: nowrap;

0 commit comments

Comments
 (0)