Skip to content

Commit

Permalink
Toggle docs + preview (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
quietbits authored Oct 3, 2023
1 parent cf60f3c commit b393c59
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
19 changes: 19 additions & 0 deletions @stellar/design-system-website/docs/components/inputs/toggle.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
slug: /toggle
---

# Toggle

<ComponentDescription componentName="Toggle" />

## Example

```tsx live
<PreviewBlock componentName="Toggle">
<Toggle id="toggle" />
</PreviewBlock>
```

## Props

<ComponentProps componentName="Toggle" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ComponentPreview } from "@site/src/components/PreviewBlock";

export const togglePreview: ComponentPreview = {
options: [
{
type: "checkbox",
prop: "disabled",
label: "Disabled",
},
{
type: "select",
prop: "checked",
customValue: true,
options: [
{
value: "",
label: "Off",
},
{
value: "on",
label: "On",
},
],
},
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { radioButtonPreview } from "@site/src/componentPreview/radioButtonPrevie
import { selectPreview } from "@site/src/componentPreview/selectPreview";
import { textareaPreview } from "@site/src/componentPreview/textareaPreview";
import { titlePreview } from "@site/src/componentPreview/titlePreview";
import { togglePreview } from "@site/src/componentPreview/togglePreview";
import { tooltipPreview } from "@site/src/componentPreview/tooltipPreview";

// =============================================================================
Expand All @@ -50,6 +51,7 @@ const previews: { [key: string]: ComponentPreview } = {
Select: selectPreview,
Textarea: textareaPreview,
Title: titlePreview,
Toggle: togglePreview,
Tooltip: tooltipPreview,
};

Expand Down
23 changes: 20 additions & 3 deletions @stellar/design-system/src/components/Toggle/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
import React, { cloneElement, useEffect, useState } from "react";
import "./styles.scss";

interface ToggleProps extends React.InputHTMLAttributes<HTMLInputElement> {
/** */
export interface ToggleProps {
/** ID of the toggle should be unique */
id: string;
/** If `true` the toggle state is "on" */
checked: boolean;
/** Use a specific input rather than a generic HTML input (useful for Formik or otherwise controlled inputs) */
customInput?: React.ReactElement;
/** Function to handle the toggle state change */
onChange?: () => void;
/** Disable the toggle */
disabled?: boolean;
}

export const Toggle: React.FC<ToggleProps> = ({
interface Props
extends ToggleProps,
React.InputHTMLAttributes<HTMLInputElement> {
id: string;
onChange?: () => void;
checked: boolean;
}

/**
* `Toggle` component is similar to `Checkbox` component, which allows to toggle/switch between on and off states.
*/
export const Toggle: React.FC<Props> = ({
id,
checked,
customInput,
onChange,
disabled,
}: ToggleProps) => {
}: Props) => {
const [checkedValue, setCheckedValue] = useState(checked);

useEffect(() => {
Expand Down

0 comments on commit b393c59

Please sign in to comment.