jsonViewer.stories.tsx 5.5 KB

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