-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* The singleSelectRows type was added into supportableResponseTypes * ActivityItemDetailsDTO was extended with SingleSeletPerRow * Create SingleSelectPerRow type for Redux store * Refactor of Matrix elements to make it reusable * Create the MatrixSingleSelect component * Create mapper to answer * Fix mobile version of matrix grid --------- Co-authored-by: Viktor Riabkov <[email protected]>
- Loading branch information
1 parent
9f6fae9
commit 7ebf2ae
Showing
19 changed files
with
334 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,5 @@ export const supportableResponseTypes = [ | |
'timeRange', | ||
'audioPlayer', | ||
'multiSelectRows', | ||
'singleSelectRows', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
40 changes: 40 additions & 0 deletions
40
src/entities/activity/ui/items/Matrix/MatrixMultiSelectItem/CheckboxButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Box from '@mui/material/Box'; | ||
|
||
import { MatrixCell } from '../MatrixCell'; | ||
|
||
import { CheckboxItem, SelectBaseBox } from '~/shared/ui'; | ||
import { useCustomMediaQuery } from '~/shared/utils'; | ||
|
||
type Props = { | ||
id: string; | ||
isChecked: boolean; | ||
text: string; | ||
|
||
onChange: () => void; | ||
}; | ||
|
||
export const CheckboxButton = ({ id, isChecked, text, onChange }: Props) => { | ||
const { lessThanSM } = useCustomMediaQuery(); | ||
|
||
return ( | ||
<Box flex={1} key={id} data-testid="matrix-checkbox-button-container"> | ||
<MatrixCell> | ||
<SelectBaseBox | ||
color={null} | ||
onHandleChange={onChange} | ||
checked={isChecked} | ||
justifyContent="center" | ||
padding={lessThanSM ? '12px 8px' : '16px'} | ||
> | ||
<CheckboxItem | ||
id={id} | ||
name={text} | ||
value={text} | ||
disabled={false} | ||
defaultChecked={isChecked} | ||
/> | ||
</SelectBaseBox> | ||
</MatrixCell> | ||
</Box> | ||
); | ||
}; |
48 changes: 48 additions & 0 deletions
48
src/entities/activity/ui/items/Matrix/MatrixMultiSelectItem/CheckboxGrid.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import Box from '@mui/material/Box'; | ||
|
||
import { CheckboxButton } from './CheckboxButton'; | ||
import { MatrixMultiSelectAnswer, MatrixSelectOption, MatrixSelectRow } from '../../../../lib'; | ||
import { MatrixHeader } from '../MatrixHeader'; | ||
import { MatrixRow } from '../MatrixRow'; | ||
|
||
type Props = { | ||
options: Array<MatrixSelectOption>; | ||
rows: Array<MatrixSelectRow>; | ||
|
||
onChange: (rowIndex: number, optionIndex: number, value: string) => void; | ||
values: MatrixMultiSelectAnswer; | ||
}; | ||
|
||
export const CheckboxGrid = ({ rows, options, onChange, values }: Props) => { | ||
return ( | ||
<Box display="flex" flex={1} flexDirection="column" data-testid="matrix-checkbox-grid"> | ||
<MatrixHeader options={options} /> | ||
|
||
{rows.map((row, rowI) => { | ||
const isEven = rowI % 2 === 0; | ||
|
||
return ( | ||
<MatrixRow | ||
key={row.id} | ||
isEven={isEven} | ||
item={{ id: row.id, imageUrl: row.rowImage, text: row.rowName, tooltip: row.tooltip }} | ||
> | ||
{options.map((option, optionI) => { | ||
const isChecked = Boolean(values[rowI]?.[optionI]); | ||
|
||
return ( | ||
<CheckboxButton | ||
key={option.id} | ||
id={option.id} | ||
text={option.text} | ||
isChecked={isChecked} | ||
onChange={() => onChange(rowI, optionI, option.text)} | ||
/> | ||
); | ||
})} | ||
</MatrixRow> | ||
); | ||
})} | ||
</Box> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
36 changes: 36 additions & 0 deletions
36
src/entities/activity/ui/items/Matrix/MatrixSingleSelectItem/RadioButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Box from '@mui/material/Box'; | ||
|
||
import { MatrixCell } from '../MatrixCell'; | ||
|
||
import { RadioOption, SelectBaseBox } from '~/shared/ui'; | ||
|
||
type Props = { | ||
id: string; | ||
isChecked: boolean; | ||
text: string; | ||
|
||
onChange: () => void; | ||
}; | ||
|
||
export const RadioButton = ({ id, text, isChecked, onChange }: Props) => { | ||
return ( | ||
<Box flex={1} key={id} data-testid="matrix-radio-button-container"> | ||
<MatrixCell> | ||
<SelectBaseBox | ||
color={null} | ||
onHandleChange={onChange} | ||
checked={isChecked} | ||
justifyContent="center" | ||
> | ||
<RadioOption | ||
id={id} | ||
name={text} | ||
value={text} | ||
disabled={false} | ||
defaultChecked={isChecked} | ||
/> | ||
</SelectBaseBox> | ||
</MatrixCell> | ||
</Box> | ||
); | ||
}; |
48 changes: 48 additions & 0 deletions
48
src/entities/activity/ui/items/Matrix/MatrixSingleSelectItem/RadioGrid.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import Box from '@mui/material/Box'; | ||
|
||
import { RadioButton } from './RadioButton'; | ||
import { MatrixSelectOption, MatrixSelectRow, SingleMultiSelectAnswer } from '../../../../lib'; | ||
import { MatrixHeader } from '../MatrixHeader'; | ||
import { MatrixRow } from '../MatrixRow'; | ||
|
||
type Props = { | ||
options: Array<MatrixSelectOption>; | ||
rows: Array<MatrixSelectRow>; | ||
|
||
onChange: (rowIndex: number, value: string) => void; | ||
values: SingleMultiSelectAnswer; | ||
}; | ||
|
||
export const RadioGrid = ({ rows, options, onChange, values }: Props) => { | ||
return ( | ||
<Box display="flex" flex={1} flexDirection="column" data-testid="matrix-radio-grid"> | ||
<MatrixHeader options={options} /> | ||
|
||
{rows.map((row, rowI) => { | ||
const isEven = rowI % 2 === 0; | ||
|
||
return ( | ||
<MatrixRow | ||
key={row.id} | ||
isEven={isEven} | ||
item={{ id: row.id, imageUrl: row.rowImage, text: row.rowName, tooltip: row.tooltip }} | ||
> | ||
{options.map((option) => { | ||
const isChecked = option.text === values[rowI]; | ||
|
||
return ( | ||
<RadioButton | ||
key={option.id} | ||
id={option.id} | ||
text={option.text} | ||
isChecked={isChecked} | ||
onChange={() => onChange(rowI, option.text)} | ||
/> | ||
); | ||
})} | ||
</MatrixRow> | ||
); | ||
})} | ||
</Box> | ||
); | ||
}; |
64 changes: 64 additions & 0 deletions
64
src/entities/activity/ui/items/Matrix/MatrixSingleSelectItem/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { useMemo } from 'react'; | ||
|
||
import { RadioGrid } from './RadioGrid'; | ||
import { SingleMultiSelectAnswer, SingleSelectionRowsItem } from '../../../../lib'; | ||
|
||
type Props = { | ||
item: SingleSelectionRowsItem; | ||
values: SingleMultiSelectAnswer; | ||
|
||
onValueChange: (value: SingleMultiSelectAnswer) => void; | ||
replaceText: (value: string) => string; | ||
}; | ||
|
||
export const MatrixRadioItem = ({ item, values, onValueChange, replaceText }: Props) => { | ||
const { options, rows } = item.responseValues; | ||
|
||
const memoizedOptions = useMemo(() => { | ||
return options.map((el) => ({ | ||
...el, | ||
tooltip: el.tooltip ? replaceText(el.tooltip) : null, | ||
})); | ||
}, [options, replaceText]); | ||
|
||
const memoizedRows = useMemo(() => { | ||
return rows.map((el) => ({ | ||
...el, | ||
tooltip: el.tooltip ? replaceText(el.tooltip) : null, | ||
})); | ||
}, [replaceText, rows]); | ||
|
||
const memoizedValues = useMemo(() => { | ||
const initialAnswer = memoizedRows.map(() => null); | ||
|
||
return values.length ? values : initialAnswer; | ||
}, [memoizedRows, values]); | ||
|
||
const handleValueChange = (rowIndex: number, value: string) => { | ||
const newValues = memoizedValues.map((row, i) => { | ||
if (i === rowIndex) { | ||
const hasAnswer = row !== null; | ||
const isSameAnswer = row === value; | ||
|
||
if (hasAnswer && isSameAnswer) { | ||
return null; | ||
} else { | ||
return value; | ||
} | ||
} | ||
|
||
return row; | ||
}); | ||
|
||
onValueChange(newValues); | ||
}; | ||
|
||
return ( | ||
<RadioGrid | ||
options={memoizedOptions} | ||
rows={memoizedRows} | ||
onChange={handleValueChange} | ||
values={memoizedValues} | ||
/> | ||
); | ||
}; |
Oops, something went wrong.