X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/f0aac0b9747eac597ed42e5169be73cf6fbad410..dd315a23e98b926d4d15b9d05f1aaa1e211548a4:/src/views-components/resource-properties-form/property-value-field.tsx diff --git a/src/views-components/resource-properties-form/property-value-field.tsx b/src/views-components/resource-properties-form/property-value-field.tsx index db2db3f7ec..99745199fe 100644 --- a/src/views-components/resource-properties-form/property-value-field.tsx +++ b/src/views-components/resource-properties-form/property-value-field.tsx @@ -3,39 +3,48 @@ // SPDX-License-Identifier: AGPL-3.0 import * as React from 'react'; -import { WrappedFieldProps, Field, formValues } from 'redux-form'; +import { WrappedFieldProps, Field, formValues, FormName } from 'redux-form'; import { compose } from 'redux'; import { Autocomplete } from '~/components/autocomplete/autocomplete'; -import { Vocabulary } from '~/models/vocabulary'; -import { PROPERTY_KEY_FIELD_NAME } from '~/views-components/resource-properties-form/property-key-field'; -import { VocabularyProp, connectVocabulary, buildProps } from '~/views-components/resource-properties-form/property-field-common'; +import { Vocabulary, isStrictTag, getTagValues, getTagValueID } from '~/models/vocabulary'; +import { PROPERTY_KEY_FIELD_ID } from '~/views-components/resource-properties-form/property-key-field'; +import { handleSelect, handleBlur, VocabularyProp, ValidationProp, connectVocabulary, buildProps } from '~/views-components/resource-properties-form/property-field-common'; import { TAG_VALUE_VALIDATION } from '~/validators/validators'; +import { escapeRegExp } from '~/common/regexp.ts'; interface PropertyKeyProp { propertyKey: string; } -type PropertyValueFieldProps = VocabularyProp & PropertyKeyProp; +type PropertyValueFieldProps = VocabularyProp & PropertyKeyProp & ValidationProp; export const PROPERTY_VALUE_FIELD_NAME = 'value'; +export const PROPERTY_VALUE_FIELD_ID = 'valueID'; -export const PropertyValueField = compose( +const connectVocabularyAndPropertyKey = compose( connectVocabulary, - formValues({ propertyKey: PROPERTY_KEY_FIELD_NAME }) -)( - (props: PropertyValueFieldProps) => + formValues({ propertyKey: PROPERTY_KEY_FIELD_ID }), +); + +export const PropertyValueField = connectVocabularyAndPropertyKey( + ({ skipValidation, ...props }: PropertyValueFieldProps) => ); + validate={skipValidation ? undefined : getValidation(props)} + {...props} /> +); const PropertyValueInput = ({ vocabulary, propertyKey, ...props }: WrappedFieldProps & PropertyValueFieldProps) => - ; + ( + + )} />; const getValidation = (props: PropertyValueFieldProps) => isStrictTag(props.propertyKey, props.vocabulary) @@ -44,19 +53,11 @@ const getValidation = (props: PropertyValueFieldProps) => const matchTagValues = ({ vocabulary, propertyKey }: PropertyValueFieldProps) => (value: string) => - getTagValues(propertyKey, vocabulary).find(v => v.includes(value)) + getTagValues(propertyKey, vocabulary).find(v => v.label === value) ? undefined : 'Incorrect value'; -const getSuggestions = (value: string, tagName: string, vocabulary: Vocabulary) => - getTagValues(tagName, vocabulary).filter(v => v.includes(value) && v !== value); - -const isStrictTag = (tagName: string, vocabulary: Vocabulary) => { - const tag = vocabulary.tags[tagName]; - return tag ? tag.strict : false; -}; - -const getTagValues = (tagName: string, vocabulary: Vocabulary) => { - const tag = vocabulary.tags[tagName]; - return tag && tag.values ? tag.values : []; +const getSuggestions = (value: string, tagName: string, vocabulary: Vocabulary) => { + const re = new RegExp(escapeRegExp(value), "i"); + return getTagValues(tagName, vocabulary).filter(v => re.test(v.label) && v.label !== value); };