Quantity Picker A component used to increase or decrease quantity in integer units.
@seed-design/react@2.1.0, @seed-design/css@2.3.0
import { QuantityPicker } from "seed-design/ui/quantity-picker" ;
export default function QuantityPickerPreview () {
return < QuantityPicker min = { 1 } max = { 99 } defaultValue = { 1 } aria-label = "Item quantity" />;
}
npx @seed-design/cli@latest add ui:quantity-picker pnpm dlx @seed-design/cli@latest add ui:quantity-picker yarn dlx @seed-design/cli@latest add ui:quantity-picker bun x @seed-design/cli@latest add ui:quantity-picker
Install dependencies
npm install @karrotmarket/react-monochrome-icon @seed-design/react yarn add @karrotmarket/react-monochrome-icon @seed-design/react pnpm add @karrotmarket/react-monochrome-icon @seed-design/react bun add @karrotmarket/react-monochrome-icon @seed-design/react Copy the code below and paste it in /**
* @file ui:quantity-picker
* @requires @seed-design/react@^2.1.0
* @requires @seed-design/css@^2.3.0
**/
"use client" ;
import { IconMinusLine, IconPlusLine, IconTrashcanLine } from "@karrotmarket/react-monochrome-icon" ;
import { QuantityPicker as SeedQuantityPicker } from "@seed-design/react" ;
import * as React from "react" ;
import { ProgressCircle } from "./progress-circle" ;
export interface QuantityPickerProps
extends Omit < SeedQuantityPicker . RootProps , "children" | "removeAriaLabel" > {
/**
* Remove 버튼의 접근성 이름입니다.
* @default " 상품 삭제 "
*/
removeAriaLabel ?: SeedQuantityPicker . RootProps [ "removeAriaLabel" ];
/**
* Decrement 버튼의 접근성 이름입니다.
* @default " 수량 줄이기 "
*/
decrementAriaLabel ?: string ;
/**
* Increment 버튼의 접근성 이름입니다.
* @default " 수량 늘리기 "
*/
incrementAriaLabel ?: string ;
/**
* Decrement 버튼에 표시할 아이콘입니다.
* @default <IconMinusLine />
*/
decrementIcon ?: React . ReactNode ;
/**
* Increment 버튼에 표시할 아이콘입니다.
* @default <IconPlusLine />
*/
incrementIcon ?: React . ReactNode ;
/**
* Remove 버튼에 표시할 아이콘입니다.
* @default <IconTrashcanLine />
*/
removeIcon ?: React . ReactNode ;
/**
* loading 상태일 때 버튼에 표시할 요소입니다.
* @default <ProgressCircle size="inherit" tone="inherit" />
*/
loadingIndicator ?: React . ReactNode ;
/** 현재 수량을 form으로 제출할 때 hidden input에 전달할 속성입니다. */
inputProps ?: SeedQuantityPicker . HiddenInputProps ;
}
/**
* @see https://crispy.carrot-soft.tech/react/components/quantity-picker
*/
export const QuantityPicker = React.forwardRef <
React.ElementRef <typeof SeedQuantityPicker.Root > ,
QuantityPickerProps
> (
(
{
decrementAriaLabel = "수량 줄이기" ,
incrementAriaLabel = "수량 늘리기" ,
removeAriaLabel = "상품 삭제" ,
decrementIcon = < IconMinusLine />,
incrementIcon = < IconPlusLine />,
removeIcon = < IconTrashcanLine />,
loadingIndicator = < ProgressCircle size = "inherit" tone = "inherit" />,
inputProps,
... rootProps
},
ref,
) => {
return (
< SeedQuantityPicker.Root ref = {ref} removeAriaLabel = {removeAriaLabel} { ... rootProps}>
< SeedQuantityPicker.DecrementButton
aria-label = {decrementAriaLabel}
icon = {decrementIcon}
loadingIndicator = {loadingIndicator}
removeIcon = {removeIcon}
/>
< SeedQuantityPicker.ValueDisplay />
< SeedQuantityPicker.IncrementButton
aria-label = {incrementAriaLabel}
icon = {incrementIcon}
loadingIndicator = {loadingIndicator}
/>
< SeedQuantityPicker.HiddenInput { ... inputProps} />
</ SeedQuantityPicker.Root >
);
},
);
QuantityPicker.displayName = "QuantityPicker" ;
/**
* This file is a snippet from Crispy, helping you get started quickly with @seed-design/* packages.
* You can extend this snippet however you want.
*/
getValueText?QuantityPickerGetValueText | undefined
@seed-design/react@2.2.0, @seed-design/css@2.4.0
layout="hug" maintains the original width to fit the content. If the parent is a Flex layout and you need to fill the remaining space, use layout="fill". In this case, the button sizes on both sides are maintained and only the Value Display area expands.
import { HStack, Text, VStack } from "@seed-design/react" ;
import { QuantityPicker } from "seed-design/ui/quantity-picker" ;
export default function QuantityPickerLayout () {
return (
< VStack width = "360px" gap = "x4" align = "stretch" >
< VStack gap = "x2" align = "stretch" >
< Text >Hug (기본)</ Text >
< HStack width = "full" >
< QuantityPicker
layout = "hug"
min = { 1 }
max = { 99 }
defaultValue = { 1 }
aria-label = "Hug 상품 수량"
/>
</ HStack >
</ VStack >
< VStack gap = "x2" align = "stretch" >
< Text >Fill</ Text >
< HStack width = "full" >
< QuantityPicker
layout = "fill"
min = { 1 }
max = { 99 }
defaultValue = { 1 }
aria-label = "Fill 상품 수량"
/>
</ HStack >
</ VStack >
</ VStack >
);
}
Using getValueText, you can add units or supplementary descriptions to the displayed quantity.
import { QuantityPicker } from "seed-design/ui/quantity-picker" ;
export default function QuantityPickerValueText () {
return (
< QuantityPicker
min = { 1 }
max = { 99 }
defaultValue = { 1 }
aria-label = "상품 수량"
getValueText = {( valueText : string ) => `${ valueText }개` }
/>
);
}
You can control the quantity state externally using value and onValueChange.
import { Text, VStack } from "@seed-design/react" ;
import { useState } from "react" ;
import { QuantityPicker } from "seed-design/ui/quantity-picker" ;
export default function QuantityPickerControlled () {
const [ quantity , setQuantity ] = useState ( 2 );
return (
< VStack gap = "x3" align = "center" >
< QuantityPicker
min = { 1 }
max = { 99 }
value = {quantity}
onValueChange = {setQuantity}
aria-label = "상품 수량"
/>
< Text textStyle = "t4Regular" >현재 수량: {quantity}개</ Text >
</ VStack >
);
}
When you use removable, the Decrement button switches to a Remove button when the value reaches min. Handle the removal action in onRemove.
import { Text, VStack } from "@seed-design/react" ;
import { useState } from "react" ;
import { ActionButton } from "seed-design/ui/action-button" ;
import { QuantityPicker } from "seed-design/ui/quantity-picker" ;
export default function QuantityPickerRemovable () {
const [ removed , setRemoved ] = useState ( false );
if (removed) {
return (
< VStack gap = "x3" align = "center" >
< Text textStyle = "t4Regular" >상품을 삭제했습니다.</ Text >
< ActionButton variant = "neutralWeak" onClick = {() => setRemoved ( false )}>
되돌리기
</ ActionButton >
</ VStack >
);
}
return (
< VStack gap = "x3" align = "center" >
< QuantityPicker
min = { 1 }
max = { 99 }
defaultValue = { 1 }
removable
aria-label = "상품 수량"
removeAriaLabel = "상품 삭제"
onRemove = {() => setRemoved ( true )}
/>
< Text textStyle = "t4Regular" >최소 수량에서 Decrement 버튼이 Remove 버튼으로 전환됩니다.</ Text >
</ VStack >
);
}
With loading, you can temporarily prevent all actions or specific actions and display a loading indicator.
import { HStack, VStack } from "@seed-design/react" ;
import { useState } from "react" ;
import { ActionButton } from "seed-design/ui/action-button" ;
import { QuantityPicker } from "seed-design/ui/quantity-picker" ;
export default function QuantityPickerLoading () {
const [ decrementLoading , setDecrementLoading ] = useState ( false );
const [ incrementLoading , setIncrementLoading ] = useState ( false );
const allLoading = decrementLoading && incrementLoading;
function toggleAllLoading () {
const nextLoading = ! allLoading;
setDecrementLoading (nextLoading);
setIncrementLoading (nextLoading);
}
return (
< VStack gap = "x3" align = "center" >
< QuantityPicker
min = { 1 }
max = { 99 }
defaultValue = { 2 }
loading = {
allLoading
? true
: {
decrement: decrementLoading,
increment: incrementLoading,
}
}
aria-label = "상품 수량"
/>
< HStack gap = "x2" >
< ActionButton variant = "neutralWeak" aria-pressed = {allLoading} onClick = {toggleAllLoading}>
전체
</ ActionButton >
< ActionButton
variant = "neutralWeak"
aria-pressed = {decrementLoading}
onClick = {() => setDecrementLoading (( current ) => ! current)}
>
Decrement
</ ActionButton >
< ActionButton
variant = "neutralWeak"
aria-pressed = {incrementLoading}
onClick = {() => setIncrementLoading (( current ) => ! current)}
>
Increment
</ ActionButton >
</ HStack >
</ VStack >
);
}
When you pass name to inputProps, the current quantity is submitted as a hidden input.
import { VStack } from "@seed-design/react" ;
import type { FormEvent } from "react" ;
import { ActionButton } from "seed-design/ui/action-button" ;
import { QuantityPicker } from "seed-design/ui/quantity-picker" ;
export default function QuantityPickerForm () {
function handleSubmit ( event : FormEvent < HTMLFormElement >) {
event. preventDefault ();
const formData = new FormData (event.currentTarget);
window. alert ( `제출한 수량: ${ formData . get ( "quantity" ) }개` );
}
return (
< VStack asChild gap = "x3" align = "center" >
< form onSubmit = {handleSubmit}>
< QuantityPicker
min = { 1 }
max = { 99 }
defaultValue = { 1 }
aria-label = "상품 수량"
inputProps = {{ name: "quantity" }}
/>
< ActionButton type = "submit" variant = "neutralSolid" >
제출
</ ActionButton >
</ form >
</ VStack >
);
}
Provide an aria-label or aria-labelledby to the Root that describes the purpose of Quantity Picker. If needed, you can change the accessibility name of each action contextually using decrementAriaLabel, incrementAriaLabel, and removeAriaLabel.
Using getValueText, you can provide the ValueDisplay number as human-readable text. This value does not affect the raw integer value submitted to the hidden input.