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 interface ValidationProp {
16 skipValidation?: boolean;
19 export const mapStateToProps = (state: RootState, validationProp: ValidationProp): VocabularyProp & ValidationProp => ({
20 skipValidation: validationProp.skipValidation,
21 vocabulary: getVocabulary(state.properties),
24 export const connectVocabulary = connect(mapStateToProps);
26 export const ITEMS_PLACEHOLDER: string[] = [];
28 export const hasError = ({ touched, invalid }: WrappedFieldMetaProps) =>
31 export const getErrorMsg = (meta: WrappedFieldMetaProps) =>
36 export const buildProps = ({ input, meta }: WrappedFieldProps) => {
39 onChange: input.onChange,
40 items: ITEMS_PLACEHOLDER,
41 renderSuggestion: (item: PropFieldSuggestion) => item.label,
42 error: hasError(meta),
43 helperText: getErrorMsg(meta),
47 // Attempts to match a manually typed value label with a value ID, when the user
48 // doesn't select the value from the suggestions list.
49 export const handleBlur = (
52 { dispatch }: WrappedFieldMetaProps,
53 { onBlur, value }: WrappedFieldInputProps,
54 fieldValue: string) =>
56 dispatch(change(formName, fieldName, fieldValue));
60 // When selecting a property value, save its ID for later usage.
61 export const handleSelect = (
64 { onChange }: WrappedFieldInputProps,
65 { dispatch }: WrappedFieldMetaProps) =>
66 (item: PropFieldSuggestion) => {
69 dispatch(change(formName, fieldName, item.id));