mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 18:44:54 +08:00
Grafana-UI: Support theme color in TagsInput (#102293)
* Grafana-UI: Support theme color in TagsInput * improve variable names * rename prop + rejigger styles * reword comment --------- Co-authored-by: joshhunt <josh.hunt@grafana.com>
This commit is contained in:

committed by
GitHub

parent
9bc4fbe900
commit
e1ec9bddbd
@ -1,4 +1,4 @@
|
|||||||
import { css } from '@emotion/css';
|
import { css, cx } from '@emotion/css';
|
||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
|
|
||||||
import { GrafanaTheme2 } from '@grafana/data';
|
import { GrafanaTheme2 } from '@grafana/data';
|
||||||
@ -12,18 +12,30 @@ interface Props {
|
|||||||
name: string;
|
name: string;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
onRemove: (tag: string) => void;
|
onRemove: (tag: string) => void;
|
||||||
|
|
||||||
|
/** Colours the tags 'randomly' based on the name. Defaults to true */
|
||||||
|
autoColors?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
* Only used internally by TagsInput
|
* Only used internally by TagsInput
|
||||||
* */
|
* */
|
||||||
export const TagItem = ({ name, disabled, onRemove }: Props) => {
|
export const TagItem = ({ name, disabled, onRemove, autoColors = true }: Props) => {
|
||||||
const { color, borderColor } = useMemo(() => getTagColorsFromName(name), [name]);
|
|
||||||
const styles = useStyles2(getStyles);
|
const styles = useStyles2(getStyles);
|
||||||
|
|
||||||
|
// If configured, use random colors based on name.
|
||||||
|
// Otherwise, a default class name will be applied to the tag.
|
||||||
|
const tagStyle = useMemo(() => {
|
||||||
|
if (autoColors) {
|
||||||
|
const { color, borderColor } = getTagColorsFromName(name);
|
||||||
|
return { backgroundColor: color, borderColor };
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
}, [name, autoColors]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<li className={styles.itemStyle} style={{ backgroundColor: color, borderColor }}>
|
<li className={cx(styles.itemStyle, !tagStyle && styles.defaultTagColor)} style={tagStyle}>
|
||||||
<span className={styles.nameStyle}>{name}</span>
|
<span className={styles.nameStyle}>{name}</span>
|
||||||
<IconButton
|
<IconButton
|
||||||
name="times"
|
name="times"
|
||||||
@ -47,7 +59,6 @@ const getStyles = (theme: GrafanaTheme2) => {
|
|||||||
alignItems: 'center',
|
alignItems: 'center',
|
||||||
height: `${height}px`,
|
height: `${height}px`,
|
||||||
lineHeight: `${height - 2}px`,
|
lineHeight: `${height - 2}px`,
|
||||||
color: '#fff',
|
|
||||||
borderWidth: '1px',
|
borderWidth: '1px',
|
||||||
borderStyle: 'solid',
|
borderStyle: 'solid',
|
||||||
borderRadius: theme.shape.radius.default,
|
borderRadius: theme.shape.radius.default,
|
||||||
@ -56,6 +67,12 @@ const getStyles = (theme: GrafanaTheme2) => {
|
|||||||
textShadow: 'none',
|
textShadow: 'none',
|
||||||
fontWeight: 500,
|
fontWeight: 500,
|
||||||
fontSize: theme.typography.size.sm,
|
fontSize: theme.typography.size.sm,
|
||||||
|
color: '#fff',
|
||||||
|
}),
|
||||||
|
defaultTagColor: css({
|
||||||
|
backgroundColor: theme.colors.background.secondary,
|
||||||
|
borderColor: theme.components.input.borderColor,
|
||||||
|
color: theme.colors.text.primary,
|
||||||
}),
|
}),
|
||||||
nameStyle: css({
|
nameStyle: css({
|
||||||
maxWidth: '25ch',
|
maxWidth: '25ch',
|
||||||
|
@ -25,6 +25,8 @@ export interface Props {
|
|||||||
addOnBlur?: boolean;
|
addOnBlur?: boolean;
|
||||||
/** Toggle invalid state */
|
/** Toggle invalid state */
|
||||||
invalid?: boolean;
|
invalid?: boolean;
|
||||||
|
/** Colours the tags 'randomly' based on the name. Defaults to true */
|
||||||
|
autoColors?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const TagsInput = ({
|
export const TagsInput = ({
|
||||||
@ -37,6 +39,7 @@ export const TagsInput = ({
|
|||||||
addOnBlur,
|
addOnBlur,
|
||||||
invalid,
|
invalid,
|
||||||
id,
|
id,
|
||||||
|
autoColors = true,
|
||||||
}: Props) => {
|
}: Props) => {
|
||||||
const [newTagName, setNewTagName] = useState('');
|
const [newTagName, setNewTagName] = useState('');
|
||||||
const styles = useStyles2(getStyles);
|
const styles = useStyles2(getStyles);
|
||||||
@ -96,7 +99,7 @@ export const TagsInput = ({
|
|||||||
{tags?.length > 0 && (
|
{tags?.length > 0 && (
|
||||||
<ul className={styles.tags}>
|
<ul className={styles.tags}>
|
||||||
{tags.map((tag) => (
|
{tags.map((tag) => (
|
||||||
<TagItem key={tag} name={tag} onRemove={onRemove} disabled={disabled} />
|
<TagItem key={tag} name={tag} onRemove={onRemove} disabled={disabled} autoColors={autoColors} />
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
)}
|
)}
|
||||||
|
Reference in New Issue
Block a user