feat: Select cancel vote

This commit is contained in:
IlyaDoronin 2023-12-14 15:07:19 +03:00
parent a6bd21d228
commit 2684612675
4 changed files with 37 additions and 12 deletions

@ -58,7 +58,7 @@ export default function SettingDropDown({ question }: SettingDropDownProps) {
maxWidth: isFigmaTablte ? "297px" : "360px", maxWidth: isFigmaTablte ? "297px" : "360px",
}} }}
> >
<Typography {/* <Typography
sx={{ sx={{
height: isMobile ? "18px" : "auto", height: isMobile ? "18px" : "auto",
fontWeight: "500", fontWeight: "500",
@ -79,7 +79,7 @@ export default function SettingDropDown({ question }: SettingDropDownProps) {
question.content.multi = target.checked; question.content.multi = target.checked;
}) })
} }
/> /> */}
<Box <Box
sx={{ sx={{
display: isMobile ? "none" : "block", display: isMobile ? "none" : "block",

@ -3,6 +3,7 @@ import {
Select as MuiSelect, Select as MuiSelect,
MenuItem, MenuItem,
FormControl, FormControl,
Typography,
useTheme, useTheme,
} from "@mui/material"; } from "@mui/material";
@ -16,6 +17,7 @@ type SelectProps = {
empty?: boolean; empty?: boolean;
onChange?: (item: string, num: number) => void; onChange?: (item: string, num: number) => void;
sx?: SxProps; sx?: SxProps;
placeholder?: string;
}; };
export const Select = ({ export const Select = ({
@ -24,6 +26,7 @@ export const Select = ({
empty, empty,
onChange, onChange,
sx, sx,
placeholder = "",
}: SelectProps) => { }: SelectProps) => {
const [activeItem, setActiveItem] = useState<number>( const [activeItem, setActiveItem] = useState<number>(
empty ? -1 : activeItemIndex empty ? -1 : activeItemIndex
@ -35,9 +38,17 @@ export const Select = ({
}, [activeItemIndex]); }, [activeItemIndex]);
const handleChange = (event: SelectChangeEvent) => { const handleChange = (event: SelectChangeEvent) => {
const activeItemIndex = Number(event.target.value); const newItemIndex = Number(event.target.value);
setActiveItem(activeItemIndex);
onChange?.(items[activeItemIndex], activeItemIndex); if (newItemIndex === activeItem) {
setActiveItem(-1);
onChange?.("", -1);
return;
}
setActiveItem(newItemIndex);
onChange?.(items[newItemIndex], newItemIndex);
}; };
return ( return (
@ -47,10 +58,19 @@ export const Select = ({
sx={{ width: "100%", height: "48px", ...sx }} sx={{ width: "100%", height: "48px", ...sx }}
> >
<MuiSelect <MuiSelect
displayEmpty
renderValue={(value) =>
value ? (
items[Number(value)]
) : (
<Typography sx={{ color: theme.palette.grey2.main }}>
{placeholder}
</Typography>
)
}
id="display-select" id="display-select"
variant="outlined" variant="outlined"
value={activeItem === -1 ? "" : String(activeItem)} value={activeItem === -1 ? "" : String(activeItem)}
displayEmpty
onChange={handleChange} onChange={handleChange}
sx={{ sx={{
width: "100%", width: "100%",

@ -3,6 +3,7 @@ import { Box, Typography, Slider, useTheme } from "@mui/material";
import { useDebouncedCallback } from "use-debounce"; import { useDebouncedCallback } from "use-debounce";
import CustomTextField from "@ui_kit/CustomTextField"; import CustomTextField from "@ui_kit/CustomTextField";
import { CustomSlider } from "@ui_kit/CustomSlider";
import { useQuizViewStore, updateAnswer } from "@root/quizView"; import { useQuizViewStore, updateAnswer } from "@root/quizView";
@ -74,10 +75,9 @@ export const Number = ({ currentQuestion }: NumberProps) => {
flexDirection: "column", flexDirection: "column",
width: "100%", width: "100%",
marginTop: "20px", marginTop: "20px",
gap: "30px" gap: "30px",
}} }}
> >
<CustomSlider <CustomSlider
value={ value={
currentQuestion.content.chooseRange currentQuestion.content.chooseRange
@ -152,9 +152,7 @@ export const Number = ({ currentQuestion }: NumberProps) => {
"& .MuiInputBase-input": { textAlign: "center" }, "& .MuiInputBase-input": { textAlign: "center" },
}} }}
/> />
<Typography> <Typography>до</Typography>
до
</Typography>
<CustomTextField <CustomTextField
placeholder="0" placeholder="0"
value={maxRange} value={maxRange}

@ -2,7 +2,7 @@ import { Box, Typography } from "@mui/material";
import { Select as SelectComponent } from "../../../pages/Questions/Select"; import { Select as SelectComponent } from "../../../pages/Questions/Select";
import { useQuizViewStore, updateAnswer } from "@root/quizView"; import { useQuizViewStore, updateAnswer, deleteAnswer } from "@root/quizView";
import type { QuizQuestionSelect } from "../../../model/questionTypes/select"; import type { QuizQuestionSelect } from "../../../model/questionTypes/select";
@ -29,9 +29,16 @@ export const Select = ({ currentQuestion }: SelectProps) => {
}} }}
> >
<SelectComponent <SelectComponent
placeholder={currentQuestion.content.default}
activeItemIndex={answer ? Number(answer) : -1} activeItemIndex={answer ? Number(answer) : -1}
items={currentQuestion.content.variants.map(({ answer }) => answer)} items={currentQuestion.content.variants.map(({ answer }) => answer)}
onChange={(_, value) => { onChange={(_, value) => {
if (value < 0) {
deleteAnswer(currentQuestion.content.id);
return;
}
updateAnswer(currentQuestion.content.id, String(value)); updateAnswer(currentQuestion.content.id, String(value));
}} }}
/> />