Merge branch '14393-vocabulary'
[arvados-workbench2.git] / src / views-components / resource-properties-form / property-field-common.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { connect } from 'react-redux';
6 import { WrappedFieldMetaProps, WrappedFieldInputProps, WrappedFieldProps } from 'redux-form';
7 import { identity } from 'lodash';
8 import { Vocabulary } from '~/models/vocabulary';
9 import { RootState } from '~/store/store';
10 import { getVocabulary } from '~/store/vocabulary/vocabulary-selctors';
11
12 export interface VocabularyProp {
13     vocabulary: Vocabulary;
14 }
15
16 export const mapStateToProps = (state: RootState): VocabularyProp => ({
17     vocabulary: getVocabulary(state.properties),
18 });
19
20 export const connectVocabulary = connect(mapStateToProps);
21
22 export const ITEMS_PLACEHOLDER: string[] = [];
23
24 export const hasError = ({ touched, invalid }: WrappedFieldMetaProps) =>
25     touched && invalid;
26
27 export const getErrorMsg = (meta: WrappedFieldMetaProps) =>
28     hasError(meta)
29         ? meta.error
30         : '';
31
32 export const handleBlur = ({ onBlur, value }: WrappedFieldInputProps) =>
33     () =>
34         onBlur(value);
35
36 export const buildProps = ({ input, meta }: WrappedFieldProps) => ({
37     value: input.value,
38     onChange: input.onChange,
39     onBlur: handleBlur(input),
40     items: ITEMS_PLACEHOLDER,
41     onSelect: input.onChange,
42     renderSuggestion: identity,
43     error: hasError(meta),
44     helperText: getErrorMsg(meta),
45 });