thinking.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. let ProductCategoryRow = React.createClass({
  2. name: "ProductCategoryRow",
  3. componentDidMount: function () {
  4. console.log("Component did mountDM", this.name);
  5. },
  6. getInitialState: function () {
  7. console.log("Get Initial State", this.name);
  8. return { data: [] };
  9. },
  10. render: function () {
  11. return (<tr><th colSpan="2">{this.props.category}</th></tr>);
  12. }
  13. });
  14. let ProductRow = React.createClass({
  15. name: "ProductRow",
  16. componentDidMount: function () {
  17. console.log("Component did mountDM", this.name);
  18. },
  19. getInitialState: function () {
  20. console.log("Get Initial State", this.name);
  21. return { data: [] };
  22. },
  23. render: function () {
  24. let name = this.props.product.stocked
  25. ? this.props.product.name
  26. : <span style={{color: 'red'}}>{this.props.product.name}</span>;
  27. return (
  28. <tr>
  29. <td>{name}</td>
  30. <td>{this.props.product.price}</td>
  31. </tr>
  32. );
  33. }
  34. });
  35. let ProductTable = React.createClass({
  36. name: "ProductTable",
  37. componentDidMount: function () {
  38. console.log("Component did mountDM", this.name);
  39. },
  40. getInitialState: function () {
  41. console.log("Get Initial State", this.name);
  42. return { data: [] };
  43. },
  44. render: function () {
  45. let rows = [];
  46. let lastCategory = null;
  47. this.props.products.forEach(function (product) {
  48. if (product.category !== lastCategory) {
  49. rows.push(<ProductCategoryRow category={product.category} key={product.category} />);
  50. }
  51. rows.push(<ProductRow product={product} key={product.name} />);
  52. lastCategory = product.category;
  53. });
  54. return (
  55. <table>
  56. <thead>
  57. <tr>
  58. <th>Name</th>
  59. <th>Price</th>
  60. </tr>
  61. </thead>
  62. <tbody>
  63. {rows}
  64. </tbody>
  65. </table>
  66. );
  67. }
  68. });
  69. let SearchBar = React.createClass({
  70. name: "SearchBar",
  71. componentDidMount: function () {
  72. console.log("Component did mountDM", this.name);
  73. },
  74. getInitialState: function () {
  75. console.log("Get Initial State", this.name);
  76. return { data: [] };
  77. },
  78. render: function () {
  79. return (
  80. <form>
  81. <input type="text" placeholder="Search..." />
  82. <p>
  83. <input type="checkbox" />
  84. {' '}
  85. Only show products in stock
  86. </p>
  87. </form>
  88. );
  89. }
  90. });
  91. let FilterableProductTable = React.createClass({
  92. name: "FilterableProductTable",
  93. componentDidMount: function () {
  94. console.log("Component did mountDM", this.name);
  95. },
  96. getInitialState: function () {
  97. console.log("Get Initial State", this.name);
  98. return { data: [] };
  99. },
  100. render: function () {
  101. return (
  102. <div>
  103. <SearchBar />
  104. <ProductTable products={this.props.products} />
  105. </div>
  106. );
  107. }
  108. });
  109. ReactDOM.render(
  110. <FilterableProductTable products={[]} />,
  111. document.getElementById("content")
  112. );