Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. /**
  2. * 关联refs中的DOM
  3. */
  4.  
  5. import $ from 'jquery'
  6. import { findDOMNode } from 'react-dom'
  7.  
  8. const isDev = process.env.NODE_ENV === 'development'
  9.  
  10. function getDisplayName(target) {
  11. if (target.displayName) {
  12. return target.displayName
  13. }
  14.  
  15. return target.constructor && target.constructor.name || 'Component'
  16. },
  17.  
  18.  
  19. /**
  20. * 关联refs中的DOM
  21. *
  22. * @param {string} name refs中的组件名
  23. *
  24. * @return {object}
  25. */
  26. export default function getEl(name) {
  27. return (target, key, descriptor) => {
  28.  
  29. // bind native node
  30. descriptor.get = function () {
  31. if (!this.refs[name]) {
  32. return null
  33. }
  34.  
  35. return findDOMNode(this.refs[name])
  36. }
  37.  
  38. const own = Object.prototype.hasOwnProperty
  39.  
  40. const jQueryPropertyName = `$${key}`
  41.  
  42. if (isDev && own.call(target, jQueryPropertyName)) {
  43. const displayName = getDisplayName(target)
  44. console.warn(`${jQueryPropertyName} also in ${displayName}`)
  45.  
  46. return
  47. }
  48.  
  49. // bind jQuery node
  50. Object.defineProperty(target, jQueryPropertyName, {
  51. get() {
  52. if (!this.refs[name]) {
  53. return null
  54. }
  55.  
  56. return $(findDOMNode(this.refs[name]))
  57. }
  58. })
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement