Skip to content

Commit

Permalink
refactor: onInput을 이용해 input에 숫자만 허용하는 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
feb-dain committed May 21, 2023
1 parent b00f438 commit 72eaec7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
15 changes: 11 additions & 4 deletions src/components/main/QuantityInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { WheelEventHandler, ChangeEventHandler, KeyboardEventHandler } from 'react';
import { WheelEventHandler, ChangeEventHandler, FormEventHandler, FormEvent } from 'react';
import { FaCaretUp, FaCaretDown } from 'react-icons/fa';
import { css, styled } from 'styled-components';
import { NOT_NUMBER } from '../../constants';
import Input from '../common/Input';

interface Props {
Expand All @@ -14,8 +15,14 @@ const QuantityInput = ({ id, value, onChange }: Props) => {
currentTarget.blur();
};

const handleDotPrevent: KeyboardEventHandler<HTMLInputElement> = (event) => {
if (event.key === '.') event.preventDefault();
const handleDigitsOnlyAllow: FormEventHandler<HTMLInputElement> = ({
currentTarget,
}: FormEvent<HTMLInputElement>) => {
const input = currentTarget;
const newValue = input.value.replace(NOT_NUMBER, '');

input.value = '';
input.value = newValue;
};

return (
Expand All @@ -34,7 +41,7 @@ const QuantityInput = ({ id, value, onChange }: Props) => {
css={QuantityInputStyle}
onWheel={handleScrollPrevent}
onChange={onChange}
onKeyDown={handleDotPrevent}
onInput={handleDigitsOnlyAllow}
/>

<FaCaretUp aria-label="button-to-raise-quantity" />
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useCart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const useSetCart = (id: number) => {
setCart((prev: CartItem[]) => {
const { cart, cartItemIndex, alreadyHasCartItem } = findCartItemIndex(prev);

if (Number.isNaN(Number(value))) return removeProduct(cart, cartItemIndex);
if (value === '') return removeProduct(cart, cartItemIndex);

if (alreadyHasCartItem) {
const updatedItem = { ...prev[cartItemIndex], quantity: Number(value) };
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useHandleQuantityInput.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChangeEventHandler, Dispatch, SetStateAction } from 'react';
import { MAX_NUMBER_LENGTH, NOT_NUMBER, QUANTITY } from '../constants';
import { MAX_NUMBER_LENGTH, QUANTITY } from '../constants';

interface Props {
setIsSelected: (value: SetStateAction<boolean>) => void;
Expand All @@ -21,9 +21,9 @@ export const useHandleQuantityInput = ({ ...props }: Props) => {
return setQuantity(QUANTITY.INITIAL);
}

const onlyTwoDigits = parseInt(value.replace(NOT_NUMBER, '').slice(0, MAX_NUMBER_LENGTH));
const onlyTwoDigits = value.slice(0, MAX_NUMBER_LENGTH);
setQuantity(onlyTwoDigits);
addToCart(String(onlyTwoDigits));
addToCart(onlyTwoDigits);
};

return handleNumberInputChange;
Expand Down

0 comments on commit 72eaec7

Please sign in to comment.