-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple CycleButton and use it in three way checkboxes
- Loading branch information
Showing
5 changed files
with
117 additions
and
30 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
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
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
6 changes: 6 additions & 0 deletions
6
packages/cyberstorm/src/newComponents/CycleButton/CycleButton.css
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,6 @@ | ||
@layer components { | ||
.ts-cyclebutton { | ||
display: inline-flex; | ||
align-items: center; | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
packages/cyberstorm/src/newComponents/CycleButton/CycleButton.tsx
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,85 @@ | ||
import "./CycleButton.css"; | ||
import React, { useState } from "react"; | ||
import { classnames } from "../../utils/utils"; | ||
import { | ||
Actionable, | ||
ActionableButtonProps, | ||
} from "../../primitiveComponents/Actionable/Actionable"; | ||
|
||
interface CycleButtonProps | ||
extends Omit<ActionableButtonProps, "primitiveType"> { | ||
noState?: boolean; | ||
value?: string; | ||
onInterract?: () => void; | ||
options?: string[]; | ||
onValueChange?: (value: string) => void; | ||
} | ||
|
||
/** | ||
* Button for cycling through values | ||
* If you want to handle state outside of this component use the "noState" param | ||
* Notes for handling state inside this component: | ||
* - First value of the list will be the initial value | ||
* - You can access the value from onValueChange. It's called each time value changes. | ||
*/ | ||
export const CycleButton = React.forwardRef< | ||
HTMLButtonElement, | ||
CycleButtonProps | ||
>((props: CycleButtonProps, forwardedRef) => { | ||
const { | ||
children, | ||
rootClasses, | ||
noState = false, | ||
onInterract, | ||
options, | ||
onValueChange, | ||
...forwardedProps | ||
} = props; | ||
|
||
if (noState) { | ||
return ( | ||
<Actionable | ||
{...forwardedProps} | ||
primitiveType={"button"} | ||
rootClasses={classnames("ts-cyclebutton", rootClasses)} | ||
ref={forwardedRef} | ||
onClick={onInterract ? () => onInterract() : undefined} | ||
> | ||
{children} | ||
</Actionable> | ||
); | ||
} | ||
|
||
const initialValue = options && options.length > 0 ? options[0] : ""; | ||
const [currentValue, setCurrentValue] = useState<string>(initialValue); | ||
|
||
return ( | ||
<Actionable | ||
{...forwardedProps} | ||
primitiveType={"button"} | ||
rootClasses={classnames("ts-cyclebutton", rootClasses)} | ||
ref={forwardedRef} | ||
onClick={() => { | ||
if (options && options.length > 0) { | ||
const currentValueIndex = options?.indexOf(currentValue); | ||
if ( | ||
currentValueIndex === -1 || | ||
currentValueIndex === options.length - 1 | ||
) { | ||
setCurrentValue(options[0]); | ||
} else { | ||
setCurrentValue(options[currentValueIndex + 1]); | ||
} | ||
} | ||
if (onInterract) { | ||
onInterract(); | ||
} | ||
}} | ||
onChange={onValueChange ? () => onValueChange(currentValue) : undefined} | ||
> | ||
{children} | ||
</Actionable> | ||
); | ||
}); | ||
|
||
CycleButton.displayName = "CycleButton"; |