56b7fe05cffc8eec7e33b8eccc5988094feecab2
[arvados-workbench2.git] / src / views-components / resource-properties-form / property-value-field.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from 'react';
6 import { WrappedFieldProps, Field, formValues, FormName, WrappedFieldInputProps, WrappedFieldMetaProps, change } from 'redux-form';
7 import { compose } from 'redux';
8 import { Autocomplete } from 'components/autocomplete/autocomplete';
9 import { Vocabulary, isStrictTag, getTagValues, getTagValueID, getTagValueLabel, PropFieldSuggestion, getPreferredTagValues } from 'models/vocabulary';
10 import { PROPERTY_KEY_FIELD_ID, PROPERTY_KEY_FIELD_NAME } from 'views-components/resource-properties-form/property-key-field';
11 import {
12     handleSelect,
13     handleBlur,
14     VocabularyProp,
15     ValidationProp,
16     connectVocabulary,
17     buildProps
18 } from 'views-components/resource-properties-form/property-field-common';
19 import { TAG_VALUE_VALIDATION } from 'validators/validators';
20 import { escapeRegExp } from 'common/regexp';
21 import { ChangeEvent } from 'react';
22
23 interface PropertyKeyProp {
24     propertyKeyId: string;
25     propertyKeyName: string;
26 }
27
28 interface PropertyValueInputProp {
29     disabled: boolean;
30 }
31
32 type PropertyValueFieldProps = VocabularyProp & PropertyKeyProp & ValidationProp & PropertyValueInputProp;
33
34 export const PROPERTY_VALUE_FIELD_NAME = 'value';
35 export const PROPERTY_VALUE_FIELD_ID = 'valueID';
36
37 const connectVocabularyAndPropertyKey = compose(
38     connectVocabulary,
39     formValues({
40         propertyKeyId: PROPERTY_KEY_FIELD_ID,
41         propertyKeyName: PROPERTY_KEY_FIELD_NAME,
42     }),
43 );
44
45 export const PropertyValueField = connectVocabularyAndPropertyKey(
46     ({ skipValidation, ...props }: PropertyValueFieldProps) =>
47         <span data-cy='property-field-value'>
48         <Field
49             name={PROPERTY_VALUE_FIELD_NAME}
50             component={PropertyValueInput}
51             validate={skipValidation ? undefined : getValidation(props)}
52             {...{...props, disabled: !props.propertyKeyName}} />
53         </span>
54 );
55
56 const PropertyValueInput = ({ vocabulary, propertyKeyId, propertyKeyName, ...props }: WrappedFieldProps & PropertyValueFieldProps) =>
57     <FormName children={data => (
58         <Autocomplete
59             {...buildProps(props)}
60             label='Value'
61             disabled={props.disabled}
62             suggestions={getSuggestions(props.input.value, propertyKeyId, vocabulary)}
63             renderSuggestion={(s: PropFieldSuggestion) => (s.description || s.label)}
64             onSelect={handleSelect(PROPERTY_VALUE_FIELD_ID, data.form, props.input, props.meta)}
65             onBlur={() => {
66                 // Case-insensitive search for the value in the vocabulary
67                 const foundValueID =  getTagValueID(propertyKeyId, props.input.value, vocabulary);
68                 if (foundValueID !== '') {
69                     props.input.value = getTagValueLabel(propertyKeyId, foundValueID, vocabulary);
70                 }
71                 handleBlur(PROPERTY_VALUE_FIELD_ID, data.form, props.meta, props.input, foundValueID)();
72             }}
73             onChange={(e: ChangeEvent<HTMLInputElement>) => {
74                 const newValue = e.currentTarget.value;
75                 const tagValueID = getTagValueID(propertyKeyId, newValue, vocabulary);
76                 handleChange(data.form, tagValueID, props.input, props.meta, newValue);
77             }}
78         />
79     )} />;
80
81 const getValidation = (props: PropertyValueFieldProps) =>
82     isStrictTag(props.propertyKeyId, props.vocabulary)
83         ? [...TAG_VALUE_VALIDATION, matchTagValues(props)]
84         : TAG_VALUE_VALIDATION;
85
86 const matchTagValues = ({ vocabulary, propertyKeyId }: PropertyValueFieldProps) =>
87     (value: string) =>
88         getTagValues(propertyKeyId, vocabulary).find(v => v.label === value)
89             ? undefined
90             : 'Incorrect value';
91
92 const getSuggestions = (value: string, tagName: string, vocabulary: Vocabulary) => {
93     const re = new RegExp(escapeRegExp(value), "i");
94     return getPreferredTagValues(tagName, vocabulary, value !== '').filter(
95         v => re.test((v.description || v.label)) && v.label !== value);
96 };
97
98 const handleChange = (
99     formName: string,
100     tagValueID: string,
101     { onChange }: WrappedFieldInputProps,
102     { dispatch }: WrappedFieldMetaProps,
103     value: string) => {
104         onChange(value);
105         dispatch(change(formName, PROPERTY_VALUE_FIELD_NAME, value));
106         dispatch(change(formName, PROPERTY_VALUE_FIELD_ID, tagValueID));
107     };