1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { connect } from 'react-redux';
6 import { change, WrappedFieldMetaProps, WrappedFieldInputProps, WrappedFieldProps } from 'redux-form';
7 import { Vocabulary, PropFieldSuggestion } from '~/models/vocabulary';
8 import { RootState } from '~/store/store';
9 import { getVocabulary } from '~/store/vocabulary/vocabulary-selectors';
11 export interface VocabularyProp {
12 vocabulary: Vocabulary;
15 export const mapStateToProps = (state: RootState): VocabularyProp => ({
16 vocabulary: getVocabulary(state.properties),
19 export const connectVocabulary = connect(mapStateToProps);
21 export const ITEMS_PLACEHOLDER: string[] = [];
23 export const hasError = ({ touched, invalid }: WrappedFieldMetaProps) =>
26 export const getErrorMsg = (meta: WrappedFieldMetaProps) =>
31 export const buildProps = ({ input, meta }: WrappedFieldProps) => {
34 onChange: input.onChange,
35 items: ITEMS_PLACEHOLDER,
36 renderSuggestion: (item:PropFieldSuggestion) => item.label,
37 error: hasError(meta),
38 helperText: getErrorMsg(meta),
42 // Attempts to match a manually typed value label with a value ID, when the user
43 // doesn't select the value from the suggestions list.
44 export const handleBlur = (
47 { dispatch }: WrappedFieldMetaProps,
48 { onBlur, value }: WrappedFieldInputProps,
49 fieldValue: string) =>
51 dispatch(change(formName, fieldName, fieldValue));
55 // When selecting a property value, save its ID for later usage.
56 export const handleSelect = (
59 { onChange }: WrappedFieldInputProps,
60 { dispatch }: WrappedFieldMetaProps ) =>
61 (item:PropFieldSuggestion) => {
64 dispatch(change(formName, fieldName, item.id));