Skip to content

Commit a70696c

Browse files
authored
v0.2.15
2 parents 60d82f7 + c80d249 commit a70696c

File tree

3 files changed

+75
-30
lines changed

3 files changed

+75
-30
lines changed

src/components/List/ListMenuGroup/ListMenuGroup.stories.tsx

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* External dependencies */
2-
import React from 'react'
2+
import React, { useCallback, useState } from 'react'
33
import base from 'paths.macro'
44
import { v4 as uuid } from 'uuid'
55
import { range } from 'lodash-es'
@@ -24,30 +24,65 @@ export default {
2424

2525
const SIDEBAR_WIDTH = 240
2626

27-
const Template = ({ ...otherListMenuGroupProps }) => (
28-
<Navigation
29-
withScroll
30-
disableResize
31-
title="사이드바"
32-
minWidth={SIDEBAR_WIDTH}
33-
>
34-
<ListMenuGroup
35-
{...otherListMenuGroupProps}
27+
const Template = ({ ...otherListMenuGroupProps }) => {
28+
const [open, setOpen] = useState(false)
29+
const [idx, setIdx] = useState(null)
30+
31+
const handleToggle = useCallback(() => {
32+
setOpen(v => !v)
33+
// if you want to automatically activate specific elemenmt
34+
// setIdx(0)
35+
}, [])
36+
37+
const handleClickGroup = useCallback((name: string) => {
38+
// eslint-disable-next-line no-console
39+
console.log(name)
40+
handleToggle()
41+
}, [handleToggle])
42+
43+
const handleClickItem = useCallback((name, key, index) => {
44+
// eslint-disable-next-line no-console
45+
console.log(name, key, index)
46+
setIdx(index)
47+
}, [])
48+
49+
return (
50+
<Navigation
51+
withScroll
52+
disableResize
53+
title="사이드바"
54+
minWidth={SIDEBAR_WIDTH}
3655
>
37-
{ range(0, 4).map(n => (
56+
<ListMenuGroup
57+
open={open}
58+
selectedMenuItemIndex={idx}
59+
onClick={handleClickGroup}
60+
onClickArrow={handleToggle}
61+
onChangeOption={handleClickItem}
62+
{...otherListMenuGroupProps}
63+
>
64+
{ range(0, 4).map(n => (
65+
<ListItem
66+
key={uuid()}
67+
optionKey={`menu-item-${n}`}
68+
content={`아이템 ${n}`}
69+
/>
70+
)) }
3871
<ListItem
3972
key={uuid()}
40-
optionKey={`menu-item-${n}`}
41-
content={`아이템 ${n}`}
73+
optionKey="item-with-a"
74+
href="https://naver.com"
75+
content="네이버 가기"
4276
/>
43-
)) }
44-
</ListMenuGroup>
45-
</Navigation>
46-
)
77+
</ListMenuGroup>
78+
</Navigation>
79+
)
80+
}
4781

4882
export const Primary = Template.bind({})
4983

5084
Primary.args = {
85+
name: 'sample group',
5186
content: '전체 상태',
5287
leftIcon: 'sent',
5388
selectedOptionIndex: null,

src/components/List/ListMenuGroup/ListMenuGroup.test.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* External dependencies */
22
import React from 'react'
3-
import { fireEvent, render, screen } from '@testing-library/react'
3+
import { render } from '@testing-library/react'
44
import { v4 as uuid } from 'uuid'
55
import { range } from 'lodash-es'
66

@@ -49,13 +49,4 @@ describe('ListMenuGroup', () => {
4949

5050
expect(rendered).toHaveAttribute('data-active-index', '2')
5151
})
52-
53-
it('should change "data-active-index"', () => {
54-
const { getByTestId } = renderComponent({ selectedMenuItemIndex: 1 })
55-
const rendered = getByTestId(SIDEBAR_MENU_GROUP_TEST_ID)
56-
57-
expect(rendered).toHaveAttribute('data-active-index', '1')
58-
fireEvent.click(screen.getByText('item 3'))
59-
expect(rendered).toHaveAttribute('data-active-index', '3')
60-
})
6152
})

src/components/List/ListMenuGroup/ListMenuGroup.tsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,21 @@ forwardedRef: React.Ref<HTMLElement>,
3535
const [currentMenuItemIndex, setCurrentMenuItemIndex] = useState<number | null>(selectedMenuItemIndex)
3636

3737
useEffect(() => {
38-
setCurrentMenuItemIndex(selectedMenuItemIndex)
38+
const childs = React.Children.toArray(children)
39+
if (isNil(selectedMenuItemIndex)
40+
|| (selectedMenuItemIndex < childs.length && selectedMenuItemIndex < 0)) {
41+
setCurrentMenuItemIndex(null)
42+
return
43+
}
44+
45+
const element = childs[selectedMenuItemIndex]
46+
47+
if (isListItem(element)) {
48+
if (element.props.href) { return }
49+
50+
setCurrentMenuItemIndex(selectedMenuItemIndex)
51+
}
52+
// eslint-disable-next-line react-hooks/exhaustive-deps
3953
}, [selectedMenuItemIndex])
4054

4155
useEffect(() => {
@@ -45,11 +59,16 @@ forwardedRef: React.Ref<HTMLElement>,
4559
// eslint-disable-next-line react-hooks/exhaustive-deps
4660
}, [open])
4761

62+
const handleClickGroup = useCallback(() => {
63+
if (onClick) {
64+
onClick(name)
65+
}
66+
}, [name, onClick])
67+
4868
const handleClickItem = useCallback((
4969
itemIndex: number,
5070
optionKey: string,
5171
) => {
52-
setCurrentMenuItemIndex(itemIndex)
5372
onChangeOption(name, optionKey, itemIndex)
5473
}, [name, onChangeOption])
5574

@@ -121,7 +140,7 @@ forwardedRef: React.Ref<HTMLElement>,
121140
name={name}
122141
className={className}
123142
currentMenuItemIndex={currentMenuItemIndex}
124-
onClick={onClick}
143+
onClick={handleClickGroup}
125144
data-testid={testId}
126145
data-active-index={currentMenuItemIndex}
127146
{...otherProps}

0 commit comments

Comments
 (0)