Skip to content

Commit

Permalink
ui: fix import from list in select fields
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Garcia Garcia <[email protected]>
  • Loading branch information
miguelgrc authored and pamfilos committed Oct 11, 2023
1 parent 017aa4f commit cd5515b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 32 deletions.
4 changes: 2 additions & 2 deletions cap/modules/fixtures/schemas/cms.json
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@
"ana_notes": {
"items": {
"pattern": "^AN-[0-9]{4}/[0-9]{3}$",
"placeholder": "e.g. AN-2010-107",
"placeholder": "e.g. AN-2010/107",
"type": "string"
},
"type": "array",
Expand Down Expand Up @@ -1160,7 +1160,7 @@
"ui:array": "StringArrayField",
"items": {
"ui:options": {
"mask":"\\AN-0000-000"
"mask":"\\AN-0000/000"
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions ui/cap-react/src/antd/admin/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ export const isItTheArrayField = (schema, uiSchema) => {
schema.type === "array" && !uiSchema["ui:field"] && !uiSchema["ui:widget"]
);
};

export const timer = ms => new Promise(res => setTimeout(res, ms));
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect, useState } from "react";
import PropTypes from "prop-types";
import { Checkbox, Input, List, Modal, Tabs, Typography } from "antd";
import axios from "../../../../axios";
import { timer } from "../../../admin/utils";

const ImportListModal = ({
open,
Expand All @@ -23,7 +24,6 @@ const ImportListModal = ({
const [fetchedResults, setFetchedResults] = useState(null);
const [currentIndex, setCurrentIndex] = useState(null);
const [data, setData] = useState(null);
// const [error, setError] = useState(null);

useEffect(() => {
if (currentIndex) {
Expand Down Expand Up @@ -60,10 +60,33 @@ const ImportListModal = ({
setFetchedResults(data);
updateAll(data, true);
} catch (err) {
// setError(error);
//
}
};

const populateItems = async (values, type) => {
for (let [index, value] of values.entries()) {
let _index = formData.length + index;
if (type == "object" && to) {
value = { [to]: value };
}
setCurrentIndex({
index: _index,
value,
});
await timer(1);
}
};

const addItem = async (values, type) => {
// eslint-disable-next-line no-unused-vars
for (const _ of values) {
onAddClick();
await timer(1);
}
populateItems(values, type);
};

const _batchImport = () => {
let values = [];
if (!data) return;
Expand All @@ -82,28 +105,9 @@ const ImportListModal = ({
let { items: { type } = {} } = schema;

if (Array.isArray(values)) {
setTimeout(() => {
values.map(() => {
onAddClick();
});
}, 1);
addItem(values, type);
}

setTimeout(
() =>
values.map((value, index) => {
let _index = formData.length + index;
if (type == "object" && to) {
value = { [to]: value };
}
setCurrentIndex({
index: _index,
value,
});
}),
1
);

onCancel();
};
return (
Expand Down Expand Up @@ -136,7 +140,7 @@ const ImportListModal = ({
<>
<Input.Search
enterButton="Fetch"
placeHolder="Insert your pattern e.x /dataset/*"
placeholder="Insert your pattern e.x /dataset/*"
onChange={e => {
e.target.value == "" &&
fetchedResults &&
Expand Down Expand Up @@ -182,8 +186,8 @@ const ImportListModal = ({
children: (
<>
<Typography.Title level={5}>
{description} || Paste your list here. Insert one item per
line:
{description ||
"Paste your list here. Insert one item per line:"}
</Typography.Title>
<Input.TextArea
rows={15}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ const LayerArrayFieldTemplate = ({ items = [] }) => {
actions={getActionsButtons(item)}
style={{
border: "1px solid #f0f0f0",
padding: "10px",
padding: "0 10px",
marginBottom: "5px",
backgroundColor: "white",
}}
>
<List.Item.Meta
title={
<Typography.Text ellipsis={{ rows: 1 }}>
<Typography.Text ellipsis>
{stringifyItem(
item?.children?.props?.uiSchema?.["ui:options"] ?? null,
item.children.props.formData
Expand All @@ -147,6 +147,7 @@ const LayerArrayFieldTemplate = ({ items = [] }) => {
children: item.children,
});
}}
style={{ padding: "10px 0" }}
/>
</List.Item>
</ErrorFieldIndicator>
Expand Down
4 changes: 1 addition & 3 deletions ui/cap-react/src/antd/forms/widgets/SelectWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ const SelectWidget = ({

const getPopupContainer = node => node.parentNode;

console.log(value, enumOptions, multiple);

const selectedIndexes = enumOptionsIndexForValue(
value,
enumOptions,
Expand Down Expand Up @@ -126,7 +124,7 @@ const SelectWidget = ({
onSearch={suggestions && debounce(handleSearch, 500)}
showSearch={suggestions}
placeholder={placeholder}
value={selectedIndexes}
value={suggestions ? value : selectedIndexes}
{...extraProps}
filterOption={filterOption}
aria-describedby={ariaDescribedByIds(id)}
Expand Down

0 comments on commit cd5515b

Please sign in to comment.