# react-length-picker

> A simple length picker that works with metric and imperial units

Latest version **3.0.5** (published 2024-07-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-length-picker
pnpm add react-length-picker
yarn add react-length-picker
bun add react-length-picker
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.5 |
| Published | 2024-07-25 |
| First published | 2024-02-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 60.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Markus Kurzmann |
| Maintainers | maks-io |
| Keywords | react, length, cm, inches, feet, measurements, centimeters, meters, height |

## Links

- npm: https://www.npmjs.com/package/react-length-picker
- Repository: https://github.com/maks-io/react-length-picker
- Homepage: https://github.com/maks-io/react-length-picker#readme
- Issues: https://github.com/maks-io/react-length-picker/issues
- npm.io page: https://npm.io/package/react-length-picker

## Dependencies (2)

- [virtuosa](https://npm.io/package/virtuosa.md) ^1.0.3
- [lodash.range](https://npm.io/package/lodash.range.md) ^3.2.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 3.0.5 (latest) — 2024-07-25
- 3.0.4 — 2024-07-09
- 3.0.3 — 2024-07-07
- 3.0.2 — 2024-07-07
- 3.0.1 — 2024-07-07
- 3.0.0 — 2024-07-07
- 2.0.3 — 2024-06-30
- 2.0.2 — 2024-06-30
- 2.0.1 — 2024-06-30
- 2.0.0 — 2024-06-30
- 1.0.3 — 2024-02-20
- 1.0.2 — 2024-02-18
- 1.0.1 — 2024-02-13
- 1.0.0 — 2024-02-03

## README

# react-length-picker 📏

[![Version](https://img.shields.io/npm/v/react-length-picker)](https://www.npmjs.com/package/react-length-picker)

A simple length picker that works with metric and imperial units

## Teaser

<img src="./documentation/reactLengthPickerGif01.gif" height="200px" alt="react-length-picker-gif" />

[(YouTube Teaser here)](https://youtube.com/shorts/hfGCPGMwaUc?feature=share)

## Live Demo

https://stackblitz.com/edit/react-length-picker-demo

## Install

```
npm install --save react-length-picker
```

Or if you use Yarn:

```
yarn add react-length-picker
```

## Usage

```tsx
import { LengthPicker } from "react-length-picker";

// ...

return (
  <SomeReactComponent>
    <LengthPicker
      unit={"metric"} // set current unit (metric vs. imperial)
      length={350} // set default length
      metricMin={300} // set minimum value for metric scale (in cm)
      metricMax={400} // set maximum value for metric scale (in cm)
      metricStep={1} // set interval/step for metric scale (in cm)
      // hint: you could use the `metricToImperial` helper function to define the following props (or vice versa)
      imperialMin={118} // set minimum value for imperial scale (in inches)
      imperialMax={157} // set maximum value for imperial scale (in inches)
      imperialStep={1} // set interval/step for imperial scale (in inches)
      // ...and many more optional props - see props section below
    />
  </SomeReactComponent>
);
```

## Props

| prop                   | required | default                                                 | type                                                                                                                                                                                                   | description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| ---------------------- | -------- | ------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ascending              | no       | `true`                                                  | `boolean`                                                                                                                                                                                              | Defines the order of available values. `true` could lead to a range like `[300, 301, 302, ..., 400]`, while false would make it `[400, 399, 398, ..., 300]`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| backgroundRenderNumber | no       | `20`                                                    | `number`                                                                                                                                                                                               | Defines the number of elements that should be rendered even if they are outside of the visible area (increasing this could be useful if users are scrolling in a fast way and unrendered placeholder elements become visible). This optional value is being forwarded to the underlying [virtuosa](https://www.npmjs.com/package/virtuosa) library and defaults to `20`.                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| containerHeight        | no       | `80`                                                    | `number`                                                                                                                                                                                               | Defines the height of the length picker.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| containerStyle         | no       | `undefined`                                             | `React.CSSProperties`                                                                                                                                                                                  | Let's you add additional css style to the main container.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| containerWidth         | no       | `160`                                                   | `number`                                                                                                                                                                                               | Defines the width of the length picker.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| disabled               | no       | `false`                                                 | `boolean`                                                                                                                                                                                              | Disables the entire length picker, meaning events like clicks, touches, scrolls, etc. are being ignored. Also, by default, the lenght picker style will change to grayscale.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| entryContainerStyle    | no       | `undefined`                                             | `React.CSSProperties \| ((index: number, currentMetricValue: number, currentImperialValue: number, isActive: boolean, visibilityStatus: IVisibilityStatus, disabled: boolean) => React.CSSProperties)` | Let's you adjust the style of one individual height entry within the list. It can either be a standard css style object, or also a function that returns one. The function itself receives multiple properties representing the entry's index, if it's currently in view and whether the picker is disabled - allowing you more styling flexibility.                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| entryContentStyle      | no       | `undefined`                                             | `React.CSSProperties \| ((index: number, currentMetricValue: number, currentImperialValue: number, isActive: boolean, disabled: boolean) => React.CSSProperties)`                                      | Same as above, but refers to the entry's content, not its container.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| entryHeight            | no       | `40`                                                    | `number`                                                                                                                                                                                               | Defines the height of one individual list entry.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| hideScrollbar          | no       | `true`                                                  | `boolean`                                                                                                                                                                                              | Let's you hide or show a picker's scrollbar.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| imperialFormatter      | no       | (check the source code for seeing the default function) | `( isPrimary: boolean, inches: number, feetAndInches: FeetAndInches, index: number ) => string \| number \| React.ReactNode`                                                                           | Let's you format imperial list entries. The function has access to the following props: `isPrimary`, indicating if the imperial unit is currently the active one, `inches`, representing the corresponding length in inches, `feetAndInches`, and object that separates inches from feet, and `index`, representing the entry's index within the list.                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| imperialMax            | no       | `157`                                                   | `number`                                                                                                                                                                                               | Defines the max value for the imperial length range.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| imperialMin            | no       | `118`                                                   | `number`                                                                                                                                                                                               | Defines the min value for the imperial length range.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| imperialStep           | no       | `1`                                                     | `number`                                                                                                                                                                                               | Defines the step value (interval) for the imperial length range.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| keyName                | yes      | -                                                       | `string`                                                                                                                                                                                               | A key of your choice, uniquely identifying this length picker.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| length                 | no       | `350`                                                   | `number`                                                                                                                                                                                               | Defines the default value when the length picker is mounted. Make sure it is within the min-max range of the current unit.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| metricFormatter        | no       | (check the source code for seeing the default function) | `( isPrimary: boolean, inches: number, feetAndInches: FeetAndInches, index: number ) => string \| number \| React.ReactNode`                                                                           | Let's you format metric list entries. The function has access to the following props: `isPrimary`, indicating if the metric unit is currently the active one, `centimeters`, representing the corresponding length in centimeters and `index`, representing the entry's index within the list.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| metricMax              | no       | `157`                                                   | `number`                                                                                                                                                                                               | Defines the max value for the metric length range.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| metricMin              | no       | `118`                                                   | `number`                                                                                                                                                                                               | Defines the min value for the metric length range.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| metricStep             | no       | `1`                                                     | `number`                                                                                                                                                                                               | Defines the step value (interval) for the metric length range.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| onLengthChange         | no       | `() => {}`                                              | `(length: number) => void`                                                                                                                                                                             | Defines the callback function when the length is changed via the length picker.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| onLengthChangeTrigger  | no       | `"CENTERED"`                                            | `"CENTERED" \| "CENTERED_EXACTLY_ONLY"`                                                                                                                                                                | Defines when the length picker would trigger a change event. This is directly connected to the underlying [virtuosa](https://www.npmjs.com/package/virtuosa) library. A value of `"CENTERED_EXACTLY_ONLY"` means, that a change event will only be triggered once the length picker has an element vertically centered, precisely. If you use `"CENTERED"` instead, elements that represent the "active" element will also trigger change events, even if they are not precisely centered yet (for instance before the CSS snap mechanism kicks in). In other words, the latter variant will trigger way more change events than the former one, in most scenarios. Please note, how you additionally also have the throttle mechanism for further tweaking its behaviour (see `throttleWait` prop below). |
| onUnitChange           | no       | `() => {}`                                              | `(unit: Unit) => void`                                                                                                                                                                                 | Defines the callback function when the unit is changed via the length picker. The `Unit` type can be either `"metric"` or `"imperial"`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| throttleWait           | no       | `32`                                                    | `number`                                                                                                                                                                                               | Defines whether the change event should be throttled. If set to `0` no throttling will occur. Every other number represent a time value in milliseconds. This optional value is being forwarded to the underlying [virtuosa](https://www.npmjs.com/package/virtuosa) library.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| unit                   | no       | `"metric"`                                              | `Unit`                                                                                                                                                                                                 | Defines the current unit of the length picker. The `Unit` type can be either `"metric"` or `"imperial"`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |

## Ideas / Open Tasks

- Add possibility to also override css styling for `SecondaryListEntry`
- Improve TypeScript (some redundancy in various interfaces, use inheritance etc.)

## If you enjoy using this...

<a href="https://www.buymeacoffee.com/maks_io" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 48px !important;" ></a>

---
_Source: https://npm.io/package/react-length-picker · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
