---
title: Quantity Selector
---

import LibraryInfo from '@theme/LibraryInfo'
import { ExternalCodeBlock, Feedback, TokensTable, PropsTable } from '../../src/components'
import { QuantitySelector } from '@telus-uds/components-web'
import { QuantitySelector as WebDocs } from 'docs/@telus-uds/components-web'
import AvailableOnWebOnly from '../../partials/_platform-considerations-web-only.mdx'

# Quantity Selector

<LibraryInfo
  packageType="Web Component"
  figmaLink="https://www.figma.com/embed?embed_host=share&url=https%3A%2F%2FXwww.figma.com/embed?embed_host=share&url=https%3A%2F%2Fwww.figma.com%2Ffile%2FTkOGIVaatADU8fDgNEA3GW%2FAllium-Community-Components-Work-in-Progress%3Fnode-id%3D1702%253A35723"
/>

## Introduction

Use `QuantitySelector` to easily decrease and increase an integer value.

[Follow the appropriate instructions](importing-components) to add this component in to your app.

## Guidance

### Setting Default Value

Use `defaultValue` to set `QuantitySelector` starting value if necessary:

```jsx live
<QuantitySelector defaultValue={2} accessibilityLabel="'Quantity selector'" />
```

### Tracking entered values

You can use `onChange` prop to track value change:

```jsx live
<QuantitySelector onChange={(amount) => alert('Value has changed')} />
```

### Controlled usage

```jsx live
function ControlledQuantitySelector() {
  const [quantity, setQuantity] = React.useState(2)
  return <QuantitySelector value={quantity} onChange={(value) => setQuantity(value)} />
}
```

### Defining Min and Max values

This component supports value validation, you can use `minNumber` and `maxNumber` to define minimum and maximum number accepted by the input.
In case the user tries to input a value out of this range, the component will display an alert.

```jsx live
<QuantitySelector minNumber={2} defaultValue={2} maxNumber={5} />
```

### Using a Label

You can use the `label` property to attach a label to this component:

```jsx live
<QuantitySelector label="Some descriptive information" />
```

### Adding a Hint and Hint position

When passing `hint` and `label` props, the component will show an additional text and using `hintPosition` set to `'inline'` (default) or `'below'` you can
specify where the hint will be positioned:

```jsx live
<>
  <QuantitySelector label="Quantity Selector" hint="Give the user some help" />{' '}
  <QuantitySelector label="Quantity Selector" hintPosition="below" hint="Give the user some help" />
</>
```

### Tooltip

This component has support for a tooltip: whenever `tooltip` prop is defined it, will add a question mark which will display
a tooltip when clicked. You can also combine `tooltip` property with `hint` and `hintPosition`:

```jsx live
<>
  <QuantitySelector
    label="Quantity Selector"
    tooltip="I'm a tooltip"
    hint="Give the user some help"
  />
  <QuantitySelector
    label="Quantity Selector"
    tooltip="I'm a tooltip"
    hintPosition="below"
    hint="Give the user some help"
  />
</>
```

### Custom `dictionary` prop

While `QuantitySelector` has a default dictionary for various error messages and label values for both `en` and `fr` copy, these messages can be overridden by passing a custom `dictionary` prop as shown in the example below.

:::caution
Should you choose to pass a custom `dictionary` prop, you have to provide all the properties in the dictionary for both `en` and `fr`, otherwise the component will not render as expected as it completely overrides the default dictionary instead of merging.
:::

```jsx live
<>
  <QuantitySelector
    dictionary={{
      en: {
        accessibility: {
          a11yLabel: 'Quantity selector',
          increaseButton: 'Increase quantity',
          decreaseButton: 'Decrease quantity'
        },
        errors: {
          numberIsTooSmall: (minNumber) =>
            `You can enter a number equal or greater than ${minNumber}.`,
          numberIsTooBig: (maxNumber) => `You can enter a number equal or less than ${maxNumber}.`,
          invalidCharacters:
            'Try using numbers only. Alphabetical or symbols characters are not allowed.'
        }
      },
      fr: {
        accessibility: {
          a11yLabel: 'Sélecteur de quantité',
          increaseButton: `Augmenter la quantité`,
          decreaseButton: `Réduire la quantité`
        },
        errors: {
          numberIsTooSmall: (minNumber) =>
            `Vous pouvez saisir un nombre égal ou supérieur à ${minNumber}.`,
          numberIsTooBig: (maxNumber) =>
            `Vous pouvez saisir un nombre égal ou inférieur à ${maxNumber}.`,
          invalidCharacters: `Essayez d'utiliser seulement des chiffres. Les caractères alphabétiques ou les symboles ne sont pas permis.`
        }
      }
    }}
  />
</>
```

### Platform considerations

<AvailableOnWebOnly />

## Accessibility

Make sure to use accessible text when hiding the label.

## Props

<PropsTable web={WebDocs} />

## Tokens

<TokensTable web={WebDocs} component="QuantitySelector" />

## Variants

You can pass `alternative` to give a rounded look to the component:

```jsx live
<QuantitySelector label="Alternative style" variant={{ alternative: true }} />
```

## Feedback

<Feedback component="QuantitySelector" />
