1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import { WrappedFieldProps, Field } from 'redux-form';
7 import { memoize } from 'lodash';
8 import { Autocomplete } from '~/components/autocomplete/autocomplete';
9 import { Vocabulary } from '~/models/vocabulary';
10 import { connectVocabulary, VocabularyProp, buildProps } from '~/views-components/resource-properties-form/property-field-common';
11 import { TAG_KEY_VALIDATION } from '~/validators/validators';
13 export const PROPERTY_KEY_FIELD_NAME = 'key';
15 export const PropertyKeyField = connectVocabulary(
16 ({ vocabulary }: VocabularyProp) =>
18 name={PROPERTY_KEY_FIELD_NAME}
19 component={PropertyKeyInput}
20 vocabulary={vocabulary}
21 validate={getValidation(vocabulary)} />);
23 const PropertyKeyInput = ({ vocabulary, ...props }: WrappedFieldProps & VocabularyProp) =>
26 suggestions={getSuggestions(props.input.value, vocabulary)}
27 {...buildProps(props)}
30 const getValidation = memoize(
31 (vocabulary: Vocabulary) =>
33 ? [...TAG_KEY_VALIDATION, matchTags(vocabulary)]
34 : TAG_KEY_VALIDATION);
36 const matchTags = (vocabulary: Vocabulary) =>
38 getTagsList(vocabulary).find(tag => tag.includes(value))
42 const getSuggestions = (value: string, vocabulary: Vocabulary) =>
43 getTagsList(vocabulary).filter(tag => tag.includes(value) && tag !== value);
45 const getTagsList = ({ tags }: Vocabulary) =>