Merge remote-tracking branch 'origin/main' into 18207-Workbench2-is-not-clearing...
[arvados-workbench2.git] / src / views-components / resource-properties-form / property-key-field.tsx
index ef51d250e1328512918baab2c8baa94ca9e3376a..791949f543fab120457550a0781fa40068127357 100644 (file)
@@ -2,38 +2,58 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import * as React from 'react';
-import { change, WrappedFieldProps, WrappedFieldMetaProps, WrappedFieldInputProps,
-    Field, FormName } from 'redux-form';
+import React from 'react';
+import { WrappedFieldProps, Field, FormName, reset, change, WrappedFieldInputProps, WrappedFieldMetaProps } 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 { TAG_KEY_VALIDATION } from '~/validators/validators';
-import { escapeRegExp } from '~/common/regexp.ts';
+import { Autocomplete } from 'components/autocomplete/autocomplete';
+import { Vocabulary, getTags, getTagKeyID, getTagKeyLabel } from 'models/vocabulary';
+import {
+    handleSelect,
+    handleBlur,
+    connectVocabulary,
+    VocabularyProp,
+    ValidationProp,
+    buildProps
+} from 'views-components/resource-properties-form/property-field-common';
+import { TAG_KEY_VALIDATION } from 'validators/validators';
+import { escapeRegExp } from 'common/regexp';
+import { ChangeEvent } from 'react';
 
 export const PROPERTY_KEY_FIELD_NAME = 'key';
 export const PROPERTY_KEY_FIELD_ID = 'keyID';
 
 export const PropertyKeyField = connectVocabulary(
-    ({ vocabulary }: VocabularyProp) =>
+    ({ vocabulary, skipValidation }: VocabularyProp & ValidationProp) =>
+        <span data-cy='property-field-key'>
         <Field
             name={PROPERTY_KEY_FIELD_NAME}
             component={PropertyKeyInput}
             vocabulary={vocabulary}
-            validate={getValidation(vocabulary)} />
+            validate={skipValidation ? undefined : getValidation(vocabulary)} />
+        </span>
 );
 
-export const PropertyKeyInput = ({ vocabulary, ...props }: WrappedFieldProps & VocabularyProp) =>
+const PropertyKeyInput = ({ vocabulary, ...props }: WrappedFieldProps & VocabularyProp) =>
     <FormName children={data => (
         <Autocomplete
             label='Key'
             suggestions={getSuggestions(props.input.value, vocabulary)}
-            onSelect={handleSelect(data.form, props.input, props.meta)}
+            onSelect={handleSelect(PROPERTY_KEY_FIELD_ID, data.form, props.input, props.meta)}
+            onBlur={() => {
+                // Case-insensitive search for the key in the vocabulary
+                const foundKeyID = getTagKeyID(props.input.value, vocabulary);
+                if (foundKeyID !== '') {
+                    props.input.value = getTagKeyLabel(foundKeyID, vocabulary);
+                }
+                handleBlur(PROPERTY_KEY_FIELD_ID, data.form, props.meta, props.input, foundKeyID)();
+            }}
+            onChange={(e: ChangeEvent<HTMLInputElement>) => {
+                const newValue = e.currentTarget.value;
+                handleChange(data.form, props.input, props.meta, newValue);
+            }}
             {...buildProps(props)}
-            onBlur={handleBlur(data.form, props.meta, props.input, vocabulary)}
         />
-    )}/>;
+    )} />;
 
 const getValidation = memoize(
     (vocabulary: Vocabulary) =>
@@ -52,25 +72,17 @@ const getSuggestions = (value: string, vocabulary: Vocabulary) => {
     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 = (
+const handleChange = (
     formName: string,
+    { onChange }: WrappedFieldInputProps,
     { dispatch }: WrappedFieldMetaProps,
-    { onBlur, value }: WrappedFieldInputProps,
-    vocabulary: Vocabulary) =>
-    () => {
-        dispatch(change(formName, PROPERTY_KEY_FIELD_ID, getTagKeyID(value, vocabulary)));
-        onBlur(value);
-    };
+    value: string) => {
+        // Properties' values are dependant on the keys, if any value is
+        // pre-existant, a change on the property key should mean that the
+        // previous value is invalid, so we better reset the whole form before
+        // setting the new tag key.
+        dispatch(reset(formName));
 
-// When selecting a property key, save its ID for later usage.
-const handleSelect = (
-    formName: string,
-    { onChange }: WrappedFieldInputProps,
-    { dispatch }: WrappedFieldMetaProps) => {
-        return (item:PropFieldSuggestion) => {
-            onChange(item.label);
-            dispatch(change(formName, PROPERTY_KEY_FIELD_ID, item.id));
-    };
-};
+        onChange(value);
+        dispatch(change(formName, PROPERTY_KEY_FIELD_NAME, value));
+    };
\ No newline at end of file