Skip to content

Commit 23fcd56

Browse files
committed
feat: add editorRef
1 parent 4f3c93c commit 23fcd56

File tree

6 files changed

+70
-27
lines changed

6 files changed

+70
-27
lines changed

README.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,22 @@ npm i react-json-editor-ui -S
2020
| onChange | Callback the data | true | null |
2121
| optionsMap | When a match for auto-complete on the input value | false | null |
2222

23+
### Ref Methods
24+
25+
| method | description | params |
26+
| ---------- | ------------------------------------------------- | --------------------------- |
27+
| updateData | Update the editor data programmatically | data: Record<string, any> |
28+
2329
### Example:
2430

2531
```jsx
2632
import * as React from 'react'
2733
import * as ReactDOM from 'react-dom'
28-
import JsonEditor from 'react-json-editor-ui'
34+
import JsonEditor, { JsonEditorRef } from 'react-json-editor-ui'
2935
import 'react-json-editor-ui/dist/react-json-editor-ui.cjs.development.css'
3036

3137
const App = () => {
38+
const editorRef = React.useRef<JsonEditorRef>(null)
3239
const [editObject, setEditObject] = React.useState<any>({
3340
name: 'may',
3441
age: null,
@@ -45,9 +52,21 @@ const App = () => {
4552
description: 'another',
4653
},
4754
})
55+
56+
// Example of updating data programmatically using ref
57+
const updateEditorData = () => {
58+
if (editorRef.current) {
59+
editorRef.current.updateData({
60+
name: 'updated name',
61+
age: 25,
62+
// ... other properties
63+
})
64+
}
65+
}
4866

4967
return (
5068
<JsonEditor
69+
ref={editorRef}
5170
data={editObject}
5271
onChange={data => {
5372
setEditObject(data)

example/index.tsx

+19-16
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,28 @@ import * as React from 'react'
55
import * as ReactDOM from 'react-dom'
66
import ReactJsonSyntaxHighlighter from 'react-json-syntax-highlighter'
77

8-
import JsonEditor from '../'
8+
import JsonEditor, { JsonEditorRef } from '../'
99

1010
const App = () => {
11+
const editorRef = React.useRef<JsonEditorRef>(null)
12+
// editorRef.current.updateData(newData)
1113
const [editObject, setEditObject] = React.useState<any>({
12-
name: 'may',
13-
age: null,
14-
address: [
15-
'Panyu Shiqiao on Canton',
16-
'Tianhe',
17-
{
18-
city: 'forida meta 11',
19-
},
20-
],
21-
others: {
22-
id: 1246,
23-
joinTime: '2017-08-20. 10:20',
24-
description: 'another',
25-
},
14+
name: 'may',
15+
age: null,
16+
address: [
17+
'Panyu Shiqiao on Canton',
18+
'Tianhe',
19+
{
20+
city: 'forida meta 11',
21+
},
22+
],
23+
others: {
24+
id: 1246,
25+
joinTime: '2017-08-20. 10:20',
26+
description: 'another',
27+
},
2628
})
27-
29+
2830
return (
2931
<div>
3032
<h1 style={{ textAlign: 'center', padding: '50px 0' }}>
@@ -42,6 +44,7 @@ const App = () => {
4244
}}
4345
>
4446
<JsonEditor
47+
ref={editorRef}
4548
data={editObject}
4649
onChange={data => {
4750
setEditObject(data)

src/components/AddItem.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ const AddItem = (props: {
8383
<InputNumber
8484
size="small"
8585
style={{ width: '100px' }}
86-
onBlur={event => changeInputValue(uniqueKey, +event.target.value)}
86+
onChange={value => {
87+
changeInputValue(uniqueKey, +value)
88+
}}
8789
/>
8890
)
8991
case DataType.BOOLEAN:

src/components/JsonView.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ function JsonView(props: JsonViewProps) {
141141
style={{ width: '100px' }}
142142
placeholder={fieldValue}
143143
value={fieldValue}
144-
onBlur={event => {
145-
onChangeValue(+event.target.value, fieldKey, sourceData)
144+
onChange={value => {
145+
onChangeValue(+value, fieldKey, sourceData)
146146
}}
147147
/>
148148
)

src/index.tsx

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import './styles/index.less'
22
import './ui/styles.css'
3-
import React, { useEffect, useState } from 'react'
3+
import React, { useEffect, useState, forwardRef, useImperativeHandle } from 'react'
44
import JsonView from './components/JsonView'
55

66
export type JsonEditorProps = {
@@ -16,12 +16,23 @@ export type JsonEditorProps = {
1616
onChange: (data: any) => void
1717
}
1818

19-
function JsonEditor(props: JsonEditorProps) {
19+
export type JsonEditorRef = {
20+
updateData: (data: Record<string, any>) => void
21+
}
22+
23+
const JsonEditor = forwardRef<JsonEditorRef, JsonEditorProps>((props, ref) => {
2024
const [editObject, setEditObject] = useState<any>(JSON.parse(JSON.stringify(props.data)))
25+
2126
useEffect(() => {
22-
props.onChange(editObject)
27+
props.onChange(editObject)
2328
}, [editObject])
2429

30+
useImperativeHandle(ref, () => ({
31+
updateData: (data: Record<string, any>) => {
32+
setEditObject(JSON.parse(JSON.stringify(data)))
33+
}
34+
}))
35+
2536
return (
2637
<div className="jsonEditorContainer" style={{ width: props.width ?? 500 }}>
2738
<JsonView
@@ -33,6 +44,6 @@ function JsonEditor(props: JsonEditorProps) {
3344
/>
3445
</div>
3546
)
36-
}
47+
})
3748

3849
export default JsonEditor

src/ui/index.tsx

+11-3
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,34 @@ export const InputNumber = ({
3737
style = {},
3838
placeholder,
3939
value,
40-
onBlur,
40+
onChange,
4141
...props
4242
}: {
4343
size?: 'small' | 'default' | 'large'
4444
style?: React.CSSProperties
4545
placeholder?: any
4646
value?: number
47-
onBlur?: (event: React.FocusEvent<HTMLInputElement>) => void
47+
onChange?: (value: number) => void
4848
[key: string]: any
4949
}) => {
5050
const sizeClass = size === 'small' ? 'ui-input-sm' : size === 'large' ? 'ui-input-lg' : 'ui-input-default'
5151

52+
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
53+
if (onChange) {
54+
// 将输入值转换为数字类型
55+
const numValue = e.target.value === '' ? undefined : Number(e.target.value);
56+
onChange(numValue as number);
57+
}
58+
};
59+
5260
return (
5361
<input
5462
type="number"
5563
className={`ui-input ui-input-number ${sizeClass}`}
5664
style={style}
5765
placeholder={String(placeholder)}
5866
value={value}
59-
onBlur={onBlur}
67+
onChange={handleChange}
6068
{...props}
6169
/>
6270
)

0 commit comments

Comments
 (0)