1.0.3 • Published 2 months ago

react-length-picker v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

react-length-picker 📏

Version

A simple length picker that works with metric and imperial units

Teaser

(YouTube Teaser here)

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

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

// ...

return (
  <SomeReactComponent>
    <LengthPicker
      unit={"metric"} // set current unit (metric vs. imperial)
      defaultLength={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)
      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

proprequireddefaulttypedescription
ascendingnotruebooleanDefines 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].
containerHeightno80numberDefines the height of the length picker.
containerStylenoundefinedReact.CSSPropertiesLet's you add additional css style to the main container.
containerWidthno160numberDefines the width of the length picker.
defaultLengthno350numberDefines the default value when the length picker is mounted. Make sure it is within the min-max range of the current unit.
entryContainerStylenoundefinedReact.CSSProperties \| ((index: number, currentMetricValue: number, currentImperialValue: number, isActive: 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 two properties representing the entry's index as well as if it's currently in view, allowing you more styling flexibility.
entryContentStylenoundefinedReact.CSSProperties \| ((index: number, currentMetricValue: number, currentImperialValue: number, isActive: boolean) => React.CSSProperties)Same as above, but refers to the entry's content, not its container.
entryHeightno40numberDefines the height of one individual list entry.
imperialFormatterno(check the source code for seeing the default function)( isPrimary: boolean, inches: number, feetAndInches: FeetAndInches, index: number ) => string \| number \| React.ReactNodeLet'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.
imperialMaxno157numberDefines the max value for the imperial length range.
imperialMinno118numberDefines the min value for the imperial length range.
imperialStepno1numberDefines the step value (interval) for the imperial length range.
metricFormatterno(check the source code for seeing the default function)( isPrimary: boolean, inches: number, feetAndInches: FeetAndInches, index: number ) => string \| number \| React.ReactNodeLet'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.
metricMaxno157numberDefines the max value for the metric length range.
metricMinno118numberDefines the min value for the metric length range.
metricStepno1numberDefines the step value (interval) for the metric length range.
onLengthChangeno() => {}(length: number) => voidDefines the callback function when the length is changed via the length picker.
onUnitChangeno() => {}(unit: Unit) => voidDefines the callback function when the unit is changed via the length picker. The Unit type can be either "metric" or "imperial".
unitno"metric"UnitDefines 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...