-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: 생년월일 입력 - 변경된 디자인 적용 및 리팩터링 #233
Closed
Closed
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
85cdf20
chore: DATE_SAPARATOR를 constants화 해서 사용
Doeunnkimm eb57da4
refactor: 변경된 디자인 적용 및 기능 리팩터링
Doeunnkimm ec42fab
chore: 정보수정 페이지에서 생년월일 부분 반영
Doeunnkimm 484ad32
chore: default값으로 인해 붙는 separator를 예외시키기 위해 filter 메서드 추가
Doeunnkimm 27c5939
fix: formattedValue가 format과 일치하는지 검사하는 검증 추가
Doeunnkimm e32c85e
feat: member/new/birth에서 유효성 검증 추가
Doeunnkimm 5dbf066
feat: 타임라인 조회 api 반영 (#231)
newminkyung c7470ee
feat: 댓글 기능 api 연동 및 ios에서 키패드 이슈 대응 (#223)
Doeunnkimm 1042a1e
refactor: 오류 페이지 컴포넌트 리팩터링 (#236)
Doeunnkimm 4645efa
fix: 존재하지 않는 유저 지도 접근시 에러 처리 (#237)
Doeunnkimm a55e0c7
feat: 응원한 사람 조회 api 응답 값 수정 반영 / 응원하기 성공 시에도 에러 토스트가 뜨는 버그 수정 (#232)
newminkyung 0264c2f
feat: 피드 이모지 추가/삭제시 낙관적 업데이트 적용 (#234)
hjy0951 c360db8
feat: 목표 수정 기능 개발 (#192)
deepbig 0f7f44b
fix: 타임라인 조회 쿼리키 수정 (#238)
newminkyung 4afb2d9
fix: 피드 페이지에서 댓글 API 요청시 누락된 목표 ID 추가 (#239)
hjy0951 d5f69b6
chore: DATE_SAPARATOR를 constants화 해서 사용
Doeunnkimm 87f1f66
refactor: 변경된 디자인 적용 및 기능 리팩터링
Doeunnkimm f11ca1b
chore: 정보수정 페이지에서 생년월일 부분 반영
Doeunnkimm 9b97ac6
chore: default값으로 인해 붙는 separator를 예외시키기 위해 filter 메서드 추가
Doeunnkimm b9200f5
fix: formattedValue가 format과 일치하는지 검사하는 검증 추가
Doeunnkimm cf4542e
feat: member/new/birth에서 유효성 검증 추가
Doeunnkimm 288018c
Merge branch 'chore/dateInput' of https://github.com/depromeet/amazin…
Doeunnkimm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const DATE_SEPARATOR = '.'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,50 @@ | ||
'use client'; | ||
|
||
import type { ChangeEvent } from 'react'; | ||
import { useState } from 'react'; | ||
import { useEffect } from 'react'; | ||
|
||
import { Input, Typography } from '@/components'; | ||
import { MAX_DATE_LENGTH_UNTIL_MONTH } from '@/constants'; | ||
|
||
import { formatDate, isValidDate } from '../../utils/date'; | ||
import { useDateInput } from '@/hooks'; | ||
import { type DateProps, type DateValueProps, typeToMaxLength } from '@/hooks/useDateInput'; | ||
import { formatDate } from '@/utils/date'; | ||
|
||
interface DateInputProps { | ||
labelName?: string; | ||
intitalValue?: string; | ||
maxLength?: number; | ||
initialValue?: DateValueProps; | ||
requireDateType?: DateProps[]; | ||
onChange?: (value: string) => void; | ||
} | ||
|
||
export const DateInput = ({ labelName = '', intitalValue = '', maxLength, onChange }: DateInputProps) => { | ||
const [formattedValue, setFormattedValue] = useState<string>(intitalValue); | ||
const placeholder = maxLength === MAX_DATE_LENGTH_UNTIL_MONTH ? 'YYYY.MM' : 'YYYY.MM.DD'; | ||
|
||
const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
const inputValue = event.target.value.replace(/\D/g, ''); | ||
let formatted = inputValue; | ||
let year, month, day; | ||
|
||
if (inputValue.length > 4 && inputValue.length <= 6) { | ||
year = inputValue.slice(0, 4); | ||
month = inputValue.slice(4, 6); | ||
month = +month > 12 ? '12' : +month === 0 ? '0' : month; | ||
formatted = formatDate([year, month], '.'); | ||
} else if (inputValue.length > 6) { | ||
year = inputValue.slice(0, 4); | ||
month = inputValue.slice(4, 6); | ||
day = inputValue.slice(6, 8); | ||
export const DateInput = ({ | ||
labelName = '', | ||
initialValue = { YYYY: '', MM: '', DD: '' }, | ||
requireDateType = ['YYYY', 'MM', 'DD'], | ||
onChange, | ||
}: DateInputProps) => { | ||
const { inputRefs, dateValues, handleInputChange, handleInputBlur } = useDateInput({ initialValue }); | ||
|
||
if (inputValue.length < 8) { | ||
formatted = formatDate([year, month, day], '.'); | ||
} else { | ||
formatted = isValidDate(year, month, day) | ||
? formatDate([year, month, day], '.') | ||
: formatDate([year, month], '.'); | ||
} | ||
} | ||
setFormattedValue(formatted); | ||
onChange && onChange(formatted); | ||
}; | ||
useEffect(() => { | ||
onChange && onChange(formatDate(requireDateType.map((type) => dateValues[type]))); | ||
}, [dateValues, onChange, requireDateType]); | ||
|
||
return ( | ||
<div className="flex flex-col gap-5xs"> | ||
<Typography type="title3" className="text-gray-50"> | ||
{labelName} | ||
</Typography> | ||
<Input | ||
type="text" | ||
placeholder={placeholder} | ||
value={formattedValue} | ||
maxLength={maxLength} | ||
onChange={handleInputChange} | ||
/> | ||
<div className="flex gap-5xs"> | ||
{requireDateType.map((type) => ( | ||
<Input | ||
key={type} | ||
ref={inputRefs[type]} | ||
placeholder={type} | ||
className="text-center" | ||
maxLength={typeToMaxLength[type]} | ||
value={dateValues[type]} | ||
onChange={(e) => handleInputChange(e, type)} | ||
onBlur={(e) => handleInputBlur(e, type)} | ||
/> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { ChangeEvent, FocusEvent } from 'react'; | ||
import { useRef, useState } from 'react'; | ||
|
||
export type DateProps = 'YYYY' | 'MM' | 'DD'; | ||
|
||
export type DateValueProps = Record<DateProps, string>; | ||
|
||
interface UseDateInputProps { | ||
initialValue: DateValueProps; | ||
} | ||
|
||
export const typeToMaxLength: Record<DateProps, number> = { | ||
YYYY: 4, | ||
MM: 2, | ||
DD: 2, | ||
} as const; | ||
|
||
export const useDateInput = ({ initialValue }: UseDateInputProps) => { | ||
const [dateValues, setDateValues] = useState<DateValueProps>(initialValue); | ||
|
||
const yearInputRef = useRef<HTMLInputElement>(null); | ||
const monthInputRef = useRef<HTMLInputElement>(null); | ||
const dayInputRef = useRef<HTMLInputElement>(null); | ||
|
||
const inputRefs = { | ||
YYYY: yearInputRef, | ||
MM: monthInputRef, | ||
DD: dayInputRef, | ||
}; | ||
|
||
const handleInputChange = (event: ChangeEvent<HTMLInputElement>, type: DateProps) => { | ||
let inputValue = event.target.value; | ||
|
||
if (type === 'YYYY' && inputValue.length === typeToMaxLength[type]) { | ||
monthInputRef.current?.focus(); | ||
} | ||
|
||
if (type === 'MM') { | ||
inputValue = +inputValue > 12 ? '12' : inputValue; | ||
if (inputValue.length === typeToMaxLength.MM) dayInputRef.current?.focus(); | ||
} | ||
|
||
setDateValues((prev) => ({ ...prev, [type]: inputValue })); | ||
}; | ||
|
||
const handleInputBlur = (event: FocusEvent<HTMLInputElement>, type: DateProps) => { | ||
const inputValue = event.target.value; | ||
|
||
if ((type === 'MM' || type === 'DD') && inputValue.length === 1) { | ||
setDateValues((prev) => ({ ...prev, [type]: `0${inputValue}` })); | ||
} | ||
}; | ||
|
||
return { dateValues, handleInputChange, handleInputBlur, inputRefs }; | ||
}; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
와 여기부터 함수 토나오네
미안하다 도은... 최고야...👍