Skip to content

Commit 098b1fe

Browse files
ldsteintannerlinsley
authored andcommitted
Max width NaN fix (TanStack#415)
* Added button to reset table state in Controlled Component story * Fix maxWidth style when value is NaN * code formatting
1 parent 750cfe3 commit 098b1fe

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

docs/src/stories/ControlledTable.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const columns = [{
2121
}, {
2222
Header: 'Last Name',
2323
id: 'lastName',
24-
accessor: d => d.lastName
24+
accessor: d => d.lastName,
25+
width: 170
2526
}]
2627
}, {
2728
Header: 'Info',
@@ -31,21 +32,31 @@ const columns = [{
3132
}]
3233
}]
3334

35+
function makeDefaultState()
36+
{
37+
return {
38+
sorted: [],
39+
page: 0,
40+
pageSize: 10,
41+
expanded: {},
42+
resized: [],
43+
filtered: []
44+
}
45+
}
46+
3447
class Story extends React.PureComponent {
3548
constructor () {
3649
super()
37-
this.state = {
38-
sorted: [],
39-
page: 0,
40-
pageSize: 10,
41-
expanded: {},
42-
resized: [],
43-
filtered: []
44-
}
50+
this.state = makeDefaultState()
51+
this.resetState = this.resetState.bind(this)
52+
}
53+
resetState(){
54+
this.setState(makeDefaultState())
4555
}
4656
render () {
4757
return (
4858
<div>
59+
4960
<div className='table-wrap'>
5061
<ReactTable
5162
className='-striped -highlight'
@@ -70,6 +81,7 @@ class Story extends React.PureComponent {
7081
/>
7182
</div>
7283
<br />
84+
<div style={{float:'right'}}><button onClick={this.resetState}>Reset State</button></div>
7385
<pre><code><strong>this.state ===</strong> {JSON.stringify(this.state, null, 2)}</code></pre>
7486
<br />
7587
</div>

src/index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
261261

262262
const flexStyles = {
263263
flex: `${flex} 0 auto`,
264-
width: `${width}px`,
265-
maxWidth: `${maxWidth}px`,
264+
width: _.asPx(width),
265+
maxWidth: _.asPx(maxWidth),
266266
}
267267

268268
return (
@@ -376,8 +376,8 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
376376
style={{
377377
...styles,
378378
flex: `${width} 0 auto`,
379-
width: `${width}px`,
380-
maxWidth: `${maxWidth}px`,
379+
width: _.asPx(width),
380+
maxWidth: _.asPx(maxWidth),
381381
}}
382382
toggleSort={e => {
383383
isSortable && this.sortColumn(column, e.shiftKey)
@@ -475,8 +475,8 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
475475
style={{
476476
...styles,
477477
flex: `${width} 0 auto`,
478-
width: `${width}px`,
479-
maxWidth: `${maxWidth}px`,
478+
width: _.asPx(width),
479+
maxWidth: _.asPx(maxWidth),
480480
}}
481481
{...rest}
482482
>
@@ -706,8 +706,8 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
706706
style={{
707707
...styles,
708708
flex: `${width} 0 auto`,
709-
width: `${width}px`,
710-
maxWidth: `${maxWidth}px`,
709+
width: _.asPx(width),
710+
maxWidth: _.asPx(maxWidth),
711711
}}
712712
{...interactionProps}
713713
{...tdProps.rest}
@@ -798,8 +798,8 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
798798
style={{
799799
...styles,
800800
flex: `${flex} 0 auto`,
801-
width: `${width}px`,
802-
maxWidth: `${maxWidth}px`,
801+
width: _.asPx(width),
802+
maxWidth: _.asPx(maxWidth),
803803
}}
804804
{...tdProps.rest}
805805
>
@@ -878,8 +878,8 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
878878
style={{
879879
...styles,
880880
flex: `${width} 0 auto`,
881-
width: `${width}px`,
882-
maxWidth: `${maxWidth}px`,
881+
width: _.asPx(width),
882+
maxWidth: _.asPx(maxWidth),
883883
}}
884884
{...columnProps.rest}
885885
{...tFootTdProps.rest}

src/utils.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default {
1919
compactObject,
2020
isSortingDesc,
2121
normalizeComponent,
22+
asPx,
2223
}
2324

2425
function get (obj, path, def) {
@@ -142,6 +143,11 @@ function groupBy (xs, key) {
142143
}, {})
143144
}
144145

146+
function asPx (value) {
147+
value = Number(value)
148+
return (Number.isNaN(value)) ? null : value + 'px'
149+
}
150+
145151
function isArray (a) {
146152
return Array.isArray(a)
147153
}

0 commit comments

Comments
 (0)