UNPKG

2.55 kBTypeScriptView Raw
1/*
2 * Copyright 2016 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import * as React from "react";
18import { polyfill } from "react-lifecycles-compat";
19
20import { AbstractPureComponent2, Intent } from "../../common";
21import * as Errors from "../../common/errors";
22import { DISPLAYNAME_PREFIX } from "../../common/props";
23import { ISliderBaseProps, MultiSlider } from "./multiSlider";
24
25export type NumberRange = [number, number];
26
27enum RangeIndex {
28 START = 0,
29 END = 1,
30}
31
32// eslint-disable-next-line deprecation/deprecation
33export type RangeSliderProps = IRangeSliderProps;
34/** @deprecated use RangeSliderProps */
35export interface IRangeSliderProps extends ISliderBaseProps {
36 /**
37 * Range value of slider. Handles will be rendered at each position in the range.
38 *
39 * @default [0, 10]
40 */
41 value?: NumberRange;
42
43 /** Callback invoked when the range value changes. */
44 onChange?(value: NumberRange): void;
45
46 /** Callback invoked when a handle is released. */
47 onRelease?(value: NumberRange): void;
48}
49
50@polyfill
51export class RangeSlider extends AbstractPureComponent2<RangeSliderProps> {
52 public static defaultProps: RangeSliderProps = {
53 ...MultiSlider.defaultSliderProps,
54 intent: Intent.PRIMARY,
55 value: [0, 10],
56 };
57
58 public static displayName = `${DISPLAYNAME_PREFIX}.RangeSlider`;
59
60 public render() {
61 const { value, ...props } = this.props;
62 return (
63 <MultiSlider {...props}>
64 <MultiSlider.Handle value={value![RangeIndex.START]} type="start" intentAfter={props.intent} />
65 <MultiSlider.Handle value={value![RangeIndex.END]} type="end" />
66 </MultiSlider>
67 );
68 }
69
70 protected validateProps(props: RangeSliderProps) {
71 const { value } = props;
72 if (value == null || value[RangeIndex.START] == null || value[RangeIndex.END] == null) {
73 throw new Error(Errors.RANGESLIDER_NULL_VALUE);
74 }
75 }
76}