Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. import * as React from 'react';
  2. import '../styles/components/choice-list.css';
  3. import ChoiceOption from './choice-option';
  4.  
  5. interface choiceMultipleState{
  6. itemsSelected: string[];
  7. }
  8. interface food{
  9. name: string,
  10. veg: boolean
  11. }
  12.  
  13. interface ChoiceMultipleProps{
  14. options: food[];
  15.  
  16. }
  17. class choiceState extends React.Component<ChoiceMultipleProps, choiceMultipleState> {
  18. state={
  19. itemsSelected: [] as string[]
  20. }
  21. render(){
  22. let selectedFood = this.findSelected();
  23. //<ChoiceOption option={option} onClick={this.clickSelect} selectedOption>
  24. return(
  25. <div>
  26. <div>
  27. <ul className="choice-list" >
  28. {this.props.options.map((item: food) => {
  29. return (
  30. <ChoiceOption item={item} onSelect={this.setSelected}/>
  31.  
  32. );
  33. })}
  34. </ul>
  35. </div>
  36. {console.log(selectedFood)}
  37. {selectedFood.length>0 && (
  38. <div>
  39. <h3> Selected </h3>
  40. <ul className= "choice-list">
  41. {selectedFood.map((item: food) => {
  42. return (
  43. <ChoiceOption item={item} onSelect={this.setSelected}/>
  44. )
  45. })}
  46. </ul>
  47. </div>
  48. )}
  49. </div>
  50. )
  51. }
  52. setSelected = (item: food) => {
  53. let newArray = this.state.itemsSelected.slice();
  54. newArray.push(item.name);
  55. this.setState({
  56. itemsSelected:newArray
  57. });
  58. }
  59. findSelected = (): food[] => {
  60. return this.props.options.filter((item: food) => {
  61. return this.state.itemsSelected.indexOf(item.name)>=0
  62. });
  63. }
  64.  
  65. }
  66. export default choiceState;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement