Skip to content

Commit 35e0a13

Browse files
committed
feat: add check empty input
1 parent 96c35ca commit 35e0a13

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

src/components/write/TagInput.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import transitions from '../../lib/styles/transitions';
66
import { mediaQuery } from '../../lib/styles/media';
77
import { useTransition, animated } from 'react-spring';
88
import OutsideClickHandler from 'react-outside-click-handler';
9+
import { isEmptyOrWhitespace } from '../../lib/utils';
910

1011
export interface TagInputProps {
1112
ref?: React.RefObject<HTMLDivElement>;
@@ -48,9 +49,8 @@ const TagInput: React.FC<TagInputProps> = ({ onChange, tags: initialTags }) => {
4849
(tag: string) => {
4950
ignore.current = true;
5051
setValue('');
51-
if (tag === '' || tags.includes(tag)) return;
52-
let processed = tag;
53-
processed = tag.trim().slice(0,255);
52+
if (isEmptyOrWhitespace(tag) || tags.includes(tag)) return;
53+
let processed = tag.trim().slice(0, 255);
5454
if (processed.indexOf(' #') > 0) {
5555
const tempTags: string[] = [];
5656
const regex = /#(\S+)/g;

src/containers/register/RegisterFormContainer.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import qs from 'qs';
1717
import { useApolloClient } from '@apollo/react-hooks';
1818
import { GET_CURRENT_USER } from '../../lib/graphql/user';
1919
import { isEmpty, trim } from 'ramda';
20+
import { isEmptyOrWhitespace } from '../../lib/utils';
2021

2122
interface RegisterFormContainerProps extends RouteComponentProps<{}> {}
2223

@@ -67,24 +68,17 @@ const RegisterFormContainer: React.FC<RegisterFormContainerProps> = ({
6768
setError(null);
6869
// validate
6970

70-
const isCustomEmpty = (str: string) => {
71-
if (typeof str !== 'string') {
72-
return isEmpty(str);
73-
}
74-
return isEmpty(trim(str.replace(/\s/g, '')));
75-
};
76-
7771
const validation = {
7872
displayName: (text: string) => {
79-
if (isCustomEmpty(text)) {
73+
if (isEmptyOrWhitespace(text)) {
8074
return '프로필 이름을 입력해주세요.';
8175
}
8276
if (text.trim().length > 45) {
8377
return '이름은 최대 45자까지 입력 할 수 있습니다.';
8478
}
8579
},
8680
username: (text: string) => {
87-
if (isCustomEmpty(text)) {
81+
if (isEmptyOrWhitespace(text)) {
8882
return '사용자 ID를 입력해주세요.';
8983
}
9084

src/lib/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import distanceInWordsToNow from 'date-fns/formatDistanceToNow';
22
import format from 'date-fns/format';
33
import koLocale from 'date-fns/locale/ko';
4+
import { isEmpty, trim } from 'ramda';
45

56
export const formatDate = (date: string): string => {
67
const d = new Date(date);
@@ -115,3 +116,10 @@ export const createFallbackTitle = (username: string | null) => {
115116
};
116117

117118
export const ssrEnabled = process.env.REACT_APP_SSR === 'enabled';
119+
120+
export const isEmptyOrWhitespace = (str: string) => {
121+
if (typeof str !== 'string') {
122+
return isEmpty(str);
123+
}
124+
return isEmpty(trim(str.replace(/\s/g, '')));
125+
};

0 commit comments

Comments
 (0)