From 56a28ec4457d9bcc8c1fd5a2b42212fa0b649ee9 Mon Sep 17 00:00:00 2001 From: Boris Sekachev Date: Thu, 13 Jul 2023 14:59:51 +0300 Subject: [PATCH 1/7] Fixed defaul value for an attribute --- cvat-ui/src/components/labels-editor/labels-editor.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cvat-ui/src/components/labels-editor/labels-editor.tsx b/cvat-ui/src/components/labels-editor/labels-editor.tsx index d342c1534a59..b185f32d4676 100644 --- a/cvat-ui/src/components/labels-editor/labels-editor.tsx +++ b/cvat-ui/src/components/labels-editor/labels-editor.tsx @@ -72,7 +72,7 @@ export default class LabelsEditor extends React.PureComponent Date: Tue, 18 Jul 2023 11:09:31 +0300 Subject: [PATCH 2/7] Applied changes to labels constructor --- .../components/labels-editor/label-form.tsx | 174 +++++++++++------- .../labels-editor/labels-editor.tsx | 2 +- 2 files changed, 108 insertions(+), 68 deletions(-) diff --git a/cvat-ui/src/components/labels-editor/label-form.tsx b/cvat-ui/src/components/labels-editor/label-form.tsx index 72fdbc90b0a5..ca3227747424 100644 --- a/cvat-ui/src/components/labels-editor/label-form.tsx +++ b/cvat-ui/src/components/labels-editor/label-form.tsx @@ -12,6 +12,7 @@ import Checkbox from 'antd/lib/checkbox'; import Select from 'antd/lib/select'; import Form, { FormInstance } from 'antd/lib/form'; import Badge from 'antd/lib/badge'; +import Modal from 'antd/lib/modal'; import { Store } from 'antd/lib/form/interface'; import { SerializedAttribute, LabelType } from 'cvat-core-wrapper'; @@ -94,6 +95,7 @@ export default class LabelForm extends React.Component { return { ...attribute, values: attrValues, + default_value: attrValues[0], input_type: attribute.type.toLowerCase(), }; }), @@ -117,7 +119,18 @@ export default class LabelForm extends React.Component { private addAttribute = (): void => { if (this.formRef.current) { const attributes = this.formRef.current.getFieldValue('attributes'); - this.formRef.current.setFieldsValue({ attributes: [...(attributes || []), { id: idGenerator() }] }); + this.formRef.current.setFieldsValue({ + attributes: [ + ...(attributes || []), + { + id: idGenerator(), + type: AttributeType.SELECT, + name: '', + values: [], + mutable: false, + }, + ], + }); } }; @@ -131,17 +144,15 @@ export default class LabelForm extends React.Component { }; /* eslint-disable class-methods-use-this */ - private renderAttributeNameInput(fieldInstance: any, attr: SerializedAttribute | null): JSX.Element { + private renderAttributeNameInput(fieldInstance: any, attr: any): JSX.Element { const { key } = fieldInstance; - const locked = attr ? attr.id as number >= 0 : false; - const value = attr ? attr.name : ''; + const attrNames = this.formRef.current?.getFieldValue('attributes') + .filter((_attr: any) => _attr.id !== attr.id).map((_attr: any) => _attr.name); return ( { pattern: patterns.validateAttributeName.pattern, message: patterns.validateAttributeName.message, }, + { + validator: (_rule: any, attrName: string) => { + if (attrNames.includes(attrName) && attr.name !== attrName) { + return Promise.reject(new Error('Attribute name must be unique for the label')); + } + return Promise.resolve(); + }, + }, ]} > - + ); } - private renderAttributeTypeInput(fieldInstance: any, attr: SerializedAttribute | null): JSX.Element { + private renderAttributeTypeInput(fieldInstance: any, attr: any): JSX.Element { const { key } = fieldInstance; - const locked = attr ? attr.id as number >= 0 : false; - const type = attr ? attr.input_type.toUpperCase() : AttributeType.SELECT; + const locked = attr.id as number >= 0; return ( - - { + const attrs = this.formRef.current?.getFieldValue('attributes'); + if (value === AttributeType.CHECKBOX) { + attrs[key].values = ['false']; + } else if (value === AttributeType.TEXT && !attrs[key].values.length) { + attrs[key].values = ''; + } else if (value === AttributeType.NUMBER || attr.type === AttributeType.CHECKBOX) { + attrs[key].values = []; + } + this.formRef.current?.setFieldsValue({ + attributes: attrs, + }); + }} + > Select @@ -188,10 +222,10 @@ export default class LabelForm extends React.Component { ); } - private renderAttributeValuesInput(fieldInstance: any, attr: SerializedAttribute | null): JSX.Element { + private renderAttributeValuesInput(fieldInstance: any, attr: any): JSX.Element { const { key } = fieldInstance; - const locked = attr ? attr.id as number >= 0 : false; - const existingValues = attr ? attr.values : []; + const locked = attr.id as number >= 0; + const existingValues = attr.values; const validator = (_: any, values: string[]): Promise => { if (locked && existingValues) { @@ -213,8 +247,6 @@ export default class LabelForm extends React.Component { { message: 'Please, specify a default value', }]} name={[key, 'values']} - fieldKey={[fieldInstance.fieldKey, 'values']} > + + ); } - private renderMutableAttributeInput(fieldInstance: any, attr: SerializedAttribute | null): JSX.Element { + private renderMutableAttributeInput(fieldInstance: any, attr: any): JSX.Element { const { key } = fieldInstance; - const locked = attr ? attr.id as number >= 0 : false; - const value = attr ? attr.mutable : false; + const locked = attr.id as number >= 0; return ( @@ -353,19 +371,31 @@ export default class LabelForm extends React.Component { ); } - private renderDeleteAttributeButton(fieldInstance: any, attr: SerializedAttribute | null): JSX.Element { + private renderDeleteAttributeButton(fieldInstance: any, attr: any): JSX.Element { const { key } = fieldInstance; - const locked = attr ? attr.id as number >= 0 : false; return (