Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3.  
  4. import shopping from './zadanie02';
  5.  
  6.  
  7. class Receipt extends React.Component {
  8. render() {
  9.  
  10.  
  11. const sum = this.props.shopping.reduce((prev, curr) => {
  12. return prev + (curr.count * curr.price);
  13. }, 0);
  14.  
  15. return (
  16. <table>
  17. <ReceiptHeader />
  18. <ReceiptItems items={ this.props.shopping } />
  19. <ReceiptFooter sum={ sum } />
  20. </table>
  21. )
  22. }
  23. }
  24.  
  25. class ReceiptHeader extends React.Component {
  26. render() {
  27. return (
  28. <thead>
  29. <tr>
  30. <th>nazwa</th>
  31. <th>cena</th>
  32. <th>ilość</th>
  33. <th>suma</th>
  34. </tr>
  35. </thead>
  36. )
  37. }
  38. }
  39.  
  40. class ReceiptItems extends React.Component {
  41. render() {
  42. const rows = this.props.items.map(item => {
  43. return (
  44. <ReceiptItem
  45. {...item}
  46.  
  47. price={item.price}
  48. count={item.count}
  49. name={item.name}
  50. />
  51. )
  52. });
  53.  
  54. return (
  55. <tbody>
  56. {rows}
  57. </tbody>
  58. )
  59. }
  60. }
  61.  
  62. class ReceiptItem extends React.Component {
  63. render() {
  64. return (
  65. <tr>
  66. <td>{this.props.name}</td>
  67. <td>{this.props.count}</td>
  68. <td>{this.props.price}</td>
  69. <td>{(this.props.count * this.props.price).toFixed(2)}</td>
  70. </tr>
  71. )
  72. }
  73. }
  74.  
  75. class ReceiptFooter extends React.Component {
  76. render() {
  77. return (
  78. <tfoot>
  79. <tr>
  80. <td>Razem</td>
  81. <td>-</td>
  82. <td>-</td>
  83. <td>{ this.props.sum.toFixed(2) }</td>
  84. </tr>
  85. </tfoot>
  86. )
  87. }
  88. }
  89.  
  90. class App extends React.Component {
  91. render() {
  92. return <Receipt shopping={ shopping } />
  93. }
  94. }
  95.  
  96. document.addEventListener('DOMContentLoaded', function(){
  97. ReactDOM.render(
  98. <App/>,
  99. document.getElementById('app')
  100. );
  101. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement