Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[M2-5961] Feature: SliderRows #432

Merged
merged 9 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/abstract/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export const supportableResponseTypes = [
'audioPlayer',
'multiSelectRows',
'singleSelectRows',
'sliderRows',
];
49 changes: 46 additions & 3 deletions src/entities/activity/lib/types/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import { ConditionalLogic } from '~/shared/api';
export type DefaultAnswer = Array<string>;
export type MatrixMultiSelectAnswer = Array<Array<string | null>>;
export type SingleMultiSelectAnswer = Array<string | null>;
export type SliderRowsAnswer = Array<number | null>;

export type Answer = DefaultAnswer | MatrixMultiSelectAnswer | SingleMultiSelectAnswer;
export type Answer =
| DefaultAnswer
| MatrixMultiSelectAnswer
| SingleMultiSelectAnswer
| SliderRowsAnswer;

export type ActivityItemType =
| 'text'
Expand Down Expand Up @@ -79,7 +84,8 @@ export type Config =
| TimeItemItemConfig
| TimeRangeItemConfig
| AudioPlayerItemConfig
| MultiSelectionRowsItemConfig;
| MultiSelectionRowsItemConfig
| SliderRowsItemConfig;

export type ResponseValues =
| EmptyResponseValues
Expand All @@ -88,7 +94,8 @@ export type ResponseValues =
| SliderValues
| SelectorValues
| AudioPlayerItemValues
| MultiSelectionRowsItemResponseValues;
| MultiSelectionRowsItemResponseValues
| SliderRowsItemResponseValues;

export type EmptyResponseValues = null;

Expand Down Expand Up @@ -334,3 +341,39 @@ export type SingleSelectionRowsItemResponseValues = {
options: Array<MatrixSelectOption>;
dataMatrix: DataMatrix;
};

export interface SliderRowsItem extends ActivityItemBase {
responseType: 'sliderRows';
config: SliderRowsItemConfig;
responseValues: SliderRowsItemResponseValues;
answer: SliderRowsAnswer;
}

export type SliderRowsItemConfig = ButtonsConfig &
TimerConfig & {
addScores: boolean;
setAlerts: boolean;
};

export type SliderRowsItemResponseValues = {
rows: SliderRows;
};

export type SliderAlerts = Array<{
value: number;
minValue: number;
maxValue: number;
alert: string;
}> | null;

export type SliderRows = Array<{
id: string;
label: string;
minLabel: string | null;
maxLabel: string | null;
minValue: number;
maxValue: number;
minImage: string | null;
maxImage: string | null;
alerts: SliderAlerts;
}>;
8 changes: 4 additions & 4 deletions src/entities/activity/ui/AdditionalTextResponse.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import TextField from '@mui/material/TextField';
import { BaseTextInput } from '~/shared/ui';

type AdditionalTextResponseProps = {
type Props = {
value: string;
onValueChange: (value: string) => void;
};

export const AdditionalTextResponse = ({ value, onValueChange }: AdditionalTextResponseProps) => {
export const AdditionalTextResponse = ({ value, onValueChange }: Props) => {
return (
<TextField
<BaseTextInput
fullWidth
size="small"
value={value}
Expand Down
3 changes: 1 addition & 2 deletions src/entities/activity/ui/ItemCardButtons.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Box from '@mui/material/Box';

import { Theme } from '~/shared/constants';
import { Box } from '~/shared/ui';
import { BaseButton } from '~/shared/ui';
import { useCustomMediaQuery } from '~/shared/utils';

Expand Down
3 changes: 1 addition & 2 deletions src/entities/activity/ui/items/AudioPlayerItem.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { useCallback } from 'react';

import Box from '@mui/material/Box';

import { AudioPlayerItem as AudioPlayerItemType } from '../../lib/types/item';

import { Box } from '~/shared/ui';
import { AudioPlayerItemBase } from '~/shared/ui';
import { AudioPlayerFinished } from '~/shared/ui/Items/AudioPlayer/lib';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useMemo } from 'react';

import Box from '@mui/material/Box';

import { Box } from '~/shared/ui';
import {
CheckboxItem,
CustomTooltip,
Expand Down
3 changes: 1 addition & 2 deletions src/entities/activity/ui/items/CheckboxItem/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { useMemo } from 'react';

import Box from '@mui/material/Box';

import { CheckboxItemOption } from './CheckboxItemOption';
import { CheckboxItem as CheckboxItemType } from '../../../lib/types/item';

import { Box } from '~/shared/ui';
import { randomizeArray, splitList, useCustomMediaQuery } from '~/shared/utils';

type Props = {
Expand Down
3 changes: 1 addition & 2 deletions src/entities/activity/ui/items/DateItem.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Box from '@mui/material/Box';

import { Box } from '~/shared/ui';
import { DateItemBase } from '~/shared/ui';

type Props = {
Expand Down
4 changes: 4 additions & 0 deletions src/entities/activity/ui/items/ItemPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CheckboxItem } from './CheckboxItem';
import { DateItem } from './DateItem';
import { MatrixCheckboxItem } from './Matrix/MatrixMultiSelectItem';
import { MatrixRadioItem } from './Matrix/MatrixSingleSelectItem';
import { SliderRows } from './Matrix/Slider';
import { RadioItem } from './RadioItem';
import { SelectorItem } from './SelectorItem';
import { SliderItem } from './SliderItem';
Expand Down Expand Up @@ -114,6 +115,9 @@ export const ItemPicker = ({ item, onValueChange, isDisabled, replaceText }: Ite
/>
);

case 'sliderRows':
return <SliderRows item={item} values={item.answer} onValueChange={onValueChange} />;

default:
return <></>;
}
Expand Down
3 changes: 1 addition & 2 deletions src/entities/activity/ui/items/Matrix/AxisListItem.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Box from '@mui/material/Box';

import { Box } from '~/shared/ui';
import { AvatarBase, CustomTooltip, Text } from '~/shared/ui';
import { useCustomMediaQuery } from '~/shared/utils';

Expand Down
3 changes: 1 addition & 2 deletions src/entities/activity/ui/items/Matrix/MatrixCell.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { PropsWithChildren } from 'react';

import Box from '@mui/material/Box';

import { Box } from '~/shared/ui';
import { useCustomMediaQuery } from '~/shared/utils';

type Props = PropsWithChildren<{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Box from '@mui/material/Box';

import { MatrixCell } from '../MatrixCell';

import { Box } from '~/shared/ui';
import { CheckboxItem, SelectBaseBox } from '~/shared/ui';
import { useCustomMediaQuery } from '~/shared/utils';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Box from '@mui/material/Box';

import { CheckboxButton } from './CheckboxButton';
import { MatrixMultiSelectAnswer, MatrixSelectOption, MatrixSelectRow } from '../../../../lib';
import { MatrixHeader } from '../MatrixHeader';
import { MatrixRow } from '../MatrixRow';

import { Box } from '~/shared/ui';

type Props = {
options: Array<MatrixSelectOption>;
rows: Array<MatrixSelectRow>;
Expand Down
3 changes: 1 addition & 2 deletions src/entities/activity/ui/items/Matrix/MatrixRow.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { PropsWithChildren } from 'react';

import Box from '@mui/material/Box';

import { AxisItem, AxisListItem } from './AxisListItem';
import { MatrixCell } from './MatrixCell';

import { Theme } from '~/shared/constants';
import { Box } from '~/shared/ui';

type Props = PropsWithChildren<{
isEven: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Box from '@mui/material/Box';

import { MatrixCell } from '../MatrixCell';

import { Box } from '~/shared/ui';
import { RadioOption, SelectBaseBox } from '~/shared/ui';

type Props = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Box from '@mui/material/Box';

import { RadioButton } from './RadioButton';
import { MatrixSelectOption, MatrixSelectRow, SingleMultiSelectAnswer } from '../../../../lib';
import { MatrixHeader } from '../MatrixHeader';
import { MatrixRow } from '../MatrixRow';

import { Box } from '~/shared/ui';

type Props = {
options: Array<MatrixSelectOption>;
rows: Array<MatrixSelectRow>;
Expand Down
61 changes: 61 additions & 0 deletions src/entities/activity/ui/items/Matrix/Slider/SliderRow.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Theme } from '~/shared/constants';
import { Box } from '~/shared/ui';
import { SliderItemBase, Text } from '~/shared/ui';

type Props = {
label: string;

value: number;
minValue: number;
maxValue: number;

minLabel: string | null;
minImage: string | null;

maxLabel: string | null;
maxImage: string | null;

isEven: boolean;

onChange: (value: string) => void;
};

export const SliderRow = ({
label,
value,
minImage,
minLabel,
minValue,
maxImage,
maxLabel,
maxValue,
onChange,
isEven,
}: Props) => {
return (
<Box bgcolor={isEven ? Theme.colors.light.surface3 : undefined} padding="50px">
<Text
variant="body1"
fontSize="20px"
fontWeight="400"
padding="0px 0px 50px 0px" // Bottom padding
sx={{ textAlign: 'center' }}
>
{label}
</Text>

<SliderItemBase
value={String(value)}
minValue={minValue}
minLabel={minLabel}
minImage={minImage}
maxValue={maxValue}
maxLabel={maxLabel}
maxImage={maxImage}
onChange={onChange}
showStickLabel={true}
showStickMarks={true}
/>
</Box>
);
};
54 changes: 54 additions & 0 deletions src/entities/activity/ui/items/Matrix/Slider/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { useCallback } from 'react';

import { SliderRow } from './SliderRow';
import { SliderRowsAnswer, SliderRowsItem } from '../../../../lib';

import { Box } from '~/shared/ui';

type Props = {
item: SliderRowsItem;
values: SliderRowsAnswer;
onValueChange: (value: SliderRowsAnswer) => void;
};

export const SliderRows = (props: Props) => {
const onHandleValueChange = useCallback(
(value: string, index: number) => {
if (props.values.length === 0) {
props.onValueChange(
Array.from({ length: props.item.responseValues.rows.length }, () => null),
);
return;
}

const newValues = [...props.values];
newValues[index] = Number(value);
return props.onValueChange(newValues);
},
[props],
);

return (
<Box display="flex" flexDirection="column">
{props.item.responseValues.rows.map((row, index) => {
const value = props.values[index];

return (
<SliderRow
key={row.id}
label={row.label}
value={value ? value : row.minValue}
minValue={row.minValue}
minLabel={row.minLabel}
minImage={row.minImage}
maxValue={row.maxValue}
maxLabel={row.maxLabel}
maxImage={row.maxImage}
onChange={(value: string) => onHandleValueChange(value, index)}
isEven={index % 2 === 0}
/>
);
})}
</Box>
);
};
3 changes: 1 addition & 2 deletions src/entities/activity/ui/items/RadioItem/RadioItemOption.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useMemo } from 'react';

import Box from '@mui/material/Box';

import { Box } from '~/shared/ui';
import {
CustomTooltip,
RadioOption,
Expand Down
2 changes: 1 addition & 1 deletion src/entities/activity/ui/items/RadioItem/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useMemo } from 'react';

import Box from '@mui/material/Box';
import RadioGroup from '@mui/material/RadioGroup';

import { RadioItemOption } from './RadioItemOption';
import { RadioItem as RadioItemType } from '../../../lib';

import { Box } from '~/shared/ui';
import { randomizeArray, splitList, useCustomMediaQuery } from '~/shared/utils';

type RadioItemProps = {
Expand Down
3 changes: 1 addition & 2 deletions src/entities/activity/ui/items/SliderItem.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Box from '@mui/material/Box';

import { SliderItem as SliderItemType } from '../../lib';

import { Box } from '~/shared/ui';
import { SliderItemBase } from '~/shared/ui';

type SliderItemProps = {
Expand Down
3 changes: 1 addition & 2 deletions src/entities/activity/ui/items/TimeItem.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Box from '@mui/material/Box';

import { Box } from '~/shared/ui';
import { TimeItemBase } from '~/shared/ui';

type Props = {
Expand Down
Loading
Loading