17266: Avoids adding a property value without a key.
[arvados-workbench2.git] / src / views-components / resource-properties-form / property-key-field.tsx
index f91b6a621a81f48f8e2a3a6c56430b6eee0f37b9..0c3a49bea217ca2276152fe02007ecac2639f604 100644 (file)
@@ -3,41 +3,42 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import * as React from 'react';
-import { change, WrappedFieldProps, WrappedFieldMetaProps, WrappedFieldInputProps, Field } from 'redux-form';
+import { WrappedFieldProps, Field, FormName } from 'redux-form';
 import { memoize } from 'lodash';
 import { Autocomplete } from '~/components/autocomplete/autocomplete';
-import { Vocabulary, getTags, getTagKeyID, PropFieldSuggestion } from '~/models/vocabulary';
-import { connectVocabulary, VocabularyProp, buildProps } from '~/views-components/resource-properties-form/property-field-common';
+import { Vocabulary, getTags, getTagKeyID } from '~/models/vocabulary';
+import { handleSelect, handleBlur, connectVocabulary, VocabularyProp, ValidationProp, buildProps, handleChange } from '~/views-components/resource-properties-form/property-field-common';
 import { TAG_KEY_VALIDATION } from '~/validators/validators';
-import { COLLECTION_TAG_FORM_NAME } from '~/store/collection-panel/collection-panel-action';
 import { escapeRegExp } from '~/common/regexp.ts';
+import { ChangeEvent } from 'react';
 
 export const PROPERTY_KEY_FIELD_NAME = 'key';
 export const PROPERTY_KEY_FIELD_ID = 'keyID';
 
 export const PropertyKeyField = connectVocabulary(
-    ({ vocabulary }: VocabularyProp) =>
-        <div>
-            <Field
-                name={PROPERTY_KEY_FIELD_NAME}
-                component={PropertyKeyInput}
-                vocabulary={vocabulary}
-                validate={getValidation(vocabulary)} />
-            <Field
-                name={PROPERTY_KEY_FIELD_ID}
-                type='hidden'
-                component='input' />
-        </div>
+    ({ vocabulary, skipValidation }: VocabularyProp & ValidationProp) =>
+        <span data-cy='property-field-key'>
+        <Field
+            name={PROPERTY_KEY_FIELD_NAME}
+            component={PropertyKeyInput}
+            vocabulary={vocabulary}
+            validate={skipValidation ? undefined : getValidation(vocabulary)} />
+        </span>
 );
 
-export const PropertyKeyInput = ({ vocabulary, ...props }: WrappedFieldProps & VocabularyProp) =>
-    <Autocomplete
-        label='Key'
-        suggestions={getSuggestions(props.input.value, vocabulary)}
-        onSelect={handleSelect(props.input, props.meta)}
-        {...buildProps(props)}
-        onBlur={handleBlur(props.meta, props.input, vocabulary)}
-    />;
+const PropertyKeyInput = ({ vocabulary, ...props }: WrappedFieldProps & VocabularyProp) =>
+    <FormName children={data => (
+        <Autocomplete
+            label='Key'
+            suggestions={getSuggestions(props.input.value, vocabulary)}
+            onSelect={handleSelect(PROPERTY_KEY_FIELD_ID, data.form, props.input, props.meta)}
+            onBlur={handleBlur(PROPERTY_KEY_FIELD_ID, data.form, props.meta, props.input, getTagKeyID(props.input.value, vocabulary))}
+            onChange={(value: ChangeEvent<HTMLInputElement>) => {
+                handleChange(PROPERTY_KEY_FIELD_ID, data.form, props.input, props.meta)(value);
+            }}
+            {...buildProps(props)}
+        />
+    )} />;
 
 const getValidation = memoize(
     (vocabulary: Vocabulary) =>
@@ -55,24 +56,3 @@ const getSuggestions = (value: string, vocabulary: Vocabulary) => {
     const re = new RegExp(escapeRegExp(value), "i");
     return getTags(vocabulary).filter(tag => re.test(tag.label) && tag.label !== value);
 };
-
-// Attempts to match a manually typed key label with a key ID, when the user
-// doesn't select the key from the suggestions list.
-const handleBlur = (
-    { dispatch }: WrappedFieldMetaProps,
-    { onBlur, value }: WrappedFieldInputProps,
-    vocabulary: Vocabulary) =>
-    () => {
-        dispatch(change(COLLECTION_TAG_FORM_NAME, PROPERTY_KEY_FIELD_ID, getTagKeyID(value, vocabulary)));
-        onBlur(value);
-    };
-
-// When selecting a property key, save its ID for later usage.
-const handleSelect = (
-    { onChange }: WrappedFieldInputProps,
-    { dispatch }: WrappedFieldMetaProps) => {
-        return (item:PropFieldSuggestion) => {
-            onChange(item.label);
-            dispatch(change(COLLECTION_TAG_FORM_NAME, PROPERTY_KEY_FIELD_ID, item.id));
-    };
-};