X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/38d27e9783f7f760cee84cc225e86144069848c4..099468843d687fdc8c6fbb0f0e3dc54f59d0de15:/src/views-components/resource-properties-form/property-key-field.tsx diff --git a/src/views-components/resource-properties-form/property-key-field.tsx b/src/views-components/resource-properties-form/property-key-field.tsx index 029d44cc13..0b08ad6d7d 100644 --- a/src/views-components/resource-properties-form/property-key-field.tsx +++ b/src/views-components/resource-properties-form/property-key-field.tsx @@ -6,7 +6,14 @@ 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 } from 'models/vocabulary'; +import { + Vocabulary, + getTags, + getTagKeyID, + getTagKeyLabel, + getPreferredTags, + PropFieldSuggestion +} from 'models/vocabulary'; import { handleSelect, handleBlur, @@ -23,9 +30,10 @@ export const PROPERTY_KEY_FIELD_NAME = 'key'; export const PROPERTY_KEY_FIELD_ID = 'keyID'; export const PropertyKeyField = connectVocabulary( - ({ vocabulary, skipValidation }: VocabularyProp & ValidationProp) => + ({ vocabulary, skipValidation, clearPropertyKeyOnSelect }: VocabularyProp & ValidationProp) => ); -const PropertyKeyInput = ({ vocabulary, ...props }: WrappedFieldProps & VocabularyProp) => +const PropertyKeyInput = ({ vocabulary, ...props }: WrappedFieldProps & VocabularyProp & { clearPropertyKeyOnSelect?: boolean }) => ( s.synonyms && s.synonyms.length > 0 + ? `${s.label} (${s.synonyms.join('; ')})` + : s.label + } + onFocus={() => { + if (props.clearPropertyKeyOnSelect && props.input.value) { + props.meta.dispatch(reset(props.meta.form)); + } + }} 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))} + 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) => { const newValue = e.currentTarget.value; handleChange(data.form, props.input, props.meta, newValue); }} - {...buildProps(props)} /> )} />; @@ -60,9 +85,11 @@ const matchTags = (vocabulary: Vocabulary) => ? undefined : 'Incorrect key'; -const getSuggestions = (value: string, vocabulary: Vocabulary) => { +const getSuggestions = (value: string, vocabulary: Vocabulary): PropFieldSuggestion[] => { const re = new RegExp(escapeRegExp(value), "i"); - return getTags(vocabulary).filter(tag => re.test(tag.label) && tag.label !== value); + return getPreferredTags(vocabulary, value).filter( + tag => (tag.label !== value && re.test(tag.label)) || + (tag.synonyms && tag.synonyms.some(s => re.test(s)))); }; const handleChange = (