Skip to content

Commit 3691087

Browse files
committed
client: Use color from top-level tags in source editor tag input
This will make tags added to a source have access to colour despite i-like-robots/react-tags#260 not being merged. Additionally, this will also update the colors when the tags are updated.
1 parent 5c7b4b7 commit 3691087

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

assets/js/templates/Source.jsx

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -290,28 +290,31 @@ ColorBox.propTypes = {
290290
color: nullable(PropTypes.string).isRequired,
291291
};
292292

293+
function mkTag(tagInfo) {
294+
function Tag({ classNames, removeButtonText, onDelete, tag }) {
295+
return (
296+
<button
297+
type="button"
298+
className={classNames.selectedTag}
299+
title={removeButtonText}
300+
onClick={onDelete}
301+
>
302+
<ColorBox color={tagInfo[tag.name]?.color ?? null} />
303+
{' '}
304+
<span className={classNames.selectedTagName}>{tag.name}</span>
305+
</button>
306+
);
307+
}
293308

294-
function Tag({ classNames, removeButtonText, onDelete, tag }) {
295-
return (
296-
<button
297-
type="button"
298-
className={classNames.selectedTag}
299-
title={removeButtonText}
300-
onClick={onDelete}
301-
>
302-
<ColorBox color={tag.color ?? null} />
303-
{' '}
304-
<span className={classNames.selectedTagName}>{tag.name}</span>
305-
</button>
306-
);
307-
}
309+
Tag.propTypes = {
310+
classNames: PropTypes.object.isRequired,
311+
removeButtonText: PropTypes.string.isRequired,
312+
onDelete: PropTypes.func.isRequired,
313+
tag: PropTypes.object.isRequired,
314+
};
308315

309-
Tag.propTypes = {
310-
classNames: PropTypes.object.isRequired,
311-
removeButtonText: PropTypes.string.isRequired,
312-
onDelete: PropTypes.func.isRequired,
313-
tag: PropTypes.object.isRequired,
314-
};
316+
return Tag;
317+
}
315318

316319

317320
const reactTagsClassNames = {
@@ -508,6 +511,8 @@ function SourceEditForm({
508511

509512
const reactTags = useRef();
510513

514+
const tagComponent = useMemo(() => mkTag(tagInfo), [tagInfo]);
515+
511516
return (
512517
<form>
513518
<ul className="source-edit-form">
@@ -552,7 +557,7 @@ function SourceEditForm({
552557
removeButtonText={_('source_tag_remove_button_label')}
553558
classNames={reactTagsClassNames}
554559
delimiters={['Enter', 'Tab', ',']}
555-
tagComponent={Tag}
560+
tagComponent={tagComponent}
556561
/>
557562
{sourceErrors['tags'] ? (
558563
<span className="error">{sourceErrors['tags']}</span>

0 commit comments

Comments
 (0)