From 768d715efc5ecf3f0108a70f695e3c32e187c900 Mon Sep 17 00:00:00 2001 From: Michal Klobukowski Date: Wed, 28 Nov 2018 12:33:30 +0100 Subject: [PATCH] Extract property field props builder Feature #14393 Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski --- .../property-field-common.tsx | 14 +++++++++++++- .../property-key-field.tsx | 15 ++++----------- .../property-value-field.tsx | 15 ++++----------- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/views-components/resource-properties-form/property-field-common.tsx b/src/views-components/resource-properties-form/property-field-common.tsx index 395be196..028c46b9 100644 --- a/src/views-components/resource-properties-form/property-field-common.tsx +++ b/src/views-components/resource-properties-form/property-field-common.tsx @@ -3,10 +3,11 @@ // SPDX-License-Identifier: AGPL-3.0 import { connect } from 'react-redux'; +import { WrappedFieldMetaProps, WrappedFieldInputProps, WrappedFieldProps } from 'redux-form'; +import { identity } from 'lodash'; import { Vocabulary } from '~/models/vocabulary'; import { RootState } from '~/store/store'; import { getVocabulary } from '~/store/vocabulary/vocabulary-selctors'; -import { WrappedFieldMetaProps, WrappedFieldInputProps } from 'redux-form'; export interface VocabularyProp { vocabulary: Vocabulary; @@ -31,3 +32,14 @@ export const getErrorMsg = (meta: WrappedFieldMetaProps) => export const handleBlur = ({ onBlur, value }: WrappedFieldInputProps) => () => onBlur(value); + +export const buildProps = ({ input, meta }: WrappedFieldProps) => ({ + value: input.value, + onChange: input.onChange, + onBlur: handleBlur(input), + items: ITEMS_PLACEHOLDER, + onSelect: input.onChange, + renderSuggestion: identity, + error: hasError(meta), + helperText: getErrorMsg(meta), +}); diff --git a/src/views-components/resource-properties-form/property-key-field.tsx b/src/views-components/resource-properties-form/property-key-field.tsx index 85cc07f6..18be627e 100644 --- a/src/views-components/resource-properties-form/property-key-field.tsx +++ b/src/views-components/resource-properties-form/property-key-field.tsx @@ -8,7 +8,7 @@ import { identity, memoize } from 'lodash'; import { Autocomplete } from '~/components/autocomplete/autocomplete'; import { Vocabulary } from '~/models/vocabulary'; import { require } from '~/validators/require'; -import { ITEMS_PLACEHOLDER, connectVocabulary, VocabularyProp, hasError, getErrorMsg, handleBlur } from '~/views-components/resource-properties-form/property-field-common'; +import { ITEMS_PLACEHOLDER, connectVocabulary, VocabularyProp, hasError, getErrorMsg, handleBlur, buildProps } from '~/views-components/resource-properties-form/property-field-common'; export const PROPERTY_KEY_FIELD_NAME = 'key'; @@ -20,18 +20,11 @@ export const PropertyKeyField = connectVocabulary( vocabulary={vocabulary} validate={getValidation(vocabulary)} />); -const PropertyKeyInput = ({ input, meta, vocabulary }: WrappedFieldProps & VocabularyProp) => +const PropertyKeyInput = ({ vocabulary, ...props }: WrappedFieldProps & VocabularyProp) => ; const getValidation = memoize( diff --git a/src/views-components/resource-properties-form/property-value-field.tsx b/src/views-components/resource-properties-form/property-value-field.tsx index 0713b06e..ff0959d9 100644 --- a/src/views-components/resource-properties-form/property-value-field.tsx +++ b/src/views-components/resource-properties-form/property-value-field.tsx @@ -10,7 +10,7 @@ import { Autocomplete } from '~/components/autocomplete/autocomplete'; import { Vocabulary } from '~/models/vocabulary'; import { require } from '~/validators/require'; import { PROPERTY_KEY_FIELD_NAME } from '~/views-components/resource-properties-form/property-key-field'; -import { ITEMS_PLACEHOLDER, VocabularyProp, connectVocabulary, hasError, getErrorMsg, handleBlur } from '~/views-components/resource-properties-form/property-field-common'; +import { ITEMS_PLACEHOLDER, VocabularyProp, connectVocabulary, hasError, getErrorMsg, handleBlur, buildProps } from '~/views-components/resource-properties-form/property-field-common'; interface PropertyKeyProp { propertyKey: string; @@ -31,18 +31,11 @@ export const PropertyValueField = compose( validate={getValidation(props)} {...props} />); -const PropertyValueInput = ({ input, meta, vocabulary, propertyKey }: WrappedFieldProps & PropertyValueFieldProps) => +const PropertyValueInput = ({ vocabulary, propertyKey, ...props }: WrappedFieldProps & PropertyValueFieldProps) => ; const getValidation = (props: PropertyValueFieldProps) => -- 2.30.2