Conversation
| @@ -0,0 +1,158 @@ | |||
| import React, { FC, useState } from 'react'; | |||
| import { action } from '@storybook/addon-actions'; | |||
There was a problem hiding this comment.
This line shouldn't be here, this is storybook things
| @@ -0,0 +1,158 @@ | |||
| import React, { FC, useState } from 'react'; | |||
| import { action } from '@storybook/addon-actions'; | |||
| import up from '../../asset/icon/expanded-up.svg'; | |||
There was a problem hiding this comment.
It would be better if you name it as upIcon or upSVG, same for the next line.
| import up from '../../asset/icon/expanded-up.svg'; | ||
| import down from '../../asset/icon/expanded-down.svg'; | ||
| import { classNames } from '../../utils/classNames'; | ||
| export type AccordionType = 'default' | 'checkbox'; |
There was a problem hiding this comment.
If you don't actually need this AccordionType type anywhere else, you could simply write it inside the IAccordionProps interface
| /** set Accordion expanded */ | ||
| expanded?: boolean; | ||
| /** set action on title clicked */ | ||
| onChange?: () => void; |
There was a problem hiding this comment.
This definition is really not strict. Since you are using TS, you would like to define a more detailed onChange function type
| /**set Accordion type */ | ||
| type?: AccordionType; | ||
| } | ||
| export type patAccordionProps = IAccordionProps; |
There was a problem hiding this comment.
Don't quite understand what is the purpose of this line?
| ...rest | ||
| } = props; | ||
| let initialState: boolean = expanded === undefined ? false : expanded; | ||
| const [show, setShow] = useState<boolean | false>(initialState); |
There was a problem hiding this comment.
You can delete line 39, and swap line 40 with
const [show, setShow] = useState<boolean>(initialState ?? false);
| const handleDefaultClick = () => { | ||
| setShow(!show); | ||
| }; | ||
| const isExpanded: string = show ? 'isExpanded' : ''; |
There was a problem hiding this comment.
Honestly, the variable name is hinting a boolean, but instead is a string. I kinda understand you wanna have some shortcut for css classname later, but this is quite contradictory naming and data type!=
| }; | ||
| const isExpanded: string = show ? 'isExpanded' : ''; | ||
| const isDisabled: string = disabled ? 'isDisabled' : ''; | ||
| const isControlExpanded: string = props.expanded ? 'isExpanded' : ''; |
There was a problem hiding this comment.
No need for props.expanded since you already destructured that.
| } | ||
|
|
||
| let accordion; | ||
| if (props.onChange === undefined) { |
There was a problem hiding this comment.
I believe you could simply just use a conditional statement inside the onClick, for example
onClick={props.onChange ? props.onChange : handleDefaultClick}
so that you dont need to have two long code block
| </button> | ||
| </div> | ||
|
|
||
| {show ? ( |
There was a problem hiding this comment.
Same here
<p className={styleClasses + ' accordion-description' + show ? '' : 'hiden'}>
{props.children}
</p>
| @@ -0,0 +1,94 @@ | |||
|
|
|||
There was a problem hiding this comment.
I would say using nesting will increase readability for Sass user
| </div> | ||
| ); | ||
| } | ||
| return accordion; |
There was a problem hiding this comment.
So now you can simply just return the JSX, instead of extract the logic into a variable with if statement. Also I realized that you used a lot of props.xxx, but you actually de-structure most of them at the beginning of your component. It seems redundant to me.
PHANTOMS0308
left a comment
There was a problem hiding this comment.
Your component is working great, the tests are good. However, you could spend some extra time to refactor your code, more specifically to remove redundant logics.
Accordion component created