Merge branch 'master' into 15067-tag-editing-by-ids
[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 * 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';
15
16 interface PropertyKeyProp {
17     propertyKey: string;
18 }
19
20 export type PropertyValueFieldProps = VocabularyProp & PropertyKeyProp;
21
22 export const PROPERTY_VALUE_FIELD_NAME = 'value';
23 export const PROPERTY_VALUE_FIELD_ID = 'valueID';
24
25 export const PropertyValueField = compose(
26     connectVocabulary,
27     formValues({ propertyKey: PROPERTY_KEY_FIELD_ID })
28 )(
29     (props: PropertyValueFieldProps) =>
30         <div>
31             <Field
32                 name={PROPERTY_VALUE_FIELD_NAME}
33                 component={PropertyValueInput}
34                 validate={getValidation(props)}
35                 {...props} />
36             <Field
37                 name={PROPERTY_VALUE_FIELD_ID}
38                 type='hidden'
39                 component='input' />
40         </div>
41 );
42
43 export const PropertyValueInput = ({ vocabulary, propertyKey, ...props }: WrappedFieldProps & PropertyValueFieldProps) =>
44     <Autocomplete
45         label='Value'
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)}
50     />;
51
52 const getValidation = (props: PropertyValueFieldProps) =>
53     isStrictTag(props.propertyKey, props.vocabulary)
54         ? [...TAG_VALUE_VALIDATION, matchTagValues(props)]
55         : TAG_VALUE_VALIDATION;
56
57 const matchTagValues = ({ vocabulary, propertyKey }: PropertyValueFieldProps) =>
58     (value: string) =>
59         getTagValues(propertyKey, vocabulary).find(v => v.label === value)
60             ? undefined
61             : 'Incorrect value';
62
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);
66 };
67
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.
70 const handleBlur = (
71     { dispatch }: WrappedFieldMetaProps,
72     { onBlur, value }: WrappedFieldInputProps,
73     vocabulary: Vocabulary,
74     tagKeyID: string) =>
75         () => {
76             dispatch(change(COLLECTION_TAG_FORM_NAME, PROPERTY_VALUE_FIELD_ID, getTagValueID(tagKeyID, value, vocabulary)));
77             onBlur(value);
78         };
79
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) => {
85             onChange(item.label);
86             dispatch(change(COLLECTION_TAG_FORM_NAME, PROPERTY_VALUE_FIELD_ID, item.id));
87     };
88 };