1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import { change, WrappedFieldProps, WrappedFieldMetaProps, WrappedFieldInputProps, Field, formValues } from 'redux-form';
7 import { compose } from 'redux';
8 import { Autocomplete } from '~/components/autocomplete/autocomplete';
9 import { Vocabulary } from '~/models/vocabulary';
10 import { PROPERTY_KEY_FIELD_ID } from '~/views-components/resource-properties-form/property-key-field';
11 import { VocabularyProp, connectVocabulary, buildProps, PropFieldSuggestion } from '~/views-components/resource-properties-form/property-field-common';
12 import { TAG_VALUE_VALIDATION } from '~/validators/validators';
13 import { COLLECTION_TAG_FORM_NAME } from '~/store/collection-panel/collection-panel-action';
15 interface PropertyKeyProp {
19 export type PropertyValueFieldProps = VocabularyProp & PropertyKeyProp;
21 export const PROPERTY_VALUE_FIELD_NAME = 'value';
22 export const PROPERTY_VALUE_FIELD_ID = 'valueID';
24 export const PropertyValueField = compose(
26 formValues({ propertyKey: PROPERTY_KEY_FIELD_ID })
28 (props: PropertyValueFieldProps) =>
31 name={PROPERTY_VALUE_FIELD_NAME}
32 component={PropertyValueInput}
33 validate={getValidation(props)}
36 name={PROPERTY_VALUE_FIELD_ID}
42 export const PropertyValueInput = ({ vocabulary, propertyKey, ...props }: WrappedFieldProps & PropertyValueFieldProps) =>
45 suggestions={getSuggestions(props.input.value, propertyKey, vocabulary)}
46 onSelect={handleSelect(props.input, props.meta)}
47 {...buildProps(props)}
48 onBlur={handleBlur(props.meta, props.input, vocabulary, propertyKey)}
51 const getValidation = (props: PropertyValueFieldProps) =>
52 isStrictTag(props.propertyKey, props.vocabulary)
53 ? [...TAG_VALUE_VALIDATION, matchTagValues(props)]
54 : TAG_VALUE_VALIDATION;
56 const matchTagValues = ({ vocabulary, propertyKey }: PropertyValueFieldProps) =>
58 getTagValues(propertyKey, vocabulary).find(v => v.label === value)
62 const getSuggestions = (value: string, tagKey: string, vocabulary: Vocabulary) =>
63 getTagValues(tagKey, vocabulary).filter(v => v.label.toLowerCase().includes(value.toLowerCase()));
65 const isStrictTag = (tagKey: string, vocabulary: Vocabulary) => {
66 const tag = vocabulary.tags[tagKey];
67 return tag ? tag.strict : false;
70 const getTagValues = (tagKey: string, vocabulary: Vocabulary) => {
71 const tag = vocabulary.tags[tagKey];
72 const ret = tag && tag.values
73 ? Object.keys(tag.values).map(
74 tagValueID => tag.values![tagValueID].labels
75 ? {"id": tagValueID, "label": tag.values![tagValueID].labels[0].label}
76 : {"id": tagValueID, "label": tagValueID})
81 const getTagValueID = (tagKeyID:string, tagValueLabel:string, vocabulary: Vocabulary) =>
82 (tagKeyID && vocabulary.tags[tagKeyID] && vocabulary.tags[tagKeyID].values)
83 ? Object.keys(vocabulary.tags[tagKeyID].values!).find(
84 k => vocabulary.tags[tagKeyID].values![k].labels.find(
85 l => l.label === tagValueLabel) !== undefined) || ''
88 // Attempts to match a manually typed value label with a value ID, when the user
89 // doesn't select the value from the suggestions list.
91 { dispatch }: WrappedFieldMetaProps,
92 { onBlur, value }: WrappedFieldInputProps,
93 vocabulary: Vocabulary,
96 dispatch(change(COLLECTION_TAG_FORM_NAME, PROPERTY_VALUE_FIELD_ID, getTagValueID(tagKeyID, value, vocabulary)));
100 // When selecting a property value, save its ID for later usage.
101 const handleSelect = (
102 { onChange }: WrappedFieldInputProps,
103 { dispatch }: WrappedFieldMetaProps) => {
104 return (item:PropFieldSuggestion) => {
105 onChange(item.label);
106 dispatch(change(COLLECTION_TAG_FORM_NAME, PROPERTY_VALUE_FIELD_ID, item.id));