Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react): add Switch component #181

Merged
merged 3 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/react/.storybook/story-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export type Stories =
| 'Skeleton'
| 'Snackbar'
| 'Stepper'
| 'Switch'
| 'Tab'
| 'TabPanel'
| 'Tabs'
Expand Down Expand Up @@ -361,6 +362,9 @@ const StoryConfig: StorybookConfig = {
Stepper: {
hierarchy: `${StorybookCategories.Surfaces}/Stepper`,
},
Switch: {
hierarchy: `${StorybookCategories.Inputs}/Switch`,
},
Tab: {
hierarchy: `${StorybookCategories.Navigation}/Tab`,
},
Expand Down
56 changes: 56 additions & 0 deletions packages/react/src/components/Switch/Switch.stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {ArgsTable, Source, Story, Canvas, Meta} from '@storybook/addon-docs';
import dedent from 'ts-dedent';
import StoryConfig from '../../../.storybook/story-config.ts';
import Switch from './Switch.tsx';
import FormControlLabel from '../FormControlLabel'

export const meta = {
component: Switch,
title: StoryConfig.Switch.hierarchy,
};

<Meta title={meta.title} component={meta.component} />

export const Template = (args) => {
return (
<Switch {...args} />
)
};

# Switch

- [Overview](#overview)
- [Props](#props)
- [Usage](#usage)

## Overview

Switches toggle the state of a single setting on or off.

<Canvas>
<Story
name="Overview"
>
{Template.bind({})}
</Story>
</Canvas>

## Props

<ArgsTable story="Overview" />

## Usage

Import and use the `Switch` component in your components as follows.

<Source
language="jsx"
dark
format
code={dedent`
import Switch from '@oxygen-ui/react/Switch';
import FormControlLabel from '@oxygen-ui/react/FormControlLabel';\n
function Demo() {
return <FormControlLabel control={<Switch />} label="Off" />;
}`}
/>
47 changes: 47 additions & 0 deletions packages/react/src/components/Switch/Switch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import MuiSwitch, {SwitchProps as MuiSwitchProps} from '@mui/material/Switch';
import clsx from 'clsx';
import {forwardRef, ForwardRefExoticComponent, ReactElement, MutableRefObject} from 'react';
import {WithWrapperProps} from '../../models';
import {composeComponentDisplayName} from '../../utils';
import './switch.scss';

export type SwitchProps = MuiSwitchProps;

const COMPONENT_NAME: string = 'Switch';

/**
* @remarks `any` is used as the generic type for the props because the generic type is not used in the component.
*/
const Switch: ForwardRefExoticComponent<SwitchProps> & WithWrapperProps = forwardRef(
(props: SwitchProps, ref: MutableRefObject<HTMLButtonElement>): ReactElement => {
const {className, ...rest} = props;

const classes: string = clsx('oxygen-switch', className);

return <MuiSwitch className={classes} {...rest} ref={ref} />;
},
) as ForwardRefExoticComponent<SwitchProps> & WithWrapperProps;

Switch.displayName = composeComponentDisplayName(COMPONENT_NAME);
Switch.muiName = COMPONENT_NAME;
Switch.defaultProps = {};

export default Switch;
32 changes: 32 additions & 0 deletions packages/react/src/components/Switch/__tests__/Switch.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import {render} from '@unit-testing';
import Switch from '../Switch';

describe('Alert', () => {
it('should render successfully', () => {
const {baseElement} = render(<Switch />);
expect(baseElement).toBeTruthy();
});

it('should match the snapshot', () => {
const {baseElement} = render(<Switch />);
expect(baseElement).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Alert should match the snapshot 1`] = `
<body>
<div>
<span
class="MuiSwitch-root MuiSwitch-sizeMedium oxygen-switch css-julti5-MuiSwitch-root"
>
<span
class="MuiButtonBase-root MuiSwitch-switchBase MuiSwitch-colorPrimary PrivateSwitchBase-root MuiSwitch-switchBase MuiSwitch-colorPrimary css-67kisp-MuiButtonBase-root-MuiSwitch-switchBase"
>
<input
class="PrivateSwitchBase-input MuiSwitch-input css-1m9pwf3"
type="checkbox"
/>
<span
class="MuiSwitch-thumb css-1fn27bw-MuiSwitch-thumb"
/>
<span
class="MuiTouchRipple-root css-8je8zh-MuiTouchRipple-root"
/>
</span>
<span
class="MuiSwitch-track css-14pk2-MuiSwitch-track"
/>
</span>
</div>
</body>
`;
20 changes: 20 additions & 0 deletions packages/react/src/components/Switch/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export {default} from './Switch';
export type {SwitchProps} from './Switch';
21 changes: 21 additions & 0 deletions packages/react/src/components/Switch/switch.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

.oxygen-switch {
/* Add styles */
}
3 changes: 3 additions & 0 deletions packages/react/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ export * from './Skeleton';
export {default as Stepper} from './Stepper';
export * from './Stepper';

export {default as Switch} from './Switch';
export * from './Switch';

export {default as Tab} from './Tab';
export * from './Tab';

Expand Down
Loading