jsonViewer.stories.jsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import React, { useState, useEffect, useRef } from 'react';
  2. import JsonViewer from '../index';
  3. import Popover from '../../popover';
  4. import Image from '../../image';
  5. import Modal from '../../modal';
  6. export default {
  7. title: 'JsonViewer',
  8. };
  9. import Rating from '../../rating';
  10. import Button from '../../button';
  11. import Tag from '../../tag';
  12. const baseStr = `{
  13. "min_position": 1,
  14. "has_more_items": true,
  15. "items_html": "Bike",
  16. "new_latent_count": 0,
  17. "data": {
  18. "length": 22,
  19. "text": "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
  20. },
  21. "numericalArray": [23, 29, 28, 26, 23],
  22. "StringArray": ["Oxygen", "Oxygen", "Oxygen", "Carbon"],
  23. "multipleTypesArray": 3,
  24. "objArray": [
  25. {},
  26. {
  27. "class": "upper",
  28. "name": "Mark",
  29. "age": 7
  30. },
  31. {
  32. "class": "upper",
  33. "name": "Tom",
  34. "age": 1
  35. },
  36. {
  37. "class": "lower",
  38. "name": "Jerry",
  39. "age": 5
  40. },
  41. {
  42. "class": "lower",
  43. "name": "Alice",
  44. "age": 3
  45. }
  46. ]
  47. }`;
  48. const customStr = `{
  49. "url": "https://semi.design/zh-CN/plus/jsonviewer",
  50. "name": "Semi Design",
  51. "boolean": false,
  52. "dialog": "showDialog",
  53. "image": "https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/abstract.jpg",
  54. "number": 100,
  55. "array": [
  56. "https://semi.design/zh-CN/plus/jsonviewer",
  57. "https://semi.design/zh-CN/plus/jsonviewer",
  58. "https://semi.design/zh-CN/plus/jsonviewer"
  59. ],
  60. "objArray": [
  61. {
  62. "name": "Semi Design1",
  63. "age": 50
  64. },
  65. {
  66. "name": "Semi Design2",
  67. "age": 100
  68. },
  69. {
  70. "name": "Semi Design3",
  71. "age": 150
  72. }
  73. ]
  74. }`;
  75. export const DefaultJsonViewer = () => {
  76. const onChangeHandler = value => {
  77. console.log(value, 'value');
  78. };
  79. const [autoWrap, setAutoWrap] = useState(true);
  80. const [lineHeight, setLineHeight] = useState(20);
  81. const jsonviewerRef = useRef(null);
  82. return (
  83. <>
  84. <JsonViewer
  85. value={baseStr}
  86. width={700}
  87. height={400}
  88. options={{ lineHeight: lineHeight, autoWrap: autoWrap, formatOptions: { tabSize: 4 } }}
  89. onChange={onChangeHandler}
  90. ref={jsonviewerRef}
  91. />
  92. </>
  93. );
  94. };
  95. export const CustomRender = () => {
  96. const customRender = [
  97. {
  98. match: (val, pathChain) => {
  99. if(pathChain !== 'root.url') {
  100. return false;
  101. }
  102. return typeof val === 'string' && val.startsWith('http');
  103. },
  104. render: (val) => {
  105. return <Popover showArrow content={'我是用户自定义的渲染'} trigger='hover'><span href={val.replace(/^"|"$/g, '')} target='_blank'>{val}</span></Popover>;
  106. },
  107. },
  108. {
  109. match: (val, pathChain) => pathChain === 'root.image' && typeof val === 'string' && val.startsWith('http'),
  110. render: (val) => {
  111. return <Popover showArrow content={<Image width={100} height={100} src={val.replace(/^"|"$/g, '')} />} trigger='hover'><span>{val}</span></Popover>;
  112. }
  113. },
  114. {
  115. match: 'Semi Design1',
  116. render: (val) => {
  117. return <Tag size='small' shape='circle'>{val}</Tag>
  118. }
  119. },
  120. {
  121. match: 'false',
  122. render: (val) => {
  123. return <Rating defaultValue={3} size={10} disabled/>
  124. }
  125. },
  126. {
  127. match: new RegExp('^\\d+$'),
  128. render: (val) => {
  129. return <span style={{color:'black',backgroundColor:'transparent',border:'1px solid #031126',borderRadius:'4px',padding:'2px 4px'}}>{val}</span>
  130. }
  131. },
  132. {
  133. match: 'showDialog',
  134. render: (val) => {
  135. return <Button onClick={showDialog} type='danger' style={{height:'18px',lineHeight:'18px'}}>{val}</Button>
  136. }
  137. }
  138. ];
  139. const [visible, setVisible] = useState(false);
  140. const showDialog = () => {
  141. setVisible(true);
  142. };
  143. const handleOk = () => {
  144. setVisible(false);
  145. console.log('Ok button clicked');
  146. };
  147. const handleCancel = () => {
  148. setVisible(false);
  149. console.log('Cancel button clicked');
  150. };
  151. const handleAfterClose = () => {
  152. console.log('After Close callback executed');
  153. };
  154. const [autoWrap, setAutoWrap] = useState(true);
  155. const [lineHeight, setLineHeight] = useState(20);
  156. const jsonviewerRef = useRef(null);
  157. return (
  158. <>
  159. <JsonViewer
  160. value={customStr}
  161. width={700}
  162. height={400}
  163. options={{ lineHeight: lineHeight, autoWrap: autoWrap,readOnly:true, customRenderRule: customRender, formatOptions: { tabSize: 4 } }}
  164. ref={jsonviewerRef}
  165. />
  166. <Modal
  167. title="基本对话框"
  168. visible={visible}
  169. onOk={handleOk}
  170. afterClose={handleAfterClose} //>=1.16.0
  171. onCancel={handleCancel}
  172. closeOnEsc={true}
  173. >
  174. This is the content of a basic modal.
  175. <br />
  176. More content...
  177. </Modal>
  178. </>
  179. );
  180. };