Guest User

Untitled

a guest
Dec 13th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. app.js
  2. ```javascript
  3. import React, { Component } from 'react';
  4. import './app.scss';
  5.  
  6. const Bracket = ({b}) => <span className="bracket">&nbsp;{b}&nbsp;</span>;
  7. const ListWrapper = ({children}) => <section>{children}</section>;
  8. const MapWrapper = ({children}) => <section>{children}</section>;
  9.  
  10. const NodeKey = ({k}) => (<label>{'"' + k + '"'}:</label>);
  11. const NodeList = ({list}) => {
  12. return (
  13. <ListWrapper>
  14. {
  15. list.map((val, index) => {
  16. return <Node key={index} val={val} last={index === list.length - 1}/>;
  17. })
  18. }
  19. </ListWrapper>
  20. )
  21. }
  22.  
  23. const NodeMap = (({map}) => {
  24. let length = Object.keys(map).length;
  25. let count = 0;
  26. return (
  27. <MapWrapper>
  28. {
  29. Object.keys(map).map(key => {
  30. count ++;
  31. return <Node key={key} k={key} val={map[key]} last={count === length}/>
  32. })
  33. }
  34. </MapWrapper>
  35. )
  36. })
  37.  
  38. const Node = ({k, val, last=false}) => {
  39. let nodes = [];
  40. if (typeof k != 'undefined') {
  41. nodes.push(<NodeKey key={0} k={k}/>);
  42. }
  43. if (val instanceof Array) {
  44. nodes.push(<Bracket b={'['}/>);
  45. nodes.push(<NodeList key={1} list={val}/>);
  46. nodes.push(<Bracket b={']'}/>);
  47. } else if(typeof val === 'object') {
  48. nodes.push(<Bracket b={'{'}/>);
  49. nodes.push(<NodeMap key={1} map={val}/>);
  50. nodes.push(<Bracket b={'}'}/>);
  51. } else {
  52. nodes.push(<span key={1}>{typeof val === 'string' ? '"' + val + '"': val}</span>);
  53. }
  54. return <div>{nodes}{!last&&','}</div>;
  55. }
  56.  
  57.  
  58. const JsonViewer = ({json}) => {
  59. return <section className={'json-viewer'}><Node val={json} last={true}/></section>;
  60. }
  61.  
  62. ```
  63.  
  64. app.scss
  65. ```scss
  66. html {
  67. font-size: 62.5%;
  68. }
  69.  
  70. body {
  71. margin: 0;
  72. padding: 0;
  73. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
  74. "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
  75. sans-serif;
  76. -webkit-font-smoothing: antialiased;
  77. -moz-osx-font-smoothing: grayscale;
  78. }
  79.  
  80. .json-viewer {
  81. font-family: Menlo;
  82. background-color:#6d5826;
  83. color: #fff;
  84. text-align: left;
  85. font-size: 1.2rem;
  86. line-height: 1.5;
  87.  
  88. .bracket {
  89. color: #dec674;
  90. }
  91.  
  92. label {
  93. font-weight: bold;
  94. }
  95.  
  96. section {
  97. padding-left: 2em;
  98. }
  99. }
  100. ```
Add Comment
Please, Sign In to add comment