diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index b714de9..5b85d42 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,3 +1,17 @@ +## [1.2.1](https://github.com/team-aliens/design-system/compare/v1.2.0...v1.2.1) (2022-12-21) + + +### Bug Fixes + +* checkBox export as 제거 ([4a383fb](https://github.com/team-aliens/design-system/commit/4a383fbd4635e954404aebfa07029bc7ae1fb7f8)) + +# [1.2.0](https://github.com/team-aliens/design-system/compare/v1.1.4...v1.2.0) (2022-12-21) + + +### Features + +* 세그멘트 버튼 추가 및 여러 버그수정 ([#20](https://github.com/team-aliens/design-system/issues/20)) ([7670d58](https://github.com/team-aliens/design-system/commit/7670d589f80c9976691bb690c17bb90d09f096ef)), closes [#12](https://github.com/team-aliens/design-system/issues/12) [#11](https://github.com/team-aliens/design-system/issues/11) [#11](https://github.com/team-aliens/design-system/issues/11) [#14](https://github.com/team-aliens/design-system/issues/14) [#15](https://github.com/team-aliens/design-system/issues/15) [#19](https://github.com/team-aliens/design-system/issues/19) + ## [1.1.4](https://github.com/team-aliens/design-system/compare/v1.1.3...v1.1.4) (2022-12-20) diff --git a/src/components/input/index.tsx b/src/components/input/index.tsx index 225fd28..e9d0f16 100644 --- a/src/components/input/index.tsx +++ b/src/components/input/index.tsx @@ -70,7 +70,9 @@ const _Input = styled.input<{ errorMsg: string }>` border-radius: 4px; border: 1px solid ${({ theme, errorMsg }) => - typeof errorMsg === 'undefined' ? theme.color.gray5 : theme.color.error}; + typeof errorMsg === 'undefined' || errorMsg === '' + ? theme.color.gray5 + : theme.color.error}; :focus { border: 2px solid ${({ theme }) => theme.color.primary}; } diff --git a/src/components/search/index.tsx b/src/components/search/index.tsx index 1ecca43..1cae95b 100644 --- a/src/components/search/index.tsx +++ b/src/components/search/index.tsx @@ -43,7 +43,7 @@ const _Wrapper = styled.label` align-items: center; width: 241px; height: 40px; - margin: ${({ margin }) => marginToCss({ margin })}; + ${({ margin }) => marginToCss({ margin })}; background: ${({ theme }) => theme.color.gray1}; box-shadow: 0 1px 20px rgba(204, 204, 204, 0.24); border-radius: 30px; diff --git a/src/components/segmentedBtn/SegmentBtn.stories.tsx b/src/components/segmentedBtn/SegmentBtn.stories.tsx new file mode 100644 index 0000000..df16d8e --- /dev/null +++ b/src/components/segmentedBtn/SegmentBtn.stories.tsx @@ -0,0 +1,27 @@ +import { ComponentMeta, ComponentStory } from '@storybook/react'; +import { useState } from 'react'; +import { SegmentedBtn } from '.'; + +export default { + title: 'component/segmentedBtn', + component: SegmentedBtn, +} as ComponentMeta; + +const selectedArr = ['A', 'B', 'C']; + +const Template: ComponentStory = (args) => { + const [state, setState] = useState(selectedArr[0]); + const onChange = (selected: string | number) => { + setState(selected); + }; + return ( + + ); +}; + +export const segmentedBtn = Template.bind({}); diff --git a/src/components/segmentedBtn/index.tsx b/src/components/segmentedBtn/index.tsx new file mode 100644 index 0000000..28a015c --- /dev/null +++ b/src/components/segmentedBtn/index.tsx @@ -0,0 +1,71 @@ +import styled, { css } from 'styled-components'; +import { marginCssType, marginToCss } from '../../utils/distance'; +import { Text } from '../styleGuide/text/Text'; + +interface PropsType extends marginCssType { + selectedArr: T[]; + cur: T; + onChange: (selected: T) => void; +} + +export const SegmentedBtn = ({ + selectedArr, + cur, + onChange, + margin, +}: PropsType) => { + return ( + <_Wrapper margin={margin}> + {selectedArr.map((selected) => ( + <_SelectedItem + onClick={() => onChange(selected)} + display="inline-block" + key={selected} + > + <_SelectedInner cur={selected === cur}>{selected} + + ))} + + ); +}; + +const _Wrapper = styled.div` + ${({ margin }) => marginToCss({ margin })}; + display: inline-block; + height: 48px; + border: 1px solid ${({ theme }) => theme.color.primaryLighten1}; + cursor: pointer; + border-radius: 4px; + > div { + :first-of-type { + left: -1px; + } + :last-of-type { + right: -1px; + } + } +`; + +const _SelectedItem = styled(Text)` + color: ${({ theme }) => theme.color.gray4}; + width: 110px; + height: 48px; + transition: 0.15s; + position: relative; + top: -1px; +`; + +const _SelectedInner = styled.div<{ cur: boolean }>` + display: flex; + justify-content: center; + align-items: center; + height: 100%; + transition: 0.15s; + ${({ theme, cur }) => + cur && + css` + background-color: ${theme.color.primary}; + border-radius: 4px; + color ${theme.color.gray1} + `}; +`; diff --git a/src/components/styleGuide/text/Text.tsx b/src/components/styleGuide/text/Text.tsx index f58f1b4..5cfb2bb 100644 --- a/src/components/styleGuide/text/Text.tsx +++ b/src/components/styleGuide/text/Text.tsx @@ -43,7 +43,7 @@ const _Text = styled.div` display: ${({ display }) => display}; color: ${({ color, theme }) => theme.color[color]}; ${({ size, theme }) => theme.font[size]}; - margin: ${({ margin }) => marginToCss({ margin })}; + ${({ margin }) => marginToCss({ margin })}; cursor: ${({ cursor }) => cursor}; text-align: ${({ align }) => align}; `; diff --git a/src/index.ts b/src/index.ts index 22aab68..df35520 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,3 +17,4 @@ export * from './components/search'; export * from './components/styleGuide/icon'; export * from './components/modal'; export * from './components/breadCrumb/BreadCrumb'; +export * from './components/segmentedBtn'; diff --git a/src/utils/distance.ts b/src/utils/distance.ts index a32bf1d..21562a5 100644 --- a/src/utils/distance.ts +++ b/src/utils/distance.ts @@ -4,32 +4,53 @@ import styled from 'styled-components'; /** 이거 배열말고 그냥 매개변수로 받아야 타이핑이 줄어들음 */ type marginType = | [number, number, number, number] - | [number | directionType, number] + | [number | directionType, number | 'auto'] | [number]; export interface marginCssType { - margin?: marginType; + margin?: marginType[] | marginType; } -export const marginToCss = ({ margin }: marginCssType) => { - if (!margin) return; +const mgReturn = (mg: marginType) => { + if (mg[0] === 0 && mg[1] === 'auto') { + return `margin: 0 auto;`; + } - switch (margin[0]) { + const unitTransform = (m: 'auto' | number) => (m === 'auto' ? m : m + 'px'); + + switch (mg[0]) { case 'top': - return `${margin[1]}px 0 0 0`; + return `margin-top: ${unitTransform(mg[1])};`; case 'bottom': - return `0 0 ${margin[1]}px 0`; + return `margin-bottom: ${unitTransform(mg[1])};`; case 'left': - return `0 0 0 ${margin[1]}px`; + return `margin-left: ${unitTransform(mg[1])};`; case 'right': - return `0 ${margin[1]}px 0 0`; + return `margin-right: ${unitTransform(mg[1])};`; default: - let css = ''; - for (let i = 0; i < margin.length; i++) css += margin[i] + 'px '; - return css; + let css = 'margin: '; + for (let j = 0; j < mg.length; j++) css += mg[j] + 'px '; + return css + ';'; } }; +export const marginToCss = ({ margin }: marginCssType) => { + if (!margin) return; + let mgCss = ''; + + if (Array.isArray(margin[0])) { + for (let i = 0; i < margin.length; i++) { + // @ts-expect-error + mgCss += mgReturn(margin[i]); + } + } else { + // @ts-expect-error + mgCss = mgReturn(margin); + } + + return mgCss; +}; + export const _Wrapper = styled.div` - margin: ${({ margin }) => marginToCss({ margin })}; + ${({ margin }) => marginToCss({ margin })}; `;