Skip to content

Commit 47e2f51

Browse files
authored
feat(Switch): added new input switch (#48)
1 parent 2361ebc commit 47e2f51

File tree

6 files changed

+74
-0
lines changed

6 files changed

+74
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@import '../../../styles/variables.scss';
2+
3+
.#{$ns}switch {
4+
height: 28px;
5+
display: flex;
6+
align-items: center;
7+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react';
2+
3+
import {Switch as SwitchBase} from '@gravity-ui/uikit';
4+
5+
import {BooleanInput} from '../../../../core';
6+
import {block} from '../../../utils';
7+
8+
import './Switch.scss';
9+
10+
const b = block('switch');
11+
12+
export const Switch: BooleanInput = ({name, input, spec}) => {
13+
const {value, onBlur, onChange, onFocus} = input;
14+
15+
const handleChange = React.useCallback(
16+
(e: React.ChangeEvent<HTMLInputElement>) => onChange(e.target.checked),
17+
[onChange],
18+
);
19+
20+
return (
21+
<SwitchBase
22+
checked={value}
23+
onChange={handleChange}
24+
onBlur={onBlur}
25+
onFocus={onFocus}
26+
disabled={spec.viewSpec.disabled}
27+
className={b()}
28+
qa={name}
29+
/>
30+
);
31+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './Switch';

src/lib/kit/components/Inputs/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export * from './OneOf';
99
export * from './OneOfCard';
1010
export * from './Secret';
1111
export * from './Select';
12+
export * from './Switch';
1213
export * from './TableArrayInput';
1314
export * from './Text';
1415
export * from './TextArea';

src/lib/kit/constants/config.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
SectionWithSubtitle,
4444
SectionWithSubtitle2,
4545
Select,
46+
Switch,
4647
TableArrayInput,
4748
TableArrayView,
4849
TableCell,
@@ -103,6 +104,7 @@ export const dynamicConfig: DynamicFormConfig = {
103104
boolean: {
104105
inputs: {
105106
base: {Component: Checkbox},
107+
switch: {Component: Switch},
106108
},
107109
layouts: {
108110
row: Row,
@@ -206,6 +208,7 @@ export const dynamicCardConfig: DynamicFormConfig = {
206208
boolean: {
207209
inputs: {
208210
base: {Component: Checkbox},
211+
switch: {Component: Switch},
209212
},
210213
layouts: {
211214
row: Row2,
@@ -299,6 +302,7 @@ export const dynamicViewConfig: DynamicViewConfig = {
299302
boolean: {
300303
views: {
301304
base: {Component: BaseView},
305+
switch: {Component: BaseView},
302306
},
303307
layouts: {
304308
row: ViewRow,
@@ -382,6 +386,7 @@ export const dynamicViewCardConfig: DynamicViewConfig = {
382386
boolean: {
383387
views: {
384388
base: {Component: BaseView},
389+
switch: {Component: BaseView},
385390
},
386391
layouts: {
387392
row: ViewRow2,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React from 'react';
2+
3+
import {ComponentStory} from '@storybook/react';
4+
5+
import {BooleanSpec, SpecTypes, Switch as SwitchBase} from '../lib';
6+
7+
import {InputPreview} from './components';
8+
9+
export default {
10+
title: 'Boolean/Switch',
11+
component: SwitchBase,
12+
};
13+
14+
const baseSpec: BooleanSpec = {
15+
type: SpecTypes.Boolean,
16+
viewSpec: {type: 'switch', layout: 'row', layoutTitle: 'Flag'},
17+
};
18+
19+
const excludeOptions = ['viewSpec.type'];
20+
21+
const template = (spec: BooleanSpec = baseSpec) => {
22+
const Template: ComponentStory<typeof SwitchBase> = (__, {viewMode}) => (
23+
<InputPreview spec={spec} excludeOptions={excludeOptions} viewMode={viewMode} />
24+
);
25+
26+
return Template;
27+
};
28+
29+
export const Switch = template();

0 commit comments

Comments
 (0)