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

Move Toggle, Radio, and Checkbox to inputs #428

Merged
merged 13 commits into from
Jan 16, 2019
Merged

Move Toggle, Radio, and Checkbox to inputs #428

merged 13 commits into from
Jan 16, 2019

Conversation

montezume
Copy link
Contributor

@montezume montezume commented Jan 11, 2019

Summary

Moves Checkbox, Radio, and Switch out of the src/components/switches folder into the src/components/inputs folder. Also moves them to the inputs section of Storybook.

BREAKING CHANGE

Toggle's now returns an event from it's onChange with the shape

{
      target: {
        id: this.props.id,
        name: this.props.name,
        checked: !this.props.isChecked,
      },
}

Toggle is now ToggleInput
Radio is now RadioInput
Checkbox is now CheckboxInput

This is to unify the API with all the other inputs (including Checkbox and Radio), which already emit events.

Closes #318

@montezume montezume requested review from dferber90, emmenko and tdeekens and removed request for dferber90 January 11, 2019 14:07
@montezume montezume changed the title chore: move Toggle, radio and checkbox to input, emit event for toggle Move Toggle, Radio, and Checkbox to inputs Jan 11, 2019
@@ -22,6 +22,17 @@ export class Toggle extends React.PureComponent {
size: 'big',
};

emitChange = () => {
const event = {
target: {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is the breaking change

@@ -72,7 +72,7 @@ describe('TimeInput', () => {
expect(container.querySelector('input')).toHaveAttribute('value', 'foo');
});

it('should call onChange when chaning the value', () => {
it('should call onChange when changing the value', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

typo fix

Copy link
Contributor

@tdeekens tdeekens left a comment

Choose a reason for hiding this comment

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

Thanks! Super nice to get them to have the same API! Hare we sure that the Toggle is an input. I think so but at the same time wonder...

Copy link
Member

@emmenko emmenko left a comment

Choose a reason for hiding this comment

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

Thanks for looking into this!

value: !this.props.isChecked,
},
};
this.props.onChange(event);
Copy link
Member

Choose a reason for hiding this comment

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

Why are we constructing the event ourselves? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can't get it to work with the native event. You are welcome to try it, but I spent about an hour on it and got nowhere with RTL. The shape is the same now as the native event though.

Copy link
Member

Choose a reason for hiding this comment

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

Oh really? Sorry about that...

Hmm that's weird, I would have expected this to work. I can have a look over the weekend.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

switching to defaultChecked fixed the probelm

src/index.js Outdated

export { default as Toggle } from './components/inputs/toggle';
export { default as Checkbox } from './components/inputs/checkbox';
export { default as Radio } from './components/inputs/radio';
Copy link
Member

Choose a reason for hiding this comment

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

I think we should name them consistently with the other component, so with Input suffix.

Copy link
Contributor

Choose a reason for hiding this comment

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

Very valid!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I renamed Toggle -> ToggleInput. If we are happy with this, I will rename the rest of them on Monday

@montezume
Copy link
Contributor Author

It's being used with formik with this workaround in the MC

// FIXME: the Toggle component should send the `event` in the `onChange` handler
function patchToggleChange(isChecked) {
  // This is bound to the function
  const props = this;
  const event = {
    target: {
      type: 'checkbox',
      name: `${props.fieldName}.isActive`,
      checked: isChecked,
    },
  };
  props.formikProps.handleChange(event);
}

@emmenko
Copy link
Member

emmenko commented Jan 11, 2019

It's being used with formik with this workaround in the MC

I know, I wrote that 😄

That's why I'm asking: why not simply onChange={this.props.onChange}?

@emmenko
Copy link
Member

emmenko commented Jan 11, 2019

The input sends the correct event, the workaround was to make it work with formik because the component didn't return an event.

@montezume
Copy link
Contributor Author

I can't get it to work with the native event with RTL.

@montezume
Copy link
Contributor Author

montezume commented Jan 11, 2019

In fact, if I try it in storybook, what is returned from the event, doesn't contain what we need (at least in the console log...)

screen shot 2019-01-11 at 4 32 41 pm

There's no checked attribute

Copy link
Contributor

@tdeekens tdeekens left a comment

Choose a reason for hiding this comment

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

Should we rename them to be radio-input, checkbox-input and toggle-input?

| `isDisabled` | `bool` | - | - | `false` | Disables the ToggleInput |
| `isChecked` | `bool` | - | - | `false` | Checks the ToggleInput |
| `onChange` | `func` | ✅ | - | - | called with the new value. |
| `size` | `string` | ✅ | oneOf(['small', 'big']) | `big` | The size of the ToggleInput component. |
Copy link
Member

Choose a reason for hiding this comment

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

Some props are missing here

@@ -38,7 +38,7 @@ export class Toggle extends React.PureComponent {
className={styles.inputWrapper}
id={this.props.id}
name={this.props.name}
onChange={() => this.props.onChange(!this.props.isChecked)}
Copy link
Member

Choose a reason for hiding this comment

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

💯

@montezume
Copy link
Contributor Author

I updated the PR with the renaming of the rest of the switches

Copy link
Member

@emmenko emmenko left a comment

Choose a reason for hiding this comment

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

🎉🙌

@montezume montezume merged commit 4f73e03 into master Jan 16, 2019
@montezume montezume deleted the ml-toggle branch January 16, 2019 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants