Skip to content

Feature/accordion#173

Open
WenSuskey wants to merge 9 commits intopatricklindev:masterfrom
WenSuskey:feature/Accordion
Open

Feature/accordion#173
WenSuskey wants to merge 9 commits intopatricklindev:masterfrom
WenSuskey:feature/Accordion

Conversation

@WenSuskey
Copy link
Copy Markdown

Accordion component created

@@ -0,0 +1,158 @@
import React, { FC, useState } from 'react';
import { action } from '@storybook/addon-actions';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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';
Copy link
Copy Markdown

@PHANTOMS0308 PHANTOMS0308 Jul 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Copy Markdown

@PHANTOMS0308 PHANTOMS0308 Jul 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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' : '';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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' : '';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for props.expanded since you already destructured that.

}

let accordion;
if (props.onChange === undefined) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ? (
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

<p className={styleClasses + ' accordion-description' + show ? '' : 'hiden'}>
  {props.children}
</p>

@@ -0,0 +1,94 @@

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say using nesting will increase readability for Sass user

</div>
);
}
return accordion;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown

@PHANTOMS0308 PHANTOMS0308 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants