Advertisement
Guest User

Untitled

a guest
May 30th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. var DisabledList = React.createClass({
  2.  
  3. // proptypes tell the parent widget what to pass into it
  4. // the DropdownList will inspect propTypes and _.pick() those keys to pass in
  5. propTypes: {
  6. disabledItems: React.PropTypes.array,
  7. ...List.type.propTypes,
  8. },
  9.  
  10. componentWillMount(){
  11. var parent = this
  12.  
  13. // we need to use a closure to allow the list item to know whether its disabled or not.
  14. // do this infrequently though b/c it is costly, hence we cache it in state
  15. this.setState({
  16. listItem: React.createClass({
  17. render() {
  18. // add css rules to make it look "disabled"
  19. var classes = parent.isDisabled(this.props.item) ? 'rw-state-disabled' : ''
  20.  
  21. return <div className={classes}>{this.props.item.name}</div>
  22. }
  23. })
  24. })
  25. },
  26.  
  27. render() {
  28. return (
  29. <List {...this.props}
  30. ref='list'
  31. itemComponent={this.state.listItem}
  32. onSelect={ item => {
  33. if (!this.isDisabled(item))
  34. this.props.onSelect(item) // only allow selection of non-disabled items
  35. }}
  36. />
  37. )
  38. },
  39.  
  40. isDisabled(item){
  41. return this.props.disabledItems
  42. && this.props.disabledItems.some( id => id === item.id )
  43. },
  44.  
  45. // Get the next item in the sequence, but keep going if the next one is disabled
  46. move(dir, item){
  47. var stop = dir === 'next' ? this.refs.list.last() : this.refs.list.first()
  48. , next = this.refs.list[dir](item);
  49.  
  50. while( next !== stop && this.isDisabled(next))
  51. next = this.refs.list[dir](next)
  52.  
  53. return this.isDisabled(next) ? item : next
  54. },
  55.  
  56.  
  57. // -- These are the basic List methods that must be implemented
  58. first() {
  59. this.move('next', null)
  60. },
  61.  
  62. last() {
  63. this.move('prev', null)
  64. },
  65.  
  66. next(...args){
  67. var item = this.refs.list.next(...args)
  68.  
  69. return this.move('next', item)
  70. },
  71.  
  72. prev(...args){
  73. var item = this.refs.list.prev(...args)
  74.  
  75. return this.move('prev', item)
  76. }
  77.  
  78. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement