1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import { change, WrappedFieldProps, WrappedFieldMetaProps, WrappedFieldInputProps, Field, formValues } from 'redux-form';
7 import { compose } from 'redux';
8 import { Autocomplete } from '~/components/autocomplete/autocomplete';
9 import { Vocabulary, getTagValueID, isStrictTag, getTagValues, PropFieldSuggestion } from '~/models/vocabulary';
10 import { PROPERTY_KEY_FIELD_ID } from '~/views-components/resource-properties-form/property-key-field';
11 import { VocabularyProp, connectVocabulary, buildProps } from '~/views-components/resource-properties-form/property-field-common';
12 import { TAG_VALUE_VALIDATION } from '~/validators/validators';
13 import { COLLECTION_TAG_FORM_NAME } from '~/store/collection-panel/collection-panel-action';
14 import { escapeRegExp } from '~/common/regexp.ts';
16 interface PropertyKeyProp {
20 export type PropertyValueFieldProps = VocabularyProp & PropertyKeyProp;
22 export const PROPERTY_VALUE_FIELD_NAME = 'value';
23 export const PROPERTY_VALUE_FIELD_ID = 'valueID';
25 export const PropertyValueField = compose(
27 formValues({ propertyKey: PROPERTY_KEY_FIELD_ID })
29 (props: PropertyValueFieldProps) =>
32 name={PROPERTY_VALUE_FIELD_NAME}
33 component={PropertyValueInput}
34 validate={getValidation(props)}
37 name={PROPERTY_VALUE_FIELD_ID}
43 export const PropertyValueInput = ({ vocabulary, propertyKey, ...props }: WrappedFieldProps & PropertyValueFieldProps) =>
46 suggestions={getSuggestions(props.input.value, propertyKey, vocabulary)}
47 onSelect={handleSelect(props.input, props.meta)}
48 {...buildProps(props)}
49 onBlur={handleBlur(props.meta, props.input, vocabulary, propertyKey)}
52 const getValidation = (props: PropertyValueFieldProps) =>
53 isStrictTag(props.propertyKey, props.vocabulary)
54 ? [...TAG_VALUE_VALIDATION, matchTagValues(props)]
55 : TAG_VALUE_VALIDATION;
57 const matchTagValues = ({ vocabulary, propertyKey }: PropertyValueFieldProps) =>
59 getTagValues(propertyKey, vocabulary).find(v => v.label === value)
63 const getSuggestions = (value: string, tagName: string, vocabulary: Vocabulary) => {
64 const re = new RegExp(escapeRegExp(value), "i");
65 return getTagValues(tagName, vocabulary).filter(v => re.test(v.label) && v.label !== value);
68 // Attempts to match a manually typed value label with a value ID, when the user
69 // doesn't select the value from the suggestions list.
71 { dispatch }: WrappedFieldMetaProps,
72 { onBlur, value }: WrappedFieldInputProps,
73 vocabulary: Vocabulary,
76 dispatch(change(COLLECTION_TAG_FORM_NAME, PROPERTY_VALUE_FIELD_ID, getTagValueID(tagKeyID, value, vocabulary)));
80 // When selecting a property value, save its ID for later usage.
81 const handleSelect = (
82 { onChange }: WrappedFieldInputProps,
83 { dispatch }: WrappedFieldMetaProps) => {
84 return (item:PropFieldSuggestion) => {
86 dispatch(change(COLLECTION_TAG_FORM_NAME, PROPERTY_VALUE_FIELD_ID, item.id));