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 schema configuration validate UI problem #515

Merged
merged 2 commits into from
Mar 31, 2023
Merged
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
141 changes: 75 additions & 66 deletions app/pages/SketchModeling/SchemaConfig/PropertiesForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,18 @@ const PropertiesForm = (props: IProps) => {
const properties = getFieldValue('properties') || [];
return (
<div>
<Form.List name="properties">
{(fields, { add, remove }) => {
<Form.List name="properties" rules={[
{
validator: (_, properties) => {
const names = properties.map((item) => item.name).filter(Boolean);
if(names.length !== new Set(names).size) {
return Promise.reject(intl.get('schema.uniqProperty'));
}
return Promise.resolve();
},
},
]}>
{(fields, { add, remove }, { errors }) => {
return (
<Form.Item noStyle>
<div className={styles.propertyAction}>
Expand All @@ -82,80 +92,79 @@ const PropertiesForm = (props: IProps) => {
</Row>
</Form.Item>
)}
{fields.map(({ key, name, ...restField }, index) => (
<React.Fragment key={key}>
<Row className={styles.fieldsItem}>
<Col span={9}>
<Form.Item
name={[name, 'name']}
{...restField}
{...itemLayout}
rules={[...nameRulesFn(), () => ({ validator: (_, value) => {
if (!value || properties.filter((item, i) => item.name === value && i !== index).length === 0) {
return Promise.resolve();
}
return Promise.reject(intl.get('schema.uniqProperty'));
} })]}
>
<Input />
</Form.Item>
</Col>
<Col span={12} className={styles.propertyItem}>
<Form.Item
name={[name, 'type']}
{...restField}
{...itemLayout}
wrapperCol={{ span: properties[index].type === 'fixed_string' ? 15 : 23 }}
rules={[
{
required: true,
message: intl.get('formRules.dataTypeRequired'),
},
]}
>
<Select
showSearch={true}
onChange={() => handleResetValue(index)}
dropdownMatchSelectWidth={false}
{fields.map(({ key, name, ...restField }, index) => {
const hasSameName = properties.some((item, i) => item.name && item.name === properties[index].name && i !== index);
return (
<React.Fragment key={key}>
<Row className={styles.fieldsItem}>
<Col span={9}>
<Form.Item
name={[name, 'name']}
{...restField}
{...itemLayout}
rules={[...nameRulesFn()]}
>
{DATA_TYPE.map((item) => {
return (
<Option value={item.value} key={item.value}>
{item.label}
</Option>
);
})}
</Select>
</Form.Item>
{fields?.[index] && properties[index].type === 'fixed_string' && (
<Input />
</Form.Item>
</Col>
<Col span={12} className={styles.propertyItem}>
<Form.Item
name={[name, 'type']}
{...restField}
className={styles.itemStringLength}
name={[name, 'fixedLength']}
{...itemLayout}
wrapperCol={{ span: properties[index].type === 'fixed_string' ? 15 : 23 }}
rules={[
...numberRulesFn(),
{
required: true,
message: intl.get('formRules.numberRequired'),
message: intl.get('formRules.dataTypeRequired'),
},
]}
>
<Input className={styles.inputStringLength} />
<Select
showSearch={true}
onChange={() => handleResetValue(index)}
dropdownMatchSelectWidth={false}
>
{DATA_TYPE.map((item) => {
return (
<Option value={item.value} key={item.value}>
{item.label}
</Option>
);
})}
</Select>
</Form.Item>
)}
</Col>
<Col span={2}>
<Form.Item noStyle>
<Button
className={styles.removeBtn}
icon={<Icon type="icon-list-close" />}
onClick={() => remove(index)}
/>
</Form.Item>
</Col>
</Row>
</React.Fragment>
))}
{fields?.[index] && properties[index].type === 'fixed_string' && (
<Form.Item
{...restField}
className={styles.itemStringLength}
name={[name, 'fixedLength']}
rules={[
...numberRulesFn(),
{
required: true,
message: intl.get('formRules.numberRequired'),
},
]}
>
<Input className={styles.inputStringLength} />
</Form.Item>
)}
</Col>
<Col span={2}>
<Form.Item noStyle>
<Button
className={styles.removeBtn}
icon={<Icon type="icon-list-close" />}
onClick={() => remove(index)}
/>
</Form.Item>
</Col>
{hasSameName && <Form.ErrorList className={styles.errors} errors={errors} />}
</Row>
</React.Fragment>
);
})}
</Form.Item>
);
}}
Expand Down
4 changes: 4 additions & 0 deletions app/pages/SketchModeling/SchemaConfig/index.module.less
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
height: 60px;
}
}
.errors {
position: relative;
top: -13px;
}
.label {
display: inline-block;
font-weight: 500;
Expand Down
7 changes: 7 additions & 0 deletions app/pages/SketchModeling/SchemaConfig/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ const SchemaConfig: React.FC = () => {
return null;
}

const handleCheck = (changedValues) => {
const { properties } = changedValues;
if (properties && properties.some(i => i.name !== undefined)) {
form.validateFields([['properties']]);
}
};
const { type, name, comment, properties } = active;

return (
Expand All @@ -123,6 +129,7 @@ const SchemaConfig: React.FC = () => {
form={form}
className={styles.configForm}
initialValues={{ name, comment, properties }}
onValuesChange={handleCheck}
onFieldsChange={handleUpdate}
name="form"
layout="vertical"
Expand Down