|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { Meta, StoryFn } from '@storybook/react-webpack5'; |
| 3 | +import { RangeSelect } from './RangeSelect'; |
| 4 | + |
| 5 | +export default { |
| 6 | + title: 'Components/RangeSelect', |
| 7 | + component: RangeSelect, |
| 8 | + argTypes: { |
| 9 | + placeholder: { control: { type: 'text' } }, |
| 10 | + isDisabled: { control: { type: 'boolean' } }, |
| 11 | + isInvalid: { control: { type: 'boolean' } }, |
| 12 | + isFullWidth: { control: { type: 'boolean' } }, |
| 13 | + onChange: { action: 'Range Changed' }, |
| 14 | + onClose: { action: 'Dropdown Closed' }, |
| 15 | + color: { |
| 16 | + control: { type: 'text' }, |
| 17 | + }, |
| 18 | + backgroundColor: { |
| 19 | + control: { type: 'text' }, |
| 20 | + }, |
| 21 | + borderColor: { |
| 22 | + control: { type: 'text' }, |
| 23 | + }, |
| 24 | + size: { |
| 25 | + options: ['xs', 'sm', 'md', 'lg'], |
| 26 | + defaultValue: 'md', |
| 27 | + control: { type: 'radio' }, |
| 28 | + }, |
| 29 | + variant: { |
| 30 | + options: ['filled', 'unstyled', 'flushed', 'outline'], |
| 31 | + defaultValue: 'outline', |
| 32 | + control: { type: 'radio' }, |
| 33 | + }, |
| 34 | + }, |
| 35 | +} as Meta<typeof RangeSelect>; |
| 36 | + |
| 37 | +const Template: StoryFn<typeof RangeSelect> = (args) => <RangeSelect {...args} />; |
| 38 | + |
| 39 | +export const Default = Template.bind({}); |
| 40 | +Default.args = { |
| 41 | + placeholder: 'Select a range', |
| 42 | + rangeFromLabel: 'From', |
| 43 | + rangeToLabel: 'To', |
| 44 | +}; |
| 45 | + |
| 46 | +export const WithValidation: StoryFn<typeof RangeSelect> = (args) => { |
| 47 | + const [value, setValue] = useState<string[]>(['', '']); |
| 48 | + const [fromError, setFromError] = useState<string>(''); |
| 49 | + const [toError, setToError] = useState<string>(''); |
| 50 | + |
| 51 | + const handleChange = (newValue: string[]) => { |
| 52 | + setValue(newValue); |
| 53 | + |
| 54 | + // Validation logic |
| 55 | + const rangeMin = 1; |
| 56 | + const rangeMax = 100; |
| 57 | + const fromStr = newValue[0] ?? ''; |
| 58 | + const toStr = newValue[1] ?? ''; |
| 59 | + const fromNum = fromStr === '' ? undefined : Number(fromStr); |
| 60 | + const toNum = toStr === '' ? undefined : Number(toStr); |
| 61 | + |
| 62 | + let nextFromError = ''; |
| 63 | + let nextToError = ''; |
| 64 | + |
| 65 | + if (fromNum !== undefined && !Number.isNaN(fromNum)) { |
| 66 | + if (fromNum < rangeMin) nextFromError = `Value must be at least ${rangeMin}`; |
| 67 | + else if (fromNum > rangeMax) nextFromError = `Value must not exceed ${rangeMax}`; |
| 68 | + } |
| 69 | + |
| 70 | + if (toNum !== undefined && !Number.isNaN(toNum)) { |
| 71 | + if (toNum < rangeMin) nextToError = `Value must be at least ${rangeMin}`; |
| 72 | + else if (toNum > rangeMax) nextToError = `Value must not exceed ${rangeMax}`; |
| 73 | + } |
| 74 | + |
| 75 | + if ( |
| 76 | + nextFromError === '' && |
| 77 | + nextToError === '' && |
| 78 | + fromNum !== undefined && |
| 79 | + toNum !== undefined && |
| 80 | + !Number.isNaN(fromNum) && |
| 81 | + !Number.isNaN(toNum) && |
| 82 | + fromNum > toNum |
| 83 | + ) { |
| 84 | + nextFromError = 'The minimum value cannot be greater than the maximum value'; |
| 85 | + } |
| 86 | + |
| 87 | + setFromError(nextFromError); |
| 88 | + setToError(nextToError); |
| 89 | + }; |
| 90 | + |
| 91 | + return ( |
| 92 | + <RangeSelect |
| 93 | + {...args} |
| 94 | + rangeFromError={fromError} |
| 95 | + rangeToError={toError} |
| 96 | + value={value} |
| 97 | + onChange={handleChange} |
| 98 | + /> |
| 99 | + ); |
| 100 | +}; |
| 101 | + |
| 102 | +WithValidation.args = { |
| 103 | + ...Default.args, |
| 104 | + placeholder: 'Select a range (1-100)', |
| 105 | +}; |
0 commit comments