Skip to content

Commit

Permalink
Refactor styled components and add local storage for expanded state
Browse files Browse the repository at this point in the history
  • Loading branch information
firsttris committed Jan 1, 2024
1 parent 8000421 commit e2a8215
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/app/ChannelsForType.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import { Icon } from '@iconify/react';
import { Channel, ChannelType } from './../types/types';
import { RainDetectionControl } from './controls/RainDetectionControl';
import { DoorControl } from './controls/DoorControl';
import { styledWithForward } from './../styled';


interface ExpandMoreProps {
expanded: string;
isExpanded: boolean;
}

const ExpandMore = styled(Icon)<ExpandMoreProps>(({ expanded }) => ({
transform: expanded === 'true' ? 'rotate(180deg)' : 'rotate(0deg)',
const ExpandMore = styledWithForward(Icon)<ExpandMoreProps>(({ isExpanded }) => ({
transform: isExpanded ? 'rotate(180deg)' : 'rotate(0deg)',
marginLeft: 'auto',
transition: 'transform 150ms cubic-bezier(0.4, 0, 0.2, 1) 0ms',
fontSize: '25px',
Expand Down Expand Up @@ -86,9 +87,12 @@ export const ChannelsForType: React.FC<ChannelTypeProps> = ({
const [hasTransitionExited, setHasTransitionExited] = useState<
boolean
>(true);
const [expanded, setExpanded] = useState<boolean>(false);
const [expanded, setExpanded] = useState<boolean>(localStorage.getItem(channelType) === 'true');

const handleExpandClick = () => setExpanded(!expanded);
const handleExpandClick = () => {
localStorage.setItem(channelType, (!expanded).toString());
setExpanded(!expanded)
};

return (
<Box key={index}>
Expand All @@ -110,7 +114,7 @@ export const ChannelsForType: React.FC<ChannelTypeProps> = ({
>
{t(channelType)}
</Typography>
<ExpandMore icon="uiw:down" expanded={expanded.toString()}/>
<ExpandMore icon="uiw:down" isExpanded={expanded}/>
</ListItemButton>
</ListItem>
{hasTransitionExited ? <Divider /> : null}
Expand Down
3 changes: 2 additions & 1 deletion src/app/controls/DoorControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { StyledIconButton } from '../components/StyledIcons';
import { KeymaticChannel } from './../../types/types';
import { useSetValueMutation } from './../../hooks/useApi';
import { styled } from '@mui/system';
import { styledWithForward } from './../../styled';

const StyledOuterBox = styled(Box)({
display: 'flex',
Expand All @@ -19,7 +20,7 @@ const StyledOuterBox = styled(Box)({
interface StyledTypographyProps {
isUncertain: boolean;
}
const StyledTypography = styled(Typography)<StyledTypographyProps>(({ isUncertain }) => ({
const StyledTypography = styledWithForward(Typography)<StyledTypographyProps>(({ isUncertain }) => ({
display: isUncertain ? 'block' : 'none',
}));

Expand Down
10 changes: 10 additions & 0 deletions src/styled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import isPropValid from '@emotion/is-prop-valid';
import { styled as muiStyled, CreateMUIStyled } from '@mui/system';


export const styledWithForward: CreateMUIStyled = (component: any, options: any) => {
return muiStyled(component, {
shouldForwardProp: (prop: string | number) => typeof prop === 'string' && isPropValid(prop),
...options,
});
};

0 comments on commit e2a8215

Please sign in to comment.