correct-order-for-public-favorites-inside-tree-project
[arvados-workbench2.git] / src / views-components / resource-properties-form / property-key-field.tsx
index fdd89d09321d3b0d472ad99753184751da17cf7c..3fb2d377aeff56574a06a919f2f33e3aea5f142e 100644 (file)
@@ -4,48 +4,34 @@
 
 import * as React from 'react';
 import { WrappedFieldProps, Field } from 'redux-form';
-import { connect } from 'react-redux';
-import { identity, memoize } from 'lodash';
-import { RootState } from '~/store/store';
-import { getVocabulary } from '~/store/vocabulary/vocabulary-selctors';
+import { memoize } from 'lodash';
 import { Autocomplete } from '~/components/autocomplete/autocomplete';
 import { Vocabulary } from '~/models/vocabulary';
-import { require } from '~/validators/require';
+import { connectVocabulary, VocabularyProp, buildProps } from '~/views-components/resource-properties-form/property-field-common';
+import { TAG_KEY_VALIDATION } from '~/validators/validators';
 
-interface VocabularyProp {
-    vocabulary: Vocabulary;
-}
+export const PROPERTY_KEY_FIELD_NAME = 'key';
 
-const mapStateToProps = (state: RootState): VocabularyProp => ({
-    vocabulary: getVocabulary(state.properties),
-});
-
-export const PropertyKeyField = connect(mapStateToProps)(
+export const PropertyKeyField = connectVocabulary(
     ({ vocabulary }: VocabularyProp) =>
         <Field
-            name='key'
+            name={PROPERTY_KEY_FIELD_NAME}
             component={PropertyKeyInput}
             vocabulary={vocabulary}
             validate={getValidation(vocabulary)} />);
 
-const PropertyKeyInput = ({ input, meta, vocabulary }: WrappedFieldProps & VocabularyProp) =>
+export const PropertyKeyInput = ({ vocabulary, ...props }: WrappedFieldProps & VocabularyProp) =>
     <Autocomplete
-        value={input.value}
-        onChange={input.onChange}
         label='Key'
-        suggestions={getSuggestions(input.value, vocabulary)}
-        items={ITEMS_PLACEHOLDER}
-        onSelect={input.onChange}
-        renderSuggestion={identity}
-        error={meta.invalid}
-        helperText={meta.error}
+        suggestions={getSuggestions(props.input.value, vocabulary)}
+        {...buildProps(props)}
     />;
 
 const getValidation = memoize(
     (vocabulary: Vocabulary) =>
         vocabulary.strict
-            ? [require, matchTags(vocabulary)]
-            : [require]);
+            ? [...TAG_KEY_VALIDATION, matchTags(vocabulary)]
+            : TAG_KEY_VALIDATION);
 
 const matchTags = (vocabulary: Vocabulary) =>
     (value: string) =>
@@ -53,10 +39,8 @@ const matchTags = (vocabulary: Vocabulary) =>
             ? undefined
             : 'Incorrect key';
 
-const getSuggestions = (value: string, vocabulary: Vocabulary) => 
+const getSuggestions = (value: string, vocabulary: Vocabulary) =>
     getTagsList(vocabulary).filter(tag => tag.includes(value) && tag !== value);
 
 const getTagsList = ({ tags }: Vocabulary) =>
     Object.keys(tags);
-
-const ITEMS_PLACEHOLDER: string[] = [];