Skip to content

Commit

Permalink
more clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
sghall committed Jun 13, 2021
1 parent c8ac25a commit 3b0bd66
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
13 changes: 10 additions & 3 deletions src/Animate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ const keyAccessor = () => '$$key$$'

class Animate extends Component {
render() {
const { show, start, enter, update, leave, interpolation, children } = this.props
const {
show,
start,
enter,
update,
leave,
interpolation,
children,
} = this.props
const data = typeof start === 'function' ? start() : start

return (
Expand All @@ -24,7 +32,7 @@ class Animate extends Component {
if (!nodes[0]) {
return null
}

const renderedChildren = children(nodes[0].state)
return renderedChildren && React.Children.only(renderedChildren)
}}
Expand Down Expand Up @@ -83,4 +91,3 @@ Animate.defaultProps = {
}

export default Animate

29 changes: 14 additions & 15 deletions src/NodeGroup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class NodeGroup extends Component {
data: null,
}
}

static getDerivedStateFromProps(nextProps, prevState) {
if (nextProps.data !== prevState.data) {
const { data, keyAccessor, start, enter, update, leave } = nextProps
Expand All @@ -45,7 +45,7 @@ class NodeGroup extends Component {

const nextKeyIndex = {}
const nextNodeKeys = []

for (let i = 0; i < data.length; i++) {
const d = data[i]
const k = keyAccessor(d, i)
Expand All @@ -61,7 +61,7 @@ class NodeGroup extends Component {
nodeHash[k] = node
}
}

for (let i = 0; i < nodeKeys.length; i++) {
const k = nodeKeys[i]
const n = nodeHash[k]
Expand All @@ -73,14 +73,14 @@ class NodeGroup extends Component {
n.type = LEAVE
}
}

const mergedNodeKeys = mergeKeys(
nodeKeys,
keyIndex,
nextNodeKeys,
nextKeyIndex,
)

for (let i = 0; i < mergedNodeKeys.length; i++) {
const k = mergedNodeKeys[i]
const n = nodeHash[k]
Expand All @@ -95,7 +95,7 @@ class NodeGroup extends Component {
n.transition(update(d, nextKeyIndex[k]))
}
}

return {
data,
nodes: mergedNodeKeys.map(key => {
Expand All @@ -108,25 +108,25 @@ class NodeGroup extends Component {

return null
}

componentDidMount() {
this.startInterval()
}

componentDidUpdate(prevProps) {
if (prevProps.data !== this.props.data && !this.unmounting) {
this.startInterval()
}
}

startInterval() {
if (!this.interval) {
this.interval = interval(this.animate)
} else {
this.interval.restart(this.animate)
}
}

componentWillUnmount() {
const { nodeKeys, nodeHash } = this.state

Expand All @@ -140,7 +140,7 @@ class NodeGroup extends Component {
nodeHash[key].stopTransitions()
})
}

animate = () => {
const { nodeKeys, nodeHash } = this.state

Expand All @@ -152,7 +152,7 @@ class NodeGroup extends Component {

const nextNodeKeys = []
const length = nodeKeys.length

for (let i = 0; i < length; i++) {
const k = nodeKeys[i]
const n = nodeHash[k]
Expand All @@ -169,7 +169,7 @@ class NodeGroup extends Component {
nextNodeKeys.push(k)
}
}

if (!pending) {
this.interval.stop()
}
Expand All @@ -181,7 +181,7 @@ class NodeGroup extends Component {
}),
}))
}

interval = null
unmounting = false

Expand Down Expand Up @@ -234,4 +234,3 @@ NodeGroup.defaultProps = {
}

export default NodeGroup

8 changes: 2 additions & 6 deletions src/NodeGroup/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,11 @@ describe('<NodeGroup />', () => {
)
})

it('should remove nodes that are not transitioning', (done) => {
it('should remove nodes that are not transitioning', done => {
const data = getData()

const wrapper = mount(
<NodeGroup
data={data}
keyAccessor={keyAccessor}
start={() => ({})}
>
<NodeGroup data={data} keyAccessor={keyAccessor} start={() => ({})}>
{renderChildren}
</NodeGroup>,
)
Expand Down

0 comments on commit 3b0bd66

Please sign in to comment.