# Quantity Picker URL: /lynx/components/quantity-picker Source: https://github.com/daangn/seed-design/blob/dev/docs/content/lynx/components/quantity-picker.mdx A component used to increase or decrease quantity in integer units. Lynx Engine 최소 버전: 3.6 사용 가능 버전: @seed-design/lynx-react@0.8.0, @seed-design/lynx-css@0.12.0 ## Preview ```tsx import "./styles"; import { useSeedClassName } from "@seed-design/lynx-react"; import { QuantityPicker } from "@/components/ui/quantity-picker"; export default function Example() { const seedClassName = useSeedClassName({ colorMode: "system" }); return ( ); } ``` ## Installation - npm: npx @seed-design/cli@latest add ui:quantity-picker - pnpm: pnpm dlx @seed-design/cli@latest add ui:quantity-picker - yarn: yarn dlx @seed-design/cli@latest add ui:quantity-picker - bun: bun x @seed-design/cli@latest add ui:quantity-picker ## Props ### Registry convenience component Registry's `QuantityPicker` is a convenience API that provides three slots needed for quantity selection in a single component. `min` and `max` are required and must be safe integers. The default value of `step` is `1`, and if `defaultValue` is omitted, it starts from `min`. Using `value` and `onValueChange` together allows you to control the value with external state. - `size`: `small`, `medium` (default), `large` - `layout`: `hug` (default), `fill`. In `fill`, the Value Display area expands to fill remaining width. - `loading`: If `true`, blocks both Decrement and Increment; if passed as an object, blocks each action separately with `{ decrement?: boolean, increment?: boolean }`. - `removable`: Changes Decrement to Remove action at minimum value. Handle item deletion in `onRemove`. - `getValueText`: Changes the quantity text displayed on screen. The actual quantity value and arguments to `onValueChange` remain as numbers. ### `@seed-design/lynx-react` compound API When combining low-level APIs directly without using Registry, use the following four public components. ```tsx import { QuantityPicker } from "@seed-design/lynx-react"; ; ``` Pass quantity range and state (`min`, `max`, `step`, `value`, `defaultValue`, `onValueChange`, `disabled`, `invalid`, `readOnly`, `loading`, `removable`, `onRemove`, `getValueText`, `dir`, `layout`, `size`) to `Root`. When `removable={true}`, you must also pass `removeAccessibilityLabel`. `DecrementButton`, `ValueDisplay`, and `IncrementButton` share values and state using Root's Context. You can pass icons, loading indicators, native `accessibility-*` attributes, and Lynx events directly to each slot. The Registry convenience component's `decrementAccessibilityLabel`, `incrementAccessibilityLabel`, `decrementIcon`, `incrementIcon`, `removeIcon`, `loadingIndicator` are props that configure this compound slot instead. Lynx compound API does not have the web-only `HiddenInput` slot. ## Examples ### Layout `layout="hug"` maintains the width that fits the content. If the parent is a Flex layout and needs to fill remaining space, use `layout="fill"`. Both button sizes are maintained and only the Value Display area expands. ```tsx import "./styles"; import { useSeedClassName } from "@seed-design/lynx-react"; import { QuantityPicker } from "@/components/ui/quantity-picker"; export default function Example() { const seedClassName = useSeedClassName({ colorMode: "system" }); return ( Layout Hug (기본) Fill ); } ``` The Quantity Picker recipe used in this example uses `display: grid`. It is compatible with Android/iOS Lynx engine 2.1 and above and HarmonyOS Lynx engine 3.4 and above. ### Value Text Using `getValueText`, you can add units or supplementary descriptions to the displayed quantity. The returned value is the display text used for Value Display and accessibility values, and does not affect internal quantity state or increment/decrement units. ```tsx import "./styles"; import { useSeedClassName } from "@seed-design/lynx-react"; import { QuantityPicker } from "@/components/ui/quantity-picker"; export default function Example() { const seedClassName = useSeedClassName({ colorMode: "system" }); return ( Value text `${valueText}개`} /> ); } ``` ### Controlled Using `value` and `onValueChange`, you can control quantity state with app state. Save the new number in the change callback and pass the saved value back as `value`. ```tsx import "./styles"; import { useState } from "@lynx-js/react"; import { useSeedClassName } from "@seed-design/lynx-react"; import { QuantityPicker } from "@/components/ui/quantity-picker"; export default function Example() { const seedClassName = useSeedClassName({ colorMode: "system" }); const [quantity, setQuantity] = useState(2); function handleValueChange(nextQuantity: number) { "background only"; setQuantity(nextQuantity); } return ( Controlled 현재 수량: {quantity}개 ); } ``` ### Removable Using `removable`, when the value reaches `min`, the Decrement button switches to a Remove button. In this case, pressing Decrement calls only `onRemove` without changing the quantity. Handle deletion or removal from the list in `onRemove`. ```tsx import "./styles"; import { useState } from "@lynx-js/react"; import { ActionButton, useSeedClassName, VStack } from "@seed-design/lynx-react"; import { QuantityPicker } from "@/components/ui/quantity-picker"; export default function Example() { const seedClassName = useSeedClassName({ colorMode: "system" }); const [removed, setRemoved] = useState(false); function handleRemove() { "background only"; setRemoved(true); } function handleRestore() { "background only"; setRemoved(false); } return ( Removable {removed ? ( 상품을 삭제했습니다. 되돌리기 ) : ( <> 최솟값에서 감소 버튼을 누르면 수량 선택기가 제거됩니다. )} ); } ``` ### Loading With `loading`, you can temporarily prevent all actions or specific actions from executing and display a loading indicator. `loading={true}` applies to both Decrement and Increment, and passing an object like `loading={{ increment: true }}` prevents only that action. ```tsx import "./styles"; import { useState } from "@lynx-js/react"; import { ActionButton, useSeedClassName } from "@seed-design/lynx-react"; import { QuantityPicker } from "@/components/ui/quantity-picker"; export default function Example() { const seedClassName = useSeedClassName({ colorMode: "system" }); const [decrementLoading, setDecrementLoading] = useState(false); const [incrementLoading, setIncrementLoading] = useState(false); const allLoading = decrementLoading && incrementLoading; function toggleAllLoading() { "background only"; const nextLoading = !allLoading; setDecrementLoading(nextLoading); setIncrementLoading(nextLoading); } function toggleDecrementLoading() { "background only"; setDecrementLoading((current) => !current); } function toggleIncrementLoading() { "background only"; setIncrementLoading((current) => !current); } return ( Loading 전체 {allLoading ? "끄기" : "켜기"} 감소 {decrementLoading ? "끄기" : "켜기"} 증가 {incrementLoading ? "끄기" : "켜기"} 감소: {decrementLoading ? "loading" : "준비"} · 증가:{" "} {incrementLoading ? "loading" : "준비"} ); } ``` `disabled`, `readOnly`, and `loading` all prevent actions from being executed. `disabled` disables both actions and passes disabled to accessibility state. `readOnly` prevents quantity changes and Remove callback calls but does not change the value to another state. `loading` temporarily prevents only the specified action and displays the action's indicator. Therefore, even at the minimum value of `removable`, if Decrement is disabled, readOnly, or decrement loading, `onRemove` is not called. ### Form Since Lynx does not have HTML `
`, hidden ``, or `FormData` submission models, it does not provide `inputProps`-based Form examples like the web. When you need to submit quantity, manage app state with `value` and `onValueChange`, and then put that state directly in the payload in the code that creates the submission or network request. This approach can separate `getValueText` for UI display and the raw numeric value to send to the server. ## Accessibility Provide `accessibility-label` describing the purpose of Quantity Picker to Root. Root is exposed by default with `accessibility-role-description="quantity picker"`, and if needed, you can directly specify native accessibility attributes like `accessibility-role-description` and `accessibility-value`. In the Registry convenience component, specify the accessibility name of each button with `decrementAccessibilityLabel` and `incrementAccessibilityLabel`. When the minimum value is reached in the `removable` state, the Decrement button switches to a Remove button, so also specify `removeAccessibilityLabel` describing the Remove action. The Registry defaults are `Decrease quantity`, `Increase quantity`, and `Delete item` respectively. In the compound API, directly specify `accessibility-label` on the Decrement/Increment buttons and pass `removeAccessibilityLabel` to Root. Buttons in the `disabled` state have disabled reflected in accessibility traits. In the `readOnly` and `loading` states, the action cannot be executed, so explain the state and restrictions together with surrounding labels or guidance text. The loading indicator is a visual display element and is hidden from accessibility elements. ## Differences from Web Version Lynx `QuantityPicker` has the following differences from the React web version. | | Web (React) | Lynx | | ----------------------- | --------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | | **Registry API** | `QuantityPicker` convenience component and `aria-*` props | `QuantityPicker` convenience component and `accessibility-*` props | | **Low-level API** | `Root`, `DecrementButton`, `ValueDisplay`, `IncrementButton`, `HiddenInput` | `Root`, `DecrementButton`, `ValueDisplay`, `IncrementButton` | | **Rendering** | Renders HTML elements and buttons | Renders Lynx native `` and accessibility elements, operates via tap | | **Accessibility names** | `aria-label`, `decrementAriaLabel`, `incrementAriaLabel`, `removeAriaLabel` | Root and slot's `accessibility-label`, Registry's `decrementAccessibilityLabel`, `incrementAccessibilityLabel`, `removeAccessibilityLabel` | | **Form submission** | Submit hidden input's `name`/`value` to HTML form with `inputProps` | HTML form, hidden input, `FormData`, `inputProps` not supported. Include app state's `value` directly in request payload | | **Compound API** | Can connect to browser form model with HiddenInput | App manages `value` and `onValueChange` without HiddenInput | Lynx does not have browser's HTML form validation and keyboard focus model, so it uses app state, native tap, and Lynx accessibility navigation instead of form submission, required validation, and DOM events.