-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Toggle 컴포넌트 재구축 및 서비스 코드 반영, 디자인시스템 v1.3.10 배포 (#793)
* refactor: Toggle 컴포넌트 재구축 및 npm v1.3.9 배포 * refactor: toggle 변경 시 발생시킬 이벤트 코드 추가 * fix: cyprses CI 버전문제 개선을 위한 package.json 수정 * chore: Toggle style Radius 추가 * build: 디자인시스템 npm v1.3.10 배포 * chore: 불필요한 콘솔 제거
- Loading branch information
1 parent
d3b9bd1
commit cdaa039
Showing
14 changed files
with
268 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
frontend-monorepo/packages/hang-log-design-system/src/components/NewToggle/Item.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { ForwardedRef, PropsWithChildren } from 'react'; | ||
import { useContext, forwardRef } from 'react'; | ||
import { NewToggleContext } from '@components/NewToggle/NewToggle'; | ||
|
||
export interface ItemProps extends PropsWithChildren { | ||
toggleKey: number | string; | ||
} | ||
|
||
const ToggleItem = ({ children, toggleKey }: ItemProps, ref?: ForwardedRef<HTMLDivElement>) => { | ||
const context = useContext(NewToggleContext); | ||
|
||
if (!context) throw new Error('NewToggle 컴포넌트가 Wrapping되어있지 않습니다.'); | ||
|
||
const { selectKey } = context; | ||
|
||
return ( | ||
<div ref={ref} style={{ display: selectKey === toggleKey ? 'block' : 'none' }}> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
|
||
export default forwardRef(ToggleItem); |
48 changes: 48 additions & 0 deletions
48
frontend-monorepo/packages/hang-log-design-system/src/components/NewToggle/List.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { ComponentPropsWithRef, ForwardedRef, KeyboardEvent } from 'react'; | ||
import { useContext, forwardRef } from 'react'; | ||
|
||
import { getListStyling } from '@components/NewToggle/Toggle.style'; | ||
import { NewToggleContext } from '@components/NewToggle/NewToggle'; | ||
|
||
export interface ToggleProps extends ComponentPropsWithRef<'li'> { | ||
toggleKey: number | string; | ||
text: string; | ||
} | ||
|
||
const List = ( | ||
{ toggleKey, text, ...attributes }: ToggleProps, | ||
ref?: ForwardedRef<HTMLLIElement> | ||
) => { | ||
const context = useContext(NewToggleContext); | ||
|
||
if (!context) throw new Error('NewToggle 컴포넌트가 Wrapping되어있지 않습니다.'); | ||
|
||
const { selectKey, handleSelect } = context; | ||
|
||
const handleEnterKeyPress = (event: KeyboardEvent<HTMLLIElement>) => { | ||
if (event.key === 'Enter') { | ||
handleSelect(toggleKey); | ||
} | ||
}; | ||
|
||
return ( | ||
<li | ||
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-to-interactive-role | ||
role="radio" | ||
aria-label={`${text}로 토글할 수 있는 버튼입니다. ${ | ||
selectKey === toggleKey ? '이미 선택되어있습니다.' : '' | ||
}`} | ||
tabIndex={0} | ||
ref={ref} | ||
css={getListStyling(selectKey === toggleKey)} | ||
aria-checked={selectKey === toggleKey} | ||
onClick={() => handleSelect(toggleKey)} | ||
onKeyDown={handleEnterKeyPress} | ||
{...attributes} | ||
> | ||
{text} | ||
</li> | ||
); | ||
}; | ||
|
||
export default forwardRef(List); |
49 changes: 49 additions & 0 deletions
49
frontend-monorepo/packages/hang-log-design-system/src/components/NewToggle/NewToggle.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type { PropsWithChildren } from 'react'; | ||
import { useCallback, useState, useMemo, createContext } from 'react'; | ||
import List from '@components/NewToggle/List'; | ||
import Item from '@components/NewToggle/Item'; | ||
import { flushSync } from 'react-dom'; | ||
import { getToggleWrapperStyling } from '@components/NewToggle/Toggle.style'; | ||
|
||
interface ToggleContextType { | ||
selectKey: number | string; | ||
handleSelect: (selectedId: number | string) => void; | ||
} | ||
|
||
export interface NewToggleProps extends PropsWithChildren { | ||
initialSelect?: number | string; | ||
additinalFunc?: (key: number | string) => void; | ||
} | ||
|
||
export const NewToggleContext = createContext<ToggleContextType | null>(null); | ||
|
||
const NewToggle = ({ initialSelect = 0, additinalFunc, children }: NewToggleProps) => { | ||
const [selected, setSelected] = useState<number | string>(initialSelect); | ||
|
||
const handleSelect = useCallback( | ||
(select: number | string) => { | ||
flushSync(() => setSelected(select)); | ||
if (additinalFunc) additinalFunc(select); | ||
}, | ||
[additinalFunc] | ||
); | ||
|
||
const context = useMemo( | ||
() => ({ | ||
selectKey: selected, | ||
handleSelect, | ||
}), | ||
[handleSelect, selected] | ||
); | ||
|
||
return ( | ||
<NewToggleContext.Provider value={context}> | ||
<div css={getToggleWrapperStyling}>{children}</div> | ||
</NewToggleContext.Provider> | ||
); | ||
}; | ||
|
||
NewToggle.List = List; | ||
NewToggle.Item = Item; | ||
|
||
export default NewToggle; |
45 changes: 45 additions & 0 deletions
45
frontend-monorepo/packages/hang-log-design-system/src/components/NewToggle/Toggle.style.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { css } from '@emotion/react'; | ||
|
||
import { Theme } from '@styles/Theme'; | ||
|
||
export const getListStyling = (isSelected: boolean) => | ||
css({ | ||
display: 'flex', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
|
||
padding: '8px 12px', | ||
border: `1px solid ${isSelected ? Theme.color.blue700 : Theme.color.gray200}`, | ||
|
||
backgroundColor: isSelected ? Theme.color.blue100 : Theme.color.white, | ||
|
||
fontSize: Theme.text.small.fontSize, | ||
lineHeight: Theme.text.small.lineHeight, | ||
color: isSelected ? Theme.color.blue700 : Theme.color.gray600, | ||
|
||
transition: `all .2s ease-in`, | ||
|
||
cursor: 'pointer', | ||
|
||
'&:hover': { | ||
color: isSelected ? Theme.color.blue700 : Theme.color.gray700, | ||
backgroundColor: isSelected ? Theme.color.blue200 : Theme.color.gray100, | ||
}, | ||
}); | ||
|
||
export const getToggleWrapperStyling = css({ | ||
width: 'fit-content', | ||
borderRadius: Theme.borderRadius.small, | ||
|
||
overflow: 'hidden', | ||
|
||
'& :first-of-type': { | ||
borderTopLeftRadius: Theme.borderRadius.small, | ||
borderBottomLeftRadius: Theme.borderRadius.small, | ||
}, | ||
|
||
'& :last-of-type': { | ||
borderTopRightRadius: Theme.borderRadius.small, | ||
borderBottomRightRadius: Theme.borderRadius.small, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
frontend-monorepo/packages/hang-log-design-system/src/stories/NewToggle.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { containerStyle } from '@stories/styles'; | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import NewToggle from '@components/NewToggle/NewToggle'; | ||
|
||
const meta = { | ||
title: 'NewToggle', | ||
component: NewToggle, | ||
args: { | ||
initialSelect: 'toggle1', | ||
}, | ||
argTypes: { | ||
initialSelect: { control: 'string' }, | ||
}, | ||
decorators: [ | ||
(Story) => ( | ||
<ul css={containerStyle}> | ||
<Story /> | ||
</ul> | ||
), | ||
], | ||
} satisfies Meta<typeof NewToggle>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default: Story = { | ||
render: ({ ...args }) => ( | ||
<NewToggle | ||
{...args} | ||
additinalFunc={(id: number | string) => { | ||
// eslint-disable-next-line no-console | ||
console.log(id); | ||
}} | ||
> | ||
<h6>NewToggle</h6> | ||
<div style={{ display: 'flex' }}> | ||
<NewToggle.List text="Toggle 1" toggleKey="toggle1" /> | ||
<NewToggle.List text="Toggle 2" toggleKey="toggle2" /> | ||
<NewToggle.List text="Toggle 3" toggleKey="toggle3" /> | ||
</div> | ||
<div> | ||
<NewToggle.Item toggleKey="toggle1">나는토글1</NewToggle.Item> | ||
<NewToggle.Item toggleKey="toggle2">나는토글2</NewToggle.Item> | ||
<NewToggle.Item toggleKey="toggle3">나는토글3</NewToggle.Item> | ||
</div> | ||
</NewToggle> | ||
), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.