|
| 1 | +import { Story, Meta } from '@storybook/react'; |
| 2 | + |
| 3 | +import React, { ComponentProps, useCallback, useEffect, useState } from 'react'; |
| 4 | + |
| 5 | +import { Slider } from './Slider'; |
| 6 | + |
| 7 | +export default { |
| 8 | + title: 'Atoms/Slider', |
| 9 | + component: Slider, |
| 10 | +} as Meta; |
| 11 | + |
| 12 | +const Template: Story<ComponentProps<typeof Slider>> = args => { |
| 13 | + const [value, setValue] = useState(args.value || 0); |
| 14 | + |
| 15 | + useEffect(() => { |
| 16 | + setValue(args.value || 0); |
| 17 | + }, [args.value]); |
| 18 | + |
| 19 | + const handleChange = useCallback( |
| 20 | + (newValue: number | number[], thumbIndex: number) => { |
| 21 | + setValue(newValue); |
| 22 | + if (args.onChange) { |
| 23 | + args.onChange(newValue, thumbIndex); |
| 24 | + } |
| 25 | + }, |
| 26 | + [args], |
| 27 | + ); |
| 28 | + |
| 29 | + return <Slider {...args} value={value} onChange={handleChange} />; |
| 30 | +}; |
| 31 | + |
| 32 | +const RangeSliderTemplate: Story<ComponentProps<typeof Slider>> = ({ |
| 33 | + value: initialValue, |
| 34 | + ...args |
| 35 | +}) => { |
| 36 | + const [value, setValue] = useState<number | number[]>( |
| 37 | + initialValue || [30, 70], |
| 38 | + ); |
| 39 | + |
| 40 | + const handleChange = (newValue: number | number[], thumbIndex: number) => { |
| 41 | + if (Array.isArray(newValue)) { |
| 42 | + setValue(newValue); |
| 43 | + } |
| 44 | + if (args.onChange) args.onChange(newValue, thumbIndex); |
| 45 | + }; |
| 46 | + |
| 47 | + const handleAfterChange = ( |
| 48 | + newValue: number | number[], |
| 49 | + thumbIndex: number, |
| 50 | + ) => { |
| 51 | + if (Array.isArray(newValue)) { |
| 52 | + setValue(newValue); |
| 53 | + } |
| 54 | + if (args.onAfterChange) args.onAfterChange(newValue, thumbIndex); |
| 55 | + }; |
| 56 | + |
| 57 | + return ( |
| 58 | + <Slider |
| 59 | + {...args} |
| 60 | + value={value} |
| 61 | + onChange={handleChange} |
| 62 | + onAfterChange={handleAfterChange} |
| 63 | + /> |
| 64 | + ); |
| 65 | +}; |
| 66 | + |
| 67 | +export const Basic = Template.bind({}); |
| 68 | +Basic.args = { |
| 69 | + value: 50, |
| 70 | + dataAttribute: 'slider-basic', |
| 71 | +}; |
| 72 | + |
| 73 | +Basic.argTypes = { |
| 74 | + value: { |
| 75 | + control: 'number', |
| 76 | + description: 'The value of the slider', |
| 77 | + }, |
| 78 | + min: { |
| 79 | + control: 'number', |
| 80 | + description: 'The minimum value of the slider', |
| 81 | + }, |
| 82 | + max: { |
| 83 | + control: 'number', |
| 84 | + description: 'The maximum value of the slider', |
| 85 | + }, |
| 86 | + step: { |
| 87 | + control: 'number', |
| 88 | + description: |
| 89 | + 'Value to be added or subtracted on each step the slider makes. Must be greater than zero. max - min should be evenly divisible by the step value', |
| 90 | + }, |
| 91 | + onChange: { |
| 92 | + action: 'onChange', |
| 93 | + description: |
| 94 | + 'Callback called on every value change. The function will be called with two arguments, the first being the new value(s) the second being thumb index', |
| 95 | + }, |
| 96 | + onAfterChange: { |
| 97 | + action: 'onAfterChange', |
| 98 | + description: |
| 99 | + 'Callback called only after moving a thumb has ended. The callback will only be called if the action resulted in a change. The function will be called with two arguments, the first being the result value(s) the second being thumb index', |
| 100 | + }, |
| 101 | + disabled: { |
| 102 | + control: 'boolean', |
| 103 | + description: "If true the thumbs can't be moved", |
| 104 | + }, |
| 105 | + className: { |
| 106 | + control: 'text', |
| 107 | + description: 'The class name to apply to the Slider', |
| 108 | + }, |
| 109 | + thumbClassName: { |
| 110 | + control: 'text', |
| 111 | + description: 'The css class set on each thumb node', |
| 112 | + }, |
| 113 | + trackClassName: { |
| 114 | + control: 'text', |
| 115 | + description: |
| 116 | + 'The css class set on the tracks between the thumbs. In addition track fragment will receive a numbered css class of the form {trackClassName}-{i}, e.g. track-0, track-1, ...', |
| 117 | + }, |
| 118 | + thumbActiveClassName: { |
| 119 | + control: 'text', |
| 120 | + description: 'The css class set on the thumb that is currently being moved', |
| 121 | + }, |
| 122 | + dataAttribute: { |
| 123 | + control: 'text', |
| 124 | + description: 'The data attribute to apply to the Slider', |
| 125 | + }, |
| 126 | + isSimple: { |
| 127 | + control: 'boolean', |
| 128 | + description: 'If false applies the rangeSlider class to the Slider', |
| 129 | + }, |
| 130 | +}; |
| 131 | + |
| 132 | +export const RangeSlider = RangeSliderTemplate.bind({}); |
| 133 | +RangeSlider.args = { |
| 134 | + value: [20, 80], |
| 135 | + step: 5, |
| 136 | + thumbClassName: 'bg-primary', |
| 137 | + trackClassName: 'bg-primary', |
| 138 | + dataAttribute: 'slider-double', |
| 139 | + isSimple: false, |
| 140 | +}; |
| 141 | + |
| 142 | +RangeSlider.argTypes = { |
| 143 | + ...Basic.argTypes, |
| 144 | +}; |
| 145 | + |
| 146 | +export const DRangeSlider = RangeSliderTemplate.bind({}); |
| 147 | +DRangeSlider.args = { |
| 148 | + value: [20, 80], |
| 149 | + step: 10, |
| 150 | + className: 'w-96 m-auto', |
| 151 | + thumbClassName: 'bg-success ring-2 ring-success', |
| 152 | + thumbActiveClassName: 'outline-2 outline-sov-white', |
| 153 | + trackClassName: 'bg-success', |
| 154 | + dataAttribute: 'slider-styled', |
| 155 | + isSimple: false, |
| 156 | +}; |
| 157 | + |
| 158 | +DRangeSlider.argTypes = { |
| 159 | + ...Basic.argTypes, |
| 160 | +}; |
0 commit comments