Skip to content

Commit

Permalink
feat(SwitchField): adds component
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Firsov committed Apr 9, 2019
1 parent cee9797 commit 6c83559
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 1 deletion.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions e2e/__tests__/switch-field.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { baisy } from '../setup/TestSuiter';


const SUITES = [
baisy.suite('Components/SwitchField', 'common'),
];


SUITES.map(suite => {
it(suite.getTestName(), suite.testStory, 20000);
});

8 changes: 7 additions & 1 deletion src/components/Switch/Switch.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// @flow

import React from 'react';
import { SwitchTag, SwitchInputTag, SwitchApperanceTag, SwitchLabelTag } from './Switch.theme';
import {
SwitchTag,
SwitchInputTag,
SwitchApperanceTag,
SwitchLabelTag,
} from './Switch.theme';

type SwitchProps = {
label?: string,
Expand Down Expand Up @@ -33,3 +38,4 @@ class Switch extends React.Component<SwitchProps> {
}

export { Switch };

42 changes: 42 additions & 0 deletions src/components/SwitchField/SwitchField.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// @flow

import React from 'react';

import type { InputType, MetaType } from '../../types';
import * as formUtils from '../../utils/forms';
import { Switch } from '../Switch';
import { FormField } from '../Form/FormField';

type SwitchFieldProps = {
label?: string,
input: InputType,
meta?: MetaType,
};

const SwitchField = ({ label, input, meta, ...rest }: SwitchFieldProps) => {
const {
name,
value,
onChange,
onFocus,
onBlur,
} = input;
const hasError = formUtils.hasError(meta);

return (
<FormField { ...rest } input={ input } meta={ meta }>
<Switch
name={ name }
label={ label }
value={ Boolean(value) }
onChange={ onChange }
onBlur={ onBlur }
onFocus={ onFocus }
hasError={ hasError }
/>
</FormField>
);
};

export { SwitchField };

19 changes: 19 additions & 0 deletions src/components/SwitchField/SwitchField.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';

export default (asStory) => {
asStory('Components/SwitchField', module, (story, { SwitchField, Column }) => {
story
.add('common', () => (
<Column>
<SwitchField label="Disabled" input={{}} />
<SwitchField label="Enabled" input={{ value: true }} />
<SwitchField
label="With error"
input={{ value: false }}
meta={{ error: 'Required', touched: true }}
/>
</Column>
));
});
};

30 changes: 30 additions & 0 deletions src/components/SwitchField/SwitchField.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// @flow
import React from 'react';
import { SwitchField } from './SwitchField';

describe('<SwitchField />', () => {
it('should pass props to the children', () => {
const onChange = jest.fn();
const wrapper = shallow(<SwitchField input={{ onChange, value: true }} />);

expect(wrapper).toMatchInlineSnapshot(`
<FormField
direction="column"
hideErrorLabel={false}
input={
Object {
"onChange": [MockFunction],
"value": true,
}
}
stretch={true}
>
<Switch
hasError={false}
onChange={[MockFunction]}
value={true}
/>
</FormField>
`);
});
});
4 changes: 4 additions & 0 deletions src/components/SwitchField/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @flow

export { SwitchField } from './SwitchField';

1 change: 1 addition & 0 deletions src/components/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export { SecondaryNavigation } from './SecondaryNavigation';
export { Select } from './Select';
export { SelectField } from './SelectField';
export { Switch } from './Switch';
export { SwitchField } from './SwitchField';
export { Table, TableBuilder } from './Table';
export { Tabs } from './Tabs';
export { Tag } from './Tag';
Expand Down

0 comments on commit 6c83559

Please sign in to comment.