// SPDX-License-Identifier: AGPL-3.0
import * as React from 'react';
-import { Input as MuiInput, Chip as MuiChip, Popper as MuiPopper, Paper as MuiPaper, FormControl, InputLabel, StyleRulesCallback, withStyles, RootRef, ListItemText, ListItem, List, FormHelperText } from '@material-ui/core';
+import {
+ Input as MuiInput,
+ Chip as MuiChip,
+ Popper as MuiPopper,
+ Paper as MuiPaper,
+ FormControl, InputLabel, StyleRulesCallback, withStyles, RootRef, ListItemText, ListItem, List, FormHelperText
+} from '@material-ui/core';
import { PopperProps } from '@material-ui/core/Popper';
import { WithStyles } from '@material-ui/core/styles';
import { noop } from 'lodash';
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-selectors';
() =>
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),
-});
+export const handleSelect = ({ onChange }: WrappedFieldInputProps) => {
+ return (item:PropFieldSuggestion) => {
+ onChange(item.id);
+ };
+};
+
+export const buildProps = ({ input, meta }: WrappedFieldProps) => {
+ return {
+ value: input.value,
+ onChange: input.onChange,
+ onBlur: handleBlur(input),
+ items: ITEMS_PLACEHOLDER,
+ onSelect: handleSelect(input),
+ renderSuggestion: (item:PropFieldSuggestion) => item.label,
+ error: hasError(meta),
+ helperText: getErrorMsg(meta),
+ };
+};
+
+export interface PropFieldSuggestion {
+ "id": string;
+ "label": string;
+}
const getValidation = memoize(
(vocabulary: Vocabulary) =>
- vocabulary.strict
+ vocabulary.strict_tags
? [...TAG_KEY_VALIDATION, matchTags(vocabulary)]
: TAG_KEY_VALIDATION);
const matchTags = (vocabulary: Vocabulary) =>
(value: string) =>
- getTagsList(vocabulary).find(tag => tag.includes(value))
+ getTagsList(vocabulary).find(tag => tag.id.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);
+ getTagsList(vocabulary).filter(tag => tag.label.includes(value) && tag.label !== value);
+
+const getTagsList = ({ tags }: Vocabulary) => {
+ const ret = tags && Object.keys(tags)
+ ? Object.keys(tags).map(
+ tagID => tags[tagID].labels
+ ? {"id": tagID, "label": tags[tagID].labels[0].label}
+ : {"id": tagID, "label": tagID})
+ : [];
+ return ret;
+};
const matchTagValues = ({ vocabulary, propertyKey }: PropertyValueFieldProps) =>
(value: string) =>
- getTagValues(propertyKey, vocabulary).find(v => v.includes(value))
+ getTagValues(propertyKey, vocabulary).find(v => v.id.includes(value))
? undefined
: 'Incorrect value';
-const getSuggestions = (value: string, tagName: string, vocabulary: Vocabulary) =>
- getTagValues(tagName, vocabulary).filter(v => v.includes(value) && v !== value);
+const getSuggestions = (value: string, tagKey: string, vocabulary: Vocabulary) =>
+ getTagValues(tagKey, vocabulary).filter(v => v.label.includes(value) && v.label !== value);
-const isStrictTag = (tagName: string, vocabulary: Vocabulary) => {
- const tag = vocabulary.tags[tagName];
+const isStrictTag = (tagKey: string, vocabulary: Vocabulary) => {
+ const tag = vocabulary.tags[tagKey];
return tag ? tag.strict : false;
};
-const getTagValues = (tagName: string, vocabulary: Vocabulary) => {
- const tag = vocabulary.tags[tagName];
- return tag && tag.values ? tag.values : [];
+const getTagValues = (tagKey: string, vocabulary: Vocabulary) => {
+ const tag = vocabulary.tags[tagKey];
+ const ret = tag && tag.values
+ ? Object.keys(tag.values).map(
+ tagValueID => tag.values![tagValueID].labels
+ ? {"id": tagValueID, "label": tag.values![tagValueID].labels[0].label}
+ : {"id": tagValueID, "label": tagValueID})
+ : [];
+ return ret;
};