15069: Adds optional validation disabling to property fields components.
[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 { WrappedFieldProps, Field, formValues, FormName } from 'redux-form';
7 import { compose } from 'redux';
8 import { Autocomplete } from '~/components/autocomplete/autocomplete';
9 import { Vocabulary, isStrictTag, getTagValues, getTagValueID } from '~/models/vocabulary';
10 import { PROPERTY_KEY_FIELD_ID } from '~/views-components/resource-properties-form/property-key-field';
11 import { handleSelect, handleBlur, VocabularyProp, connectVocabulary, buildProps } from '~/views-components/resource-properties-form/property-field-common';
12 import { TAG_VALUE_VALIDATION } from '~/validators/validators';
13 import { escapeRegExp } from '~/common/regexp.ts';
14
15 interface PropertyKeyProp {
16     propertyKey: string;
17 }
18
19 interface ValidationProp {
20     skipValidation?: boolean;
21 }
22
23 type PropertyValueFieldProps = VocabularyProp & PropertyKeyProp;
24
25 export const PROPERTY_VALUE_FIELD_NAME = 'value';
26 export const PROPERTY_VALUE_FIELD_ID = 'valueID';
27
28 const connectVocabularyAndPropertyKey = compose<any>(
29     connectVocabulary,
30     formValues({ propertyKey: PROPERTY_KEY_FIELD_ID }),
31 );
32
33 export const PropertyValueField = connectVocabularyAndPropertyKey(
34     ({skipValidation, ...props}: PropertyValueFieldProps & ValidationProp) =>
35         <Field
36             name={PROPERTY_VALUE_FIELD_NAME}
37             component={PropertyValueInput}
38             validate={skipValidation ? undefined : getValidation(props)}
39             {...props} />
40 );
41
42 const PropertyValueInput = ({ vocabulary, propertyKey, ...props }: WrappedFieldProps & PropertyValueFieldProps) =>
43     <FormName children={data => (
44         <Autocomplete
45             label='Value'
46             suggestions={getSuggestions(props.input.value, propertyKey, vocabulary)}
47             onSelect={handleSelect(PROPERTY_VALUE_FIELD_ID, data.form, props.input, props.meta)}
48             onBlur={handleBlur(PROPERTY_VALUE_FIELD_ID, data.form, props.meta, props.input, getTagValueID(propertyKey, props.input.value, vocabulary))}
49             {...buildProps(props)}
50         />
51     )}/>;
52
53 const getValidation = (props: PropertyValueFieldProps) =>
54     isStrictTag(props.propertyKey, props.vocabulary)
55         ? [...TAG_VALUE_VALIDATION, matchTagValues(props)]
56         : TAG_VALUE_VALIDATION;
57
58 const matchTagValues = ({ vocabulary, propertyKey }: PropertyValueFieldProps) =>
59     (value: string) =>
60         getTagValues(propertyKey, vocabulary).find(v => v.label === value)
61             ? undefined
62             : 'Incorrect value';
63
64 const getSuggestions = (value: string, tagName: string, vocabulary: Vocabulary) => {
65     const re = new RegExp(escapeRegExp(value), "i");
66     return getTagValues(tagName, vocabulary).filter(v => re.test(v.label) && v.label !== value);
67 };
68