Skip to content

Commit

Permalink
'collapseOnChange' and 'freezeWhenExpanded' (#61)
Browse files Browse the repository at this point in the history
* rough draft of "closeSubComponentOnDataChange" and "preventAutoSortWhenComponentIsOpen"

* changed flag name from ...component to ...subcomponent

* fixed problem with sorting not immediately being applied. passes tests now

* changed var names

* Cleanup and simplification

* Fix deps

* Better freezing strategy

* subcomponent reverse
  • Loading branch information
janzheng authored and tannerlinsley committed Feb 6, 2017
1 parent aa48ef5 commit 1e4f838
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 14 deletions.
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"react-dom": "^15.4.2",
"react-json-tree": "^0.10.1",
"standard": "^8.0.0",
"storybook": "^0.0.0",
"stylus": "^0.54.5",
"uglifyify": "3.0.3"
},
Expand Down
4 changes: 2 additions & 2 deletions src/componentMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,13 @@ export default {
hasHeaderGroups
}
},
getSortedData (nextProps, nextState) {
getSortedData (state) {
const {
manual,
sorting,
allDecoratedColumns,
resolvedData
} = this.getResolvedState(nextProps, nextState)
} = state

const resolvedSorting = sorting.length ? sorting : this.getInitSorting(allDecoratedColumns)

Expand Down
50 changes: 41 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export const ReactTableDefaults = {
defaultPageSize: 20,
showPageJump: true,
expanderColumnWidth: 35,
collapseOnChange: true,
freezeWhenExpanded: false,

// Controlled State Overrides
// page: undefined,
Expand Down Expand Up @@ -154,10 +156,10 @@ export default React.createClass({

getResolvedState (props, state) {
const resolvedState = {
...this.state,
...state,
...this.props,
...props
..._.compactObject(this.state),
..._.compactObject(state),
..._.compactObject(this.props),
..._.compactObject(props)
}
return resolvedState
},
Expand All @@ -173,6 +175,7 @@ export default React.createClass({
componentWillReceiveProps (nextProps, nextState) {
const oldState = this.getResolvedState()
const newState = this.getResolvedState(nextProps, nextState)

// Props that trigger a data update
if (
oldState.data !== newState.data ||
Expand All @@ -187,22 +190,49 @@ export default React.createClass({
setStateWithData (newState, cb) {
const oldState = this.getResolvedState()
const newResolvedState = this.getResolvedState({}, newState)
const { freezeWhenExpanded } = newResolvedState

// Default to unfrozen state
newResolvedState.frozen = false

// If freezeWhenExpanded is set, check for frozen conditions
if (freezeWhenExpanded) {
// if any rows are expanded, freeze the existing data and sorting
const keys = Object.keys(newResolvedState.expandedRows)
for (var i = 0; i < keys.length; i++) {
if (newResolvedState.expandedRows[keys[i]]) {
newResolvedState.frozen = true
break
}
}
}

// If the data isn't frozen and either the data or
// sorting model has changed, update the data
if (
oldState.resolvedData !== newResolvedState.resolvedData ||
oldState.sorting !== newResolvedState.sorting
(oldState.frozen && !newResolvedState.frozen) ||
oldState.sorting !== newResolvedState.sorting ||
(!newResolvedState.frozen && oldState.resolvedData !== newResolvedState.resolvedData)
) {
Object.assign(newState, this.getSortedData({}, newState))
// If collapseOnChange is set, automatically close expanded subcomponents
if (this.props.collapseOnChange) {
newResolvedState.expandedRows = {}
}
Object.assign(newResolvedState, this.getSortedData(newResolvedState))
}

// Calculate pageSize all the time
if (newResolvedState.resolvedData) {
newState.pages = newResolvedState.manual ? newResolvedState.pages : Math.ceil(newResolvedState.resolvedData.length / newResolvedState.pageSize)
newResolvedState.pages = newResolvedState.manual ? newResolvedState.pages : Math.ceil(newResolvedState.resolvedData.length / newResolvedState.pageSize)
}
return this.setState(newState, cb)

return this.setState(newResolvedState, cb)
},

shouldComponentUpdate (nextProps, nextState) {
const oldState = this.getResolvedState()
const newState = this.getResolvedState(nextProps, nextState)

// State changes that trigger a render
if (
oldState.sortedData !== newState.sortedData ||
Expand Down Expand Up @@ -390,6 +420,7 @@ export default React.createClass({
}
return (
<ThComponent
key={i}
className={classnames(
'rt-expander-header',
classes
Expand Down Expand Up @@ -669,6 +700,7 @@ export default React.createClass({
// Return the regular expander cell
return (
<TdComponent
key={i2}
className={classnames(
classes,
{hidden: !show}
Expand Down
13 changes: 12 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export default {
makeTemplateComponent,
groupBy,
isArray,
splitProps
splitProps,
compactObject
}

function get (obj, path, def) {
Expand Down Expand Up @@ -173,3 +174,13 @@ function splitProps ({className, style, ...rest}) {
rest
}
}

function compactObject (obj) {
const newObj = {}
for (var key in obj) {
if (obj.hasOwnProperty(key) && obj[key] !== undefined && typeof obj[key] !== 'undefined') {
newObj[key] = obj[key]
}
}
return newObj
}
2 changes: 1 addition & 1 deletion stories/SubComponents.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,4 @@ export default (
/>
)
`
}
}
6 changes: 5 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2998,7 +2998,7 @@ indexof@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/indexof/-/indexof-0.0.1.tgz#82dc336d232b9062179d05ab3293a66059fd435d"

inflight@^1.0.4:
inflight@^1.0.4, inflight@^1.0.6:
version "1.0.6"
resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9"
dependencies:
Expand Down Expand Up @@ -5248,6 +5248,10 @@ standard@^8.0.0:
version "1.3.1"
resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.3.1.tgz#faf51b9eb74aaef3b3acf4ad5f61abf24cb7b93e"

storybook@^0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/storybook/-/storybook-0.0.0.tgz#b3b9508fb99fd83615674917583cfe9e1532931d"

stream-browserify@^2.0.0, stream-browserify@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/stream-browserify/-/stream-browserify-2.0.1.tgz#66266ee5f9bdb9940a4e4514cafb43bb71e5c9db"
Expand Down

2 comments on commit 1e4f838

@janzheng
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you like me to update the readme file with documentation? ;)

@tannerlinsley
Copy link
Collaborator

@tannerlinsley tannerlinsley commented on 1e4f838 Feb 9, 2017 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.