2015-10-21 20 views
39

Potresti aiutarmi con questa semplice domanda. Ho bisogno di aggiungere la classe "attiva" dopo aver fatto clic sul pulsante e rimuovere tutte le altre classi "attive".Come aggiungere una classe con React.js?

Guardate qui per favore: http://goo.gl/duLPlG

var Tags = React.createClass({ 
    setFilter: function(filter) { 
    this.props.onChangeFilter(filter); 
    }, 
    render: function() { 
    return <div className="tags"> 
     <button className="btn active" onClick={this.setFilter.bind(this, '')}>All</button> 
     <button className="btn" onClick={this.setFilter.bind(this, 'male')}>male</button> 
     <button className="btn" onClick={this.setFilter.bind(this, 'female')}>female</button> 
     <button className="btn" onClick={this.setFilter.bind(this, 'child')}>child</button> 
     <button className="btn" onClick={this.setFilter.bind(this, 'blonde')}>blonde</button> 
    </div> 
    } 
}); 

var Kid = React.createClass({ 
    render: function() { 
    return <ul> 
     <li>{this.props.name}</li> 
     </ul> 
    } 
}); 

var List = React.createClass({ 
    getInitialState: function() { 
    return { 
     filter: '' 
    }; 
    }, 
    changeFilter: function(filter) { 
    this.setState({ 
     filter: filter 
    }); 
    }, 
    render: function() { 
    var list = this.props.Data; 

    if (this.state.filter !== '') { 
     list = list.filter((i)=> i.tags.indexOf(this.state.filter) !== -1); 
     console.log(list); 
    } 

    list = list.map(function(Props){ 
     return <Kid {...Props} /> 
    }); 

    return <div> 
     <h2>Kids Finder</h2> 
     <Tags onChangeFilter={this.changeFilter}/> 
     {list} 
    </div> 
    } 
}); 

var options = { 
    Data: [{ 
    name: 'Eric Cartman', 
    tags: ['male', 'child'] 
    },{ 
    name: 'Wendy Testaburger', 
    tags: ['female', 'child'] 
    },{ 
    name: 'Randy Marsh', 
    tags: ['male'] 
    },{ 
    name: 'Butters Stotch', 
    tags: ['male', 'blonde', 'child'] 
    },{ 
    name: 'Bebe Stevens', 
    tags: ['female', 'blonde', 'child'] 
    }] 
}; 

var element = React.createElement(List, options); 
React.render(element, document.body); 

Come faccio a renderla migliore qui?

+0

Suggerirei di creare un componente "pulsante" piuttosto che il semplice rendering di un gruppo di collegamenti sotto il componente "Tag". Quindi per ciascun pulsante è possibile impostare condizionalmente l'attributo className. Questo potrebbe aiutare: https://github.com/JedWatson/classnames –

+1

@JohnGibbons, non voglio aggiungere altre utilità. Penso che sia una cosa semplice. Aggiorna progetto codepen con . –

+0

Ricorda quale pulsante è stato selezionato nello stato del componente. –

risposta

31

È semplice. dare un'occhiata a questo

https://codepen.io/anon/pen/mepogj?editors=001

fondamentalmente si vuole affrontare con gli stati del componente in modo da controllare quello attualmente attivo. è necessario includere

getInitialState: function(){} 
//and 
isActive: function(){} 

check-out il codice sul link

+0

Freddo. Molte grazie! –

+4

non farebbe male ad accettare la risposta;) –

+6

Apparentemente sarebbe – Chris

4

Dal momento che il componente <Tags> chiama una funzione sul relativo genitore, non è necessario uno stato aggiuntivo: è sufficiente passare il filtro al componente <Tags> come sostegno e utilizzarlo per il rendering dei pulsanti. In questo modo:

Cambia la tua funzione di rendere all'interno il componente <Tags> a:

render: function() { 
    return <div className = "tags"> 
    <button className = {this._checkActiveBtn('')} onClick = {this.setFilter.bind(this, '')}>All</button> 
    <button className = {this._checkActiveBtn('male')} onClick = {this.setFilter.bind(this, 'male')}>male</button> 
    <button className = {this._checkActiveBtn('female')} onClick = {this.setFilter.bind(this, 'female')}>female</button> 
    <button className = {this._checkActiveBtn('blonde')} onClick = {this.setFilter.bind(this, 'blonde')}>blonde</button> 
    </div> 
}, 

e aggiungere una funzione all'interno <Tags>:

_checkActiveBtn: function(filterName) { 
    return (filterName == this.props.activeFilter) ? "btn active" : "btn"; 
} 

E dentro il componente <List>, passare lo stato del filtro al <tags> componente come sostegno:

return <div> 
    <h2>Kids Finder</h2> 
    <Tags filter = {this.state.filter} onChangeFilter = {this.changeFilter} /> 
    {list} 
</div> 

Quindi dovrebbe funzionare come previsto. Codepen here (spero che il collegamento funzioni)

11

questo è molto utile:

https://github.com/JedWatson/classnames

È possibile fare cose come

classNames('foo', 'bar'); // => 'foo bar' 
classNames('foo', { bar: true }); // => 'foo bar' 
classNames({ 'foo-bar': true }); // => 'foo-bar' 
classNames({ 'foo-bar': false }); // => '' 
classNames({ foo: true }, { bar: true }); // => 'foo bar' 
classNames({ foo: true, bar: true }); // => 'foo bar' 

// lots of arguments of various types 
classNames('foo', { bar: true, duck: false }, 'baz', { quux: true }); // => 'foo bar baz quux' 

// other falsy values are just ignored 
classNames(null, false, 'bar', undefined, 0, 1, { baz: null }, ''); // => 'bar 1' 

o usalo in questo modo

var btnClass = classNames('btn', this.props.className, { 
    'btn-pressed': this.state.isPressed, 
    'btn-over': !this.state.isPressed && this.state.isHovered 
}); 
+0

classNames è ottimo, +1 – IliasT