Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dropdown option to tag widget #92

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions src/editor/widgets/tag/TagWidget.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CSSTransition } from 'react-transition-group';
import { CloseIcon } from '../../../Icons';
import i18n from '../../../i18n';
import Autocomplete from '../Autocomplete';
import Select from 'react-select';

const getDraftTag = existingDraft =>
existingDraft ? existingDraft : {
Expand All @@ -21,6 +22,22 @@ const TagWidget = props => {

// All except draft tag
const tags = all.filter(b => b != draftTag);
const isDropdown = props.dropdown && props.vocabulary ? true : false;
if (isDropdown){
var mappedTags = [];
var selectedTag = {};
for (var vo=0; vo<props.vocabulary.length; vo++){
const vocab = props.vocabulary[vo];
const label = vocab.label ? vocab.label : vocab;
const uri = vocab.uri ? vocab.uri : '';
const optionDict = {'label': label, 'value': label, 'uri': uri}
mappedTags.push(optionDict);
if (tags.length > 0 && tags[0]['value'] == label){
selectedTag = optionDict;
}
}
}


const [ showDelete, setShowDelete ] = useState(false);

Expand Down Expand Up @@ -53,17 +70,19 @@ const TagWidget = props => {
const { draft, ...toSubmit } = tag.label ?
{ ...draftTag, value: tag.label, source: tag.uri } :
{ ...draftTag, value: tag };

if (draftTag.value.trim().length === 0) {
const dropdowncheck = isDropdown ? tags.length == 0 : true;
if (draftTag.value.trim().length === 0 && dropdowncheck) {
props.onAppendBody(toSubmit);
} else if (isDropdown) {
props.onUpdateBody(tags[0], toSubmit);
} else {
props.onUpdateBody(draftTag, toSubmit);
}
}

return (
<div className="r6o-widget r6o-tag">
{ tags.length > 0 &&
{ tags.length > 0 && !isDropdown &&
<ul className="r6o-taglist">
{ tags.map(tag =>
<li key={tag.value} onClick={toggle(tag.value)}>
Expand All @@ -82,8 +101,14 @@ const TagWidget = props => {
)}
</ul>
}

{!props.readOnly &&
{!props.readOnly && isDropdown &&
<Select
onChange={onSubmit}
options={mappedTags}
value={selectedTag}
/>
}
{!props.readOnly && !isDropdown &&
<Autocomplete
focus={props.focus}
placeholder={i18n.t('Add tag...')}
Expand Down