Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { Box, Chip, styled, useMediaQuery } from "@mui/material";
import Slider from "@mui/material/Slider";
import { getSliderColor } from "components/RatingsSlider/getSliderColor";
import SliderLabel from "components/RatingsSlider/SliderLabel";
import { useTranslation } from "i18n";
import { PROFILE } from "i18n/namespaces";
import { theme } from "theme";
interface ColorProps {
sliderColor: string;
}
interface SliderProps {
value: number | undefined;
onChange: (value: number | number[]) => void;
}
const StyledSlider = styled(Slider, {
shouldForwardProp: (prop) => prop !== "sliderColor", // Prevents `color` prop from being passed to the DOM
})<ColorProps>(({ theme, sliderColor }) => ({
'& .MuiSlider-markLabel[data-index="0"]': {
transform: "translateX(0%)",
},
'& .MuiSlider-markLabel[data-index="3"]': {
transform: "translateX(-100%)",
},
// Hide mark labels on mobile
[theme.breakpoints.down("sm")]: {
"& .MuiSlider-markLabel": {
display: "none",
},
marginBottom: theme.spacing(3),
},
height: "0.5rem",
borderRadius: "1.5625rem",
marginTop: theme.spacing(6),
marginBottom: theme.spacing(1),
"& .MuiSlider-track": {
backgroundColor: sliderColor,
height: "0.625rem",
borderRadius: "1.5625rem",
borderColor: sliderColor,
},
"& .MuiSlider-rail": {
height: "0.625rem",
borderRadius: "1.5625rem",
backgroundColor: "transparent",
border: "1px solid black",
},
"& .MuiSlider-thumb": {
height: "1.25rem",
width: "1.25rem",
backgroundColor: sliderColor,
},
"& .MuiSlider-valueLabel": {
left: "calc(-50% + 0.25rem)",
lineHeight: 1.2,
background: "unset",
padding: 0,
width: 32,
height: 32,
borderRadius: "50% 50% 50% 0",
backgroundColor: sliderColor,
transformOrigin: "bottom left",
transform: "translate(50%, -100%) rotate(-45deg) scale(0)",
"&::before": { display: "none" },
"&.MuiSlider-valueLabelOpen": {
transform: "translate(50%, -100%) rotate(-45deg) scale(1)",
},
"& > *": {
transform: "rotate(45deg)",
},
},
"& .MuiSlider-mark": {
display: "none",
},
}));
export default function RatingsSlider({ value, onChange }: SliderProps) {
const { t } = useTranslation(PROFILE);
const isMobile = useMediaQuery(theme.breakpoints.down("sm"));
const marks = [
{
value: 0,
label: t("profile:leave_reference.rating_negative"),
},
{
value: 0.33,
label: t("profile:leave_reference.rating_neutral"),
},
{
value: 0.67,
label: t("profile:leave_reference.rating_positive"),
},
{
value: 1,
label: t("profile:leave_reference.rating_amazing"),
},
];
const getCurrentLabel = (val: number | undefined) => {
Iif (val === undefined) return marks[1].label;
Iif (val <= 0.165) return marks[0].label;
Iif (val <= 0.5) return marks[1].label;
Iif (val <= 0.835) return marks[2].label;
return marks[3].label;
};
return (
<Box>
<StyledSlider
aria-label={t("profile:leave_reference.ratings_slider_label")}
sliderColor={getSliderColor(value)}
value={value ?? marks[1].value}
min={0}
max={1}
step={0.01}
marks={marks}
valueLabelDisplay="on"
valueLabelFormat={() => <SliderLabel value={value} />}
onChange={(event, value) => {
onChange(value);
}}
/>
{isMobile && (
<Box sx={{ display: "flex", justifyContent: "center", mt: 2 }}>
<Chip
label={getCurrentLabel(value)}
sx={{
backgroundColor: getSliderColor(value),
color: "white",
fontWeight: 600,
fontSize: "1rem",
px: 2,
py: 2.5,
}}
/>
</Box>
)}
</Box>
);
}
|