Skip to content

Commit 93de14c

Browse files
committed
Add errorMessageTimeout prop #121
1 parent 75567c4 commit 93de14c

File tree

4 files changed

+7
-4
lines changed

4 files changed

+7
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ The only *required* value is `data` (although you will need to provide a `setDat
147147
| `customButtons` | `CustomButtonDefinition[]` | `[]` | You can add your own buttons to the Edit Buttons panel if you'd like to be able to perform a custom operation on the data. See [Custom Buttons](#custom-buttons) |
148148
| `jsonParse` | `(input: string) => JsonData` | `JSON.parse` | When editing a block of JSON directly, you may wish to allow some "looser" input -- e.g. 'single quotes', trailing commas, or unquoted field names. In this case, you can provide a third-party JSON parsing method. I recommend [JSON5](https://json5.org/), which is what is used in the [Demo](https://carlosnz.github.io/json-edit-react/) |
149149
| `jsonStringify` | `(data: JsonData) => string` | `(data) => JSON.stringify(data, null, 2)` | Similarly, you can override the default presentation of the JSON string when starting editing JSON. You can supply different formatting parameters to the native `JSON.stringify()`, or provide a third-party option, like the aforementioned JSON5. |
150+
| `errorMessageTimeout` | `number` | `2500` | Time (in milliseconds) to display the error message in the UI. | |
150151

151152
## Managing state
152153

src/JsonEditor.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const Editor: React.FC<JsonEditorProps> = ({
6363
customButtons = [],
6464
jsonParse = JSON.parse,
6565
jsonStringify = (data: JsonData) => JSON.stringify(data, null, 2),
66+
errorMessageTimeout = 2500,
6667
}) => {
6768
const { getStyles } = useTheme()
6869
const collapseFilter = useCallback(getFilterFunction(collapse), [collapse])
@@ -281,6 +282,7 @@ const Editor: React.FC<JsonEditorProps> = ({
281282
parentData: null,
282283
jsonParse,
283284
jsonStringify,
285+
errorMessageTimeout,
284286
}
285287

286288
const mainContainerStyles = { ...getStyles('container', nodeData), minWidth, maxWidth }

src/hooks/useCommon.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
type ValueData,
1313
type ValueNodeProps,
1414
type JsonData,
15-
ERROR_DISPLAY_TIME,
1615
} from '../types'
1716
import { toPathString } from '../helpers'
1817

@@ -34,6 +33,7 @@ export const useCommon = ({ props, collapsed }: CommonProps) => {
3433
restrictAddFilter,
3534
restrictDragFilter,
3635
translate,
36+
errorMessageTimeout,
3737
} = props
3838
const { currentlyEditingElement, setCurrentlyEditingElement } = useTreeState()
3939
const [error, setError] = useState<string | null>(null)
@@ -54,7 +54,7 @@ export const useCommon = ({ props, collapsed }: CommonProps) => {
5454
const showError = (errorString: ErrorString) => {
5555
if (showErrorMessages) {
5656
setError(errorString)
57-
setTimeout(() => setError(null), ERROR_DISPLAY_TIME)
57+
setTimeout(() => setError(null), errorMessageTimeout)
5858
}
5959
console.warn('Error', errorString)
6060
}

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { type LocalisedStrings, type TranslateFunction } from './localisation'
22

3-
export const ERROR_DISPLAY_TIME = 2500 // ms
4-
53
export type JsonData = CollectionData | ValueData
64

75
export interface JsonEditorProps {
@@ -46,6 +44,7 @@ export interface JsonEditorProps {
4644
customButtons?: CustomButtonDefinition[]
4745
jsonParse?: (input: string) => JsonData
4846
jsonStringify?: (input: JsonData) => string
47+
errorMessageTimeout?: number // ms
4948
}
5049

5150
const ValueDataTypes = ['string', 'number', 'boolean', 'null'] as const
@@ -189,6 +188,7 @@ interface BaseNodeProps {
189188
translate: TranslateFunction
190189
customNodeDefinitions: CustomNodeDefinition[]
191190
customButtons: CustomButtonDefinition[]
191+
errorMessageTimeout: number
192192
}
193193

194194
export interface CollectionNodeProps extends BaseNodeProps {

0 commit comments

Comments
 (0)