Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. import { createElement as h, PureComponent, cloneElement } from 'react'
  2. import { spring, TransitionMotion } from 'react-motion'
  3.  
  4. export default class Eccentric extends PureComponent {
  5. static defaultProps = {
  6. component: h('div'),
  7. data: [],
  8. render: (key, data, style) => null,
  9. getKey: (datum, i) => datum.key, // must return valid string
  10. getDefaultStyles: datum => {},
  11. getStyles: datum => {},
  12. getWillEnter: datum => {},
  13. getWillLeave: datum => {}
  14. };
  15.  
  16. render () {
  17. return h(TransitionMotion, {
  18. defaultStyles: this.getDefaultStyles(),
  19. styles: this.getStyles(),
  20. willEnter: this.willEnter,
  21. willLeave: this.willLeave,
  22. children: this.renderCurrentStyles
  23. })
  24. }
  25.  
  26. renderCurrentStyles = currentStyles => {
  27. const { component: C, render } = this.props
  28. const children = Array.isArray(render)
  29. ? render.map(fn => currentStyles.map(this.createRenderFn(fn)))
  30. : render
  31.  
  32. return cloneElement(this.props.component, { children })
  33. };
  34.  
  35. createRenderFn = renderFn => ({ key, data, style }, i) => renderFn(key, data, style, i);
  36.  
  37. getDefaultStyles = () => {
  38. const {
  39. getKey,
  40. getDefaultStyles,
  41. data
  42. } = this.props
  43.  
  44. return data.map((data, i) => {
  45. return {
  46. key: getKey(data, i),
  47. data,
  48. style: getDefaultStyles(data, i)
  49. }
  50. })
  51. };
  52.  
  53. getStyles = () => {
  54. const {
  55. getKey,
  56. getStyles,
  57. data
  58. } = this.props
  59.  
  60. return data.map((data, i) => {
  61. return {
  62. key: getKey(data, i),
  63. data,
  64. style: getStyles(data, i)
  65. }
  66. })
  67. };
  68.  
  69. willEnter = config => {
  70. const { getWillEnter } = this.props
  71. return getWillEnter(config)
  72. };
  73.  
  74. willLeave = config => {
  75. const { getWillLeave } = this.props
  76. return getWillLeave(config)
  77. };
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement