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

Wc 106 redux form toggle #310

Merged
merged 4 commits into from
Oct 5, 2017
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
30 changes: 30 additions & 0 deletions src/forms/redux-form/TogglePill.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import TogglePill from '../TogglePill';

/**
* @param {Object} props - props passed in from parent and redux-form
* @description wraps standard TogglePill web component for use with redux-form
* deconstructs props that redux-forms sets and sets them on TogglePill
*
* NOTE: redux-form expects checkboxes value to be true/false and will set them as so
* https://github.com/erikras/redux-form/issues/2922, therefore using the value here to
* set isActive
*
* @return {Component} TogglePill
*/
const ReduxFormTogglePill = props => {
// not adding meta error to TogglePill prop for now
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// since TogglePills should function in a group with one error
const {
meta, // eslint-disable-line no-unused-vars
input,
...other
} = props;

return <TogglePill {...input} isActive={input.value == 'true' && !other.useRadio} {...other} />;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reading other.useRadio since I do not know if there are similar idiosyncracies for Redux Form radio values. I think if we come up against the same thing for the radios, we can remove this check.

};

ReduxFormTogglePill.displayName = 'ReduxFormTogglePill';

export default ReduxFormTogglePill;

10 changes: 10 additions & 0 deletions src/forms/redux-form/__snapshots__/togglePill.test.jsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`redux-form TogglePill renders a TogglePill component with expected attributes from mock data 1`] = `
<WithToggle
isActive={false}
label="Parenting"
name="parenting"
value={false}
/>
`;
34 changes: 34 additions & 0 deletions src/forms/redux-form/togglePill.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react';
import { shallow, mount } from 'enzyme';
import ReduxFormTogglePill from './TogglePill';

describe('redux-form TogglePill', function() {

// props given in the structure that
// redux form would
const togglePillProps = {
input: {
label: 'Parenting',
name: 'parenting',
value: false, // as a checkbox, redux form will pass true / false for values
}
};

it('renders a TogglePill component with expected attributes from mock data', () => {
const component = shallow(<ReduxFormTogglePill {...togglePillProps} />);
expect(component).toMatchSnapshot();
});

it('renders a TogglePill with isActive prop as false when value is false', () => {
const component = mount(<ReduxFormTogglePill {...togglePillProps} />);
expect(component.find('TogglePill').prop('isActive')).toBe(false);
});

it('renders a TogglePill with isActive prop as true when value is true', () => {
togglePillProps.input.value = true;
const component = mount(<ReduxFormTogglePill {...togglePillProps} />);
expect(component.find('TogglePill').prop('isActive')).toBe(false);
});

});