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;
17 clearPropertyKeyOnSelect?: boolean;
20 export const mapStateToProps = (state: RootState, ownProps: ValidationProp): VocabularyProp & ValidationProp => ({
21 skipValidation: ownProps.skipValidation,
22 vocabulary: getVocabulary(state.properties),
25 export const connectVocabulary = connect(mapStateToProps);
27 export const ITEMS_PLACEHOLDER: string[] = [];
29 export const hasError = ({ touched, invalid }: WrappedFieldMetaProps) =>
32 export const getErrorMsg = (meta: WrappedFieldMetaProps) =>
37 export const buildProps = ({ input, meta }: WrappedFieldProps) => {
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));