thinking.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. let ProductCategoryRow = React.createClass({
  2. name: "ProductCategoryRow",
  3. componentDidMount: function () {
  4. console.log("Component did mountDM", this.name, "State", this.state, "Props", this.props);
  5. },
  6. getInitialState: function () {
  7. console.log("Get Initial State", this.name);
  8. return null;
  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, "State", this.state, "Props", this.props);
  18. },
  19. getInitialState: function () {
  20. console.log("Get Initial State", this.name);
  21. return null;
  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, "State", this.state, "Props", this.props);
  39. },
  40. getInitialState: function () {
  41. console.log("Get Initial State", this.name);
  42. return null;
  43. },
  44. render: function () {
  45. let rows = [];
  46. let lastCategory = null;
  47. this.props.products.forEach(function (product) {
  48. // Apply name and stock filters.
  49. if (product.name.indexOf(this.props.filterText) === -1 || (!product.stocked && this.props.inStockOnly)) {
  50. return;
  51. }
  52. if (product.category !== lastCategory) {
  53. rows.push(<ProductCategoryRow category={product.category} key={product.category} />);
  54. }
  55. rows.push(<ProductRow product={product} key={product.name} />);
  56. lastCategory = product.category;
  57. }.bind(this));
  58. return (
  59. <table>
  60. <thead>
  61. <tr>
  62. <th>Name</th>
  63. <th>Price</th>
  64. </tr>
  65. </thead>
  66. <tbody>
  67. {rows}
  68. </tbody>
  69. </table>
  70. );
  71. }
  72. });
  73. let SearchBar = React.createClass({
  74. name: "SearchBar",
  75. componentDidMount: function () {
  76. console.log("Component did mountDM", this.name, "State", this.state, "Props", this.props);
  77. },
  78. getInitialState: function () {
  79. console.log("Get Initial State", this.name, "Props", this.props);
  80. return null;
  81. },
  82. render: function () {
  83. return (
  84. <form>
  85. <input type="text" placeholder="Search..." value={this.props.filterText} />
  86. <p>
  87. <input type="checkbox" checked={this.props.inStockOnly} />
  88. {' '}
  89. Only show products in stock
  90. </p>
  91. </form>
  92. );
  93. }
  94. });
  95. let FilterableProductTable = React.createClass({
  96. name: "FilterableProductTable",
  97. componentDidMount: function () {
  98. console.log("Component did mountDM", this.name, this.name, "State", this.state, "Props", this.props);
  99. },
  100. getInitialState: function () {
  101. let state = {
  102. filterText: "ball",
  103. inStockOnly: true
  104. };
  105. console.log("Get Initial State", this.name, state, "Props", this.props);
  106. return state;
  107. },
  108. render: function () {
  109. return (
  110. <div>
  111. <SearchBar
  112. filterText={this.state.filterText}
  113. inStockOnly={this.state.inStockOnly}
  114. />
  115. <ProductTable
  116. products={this.props.products}
  117. filterText={this.state.filterText}
  118. inStockOnly={this.state.inStockOnly}
  119. />
  120. </div>
  121. );
  122. }
  123. });
  124. let PRODUCTS = [
  125. {category: 'Sporting Goods', price: '$49.99', stocked: true, name: 'Football'},
  126. {category: 'Sporting Goods', price: '$9.99', stocked: true, name: 'Baseball'},
  127. {category: 'Sporting Goods', price: '$29.99', stocked: false, name: 'Basketball'},
  128. {category: 'Electronics', price: '$99.99', stocked: true, name: 'iPod Touch'},
  129. {category: 'Electronics', price: '$399.99', stocked: false, name: 'iPhone 5'},
  130. {category: 'Electronics', price: '$199.99', stocked: true, name: 'Nexus 7'}
  131. ];
  132. ReactDOM.render(
  133. <FilterableProductTable products={PRODUCTS} />,
  134. document.getElementById("content")
  135. );