Adds support for tagging vocab terms with label + uri

This commit is contained in:
Rainer Simon 2021-09-13 18:13:35 +02:00
parent b681ad6a00
commit e95c98391c
2 changed files with 19 additions and 8 deletions

View File

@ -18,7 +18,9 @@ const Autocomplete = props => {
const onInputValueChange = ({ inputValue }) => { const onInputValueChange = ({ inputValue }) => {
if (inputValue.length > 0) { if (inputValue.length > 0) {
const prefixMatches = props.vocabulary.filter(item => { const prefixMatches = props.vocabulary.filter(item => {
return item.toLowerCase().startsWith(inputValue.toLowerCase()); // Item could be string or { label, uri } tuple
const label = item.label ? item.label : item;
return label.toLowerCase().startsWith(inputValue.toLowerCase());
}); });
setInputItems(prefixMatches); setInputItems(prefixMatches);
@ -39,15 +41,16 @@ const Autocomplete = props => {
} = useCombobox({ } = useCombobox({
items: inputItems, items: inputItems,
onInputValueChange: onInputValueChange, onInputValueChange: onInputValueChange,
onSelectedItemChange: ({ inputValue }) => { onSelectedItemChange: ({ selectedItem }) => {
onSubmit(inputValue); onSubmit(selectedItem);
setInputValue('');
} }
}); });
const onSubmit = inputValue => { const onSubmit = inputValue => {
setInputValue(''); setInputValue('');
if (inputValue.trim().length > 0)
const label = inputValue.label ? inputValue.label : inputValue;
if (label.trim().length > 0)
props.onSubmit(inputValue); props.onSubmit(inputValue);
} }
@ -65,11 +68,16 @@ const Autocomplete = props => {
} }
} }
// This is a horrible hack - need to get rid of downshift altogether!
const inputProps = getInputProps({ onKeyUp });
if (inputProps.value === '[object Object]')
inputProps.value = '';
return ( return (
<div className="r6o-autocomplete" ref={element}> <div className="r6o-autocomplete" ref={element}>
<div {...getComboboxProps()}> <div {...getComboboxProps()}>
<input <input
{...getInputProps({ onKeyUp })} {...inputProps}
placeholder={props.placeholder} /> placeholder={props.placeholder} />
</div> </div>
<ul {...getMenuProps()}> <ul {...getMenuProps()}>
@ -81,7 +89,7 @@ const Autocomplete = props => {
} }
key={`${item}${index}`} key={`${item}${index}`}
{...getItemProps({ item, index })}> {...getItemProps({ item, index })}>
{item} {item.label ? item.label : item}
</li> </li>
))} ))}
</ul> </ul>

View File

@ -50,7 +50,10 @@ const TagWidget = props => {
} }
const onSubmit = tag => { const onSubmit = tag => {
const { draft, ...toSubmit } = { ...draftTag, value: tag }; const { draft, ...toSubmit } = tag.label ?
{ ...draftTag, value: tag.label, source: tag.uri } :
{ ...draftTag, value: tag };
if (draftTag.value.trim().length === 0) { if (draftTag.value.trim().length === 0) {
props.onAppendBody(toSubmit); props.onAppendBody(toSubmit);
} else { } else {