Replace advanced search properties fields with ones supporting vocablary
[arvados.git] / src / views-components / resource-properties-form / property-value-field.tsx
index eed8e75b6c2ee56d8f7b725b0dc91af15d061f5c..13dcfeb544278acf62db0b41aca9c113333043d3 100644 (file)
@@ -3,54 +3,44 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import * as React from 'react';
-import { WrappedFieldProps, Field } from 'redux-form';
-import { connect } from 'react-redux';
-import { identity } from 'lodash';
-import { RootState } from '~/store/store';
-import { getVocabulary } from '~/store/vocabulary/vocabulary-selctors';
+import { WrappedFieldProps, Field, formValues } from 'redux-form';
+import { compose } from 'redux';
 import { Autocomplete } from '~/components/autocomplete/autocomplete';
 import { Vocabulary } from '~/models/vocabulary';
-import { require } from '~/validators/require';
-
-interface VocabularyProp {
-    vocabulary: 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 { TAG_VALUE_VALIDATION } from '~/validators/validators';
 
 interface PropertyKeyProp {
     propertyKey: string;
 }
 
-type PropertyValueFieldProps = VocabularyProp & PropertyKeyProp;
+export type PropertyValueFieldProps = VocabularyProp & PropertyKeyProp;
 
-const mapStateToProps = (state: RootState): VocabularyProp => ({
-    vocabulary: getVocabulary(state.properties),
-});
+export const PROPERTY_VALUE_FIELD_NAME = 'value';
 
-export const PropertyValueField = connect(mapStateToProps)(
+export const PropertyValueField = compose(
+    connectVocabulary,
+    formValues({ propertyKey: PROPERTY_KEY_FIELD_NAME })
+)(
     (props: PropertyValueFieldProps) =>
         <Field
-            name='value'
+            name={PROPERTY_VALUE_FIELD_NAME}
             component={PropertyValueInput}
             validate={getValidation(props)}
             {...props} />);
 
-const PropertyValueInput = ({ input, meta, vocabulary, propertyKey }: WrappedFieldProps & PropertyValueFieldProps) =>
+export const PropertyValueInput = ({ vocabulary, propertyKey, ...props }: WrappedFieldProps & PropertyValueFieldProps) =>
     <Autocomplete
-        value={input.value}
-        onChange={input.onChange}
         label='Value'
-        suggestions={getSuggestions(input.value, propertyKey, vocabulary)}
-        items={ITEMS_PLACEHOLDER}
-        onSelect={input.onChange}
-        renderSuggestion={identity}
-        error={meta.invalid}
-        helperText={meta.error}
+        suggestions={getSuggestions(props.input.value, propertyKey, vocabulary)}
+        {...buildProps(props)}
     />;
 
 const getValidation = (props: PropertyValueFieldProps) =>
     isStrictTag(props.propertyKey, props.vocabulary)
-        ? [require, matchTagValues(props)]
-        : [require];
+        ? [...TAG_VALUE_VALIDATION, matchTagValues(props)]
+        : TAG_VALUE_VALIDATION;
 
 const matchTagValues = ({ vocabulary, propertyKey }: PropertyValueFieldProps) =>
     (value: string) =>
@@ -68,7 +58,5 @@ const isStrictTag = (tagName: string, vocabulary: Vocabulary) => {
 
 const getTagValues = (tagName: string, vocabulary: Vocabulary) => {
     const tag = vocabulary.tags[tagName];
-    return tag ? tag.values : [];
+    return tag && tag.values ? tag.values : [];
 };
-
-const ITEMS_PLACEHOLDER: string[] = [];