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 export const PropertyKeyInput = ({ vocabulary, ...props }: WrappedFieldProps & VocabularyProp) =>
26 suggestions={getSuggestions(props.input.value, vocabulary)}
27 {...buildProps(props)}
30 const getValidation = memoize(
31 (vocabulary: Vocabulary) =>
32 vocabulary.strict_tags
33 ? [...TAG_KEY_VALIDATION, matchTags(vocabulary)]
34 : TAG_KEY_VALIDATION);
36 const matchTags = (vocabulary: Vocabulary) =>
38 getTagsList(vocabulary).find(tag => tag.id.includes(value))
42 const getSuggestions = (value: string, vocabulary: Vocabulary) =>
43 getTagsList(vocabulary).filter(tag => tag.label.includes(value) && tag.label !== value);
45 const getTagsList = ({ tags }: Vocabulary) => {
46 const ret = tags && Object.keys(tags)
47 ? Object.keys(tags).map(
48 tagID => tags[tagID].labels
49 ? {"id": tagID, "label": tags[tagID].labels[0].label}
50 : {"id": tagID, "label": tagID})