/* * Copyright 2025 Palantir Technologies, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { useCallback, useState } from "react"; import { H5, HandleInteractionKind, Intent, MultiSlider, MultiSliderHandle, Radio, RadioGroup, Switch, } from "@blueprintjs/core"; import { Example, type ExampleProps, handleBooleanChange, handleValueChange, } from "@blueprintjs/docs-theme"; type ShownIntents = "danger" | "warning" | "both"; interface SliderValues { dangerStart: number; warningStart: number; warningEnd: number; dangerEnd: number; } const initialValues = { /* eslint-disable sort-keys */ dangerStart: 12, warningStart: 36, warningEnd: 72, dangerEnd: 90, /* eslint-enable sort-keys */ }; export const MultiSliderExample: React.FC = props => { const [interactionKind, setInteractionKind] = useState( HandleInteractionKind.PUSH, ); const [shownIntents, setShownIntents] = useState("both"); const [showTrackFill, setShowTrackFill] = useState(true); const [values, setValues] = useState(initialValues); const [vertical, setVertical] = useState(false); const showDanger = shownIntents !== "warning"; const showWarning = shownIntents !== "danger"; const getUpdatedHandles = useCallback( (newValues: number[]): Partial => { switch (shownIntents) { case "both": { const [dangerStart, warningStart, warningEnd, dangerEnd] = newValues; /* eslint-disable-next-line sort-keys */ return { dangerStart, warningStart, warningEnd, dangerEnd }; } case "danger": { const [dangerStart, dangerEnd] = newValues; /* eslint-disable-next-line sort-keys */ return { dangerStart, dangerEnd }; } case "warning": { const [warningStart, warningEnd] = newValues; /* eslint-disable-next-line sort-keys */ return { warningStart, warningEnd }; } } }, [shownIntents], ); const handleChange = useCallback( (rawValues: number[]) => { // newValues is always in sorted order, and handled cannot be unsorted by dragging with lock/push interactions. const newValuesMap = { ...values, ...getUpdatedHandles(rawValues) }; const newValues = Object.keys(newValuesMap).map( (key: string) => newValuesMap[key as keyof SliderValues], ); newValues.sort((a, b) => a - b); const [dangerStart, warningStart, warningEnd, dangerEnd] = newValues; /* eslint-disable-next-line sort-keys */ setValues({ dangerStart, warningStart, warningEnd, dangerEnd }); }, [getUpdatedHandles, values], ); const options = ( <>
Props
Handle interaction
Example
); return ( {/* up to four handles, toggle-able in pairs */} {showDanger && ( )} {showWarning && ( )} {showWarning && ( )} {showDanger && ( )} ); };