-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
101 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { Button as NextButton } from '@nextui-org/react' | ||
import { Button as NextButton, ButtonGroup as NextButtonGroup } from '@nextui-org/react' | ||
import { withFragment } from '../../withFragment' | ||
|
||
export const Button = withFragment(NextButton, 'button'); | ||
export const ButtonGroup = withFragment(NextButtonGroup, 'buttonGroup'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './base'; | ||
export * from './chart/Chart'; | ||
export * from './color-input/ColorInput'; | ||
export * from './multi-switch/MultiSwitch'; | ||
export * from './color-selector/ColorSelector'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { MultiSwitch } from "./MultiSwitch"; | ||
|
||
// More on how to set up stories at: https://storybook.js.org/docs/react/writing-stories/introduction | ||
const meta: Meta<typeof MultiSwitch> = { | ||
title: "components/MultiSwitch", | ||
component: MultiSwitch, | ||
tags: ["autodocs"], | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
// More on writing stories with args: https://storybook.js.org/docs/react/writing-stories/args | ||
export const Small: Story = { | ||
args: { | ||
onValueChange: console.log, | ||
size: 'sm', | ||
}, | ||
}; | ||
|
||
export const Medium: Story = { | ||
args: { | ||
onValueChange: console.log, | ||
size: 'md', | ||
}, | ||
}; | ||
|
||
export const Large: Story = { | ||
args: { | ||
onValueChange: console.log, | ||
size: 'lg', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { forwardRef, useEffect, useState } from 'react'; | ||
import { Button, ButtonGroup } from '../base'; | ||
import { withFragment } from '../../withFragment'; | ||
import { IconCheck, IconSlash, IconX } from '@tabler/icons-react'; | ||
import { ButtonGroupProps, tv } from '@nextui-org/react'; | ||
|
||
/** | ||
* Primary UI component for selecting a color | ||
*/ | ||
|
||
export interface MultiSwitchProps extends Omit<ButtonGroupProps, 'className'> { | ||
defaultValue?: number; | ||
onValueChange: (value: number) => void; | ||
} | ||
|
||
const multiSwitch = tv({ | ||
slots: { | ||
button: 'min-w-0', | ||
}, | ||
variants: { | ||
size: { | ||
sm: { | ||
button: 'px-3' | ||
}, | ||
md: { | ||
button: 'px-4' | ||
}, | ||
lg: { | ||
button: 'px-5' | ||
} | ||
} | ||
} | ||
}); | ||
|
||
export const MultiSwitch = withFragment(forwardRef<HTMLInputElement, MultiSwitchProps>(({ | ||
defaultValue, | ||
onValueChange, | ||
size = 'md', | ||
...props | ||
}: MultiSwitchProps, ref) => { | ||
const [value, setValue] = useState(defaultValue ?? 0); | ||
const { button } = multiSwitch({ size }); | ||
|
||
useEffect(() => onValueChange && onValueChange(value), [value, onValueChange]); | ||
|
||
return ( | ||
<> | ||
<input ref={ref} value={value} type="number" className="hidden" min={0} max={2} required /> | ||
<ButtonGroup size={size} {...props}> | ||
<Button className={button()} onClick={() => setValue(0)} {...value === 0 ? { color: 'danger' } : {}}><IconX size={20} /></Button> | ||
<Button className={button()} onClick={() => setValue(1)} {...value === 1 ? { color: 'default', variant: 'solid' } : {}}><IconSlash size={20} /></Button> | ||
<Button className={button()} onClick={() => setValue(2)} {...value === 2 ? { color: 'success' } : {}}><IconCheck size={20} /></Button> | ||
</ButtonGroup> | ||
</> | ||
); | ||
}), 'multiSwitch'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters