Skip to content

Commit c714d10

Browse files
authored
Merge pull request #132 from boostcampwm-2022/131-textfield-컴포넌트-구현
TextField 컴포넌트 구현
2 parents 0ff934a + 4d0245e commit c714d10

File tree

5 files changed

+119
-5
lines changed

5 files changed

+119
-5
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react';
2+
import { Story } from '@storybook/react';
3+
import TextField, { TextFieldPropType } from './TextField';
4+
5+
export default {
6+
component: TextField,
7+
title: '@shared/TextField',
8+
};
9+
10+
const Template: Story<TextFieldPropType> = (args) => <TextField {...args}></TextField>;
11+
12+
export const Default = Template.bind({});
13+
Default.args = {};
14+
15+
export const HelperText = Template.bind({});
16+
HelperText.args = { helperText: 'this is helper text' };
17+
18+
export const Disabled = Template.bind({});
19+
Disabled.args = { disabled: true };
20+
21+
export const Error = Template.bind({});
22+
Error.args = { error: true, helperText: 'this is helper text' };
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { css } from '@emotion/react';
2+
3+
export const TextFieldStyle = (theme, width, error, disabled) => css`
4+
width: ${width};
5+
background-color: ${disabled ? theme.colors.gray3 : theme.colors.secondary};
6+
color: ${error ? theme.colors.red : disabled ? theme.colors.gray2 : theme.colors.black};
7+
padding: 12px;
8+
9+
font-size: ${theme.fontSize.small};
10+
11+
border: 1px solid ${!disabled && error ? theme.colors.red : theme.colors.gray2};
12+
border-radius: ${theme.borderRaduis};
13+
14+
&::placeholder {
15+
color: ${theme.colors.gray2};
16+
}
17+
`;
18+
19+
export const TextFieldHelperTextStyle = (theme, error) => css`
20+
color: ${error ? theme.colors.red : theme.colors.gray2};
21+
22+
font-size: ${theme.fontSize.xSmall};
23+
`;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { css } from '@emotion/react';
2+
import { flexColumn } from '@styles/globalStyle';
3+
import React from 'react';
4+
import { TextFieldHelperTextStyle, TextFieldStyle } from './TextField.style';
5+
6+
export interface TextFieldPropType {
7+
width?: string;
8+
placeholder?: string;
9+
disabled?: boolean;
10+
readOnly?: boolean;
11+
error?: boolean;
12+
helperText?: string;
13+
onChange?: React.ChangeEventHandler<HTMLInputElement>;
14+
value?: string;
15+
}
16+
17+
const TextField = ({
18+
width,
19+
placeholder,
20+
disabled = false,
21+
readOnly = false,
22+
error = false,
23+
helperText,
24+
onChange,
25+
value,
26+
}: TextFieldPropType) => {
27+
return (
28+
<div css={flexColumn({ gap: '4px' })}>
29+
<input
30+
css={(theme) => TextFieldStyle(theme, width, error, disabled)}
31+
type="text"
32+
placeholder={placeholder}
33+
disabled={disabled}
34+
readOnly={readOnly}
35+
onChange={onChange}
36+
value={value}
37+
/>
38+
{!disabled && helperText?.trim().length > 0 ? (
39+
<span css={(theme) => TextFieldHelperTextStyle(theme, error)}>{helperText}</span>
40+
) : null}
41+
</div>
42+
);
43+
};
44+
45+
export default TextField;

frontend/src/styles/globalStyle.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,33 @@ const globalStyle = css`
4141
}
4242
`;
4343

44-
export const flexRow = ({ gap }) => css`
44+
interface flexPropType {
45+
gap?: string;
46+
justifyContent?: 'center' | 'space-between';
47+
alignItems?: 'center';
48+
}
49+
50+
export const flexRow = ({
51+
gap = '0px',
52+
justifyContent = 'center',
53+
alignItems = 'center',
54+
}: flexPropType) => css`
4555
display: flex;
4656
flex-direction: row;
57+
justify-content: ${justifyContent};
58+
align-items: ${alignItems};
4759
gap: ${gap};
4860
`;
4961

50-
export const flexColumn = ({ gap }) => css`
62+
export const flexColumn = ({
63+
gap = '0px',
64+
justifyContent = 'center',
65+
alignItems = 'center',
66+
}: flexPropType) => css`
5167
display: flex;
5268
flex-direction: column;
69+
justify-content: ${justifyContent};
70+
align-items: ${alignItems};
5371
gap: ${gap};
5472
`;
5573

frontend/src/styles/theme.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ const theme = {
77
black: '#222222',
88
gray1: '#888888',
99
gray2: '#BBBBBB',
10-
gray3: '#D7D7D7',
11-
white: '#ffffff',
12-
blue1: '#1F7CD2',
10+
gray3: '#EEECEC',
11+
white: '#F5F5F5',
1312
},
13+
fontSize: {
14+
xSmall: '12px',
15+
small: '16px',
16+
medium: '20px',
17+
laege: '24px',
18+
},
19+
borderRaduis: '8px',
1420
};
1521

1622
export default theme;

0 commit comments

Comments
 (0)