thinking.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. handleChange: function (e) {
  83. this.props.onUserInput(
  84. this.refs.filterTextInput.value,
  85. this.refs.inStockOnlyInput.checked
  86. );
  87. },
  88. render: function () {
  89. return (
  90. <form>
  91. <input
  92. type="text"
  93. placeholder="Search..."
  94. ref="filterTextInput"
  95. value={this.props.filterText}
  96. onChange={this.handleChange}
  97. />
  98. <p>
  99. <input
  100. type="checkbox"
  101. ref="inStockOnlyInput"
  102. checked={this.props.inStockOnly}
  103. onChange={this.handleChange}
  104. />
  105. {" "}
  106. Only show products in stock
  107. </p>
  108. </form>
  109. );
  110. }
  111. });
  112. let FilterableProductTable = React.createClass({
  113. name: "FilterableProductTable",
  114. componentDidMount: function () {
  115. console.log("Component did mountDM", this.name, this.name, "State", this.state, "Props", this.props);
  116. },
  117. getInitialState: function () {
  118. let state = {
  119. filterText: "",
  120. inStockOnly: false
  121. };
  122. console.log("Get Initial State", this.name, state, "Props", this.props);
  123. return state;
  124. },
  125. handleUserInput: function (filterText, inStockOnly) {
  126. console.log("FPT.OSC", arguments);
  127. this.setState({ filterText: filterText, inStockOnly: inStockOnly });
  128. },
  129. render: function () {
  130. return (
  131. <div>
  132. <SearchBar
  133. filterText={this.state.filterText}
  134. inStockOnly={this.state.inStockOnly}
  135. onUserInput={this.handleUserInput}
  136. />
  137. <ProductTable
  138. products={this.props.products}
  139. filterText={this.state.filterText}
  140. inStockOnly={this.state.inStockOnly}
  141. />
  142. </div>
  143. );
  144. }
  145. });
  146. let PRODUCTS = [
  147. { category: "Sporting Goods", price: "$49.99", stocked: true, name: "Football" },
  148. { category: "Sporting Goods", price: "$9.99", stocked: true, name: "Baseball" },
  149. { category: "Sporting Goods", price: "$29.99", stocked: false, name: "Basketball" },
  150. { category: "Electronics", price: "$99.99", stocked: true, name: "iPod Touch" },
  151. { category: "Electronics", price: "$399.99", stocked: false, name: "iPhone 5" },
  152. { category: "Electronics", price: "$199.99", stocked: true, name: "Nexus 7" }
  153. ];
  154. ReactDOM.render(
  155. <FilterableProductTable products={PRODUCTS} />,
  156. document.getElementById("content")
  157. );