|
| 1 | +/* |
| 2 | + * Copyright (c) 2024. Devtron Inc. |
| 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 | + |
| 17 | +import { useState } from 'react' |
| 18 | +import type { Meta, StoryObj } from '@storybook/react' |
| 19 | + |
| 20 | +import { ComponentSizeType, iconMap, Switch, SWITCH_VARIANTS, SwitchProps } from '@devtron-labs/devtron-fe-common-lib' |
| 21 | + |
| 22 | +type BaseComponentPropsType = Omit<SwitchProps, 'onChange' | 'isChecked'> |
| 23 | + |
| 24 | +const BaseComponent = (props: BaseComponentPropsType) => { |
| 25 | + const [isChecked, setIsChecked] = useState<boolean>(false) |
| 26 | + |
| 27 | + const handleChange = () => { |
| 28 | + setIsChecked((prev) => !prev) |
| 29 | + } |
| 30 | + |
| 31 | + const switchProps = { |
| 32 | + ...props, |
| 33 | + isChecked, |
| 34 | + onChange: handleChange, |
| 35 | + } as SwitchProps |
| 36 | + |
| 37 | + return <Switch {...switchProps} /> |
| 38 | +} |
| 39 | + |
| 40 | +const meta = { |
| 41 | + component: BaseComponent, |
| 42 | + argTypes: { |
| 43 | + variant: { |
| 44 | + options: Object.keys(SWITCH_VARIANTS) as Array<keyof typeof SWITCH_VARIANTS>, |
| 45 | + control: { type: 'radio' }, |
| 46 | + }, |
| 47 | + shape: { |
| 48 | + options: ['rounded', 'square'], |
| 49 | + control: { type: 'radio' }, |
| 50 | + }, |
| 51 | + iconName: { |
| 52 | + options: [null, ...Object.keys(iconMap)], |
| 53 | + control: { type: 'select' }, |
| 54 | + }, |
| 55 | + iconColor: { |
| 56 | + control: { type: 'text' }, |
| 57 | + }, |
| 58 | + indeterminate: { |
| 59 | + options: [true, false], |
| 60 | + control: { type: 'radio' }, |
| 61 | + }, |
| 62 | + size: { |
| 63 | + options: [ComponentSizeType.medium, ComponentSizeType.small], |
| 64 | + control: { type: 'radio' }, |
| 65 | + }, |
| 66 | + isDisabled: { |
| 67 | + options: [true, false], |
| 68 | + control: { type: 'radio' }, |
| 69 | + }, |
| 70 | + isLoading: { |
| 71 | + options: [true, false], |
| 72 | + control: { type: 'radio' }, |
| 73 | + }, |
| 74 | + tooltipContent: { |
| 75 | + control: { type: 'text' }, |
| 76 | + }, |
| 77 | + }, |
| 78 | +} satisfies Meta<BaseComponentPropsType> |
| 79 | + |
| 80 | +type Story = StoryObj<typeof meta> |
| 81 | + |
| 82 | +const SWITCH_TEMPLATE: Story = { |
| 83 | + args: { |
| 84 | + name: 'switch-story', |
| 85 | + dataTestId: 'switch-story', |
| 86 | + variant: 'positive', |
| 87 | + size: ComponentSizeType.medium, |
| 88 | + isDisabled: false, |
| 89 | + isLoading: false, |
| 90 | + tooltipContent: '', |
| 91 | + shape: 'rounded', |
| 92 | + }, |
| 93 | +} |
| 94 | + |
| 95 | +export const Default: Story = { |
| 96 | + ...SWITCH_TEMPLATE, |
| 97 | + args: { |
| 98 | + ...SWITCH_TEMPLATE.args, |
| 99 | + } satisfies BaseComponentPropsType, |
| 100 | +} |
| 101 | + |
| 102 | +export default meta |
0 commit comments