add: restore colored tag chip
This commit is contained in:
parent
edc6104a09
commit
5670a12910
@ -1,48 +1,15 @@
|
|||||||
import { Chip, colors } from "@mui/material";
|
import * as colors from "@mui/material/colors";
|
||||||
import { ChipTypeMap } from "@mui/material/Chip";
|
import Chip, { ChipTypeMap } from "@mui/material/Chip";
|
||||||
import { emphasize, Theme } from "@mui/material/styles";
|
import { emphasize, styled, Theme, useTheme } from "@mui/material/styles";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { Link as RouterLink } from "react-router-dom";
|
import { Link as RouterLink } from "react-router-dom";
|
||||||
|
|
||||||
type TagChipStyleProp = {
|
type TagChipStyleProp = {
|
||||||
color: string;
|
color: `rgba(${number},${number},${number},${number})` | `#${string}` | 'default';
|
||||||
};
|
};
|
||||||
|
|
||||||
const useTagStyles = (theme: Theme) => ({
|
|
||||||
root: (props: TagChipStyleProp) => ({
|
|
||||||
color: theme.palette.getContrastText(props.color),
|
|
||||||
backgroundColor: props.color,
|
|
||||||
}),
|
|
||||||
clickable: (props: TagChipStyleProp) => ({
|
|
||||||
"&:hover, &:focus": {
|
|
||||||
backgroundColor: emphasize(props.color, 0.08),
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
deletable: {
|
|
||||||
"&:focus": {
|
|
||||||
backgroundColor: (props: TagChipStyleProp) => emphasize(props.color, 0.2),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
outlined: {
|
|
||||||
color: (props: TagChipStyleProp) => props.color,
|
|
||||||
border: (props: TagChipStyleProp) => `1px solid ${props.color}`,
|
|
||||||
"$clickable&:hover, $clickable&:focus, $deletable&:focus": {
|
|
||||||
// backgroundColor:(props:TagChipStyleProp)=> (props.color,theme.palette.action.hoverOpacity),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
icon: {
|
|
||||||
color: "inherit",
|
|
||||||
},
|
|
||||||
deleteIcon: {
|
|
||||||
// color:(props:TagChipStyleProp)=> (theme.palette.getContrastText(props.color),0.7),
|
|
||||||
"&:hover, &:active": {
|
|
||||||
color: (props: TagChipStyleProp) => theme.palette.getContrastText(props.color),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const { blue, pink } = colors;
|
const { blue, pink } = colors;
|
||||||
const getTagColorName = (tagname: string): string => {
|
const getTagColorName = (tagname: string): TagChipStyleProp['color'] => {
|
||||||
if (tagname.startsWith("female")) {
|
if (tagname.startsWith("female")) {
|
||||||
return pink[600];
|
return pink[600];
|
||||||
} else if (tagname.startsWith("male")) {
|
} else if (tagname.startsWith("male")) {
|
||||||
@ -57,8 +24,21 @@ type ColorChipProp = Omit<ChipTypeMap["props"], "color"> & TagChipStyleProp & {
|
|||||||
|
|
||||||
export const ColorChip = (props: ColorChipProp) => {
|
export const ColorChip = (props: ColorChipProp) => {
|
||||||
const { color, ...rest } = props;
|
const { color, ...rest } = props;
|
||||||
// const classes = useTagStyles({color : color !== "default" ? color : "#000"});
|
const theme = useTheme();
|
||||||
return <Chip color="default" {...rest}></Chip>;
|
|
||||||
|
let newcolor = color;
|
||||||
|
if (color === "default"){
|
||||||
|
newcolor = "#ebebeb";
|
||||||
|
}
|
||||||
|
return <Chip
|
||||||
|
sx={{
|
||||||
|
color: theme.palette.getContrastText(newcolor),
|
||||||
|
backgroundColor: newcolor,
|
||||||
|
["&:hover, &:focus"]: {
|
||||||
|
backgroundColor: emphasize(newcolor, 0.08),
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
{...rest}></Chip>;
|
||||||
};
|
};
|
||||||
|
|
||||||
type TagChipProp = Omit<ChipTypeMap["props"], "color"> & {
|
type TagChipProp = Omit<ChipTypeMap["props"], "color"> & {
|
||||||
@ -67,29 +47,32 @@ type TagChipProp = Omit<ChipTypeMap["props"], "color"> & {
|
|||||||
|
|
||||||
export const TagChip = (props: TagChipProp) => {
|
export const TagChip = (props: TagChipProp) => {
|
||||||
const { tagname, label, clickable, ...rest } = props;
|
const { tagname, label, clickable, ...rest } = props;
|
||||||
let newlabel: string | undefined = undefined;
|
const colorName = getTagColorName(tagname);
|
||||||
|
|
||||||
|
let newlabel: React.ReactNode = label;
|
||||||
if (typeof label === "string") {
|
if (typeof label === "string") {
|
||||||
if (label.startsWith("female:")) {
|
const female = "female:";
|
||||||
newlabel = "♀ " + label.slice(7);
|
const male = "male:";
|
||||||
} else if (label.startsWith("male:")) {
|
if (label.startsWith(female)) {
|
||||||
newlabel = "♂ " + label.slice(5);
|
newlabel = "♀ " + label.slice(female.length);
|
||||||
|
} else if (label.startsWith(male)) {
|
||||||
|
newlabel = "♂ " + label.slice(male.length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const inner = clickable
|
const inner = clickable
|
||||||
? (
|
? (
|
||||||
<ColorChip
|
<ColorChip
|
||||||
color={getTagColorName(tagname)}
|
color={colorName}
|
||||||
clickable={clickable}
|
clickable={clickable}
|
||||||
label={newlabel ?? label}
|
label={newlabel ?? label}
|
||||||
{...rest}
|
{...rest}
|
||||||
component={RouterLink}
|
component={RouterLink}
|
||||||
to={`/search?allow_tag=${tagname}`}
|
to={`/search?allow_tag=${tagname}`}
|
||||||
>
|
/>
|
||||||
</ColorChip>
|
|
||||||
)
|
)
|
||||||
: (
|
: (
|
||||||
<ColorChip color={getTagColorName(tagname)} clickable={clickable} label={newlabel ?? label} {...rest}>
|
<ColorChip color={colorName} clickable={clickable} label={newlabel ?? label} {...rest}/>
|
||||||
</ColorChip>
|
|
||||||
);
|
);
|
||||||
return inner;
|
return inner;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user