Skip to content
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

chore: 바퀴 수의 소수점 입력을 0.5단위로 제한합니다. #375

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions components/molecules/text-field/form-text-field.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use client';

import React, { ChangeEvent, forwardRef } from 'react';
import React, { ChangeEvent, forwardRef, KeyboardEvent } from 'react';

import { css, cx } from '@/styled-system/css';
import { preventMinus } from '@/utils';
import { preventCharacters } from '@/utils';

import {
absoluteStyles,
Expand Down Expand Up @@ -43,6 +43,7 @@ export const FormTextField = forwardRef<HTMLInputElement, FormTextFieldProps>(
subText,
placeholder,
unit,
preventDecimal,
className,
maxLength,
wrapperClassName,
Expand All @@ -66,6 +67,10 @@ export const FormTextField = forwardRef<HTMLInputElement, FormTextFieldProps>(
} else void onChange(event);
};

const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
preventCharacters(event, ['-', preventDecimal ? '.' : '']);
};

return (
<TextFieldWrapper
isRequired={isRequired}
Expand All @@ -82,7 +87,7 @@ export const FormTextField = forwardRef<HTMLInputElement, FormTextFieldProps>(
maxLength={maxLength}
onFocus={() => handlers.onChangeFocus(true)}
onBlur={() => handlers.onChangeFocus(false)}
onKeyDown={inputType === 'number' ? preventMinus : undefined}
onKeyDown={inputType === 'number' ? handleKeyDown : undefined}
onChange={handleInputChange}
className={cx(
css(
Expand Down
34 changes: 27 additions & 7 deletions components/molecules/text-field/text-field.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use client';

import { ChangeEvent } from 'react';
import { ChangeEvent, KeyboardEvent } from 'react';

import { css, cx } from '@/styled-system/css';
import { preventMinus } from '@/utils';
import { preventCharacters } from '@/utils';

import {
absoluteStyles,
Expand All @@ -24,6 +24,7 @@ import { useTextField } from './use-text-field';
* @param subText 추가 설명 텍스트
* @param placeholder placeholder 값
* @param unit 입력값 단위
* @param step 소수점 단위 제한
* @param maxLength input의 최대길이
* @param className input태그 추가 스타일
* @param wrapperClassName text-field-wrapper 컴포넌트 추가 스타일 부여
Expand All @@ -39,6 +40,8 @@ export function TextField({
subText,
placeholder,
unit,
step,
preventDecimal,
maxLength,
className,
wrapperClassName,
Expand All @@ -52,11 +55,28 @@ export function TextField({

const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => {
let newValue = event.target.value;
let numValue = parseFloat(event.target.value);

if (maxLength && newValue.length >= maxLength) {
newValue = newValue.slice(0, maxLength);
void onChange?.(newValue);
} else void onChange?.(newValue);
//소수점 단위가 제한되어 있을 때
if (step && numValue % step !== 0) {
numValue = Math.round(numValue * 2) / 2;
newValue = numValue.toString();
}

const lengthWithoutDecimal = newValue.replace('.', '').length;

//소수점도 고려한 maxLength 적용
if (maxLength && lengthWithoutDecimal >= maxLength) {
newValue = newValue.slice(
0,
maxLength + (newValue.includes('.') ? 2 : 0),
);
}
void onChange?.(newValue);
};

const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
preventCharacters(event, ['-', preventDecimal ? '.' : '']);
};

return (
Expand All @@ -78,7 +98,7 @@ export function TextField({
onChange={handleInputChange}
onFocus={() => handlers.onChangeFocus(true)}
onBlur={() => handlers.onChangeFocus(false)}
onKeyDown={inputType === 'number' ? preventMinus : undefined}
onKeyDown={inputType === 'number' ? handleKeyDown : undefined}
className={cx(
css(
shouldEmphasize
Expand Down
2 changes: 2 additions & 0 deletions components/molecules/text-field/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export interface TextFieldProps {
placeholder?: string;
unit?: string;
maxLength?: number;
step?: number;
preventDecimal?: boolean;
className?: string;
wrapperClassName?: string;
absoluteClassName?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export function DistanceFieldWithBadge({
<TextField
inputType="number"
maxLength={isAssistiveTabIndexZero ? 5 : 3}
step={isAssistiveTabIndexZero ? undefined : 0.5}
preventDecimal={isAssistiveTabIndexZero}
placeholder="0"
value={value ? String(value) : ''}
unit={unit}
Expand Down
4 changes: 3 additions & 1 deletion features/record/components/organisms/distance-page-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ export function DistancePageModal({
? '레인 길이에 따라 자동으로 거리를 계산해드릴게요'
: undefined
}
maxLength={isAssistiveIndexOne ? 3 : 5}
maxLength={isAssistiveIndexZero ? 5 : 3}
step={isAssistiveIndexZero ? undefined : 0.5}
preventDecimal={isAssistiveIndexZero}
value={isAssistiveIndexZero ? totalMeter : totalLaps}
unit={isAssistiveIndexZero ? '미터(m)' : '바퀴'}
wrapperClassName={css({ marginTop: '16px' })}
Expand Down
4 changes: 4 additions & 0 deletions features/record/components/organisms/sub-info-text-fields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function SubInfoTextFields() {
name: 'heartRate',
}) as number
}
preventDecimal
maxLength={3}
inputType="number"
label="심박수"
Expand All @@ -34,6 +35,7 @@ export function SubInfoTextFields() {
name: 'paceMinutes',
}) as number
}
preventDecimal
maxLength={2}
inputType="number"
label="페이스"
Expand All @@ -48,6 +50,7 @@ export function SubInfoTextFields() {
name: 'paceSeconds',
}) as number
}
preventDecimal
maxLength={2}
inputType="number"
unit="/100m"
Expand All @@ -62,6 +65,7 @@ export function SubInfoTextFields() {
name: 'kcal' as string,
}) as number
}
preventDecimal
maxLength={4}
inputType="number"
label="칼로리"
Expand Down
2 changes: 1 addition & 1 deletion utils/input/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './prevent-minus';
export * from './prevent-characters';
10 changes: 10 additions & 0 deletions utils/input/prevent-characters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { KeyboardEvent } from 'react';

export const preventCharacters = (
event: KeyboardEvent<HTMLInputElement>,
blockedChars: string[],
) => {
if (blockedChars.includes(event.key)) {
event.preventDefault();
}
};
7 changes: 0 additions & 7 deletions utils/input/prevent-minus.ts

This file was deleted.

Loading