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

fix: fix input annotation issue when creating a project #4358

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 20 additions & 17 deletions packages/shared/src/components/Inputs/PropertiesInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { has, isEmpty } from 'lodash';
import React, { ReactNode, useEffect, useMemo, useState } from 'react';
import PropertyItem, { Props as ItemProps } from './item';
import { AddButton, Wrapper } from './styles';
import { nanoid } from 'nanoid';

interface Props {
name?: string;
Expand All @@ -20,6 +21,7 @@ interface Props {
}

interface ValueType {
id: string;
key: string;
value?: any;
}
Expand Down Expand Up @@ -48,37 +50,38 @@ function PropertiesInput({
const newHiddenValues: ValueType[] = [];
const newReadOnlyValues: ValueType[] = [];
const newArrayValues: ValueType[] = [];
let empty = false;

Object.keys(selfValue).forEach(key => {
const entry = { key, value: selfValue[key], id: nanoid() };
if (hiddenKeys.some(hiddenKey => new RegExp(hiddenKey).test(key))) {
newHiddenValues.push({
key,
value: selfValue[key],
});
newHiddenValues.push(entry);
} else if (readOnlyKeys.some(readOnlyKey => new RegExp(readOnlyKey).test(key))) {
newReadOnlyValues.push({
key,
value: selfValue[key],
});
newReadOnlyValues.push(entry);
} else {
newArrayValues.push({
key,
value: selfValue[key],
});
newArrayValues.push(entry);
}
});

if (isEmpty(newArrayValues) && isEmpty(newReadOnlyValues)) {
newArrayValues.push({ key: '' });
newArrayValues.push({ key: '', id: nanoid() });
empty = true;
}

// todo
// arrayValue is not synchronized with selfvalue
// after each modification may cause potential issues
let valueSize = newArrayValues.length;
if (valueSize !== arrayValues.length || empty) {
setArrayValues(newArrayValues);
}

setArrayValues(newArrayValues);
setHiddenValues(newHiddenValues);
setReadOnlyValues(newReadOnlyValues);
}, [selfValue]);

const handleAdd = () => {
setArrayValues([...arrayValues, { key: '' }]);
setArrayValues([...arrayValues, { key: '', id: nanoid() }]);
};

const triggerChange = (values: ValueType[]) => {
Expand Down Expand Up @@ -145,7 +148,7 @@ function PropertiesInput({
{readOnlyValues.map((item, index) => (
<PropertyItem
index={index}
key={`readonly-${item.key}`}
key={`readonly-${item.id}`}
value={item}
defaultValue={item}
readOnly
Expand All @@ -154,7 +157,7 @@ function PropertiesInput({
))}
{arrayValues.map((item, index) => (
<PropertyItem
key={`array-${item.key}`}
key={`array-${item.id}`}
index={index}
value={item || {}}
defaultValue={item || {}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ObjectInput from '../ObjectInput';
import { DeleteButton, Item } from './styles';

interface ValueType {
id: string;
key: string;
value?: any;
}
Expand Down
Loading