diff --git a/ui/cap-react/src/antd/App/App.js b/ui/cap-react/src/antd/App/App.js index 4fad97f13..6d3182729 100644 --- a/ui/cap-react/src/antd/App/App.js +++ b/ui/cap-react/src/antd/App/App.js @@ -28,6 +28,7 @@ import { MosesContext } from "cap-moses"; import { PRIMARY_COLOR } from "../utils/theme"; import { customFieldTypes, customFields } from "../forms/mosesConfig"; import { isEmpty } from "lodash-es"; +import { transformSchema } from "../partials/Utils/schema"; const AdminPage = lazy(() => import("../admin")); @@ -94,6 +95,7 @@ const App = ({ }} customFieldTypes={customFieldTypes} customFields={customFields} + transformSchema={transformSchema} > { + const { readonlyAsDisabled = true } = formContext; + + const { enumOptions, enumDisabled, suggestions, params, emptyValue } = + options; + + const [loading, setLoading] = useState(false); + const [data, setData] = useState([]); + + const handleChange = nextValue => { + onChange( + enumOptionsValueForIndex(nextValue, enumOptions || data, emptyValue) + ); + }; + + const handleBlur = () => + onBlur(id, enumOptionsValueForIndex(value, enumOptions, emptyValue)); + + const handleFocus = () => + onFocus(id, enumOptionsValueForIndex(value, enumOptions, emptyValue)); + + const handleSearch = newValue => { + updateSearch(newValue, setData); + }; + + const filterOption = (input, option) => { + if (option && isString(option.label)) { + // labels are strings in this context + return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0; + } + return false; + }; + + const getPopupContainer = node => node.parentNode; + + const selectedIndexes = enumOptionsIndexForValue( + value, + enumOptions, + multiple + ); + + const _replace_hash_with_current_indexes = path => { + let indexes = id.split("_").filter(item => !isNaN(item)), + index_cnt = 0; + + return path.map(item => { + item = item === "#" ? indexes[index_cnt] : item; + if (!isNaN(item)) ++index_cnt; + return item; + }); + }; + + const updateSearch = (value, callback) => { + let data = fromJS(formData); + if (params) { + Object.entries(params).map(param => { + const path = _replace_hash_with_current_indexes(param[1]); + suggestions.replace( + `${param[0]}=`, + `${param[0]}=${data.getIn(path, "") || ""}` + ); + }); + } + setLoading(true); + axios + .get(`${suggestions}${value}`) + .then(({ data }) => { + callback(data.map(value => ({ value, label: value }))); + setLoading(false); + }) + .catch(() => { + callback([]); + setLoading(false); + }); + }; + + let valuesToRender = suggestions ? data : enumOptions; + + // Antd's typescript definitions do not contain the following props that are actually necessary and, if provided, + // they are used, so hacking them in via by spreading `extraProps` on the component to avoid typescript errors + const extraProps = { + name: id, + }; + return ( +