Skip to content

Commit

Permalink
Adds support for tagging vocab terms with label + uri
Browse files Browse the repository at this point in the history
  • Loading branch information
rsimon committed Sep 13, 2021
1 parent b681ad6 commit e95c983
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
22 changes: 15 additions & 7 deletions src/editor/widgets/Autocomplete.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ const Autocomplete = props => {
const onInputValueChange = ({ inputValue }) => {
if (inputValue.length > 0) {
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);
Expand All @@ -39,15 +41,16 @@ const Autocomplete = props => {
} = useCombobox({
items: inputItems,
onInputValueChange: onInputValueChange,
onSelectedItemChange: ({ inputValue }) => {
onSubmit(inputValue);
setInputValue('');
onSelectedItemChange: ({ selectedItem }) => {
onSubmit(selectedItem);
}
});

const onSubmit = inputValue => {
setInputValue('');
if (inputValue.trim().length > 0)

const label = inputValue.label ? inputValue.label : inputValue;
if (label.trim().length > 0)
props.onSubmit(inputValue);
}

Expand All @@ -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 (
<div className="r6o-autocomplete" ref={element}>
<div {...getComboboxProps()}>
<input
{...getInputProps({ onKeyUp })}
{...inputProps}
placeholder={props.placeholder} />
</div>
<ul {...getMenuProps()}>
Expand All @@ -81,7 +89,7 @@ const Autocomplete = props => {
}
key={`${item}${index}`}
{...getItemProps({ item, index })}>
{item}
{item.label ? item.label : item}
</li>
))}
</ul>
Expand Down
5 changes: 4 additions & 1 deletion src/editor/widgets/tag/TagWidget.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ const TagWidget = props => {
}

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) {
props.onAppendBody(toSubmit);
} else {
Expand Down

0 comments on commit e95c983

Please sign in to comment.