From 511702e8333a82459900a4118b65bf1f9d9c5ff3 Mon Sep 17 00:00:00 2001 From: Michal Klobukowski Date: Tue, 27 Nov 2018 21:10:22 +0100 Subject: [PATCH] Create PropertyKeyField Feature #14393 Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski --- .../property-key-field.tsx | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/views-components/resource-properties-form/property-key-field.tsx diff --git a/src/views-components/resource-properties-form/property-key-field.tsx b/src/views-components/resource-properties-form/property-key-field.tsx new file mode 100644 index 00000000..fdd89d09 --- /dev/null +++ b/src/views-components/resource-properties-form/property-key-field.tsx @@ -0,0 +1,62 @@ +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: AGPL-3.0 + +import * as React from 'react'; +import { WrappedFieldProps, Field } from 'redux-form'; +import { connect } from 'react-redux'; +import { identity, memoize } from 'lodash'; +import { RootState } from '~/store/store'; +import { getVocabulary } from '~/store/vocabulary/vocabulary-selctors'; +import { Autocomplete } from '~/components/autocomplete/autocomplete'; +import { Vocabulary } from '~/models/vocabulary'; +import { require } from '~/validators/require'; + +interface VocabularyProp { + vocabulary: Vocabulary; +} + +const mapStateToProps = (state: RootState): VocabularyProp => ({ + vocabulary: getVocabulary(state.properties), +}); + +export const PropertyKeyField = connect(mapStateToProps)( + ({ vocabulary }: VocabularyProp) => + ); + +const PropertyKeyInput = ({ input, meta, vocabulary }: WrappedFieldProps & VocabularyProp) => + ; + +const getValidation = memoize( + (vocabulary: Vocabulary) => + vocabulary.strict + ? [require, matchTags(vocabulary)] + : [require]); + +const matchTags = (vocabulary: Vocabulary) => + (value: string) => + getTagsList(vocabulary).find(tag => tag.includes(value)) + ? undefined + : 'Incorrect key'; + +const getSuggestions = (value: string, vocabulary: Vocabulary) => + getTagsList(vocabulary).filter(tag => tag.includes(value) && tag !== value); + +const getTagsList = ({ tags }: Vocabulary) => + Object.keys(tags); + +const ITEMS_PLACEHOLDER: string[] = []; -- 2.30.2