-
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.
Create the MatrixSingleSelect component
- Loading branch information
Viktor Riabkov
committed
Apr 4, 2024
1 parent
2874226
commit 9a1a9d2
Showing
6 changed files
with
160 additions
and
4 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
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
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}> | ||
<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"> | ||
<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) => { | ||
Check warning on line 30 in src/entities/activity/ui/items/Matrix/MatrixSingleSelectItem/RadioGrid.tsx GitHub Actions / ESLintsrc/entities/activity/ui/items/Matrix/MatrixSingleSelectItem/RadioGrid.tsx#L30
|
||
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} | ||
/> | ||
); | ||
}; |