Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(SideNav): Composition できるように修正 #5157

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

masa0527
Copy link
Contributor

@masa0527 masa0527 commented Dec 2, 2024

関連URL

https://smarthr.atlassian.net/browse/SHRUI-1143

概要

SideNavのstoryの見直しをしました。

変更内容

確認方法

Chromaticで確認

@masa0527 masa0527 requested a review from a team as a code owner December 2, 2024 03:28
Copy link

pkg-pr-new bot commented Dec 2, 2024

Open in Stackblitz

npm i https://pkg.pr.new/kufu/smarthr-ui@5157

commit: 5bc5ca6

Copy link
Collaborator

@uknmr uknmr left a comment

Choose a reason for hiding this comment

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

諸々コメントしましたが、items を deprecated し、SideNav と SideNavItem という composition で使う状態にしてから Story を書いたほうが良さそうに思いました(書き直しになってしまいそう)。

@masa0527 masa0527 changed the title docs: SideNavのstoryを見直し feat(SideNav): Composition できるように修正 Dec 5, 2024
@masa0527
Copy link
Contributor Author

masa0527 commented Dec 6, 2024

@uknmr
composition に対応してみました!

React.cloneElement(
child as React.ReactElement<ComponentPropsWithoutRef<typeof SideNavItemButton>>,
{
onClick,
Copy link
Collaborator

Choose a reason for hiding this comment

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

imo: Composition pattern の場合、SideNavItemButton に onClick を個別で渡せるので、onClick や id の仕組みを見直しても良さそうです。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

idについていい仕組みを思いつかなかったんですが
SideNavonClickも持たせつつだとcontextとか使うイメージですかね?

export const SideNavItemButton: FC<Props & ElementProps> = ({
  id,
  onClick,
  isSelected = false,
  children,
}) => {
  const context = useContext(SideNavContext)

  const handleClick = onClick ?? context?.onClick
    ? (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) =>
        (onClick ?? context?.onClick)?.(e, id)
    : undefined

Copy link
Collaborator

Choose a reason for hiding this comment

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

AppNavi や SideMenu と同じような扱いで、管理はプロダクト側でやるでも良いのではと思いました!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

propsにonClickがある場合はそちらを優先するようにしてみました
18492f4

packages/smarthr-ui/src/components/SideNav/SideNav.tsx Outdated Show resolved Hide resolved
@@ -22,9 +24,11 @@ type Props = {
onClick?: OnClick
}

type ElementProps = Omit<ComponentPropsWithoutRef<'li'>, keyof Props>
Copy link
Collaborator

Choose a reason for hiding this comment

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

SideNavItemButton という名前を持っているのに li なのが気になる(上述の SideNav の方で組み立てれば解決しそう)

Comment on lines 120 to 121
export const OnClick: StoryObj<typeof SideNav> = {
args: {
Copy link
Collaborator

Choose a reason for hiding this comment

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

漏れ

Suggested change
export const OnClick: StoryObj<typeof SideNav> = {
args: {
export const OnClick: StoryObj<typeof SideNav> = {
name: 'onClick',
args: {

Comment on lines 128 to 132
export const ClassName = {
args: {
className: 'shr-bg-white',
},
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

ElementProps で型として定義されたので、Props からは消し & Story からも消しでよさそう。

Suggested change
export const ClassName = {
args: {
className: 'shr-bg-white',
},
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

classname消しました!

},
},
args: {
items: _sideItems.notSelected,
Copy link
Collaborator

Choose a reason for hiding this comment

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

効いてなさそう(消してもよさそう

args: {
items: _sideItems.notSelected,
},
render: (args) => <SideNav items={args.items} />,
Copy link
Collaborator

Choose a reason for hiding this comment

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

imo: items より SideNavItemButton の Story に見えるので、items={_items} とする Story でもよさそうです。
(default の render で items ?? _items.map((item) => <SIdeNavItemButton />) とするイメージ

Copy link
Contributor Author

Choose a reason for hiding this comment

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

修正しました!

Comment on lines 85 to 91
argTypes: {
items: {
control: 'radio',
options: Object.keys(_sideItems),
mapping: _sideItems,
},
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

この Story 以外で items の「Set object」を押すとエラーがでてしまうので、default に記述したい。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

defaultではitemのControlはfalseにして
itemsのstoryのみobjectとしました

objectいじるとエラーにはなるけど他もそうなので 🤔

parameters: {
chromatic: { disableSnapshot: true },
},
tags: ['skip-test-runner'],
Copy link
Collaborator

Choose a reason for hiding this comment

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

q: li で怒られる?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

<ul>をrenderに追加して対応

Comment on lines 45 to 46
hover: ['li'],
focusVisible: ['button'],
Copy link
Collaborator

Choose a reason for hiding this comment

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

hover と focusVisible が同時に発火してしまっているので分けたいです。
AppNavi などを見ると 1Story の中で分けるイメージがつかめるかも。 https://github.com/kufu/smarthr-ui/blob/master/packages/smarthr-ui/src/components/AppNavi/stories/VRTAppNavi.stories.tsx#L18

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.

2 participants