thinking.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. ReactDOM.render(
  70. <ProductTable products={[]} />,
  71. document.getElementById("content")
  72. );