-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Supports language selection Supports jsonschema validation It's been reused for the itemsDisplayTitle in array fields
- Loading branch information
Showing
13 changed files
with
578 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import CodeEditor from "../../utils/CodeEditor"; | ||
import axios from "axios"; | ||
import { useEffect, useState } from "react"; | ||
import { Typography } from "antd"; | ||
import { debounce } from "lodash-es"; | ||
import { URL_REGEX } from "../../utils"; | ||
|
||
const CodeEditorField = ({ value, onChange, schema, uiSchema, readonly }) => { | ||
const { validateWith, validateWithUrl, validateWithJson } = schema; | ||
const uiOptions = uiSchema["ui:options"]; | ||
const { | ||
language = uiOptions.codeEditor?.language, | ||
height = uiOptions.codeEditor?.height, | ||
codeEditor, | ||
} = uiOptions || {}; | ||
|
||
const [error, setError] = useState(); | ||
|
||
const [validationSchema, setValidationSchema] = useState(); | ||
|
||
const handleUrlError = () => { | ||
setValidationSchema(); | ||
setError("Error querying validation schema URL"); | ||
}; | ||
|
||
useEffect(() => { | ||
setError(); | ||
if (validateWith === "url" && validateWithUrl) { | ||
const fetchSchema = debounce(() => { | ||
axios | ||
.get(validateWithUrl) | ||
.then((response) => { | ||
setValidationSchema(response.data); | ||
}) | ||
.catch(() => { | ||
handleUrlError(); | ||
}); | ||
}, 2000); | ||
if (new RegExp(URL_REGEX).test(validateWithUrl)) { | ||
fetchSchema(); | ||
} else { | ||
handleUrlError(); | ||
} | ||
return () => { | ||
// Cleaning up the debounce when validateWithUrl changes | ||
fetchSchema.cancel(); | ||
}; | ||
} else if (validateWith === "json" && validateWithJson) { | ||
const parsed = (() => { | ||
try { | ||
return JSON.parse(validateWithJson); | ||
} catch { | ||
setError("Error parsing validation JSON"); | ||
return; | ||
} | ||
}).call(); | ||
setValidationSchema(parsed); | ||
} else { | ||
setValidationSchema(); | ||
} | ||
}, [validateWith, validateWithUrl, validateWithJson]); | ||
|
||
return ( | ||
<> | ||
<CodeEditor | ||
// Key needed to refresh the component on settings change due to the custom logic in CodeViewer | ||
key={`${language}${readonly}${validationSchema}`} | ||
isReadOnly={readonly} | ||
height={height} | ||
value={value} | ||
handleEdit={(v) => onChange(v)} | ||
reset | ||
lang={!validationSchema && language} | ||
validationSchema={!readonly && validationSchema} | ||
{...codeEditor} | ||
/> | ||
<Typography.Text type="danger">{error}</Typography.Text> | ||
</> | ||
); | ||
}; | ||
|
||
export default CodeEditorField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import CodeEditorField from "./CodeEditorField"; | ||
import IdFetcher from "./IdFetcher"; | ||
import TagsField from "./TagsField"; | ||
|
||
const fields = { | ||
tags: TagsField, | ||
idFetcher: IdFetcher, | ||
codeEditor: CodeEditorField, | ||
}; | ||
|
||
export default fields; |
Oops, something went wrong.