Skip to content

Commit

Permalink
Merge pull request #173 from Dias999/feature/Storybook-Switch
Browse files Browse the repository at this point in the history
feat: storybook / tsdocs - Switch
  • Loading branch information
MrMaz authored Aug 14, 2024
2 parents ab53f3e + 0ee01e9 commit 1b94ec2
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/react-material-ui/__tests__/Switch.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import '@testing-library/jest-dom';
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import Switch from '../src/components/Switch/Switch';
import { Switch } from '../src/components/Switch/Switch';

describe('Switch Component', () => {
it('should render correctly', () => {
Expand Down
41 changes: 36 additions & 5 deletions packages/react-material-ui/src/components/Switch/Switch.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,49 @@
import React from 'react';
import MuiSwitch, { SwitchProps } from '@mui/material/Switch';
import MuiSwitch, { SwitchProps as MuiSwitchProps } from '@mui/material/Switch';
import FormGroup from '@mui/material/FormGroup';
import FormControlLabel from '@mui/material/FormControlLabel';
import Text from '../Text';
import { TextProps } from 'interfaces';

type Props = {
/**
* Props for the `Switch` component.
*/
export type SwitchProps = MuiSwitchProps & {
/**
* The label text displayed alongside the switch.
*/
label?: string;

/**
* Custom styles for the label text. This prop is passed down to the `Text` component.
*/
textProps?: TextProps;
};

const Switch = (props: Props & SwitchProps) => {
/**
* The `Switch` component is a UI element used to toggle between two states,
* typically representing "on" and "off". It supports features such as labeling,
* custom label styling, and controlled or uncontrolled state management.
* It's props extend from [Material UI's Switch](https://mui.com/material-ui/api/switch/#props) component props, so every
* prop is interchangeable between those two.
*
* @see [Storybook - Switch](https://storybook.rockets.tools/?path=/docs/switch)
*
* @example
* ```tsx
* <Switch
* label="Enable Notifications"
* checked={true}
* onChange={(e) => console.log(e.target.checked)}
* textProps={{ fontSize: 14, color: 'primary.main' }}
* />
* ```
*
* @param SwitchProps - The props for the `Switch` component.
* @param MuiSwitchProps - MUI {@link [MuiSwitchProps](https://mui.com/material-ui/api/switch/#props)}
*/

export const Switch = (props: SwitchProps) => {
const {
label,
disabled,
Expand Down Expand Up @@ -46,5 +79,3 @@ const Switch = (props: Props & SwitchProps) => {
</>
);
};

export default Switch;
2 changes: 1 addition & 1 deletion packages/react-material-ui/src/components/Switch/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default } from './Switch';
export { Switch, SwitchProps } from './Switch';
2 changes: 1 addition & 1 deletion packages/react-material-ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export { Notifications, NotificationsProps } from './components/Notifications';
export { default as RadioGroup } from './components/RadioGroup';
export { SideModal, SideModalProps } from './components/SideModal';
export { default as Select } from './components/Select';
export { default as Switch } from './components/Switch';
export { Switch, SwitchProps } from './components/Switch';

import Table, {
useTable,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import Switch from '../../components/Switch';
import { Switch } from '../../components/Switch';
import { WidgetProps } from '@rjsf/utils';

const CustomSwitchWidget = (props: WidgetProps) => (
Expand Down
67 changes: 67 additions & 0 deletions stories/Switch.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type { Meta, StoryObj } from '@storybook/react';
import { fn } from '@storybook/test';

import { Switch } from '@concepta/react-material-ui';

const meta = {
component: Switch,
tags: ['autodocs'],
args: {},
argTypes: {
label: { control: 'text' },
onChange: { action: 'changed' },
},
} satisfies Meta<typeof Switch>;

export default meta;

type Story = StoryObj<typeof meta>;

export const Default: Story = {
args: {},
};

export const WithLabel: Story = {
args: {
label: 'Switch Label',
},
};

export const CustomLabelStyles: Story = {
args: {
label: 'Custom Styled Label',
textProps: {
fontSize: 18,
fontWeight: 600,
color: 'secondary.main',
},
},
};

export const Checked: Story = {
args: {
checked: true,
},
};

export const Disabled: Story = {
args: {
disabled: true,
},
};

export const Required: Story = {
args: {
label: 'Required Switch',
required: true,
},
};

/**
* Check the actions tab
*/
export const OnChangeCallback: Story = {
args: {
onChange: fn(),
},
};

0 comments on commit 1b94ec2

Please sign in to comment.