Guest User

Untitled

a guest
Oct 15th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. import React, { Component } from 'react';
  2.  
  3. import Image from './Image';
  4.  
  5. class Item extends Component {
  6. constructor() {
  7. super();
  8. this.state = {
  9. displayOrder: ['image', 'name', 'price']
  10. };
  11. }
  12.  
  13. getState() {
  14. const stateObj = Object.assign({}, this.state);
  15. return stateObj;
  16. }
  17.  
  18. getProps() {
  19. const propsObj = Object.assign({}, this.props);
  20. return propsObj;
  21. }
  22.  
  23. newRow(rowName, inputVal) {
  24. let divClass = `c-item-row c-item__${rowName}`;
  25. return <div key={rowName} className={divClass}>{inputVal}</div>;
  26. }
  27.  
  28. getRows() {
  29. const listOfRows = this.getState().displayOrder;
  30. const propsObj = this.getProps();
  31. const propsArray = [];
  32.  
  33. listOfRows.forEach(thisIndex => {
  34. if(propsObj.hasOwnProperty(thisIndex)) {
  35. const thisValue = this.getFormattedValue(thisIndex, propsObj[thisIndex]);
  36. const newRow = this.newRow(thisIndex, thisValue);
  37. propsArray.push(newRow);
  38. }
  39. });
  40.  
  41. return propsArray;
  42. }
  43.  
  44. getFormattedValue(inputType, inputVal) {
  45. if(inputType === 'price')
  46. return this.prettifyPrice(inputVal);
  47.  
  48. if(inputType === 'image')
  49. return <Image src={inputVal} />
  50.  
  51. return inputVal;
  52. }
  53.  
  54. prettifyPrice(inputVal) {
  55. let price = inputVal.toFixed(2);
  56. price = price.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  57. return `$${price}`;
  58. }
  59.  
  60. render() {
  61. const propsObj = this.getRows();
  62.  
  63. return (
  64. <div className="c-item">
  65. <div className="c-item__display">
  66. {propsObj}
  67. </div>
  68. </div>
  69. );
  70. }
  71. }
  72.  
  73. export default Item;
Add Comment
Please, Sign In to add comment