This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor strategy item component for reuse
Co-authored-by: Fredrik Strand Oseberg <[email protected]>
- Loading branch information
1 parent
334ea57
commit 5a59a1d
Showing
5 changed files
with
125 additions
and
88 deletions.
There are no files selected for viewing
File renamed without changes.
95 changes: 95 additions & 0 deletions
95
src/component/common/StrategyItemContainer/StrategyItemContainer.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,95 @@ | ||
import { DragEventHandler, FC, ReactNode } from 'react'; | ||
import { DragIndicator } from '@mui/icons-material'; | ||
import { styled, IconButton, Box } from '@mui/material'; | ||
import classNames from 'classnames'; | ||
import { IFeatureStrategy } from 'interfaces/strategy'; | ||
import { | ||
getFeatureStrategyIcon, | ||
formatStrategyName, | ||
} from 'utils/strategyNames'; | ||
import StringTruncator from 'component/common/StringTruncator/StringTruncator'; | ||
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender'; | ||
import { useStyles } from './StrategyItemContainer.styles'; | ||
|
||
interface IStrategyItemContainerProps { | ||
strategy: IFeatureStrategy; | ||
onDragStart?: DragEventHandler<HTMLButtonElement>; | ||
onDragEnd?: DragEventHandler<HTMLButtonElement>; | ||
actions?: ReactNode; | ||
orderNumber?: number; | ||
} | ||
|
||
const DragIcon = styled(IconButton)(({ theme }) => ({ | ||
padding: 0, | ||
cursor: 'inherit', | ||
transition: 'color 0.2s ease-in-out', | ||
})); | ||
|
||
const StyledIndexLabel = styled('div')(({ theme }) => ({ | ||
fontSize: theme.typography.fontSize, | ||
color: theme.palette.text.secondary, | ||
position: 'absolute', | ||
display: 'none', | ||
right: 'calc(100% + 6px)', | ||
top: theme.spacing(2.5), | ||
[theme.breakpoints.up('md')]: { | ||
display: 'block', | ||
}, | ||
})); | ||
|
||
export const StrategyItemContainer: FC<IStrategyItemContainerProps> = ({ | ||
strategy, | ||
onDragStart, | ||
onDragEnd, | ||
actions, | ||
children, | ||
orderNumber, | ||
}) => { | ||
const { classes: styles } = useStyles(); | ||
const Icon = getFeatureStrategyIcon(strategy.name); | ||
|
||
return ( | ||
<Box sx={{ position: 'relative' }}> | ||
<ConditionallyRender | ||
condition={orderNumber !== undefined} | ||
show={<StyledIndexLabel>{orderNumber}</StyledIndexLabel>} | ||
/> | ||
|
||
<div className={styles.container}> | ||
<div | ||
className={classNames(styles.header, { | ||
[styles.headerDraggable]: Boolean(onDragStart), | ||
})} | ||
> | ||
<ConditionallyRender | ||
condition={Boolean(onDragStart)} | ||
show={() => ( | ||
<DragIcon | ||
draggable | ||
disableRipple | ||
size="small" | ||
onDragStart={onDragStart} | ||
onDragEnd={onDragEnd} | ||
sx={{ cursor: 'move' }} | ||
> | ||
<DragIndicator | ||
titleAccess="Drag to reorder" | ||
cursor="grab" | ||
sx={{ color: 'neutral.main' }} | ||
/> | ||
</DragIcon> | ||
)} | ||
/> | ||
<Icon className={styles.icon} /> | ||
<StringTruncator | ||
maxWidth="150" | ||
maxLength={15} | ||
text={formatStrategyName(strategy.name)} | ||
/> | ||
<div className={styles.actions}>{actions}</div> | ||
</div> | ||
<div className={styles.body}>{children}</div> | ||
</div> | ||
</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
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