Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: fix validation for multiselect fields and import from list #2848

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 6 additions & 0 deletions ui/cap-react/src/antd/admin/components/PropKeyEditorForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const PropertyKeyEditorForm = ({
}) => {
let type;

const cleanupSelect = () =>
schema.type === "array" ? delete formData.enum : delete formData.items;

// in case we can not define the type of the element from the uiSchema,
// extract the type from the schema
if (
Expand All @@ -23,6 +26,9 @@ const PropertyKeyEditorForm = ({
} else {
if (uiSchema["ui:widget"]) {
type = uiSchema["ui:widget"];
if (uiSchema["ui:widget"] === "select") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a chance we don't have a ui:widget specified, but RJSF still will render the select widget
e.g.

    "multipleChoicesList": {
      "type": "array",
      "title": "A multiple choices list",
      "items": {
        "type": "string",
        "enum": [
          "foo",
          "bar",
          "fuzz",
          "qux"
        ]
      },
      "uniqueItems": true
    },

do we need to handle better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We won't have those cases from the builder, only maybe for imported or manually created schemas, but I would say we should have those schemas follow the "form builder syntax" instead of implementing more custom checks that can make the code more confusing. What do you think?

cleanupSelect();
}
}
if (uiSchema["ui:field"]) {
type = uiSchema["ui:field"];
Expand Down
113 changes: 61 additions & 52 deletions ui/cap-react/src/antd/admin/utils/fieldTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,65 +643,79 @@ const simple = {
readOnly: extra.optionsSchema.readOnly,
isRequired: extra.optionsSchema.isRequired,
},
dependencies: {
type: {
oneOf: [
{
properties: {
type: {
enum: ["string"],
},
enum: {
title: "Define your options",
type: "array",
description: "The options for the widget",
items: {
title: "Option",
type: "string",
},
},
allOf: [
{
if: {
properties: {
type: {
const: "string",
},
},
{
properties: {
type: {
enum: ["number"],
},
enum: {
title: "Define your options",
type: "array",
description: "The options for the widget",
items: {
title: "Option",
type: "number",
},
},
then: {
properties: {
enum: {
title: "Define your options",
type: "array",
description: "The options for the widget",
items: {
title: "Option",
type: "string",
},
},
},
{
properties: {
type: {
enum: ["array"],
},
},
},
{
if: {
properties: {
type: {
const: "number",
},
},
},
then: {
properties: {
enum: {
title: "Define your options",
type: "array",
description: "The options for the widget",
items: {
title: "Define your options",
type: "object",
properties: {
enum: {
title: "Options List",
type: "array",
items: {
title: "Option",
type: "string",
},
title: "Option",
type: "number",
},
},
},
},
},
{
if: {
properties: {
type: {
const: "array",
},
},
},
then: {
properties: {
items: {
title: "Define your options",
type: "object",
properties: {
enum: {
title: "Options List",
type: "array",
items: {
title: "Option",
type: "string",
},
},
},
},
},
],
},
},
},
],
},
optionsSchemaUiSchema: {
readOnly: extra.optionsSchemaUiSchema.readOnly,
Expand All @@ -715,13 +729,8 @@ const simple = {
},
default: {
schema: {
enum: ["Option A", "Option B", "Option C"],
type: "string",
uniqueItems: true,
items: {
type: "string",
enum: ["Option A", "Option B", "Option C", "Option D"],
},
},
uiSchema: {
"ui:widget": "select",
Expand Down
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 "../../../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>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove {{ rows: 1 }}? seems not to work for long text

Copy link
Contributor Author

@miguelgrc miguelgrc Sep 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried and it seems to work the same, did you notice any issue? For some reason I was getting a warning saying that rows is not a valid property for ellipsis (even though it should be...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why but they're excluding rows and expandable from the ellipsis props in antd, and in the documentation they appear as valid props (https://github.com/ant-design/ant-design/blob/master/components/typography/Text.tsx#L34)

{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
2 changes: 2 additions & 0 deletions ui/cap-react/src/antd/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ export const permissionsPerUser = permissions => {
emailsArray,
};
};

export const timer = ms => new Promise(res => setTimeout(res, ms));