Skip to content

Commit 2063d55

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 5633c4b commit 2063d55

File tree

1 file changed

+61
-43
lines changed

1 file changed

+61
-43
lines changed

client/js/templates/Source.jsx

Lines changed: 61 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -287,54 +287,61 @@ ColorBox.propTypes = {
287287
color: nullable(PropTypes.string).isRequired,
288288
};
289289

290+
function mkTag(tagInfo) {
291+
function Tag({ classNames, tag, ...tagProps }) {
292+
return (
293+
<button
294+
type="button"
295+
className={classNames.tag}
296+
{...tagProps}
297+
>
298+
<ColorBox color={tagInfo[tag.label]?.color ?? null} />
299+
{' '}
300+
<span className={classNames.tagName}>{tag.label}</span>
301+
</button>
302+
);
303+
}
290304

291-
function Tag({ classNames, tag, ...tagProps }) {
292-
return (
293-
<button
294-
type="button"
295-
className={classNames.tag}
296-
{...tagProps}
297-
>
298-
<ColorBox color={tag.color ?? null} />
299-
{' '}
300-
<span className={classNames.tagName}>{tag.label}</span>
301-
</button>
302-
);
305+
Tag.propTypes = {
306+
classNames: PropTypes.object.isRequired,
307+
tag: PropTypes.object.isRequired,
308+
tagProps: PropTypes.object.isRequired,
309+
'aria-disabled': PropTypes.bool.isRequired,
310+
title: PropTypes.string.isRequired,
311+
onClick: PropTypes.func.isRequired,
312+
};
313+
314+
return Tag;
303315
}
304316

305-
Tag.propTypes = {
306-
classNames: PropTypes.object.isRequired,
307-
tag: PropTypes.object.isRequired,
308-
tagProps: PropTypes.object.isRequired,
309-
'aria-disabled': PropTypes.bool.isRequired,
310-
title: PropTypes.string.isRequired,
311-
onClick: PropTypes.func.isRequired,
312-
};
313317

318+
function mkTagOption(tagInfo) {
319+
function TagOption({ children, classNames, option, ...optionProps }) {
320+
const classes = [
321+
classNames.option,
322+
option.active ? 'is-active' : '',
323+
option.selected ? 'is-selected' : '',
324+
];
325+
326+
return (
327+
<div className={classes.join(' ')} {...optionProps}>
328+
<ColorBox color={tagInfo[option.label]?.color ?? null} />
329+
{' '}
330+
{children}
331+
</div>
332+
);
333+
}
314334

315-
function TagOption({ children, classNames, option, ...optionProps }) {
316-
const classes = [
317-
classNames.option,
318-
option.active ? 'is-active' : '',
319-
option.selected ? 'is-selected' : '',
320-
];
335+
TagOption.propTypes = {
336+
classNames: PropTypes.object.isRequired,
337+
tag: PropTypes.object.isRequired,
338+
children: PropTypes.any.isRequired,
339+
// TODO: Add extra proptypes.
340+
};
321341

322-
return (
323-
<div className={classes.join(' ')} {...optionProps}>
324-
<ColorBox color={option.color ?? null} />
325-
{' '}
326-
{children}
327-
</div>
328-
);
342+
return TagOption;
329343
}
330344

331-
TagOption.propTypes = {
332-
classNames: PropTypes.object.isRequired,
333-
tag: PropTypes.object.isRequired,
334-
children: PropTypes.any.isRequired,
335-
// TODO: Add extra proptypes.
336-
};
337-
338345

339346
const reactTagsClassNames = {
340347
root: 'react-tags',
@@ -487,7 +494,7 @@ function SourceEditForm({
487494
);
488495

489496
const tagSuggestions = useMemo(
490-
() => Object.entries(tagInfo).map(([label, { id, color }]) => ({ value: id, label, color })),
497+
() => Object.entries(tagInfo).map(([label, { id }]) => ({ value: id, label })),
491498
[tagInfo]
492499
);
493500

@@ -530,6 +537,17 @@ function SourceEditForm({
530537

531538
const reactTags = useRef();
532539

540+
const {
541+
tagComponent,
542+
tagOptionComponent,
543+
} = useMemo(
544+
() => ({
545+
tagComponent: mkTag(tagInfo),
546+
tagOptionComponent: mkTagOption(tagInfo),
547+
}),
548+
[tagInfo]
549+
);
550+
533551
return (
534552
<form>
535553
<ul className="source-edit-form">
@@ -575,8 +593,8 @@ function SourceEditForm({
575593
deleteButtonText={_('source_tag_remove_button_label')}
576594
// classNames={reactTagsClassNames}
577595
delimiterKeys={['Enter', 'Tab', ',']}
578-
renderTag={Tag}
579-
renderOption={TagOption}
596+
renderTag={tagComponent}
597+
renderOption={tagOptionComponent}
580598
/>
581599
{sourceErrors['tags'] ? (
582600
<span className="error">{sourceErrors['tags']}</span>

0 commit comments

Comments
 (0)