Skip to content

Commit 5bba75e

Browse files
authored
Add viewOnly prop (#173)
1 parent 37e8011 commit 5bba75e

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ The only *required* property is `data` (although you will need to provide a `set
148148
| `restrictAdd` | `boolean\|FilterFunction` | `false` | As with `restrictEdit` but for adding new properties |
149149
| `restrictTypeSelection` | `boolean\|DataType[]\|TypeFilterFunction` | `false` | For restricting the data types the user can select. Can be a list of data types (e.g. `[ 'string', 'number', 'boolean', 'array', 'object', 'null' ]`), a boolean. or a function — see [TypeFilterFunction](#typefilterfunction) |
150150
| `restrictDrag` | `boolean\|FilterFunction` | `true` | Set to `false` to enable drag and drop functionality — see [Drag-n-drop](#drag-n-drop) |
151+
| `viewOnly` | `boolean` | | A shorthand if you just want the component to be a viewer, with no editing. Overrides any values of the above edit restrictions. |
151152

152153
### Look and Feel / UI
153154

@@ -329,7 +330,7 @@ For restricting data types, the (Type) filter function is slightly more sophisti
329330
- `"object"`
330331
- `"array"`
331332

332-
There is no specific restriction function for editing object key names, but they must return `true` for *both* `restrictEdit` and `restrictDelete` (and `restrictAdd` for collections), since changing a key name is equivalent to deleting a property and adding a new one.
333+
There is no specific restriction function for editing object key names, but they must return `false` for *both* `restrictEdit` and `restrictDelete` (and `restrictAdd` for collections), since changing a key name is equivalent to deleting a property and adding a new one.
333334

334335
You can also set a dynamic default value by passing a filter function to the `defaultValue` prop -- the input is the same as the above, but also takes the new `key` value as its second parameter, so the new value can depend on the new key added.
335336

demo/src/App.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ function App() {
382382
}
383383
: false
384384
}
385+
// viewOnly
385386
restrictEdit={restrictEdit}
386387
// restrictEdit={(nodeData) => !(typeof nodeData.value === 'string')}
387388
restrictDelete={restrictDelete}

src/JsonEditor.tsx

+22-5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const Editor: React.FC<JsonEditorProps> = ({
5151
restrictAdd = false,
5252
restrictTypeSelection = false,
5353
restrictDrag = true,
54+
viewOnly,
5455
searchFilter: searchFilterInput,
5556
searchText,
5657
searchDebounceTime = 350,
@@ -249,10 +250,22 @@ const Editor: React.FC<JsonEditorProps> = ({
249250
})
250251
}
251252

252-
const restrictEditFilter = useMemo(() => getFilterFunction(restrictEdit), [restrictEdit])
253-
const restrictDeleteFilter = useMemo(() => getFilterFunction(restrictDelete), [restrictDelete])
254-
const restrictAddFilter = useMemo(() => getFilterFunction(restrictAdd), [restrictAdd])
255-
const restrictDragFilter = useMemo(() => getFilterFunction(restrictDrag), [restrictDrag])
253+
const restrictEditFilter = useMemo(
254+
() => getFilterFunction(restrictEdit, viewOnly),
255+
[restrictEdit, viewOnly]
256+
)
257+
const restrictDeleteFilter = useMemo(
258+
() => getFilterFunction(restrictDelete, viewOnly),
259+
[restrictDelete, viewOnly]
260+
)
261+
const restrictAddFilter = useMemo(
262+
() => getFilterFunction(restrictAdd, viewOnly),
263+
[restrictAdd, viewOnly]
264+
)
265+
const restrictDragFilter = useMemo(
266+
() => getFilterFunction(restrictDrag, viewOnly),
267+
[restrictDrag, viewOnly]
268+
)
256269
const searchFilter = useMemo(() => getSearchFilter(searchFilterInput), [searchFilterInput])
257270

258271
const fullKeyboardControls = useMemo(
@@ -426,7 +439,11 @@ const updateDataObject = (
426439
}
427440
}
428441

429-
const getFilterFunction = (propValue: boolean | number | FilterFunction): FilterFunction => {
442+
const getFilterFunction = (
443+
propValue: boolean | number | FilterFunction,
444+
viewOnly?: boolean
445+
): FilterFunction => {
446+
if (viewOnly) return () => true
430447
if (typeof propValue === 'boolean') return () => propValue
431448
if (typeof propValue === 'number') return ({ level }) => level >= propValue
432449
return propValue

src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface JsonEditorProps {
2828
restrictAdd?: boolean | FilterFunction
2929
restrictTypeSelection?: boolean | DataType[] | TypeFilterFunction
3030
restrictDrag?: boolean | FilterFunction
31+
viewOnly?: boolean
3132
searchText?: string
3233
searchFilter?: 'key' | 'value' | 'all' | SearchFilterFunction
3334
searchDebounceTime?: number

0 commit comments

Comments
 (0)