Skip to content

Commit 03b336f

Browse files
authored
fix(components): Fix table vertical scroll (#460)
* Fix table vertical scroll when maxHeight is set to the table container * Fix snapshot test
1 parent 388a19f commit 03b336f

File tree

3 files changed

+18
-31
lines changed

3 files changed

+18
-31
lines changed

src/components/Table/Table.js

+11-17
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ const scrollableStyles = ({ scrollable, height }) =>
8989
const ScrollContainer = styled.div`
9090
${containerStyles};
9191
${scrollableStyles};
92-
${shadowSingle};
93-
${noShadowStyles};
9492
`;
9593

9694
/**
@@ -151,20 +149,18 @@ class Table extends Component {
151149
tableBodyHeight: null
152150
};
153151

154-
componentDidUpdate({ scrollable, rowHeaders }) {
155-
const shouldAddScroll =
156-
(!scrollable && this.props.scrollable) ||
157-
(rowHeaders && !this.props.rowHeaders);
158-
159-
const shouldRemoveScroll =
160-
(scrollable && !this.props.scrollable) ||
161-
(!rowHeaders && this.props.rowHeaders);
152+
componentDidMount() {
153+
if (this.props.scrollable) {
154+
this.addVerticalScroll();
155+
}
156+
}
162157

163-
if (shouldAddScroll) {
158+
componentDidUpdate({ scrollable }) {
159+
if (!scrollable && this.props.scrollable) {
164160
this.addVerticalScroll();
165161
}
166162

167-
if (shouldRemoveScroll) {
163+
if (scrollable && !this.props.scrollable) {
168164
this.removeVerticalScroll();
169165
}
170166
}
@@ -190,11 +186,9 @@ class Table extends Component {
190186

191187
calculateTableBodyHeight = () => {
192188
this.setState({
193-
tableBodyHeight: `${
194-
isNil(this.tableContainer)
195-
? 'unset'
196-
: this.tableContainer.parentNode.offsetHeight
197-
}px`
189+
tableBodyHeight: isNil(this.tableContainer)
190+
? 'unset'
191+
: `${this.tableContainer.parentNode.offsetHeight}px`
198192
});
199193
};
200194

src/components/Table/Table.story.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ storiesOf(`${GROUPS.COMPONENTS}|Table`, module)
6565
.add(
6666
'Table',
6767
withInfo()(() => (
68-
<div style={{ width: '98vw', height: 150 }}>
68+
<div style={{ width: '98vw', maxHeight: 150 }}>
6969
<Table
7070
headers={headers}
7171
rows={rows}

src/components/Table/__snapshots__/Table.spec.js.snap

+6-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ exports[`Table Style tests should not render a scrollable table if the rowHeader
88
99
.circuit-47 {
1010
border-radius: 4px;
11-
box-shadow: 0 0 0 1px rgba(12,15,20,0.02), 0 0 1px 0 rgba(12,15,20,0.06), 0 2px 2px 0 rgba(12,15,20,0.06);
1211
}
1312
1413
@media (max-width:767px) {
@@ -390,7 +389,6 @@ exports[`Table Style tests should render a collapsed table 1`] = `
390389
391390
.circuit-47 {
392391
border-radius: 4px;
393-
box-shadow: 0 0 0 1px rgba(12,15,20,0.02), 0 0 1px 0 rgba(12,15,20,0.06), 0 2px 2px 0 rgba(12,15,20,0.06);
394392
}
395393
396394
@media (max-width:767px) {
@@ -773,7 +771,6 @@ exports[`Table Style tests should render a condensed table 1`] = `
773771
774772
.circuit-47 {
775773
border-radius: 4px;
776-
box-shadow: 0 0 0 1px rgba(12,15,20,0.02), 0 0 1px 0 rgba(12,15,20,0.06), 0 2px 2px 0 rgba(12,15,20,0.06);
777774
}
778775
779776
@media (max-width:767px) {
@@ -1265,12 +1262,6 @@ tbody .circuit-9:last-child td {
12651262
}
12661263
}
12671264
1268-
.circuit-39 {
1269-
height: 100%;
1270-
overflow-y: auto;
1271-
box-shadow: 0 0 0 1px rgba(12,15,20,0.02), 0 0 1px 0 rgba(12,15,20,0.06), 0 2px 2px 0 rgba(12,15,20,0.06);
1272-
}
1273-
12741265
.circuit-37 {
12751266
background-color: #FFFFFF;
12761267
border-collapse: separate;
@@ -1321,12 +1312,17 @@ tbody .circuit-9:last-child td {
13211312
opacity: 1;
13221313
}
13231314
1315+
.circuit-39 {
1316+
height: unset;
1317+
overflow-y: auto;
1318+
}
1319+
13241320
<div
13251321
className="circuit-41 circuit-42"
13261322
>
13271323
<div
13281324
className="circuit-39 circuit-40"
1329-
height={null}
1325+
height="unset"
13301326
onScroll={[Function]}
13311327
>
13321328
<table
@@ -1456,7 +1452,6 @@ exports[`Table Style tests should render with default styles 1`] = `
14561452
14571453
.circuit-47 {
14581454
border-radius: 4px;
1459-
box-shadow: 0 0 0 1px rgba(12,15,20,0.02), 0 0 1px 0 rgba(12,15,20,0.06), 0 2px 2px 0 rgba(12,15,20,0.06);
14601455
}
14611456
14621457
@media (max-width:767px) {
@@ -1838,7 +1833,6 @@ exports[`Table Style tests should render with rowHeader styles 1`] = `
18381833
18391834
.circuit-47 {
18401835
border-radius: 4px;
1841-
box-shadow: 0 0 0 1px rgba(12,15,20,0.02), 0 0 1px 0 rgba(12,15,20,0.06), 0 2px 2px 0 rgba(12,15,20,0.06);
18421836
}
18431837
18441838
@media (max-width:767px) {
@@ -2215,7 +2209,6 @@ tbody .circuit-11:last-child td {
22152209
exports[`Table Style tests should render without the table shadow 1`] = `
22162210
.circuit-47 {
22172211
border-radius: 4px;
2218-
box-shadow: 0 0 0 1px rgba(12,15,20,0.02), 0 0 1px 0 rgba(12,15,20,0.06), 0 2px 2px 0 rgba(12,15,20,0.06);
22192212
}
22202213
22212214
@media (max-width:767px) {

0 commit comments

Comments
 (0)