From 58972f0a119e4aa9fb11967c954ff7809e9e3cb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20G=C5=82ownia?= Date: Wed, 17 Jan 2024 14:46:11 +0100 Subject: [PATCH] feat(ui-toolkit): add leverage slider component (#5594) --- .../slider/leverage-slider.stories.tsx | 36 ++++++++++++ .../src/components/slider/leverage-slider.tsx | 58 +++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 libs/ui-toolkit/src/components/slider/leverage-slider.stories.tsx create mode 100644 libs/ui-toolkit/src/components/slider/leverage-slider.tsx diff --git a/libs/ui-toolkit/src/components/slider/leverage-slider.stories.tsx b/libs/ui-toolkit/src/components/slider/leverage-slider.stories.tsx new file mode 100644 index 000000000..198658214 --- /dev/null +++ b/libs/ui-toolkit/src/components/slider/leverage-slider.stories.tsx @@ -0,0 +1,36 @@ +import type { Story, Meta } from '@storybook/react'; +import { LeverageSlider } from './leverage-slider'; +import { useState } from 'react'; + +export default { + component: LeverageSlider, + title: 'LeverageSlider', +} as Meta; + +const Template: Story = ({ value: val, min, max, ...args }) => { + const [value, setValue] = useState(val); + + const onValueChange = (val: [number]) => { + setValue(val); + }; + + return ( + <> + +
{value}
+ + ); +}; + +export const Default = Template.bind({}); + +Default.args = { + max: 100, + step: 0.1, + value: [100], +}; diff --git a/libs/ui-toolkit/src/components/slider/leverage-slider.tsx b/libs/ui-toolkit/src/components/slider/leverage-slider.tsx new file mode 100644 index 000000000..91ec29747 --- /dev/null +++ b/libs/ui-toolkit/src/components/slider/leverage-slider.tsx @@ -0,0 +1,58 @@ +import * as SliderPrimitive from '@radix-ui/react-slider'; +import type { SliderProps } from '@radix-ui/react-slider'; +import classNames from 'classnames'; + +export const LeverageSlider = ( + props: Omit & Required> +) => { + const step = [2, 5, 10, 20, 25].find((step) => props.max / step <= 6); + const min = 1; + const value = props.value?.[0] || props.defaultValue?.[0]; + return ( + + + + + {step && + new Array(Math.floor(props.max / step) + 1) + .fill(null) + .map((v, i) => { + const labelValue = step * i || 1; + const higherThanValue = value && labelValue > value; + return ( + + + {labelValue}x + + ); + })} + + + + + + + + ); +};