-
Notifications
You must be signed in to change notification settings - Fork 53
Checkbox base prototype #2263
base: master
Are you sure you want to change the base?
Checkbox base prototype #2263
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import * as React from 'react' | ||
|
||
interface CheckboxBaseProps { | ||
checked?: boolean | ||
onChange?: React.ChangeEventHandler | ||
onClick?: React.MouseEventHandler | ||
slots?: { | ||
root?: any | ||
input?: any | ||
} | ||
slotProps?: { | ||
root?: any | ||
input?: any | ||
} | ||
classes?: any | ||
Comment on lines
+7
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will these types be more refined in the final version? |
||
} | ||
|
||
const Testhelper = (prop?: Function, propName?: string) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
if (!prop) { | ||
return {} | ||
} | ||
return { | ||
[propName]: prop, | ||
} | ||
} | ||
|
||
const CheckboxBase: React.FunctionComponent<CheckboxBaseProps> = props => { | ||
const { checked, onChange, onClick, classes = {}, slots = {}, slotProps = {}, ...rest } = props | ||
const { root: RootSlot = 'div', input: InputSlot = 'input' } = slots | ||
const { root: rootClass, input: inputClass } = classes | ||
const { root: rootProps, input: inputProps } = slotProps | ||
|
||
const [isChecked, setIsChecked] = React.useState(!!checked) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it still valid that the state will need to be pulled out of the base component? |
||
const realIsChecked = checked === undefined ? isChecked : !!checked | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm having a really hard time figuring out why we need this instead of using the regular |
||
|
||
const onChangeHandler = React.useCallback( | ||
(ev: any) => { | ||
if (onClick) { | ||
onClick(ev) | ||
} | ||
if (!ev.defaultPrevented) { | ||
if (onChange) { | ||
onChange(ev) | ||
} | ||
} | ||
if (!ev.defaultPrevented) { | ||
setIsChecked(!realIsChecked) | ||
} | ||
Comment on lines
+41
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could fuse this two ifs into one. |
||
}, | ||
[setIsChecked, realIsChecked, onChange], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to add |
||
) | ||
|
||
const onKeyDown = React.useCallback( | ||
(ev: React.KeyboardEvent) => { | ||
console.log(ev.keyCode) | ||
switch (ev.keyCode) { | ||
case 13: | ||
case 32: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. according to ARIA only space should toggle the checked state. How would we allow customization (if some partner wants to toggle on enter as well)? Would that need to be a separate or a custom component? |
||
setIsChecked(!realIsChecked) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In both Semantic UI React and Stardust there is the concept of auto controlled state which allows the consumer to take over and control the state from outside by using a combination of the chnge handler and override state prop. It is especially useful in components like Popup or Dialog where the consumer can add control if the surface should be opened/closed initially or based on external event, as well as additional logic to extend the internal handlers. Maybe this is a simplified version of the API, but it would be good to keep it as it turned out to be very effective and consistent way of controlling the state from outside. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small note, the concept of "auto controlled state" comes from React itself. Form controls, like the The principles that follow from this is that:
|
||
} | ||
} | ||
}, | ||
[setIsChecked, realIsChecked], | ||
) | ||
|
||
return ( | ||
<RootSlot | ||
onKeyDown={onKeyDown} | ||
onClick={onChangeHandler} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be called onClickHandlefr? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Convention is https://reactjs.org/docs/handling-events.html
|
||
className={rootClass} | ||
{...rest} | ||
{...rootProps} | ||
{...Testhelper(onChange, 'onChange')} | ||
> | ||
<InputSlot checked={realIsChecked} type="checkbox" className={inputClass} {...inputProps} /> | ||
{props.children} | ||
</RootSlot> | ||
) | ||
} | ||
export default CheckboxBase |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,185 @@ | ||||||
import * as React from 'react' | ||||||
import { mount } from 'enzyme' | ||||||
import CheckboxBase from 'src/components/Checkbox/CheckboxBase' | ||||||
|
||||||
describe("Baby's first test", () => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's organize our tests by: describe(displayName, () => {
describe(propName, () => {
it('does something', () => {})
it('does not do something', () => {})
})
}) This pattern of Component > prop is used for all tests and docs. Following it ensures we can do a better of job of ensuring we have full coverage of documentation and tests. Having complete docs/tests on public APIs means we can also rev the library with more confidence and less bugs. |
||||||
it('renders something', () => { | ||||||
expect(mount(<CheckboxBase />)).toBeTruthy() | ||||||
}) | ||||||
|
||||||
it('has an input', () => { | ||||||
const control = mount(<CheckboxBase />) | ||||||
const input = control | ||||||
.find('input') | ||||||
.first() | ||||||
.getDOMNode() | ||||||
expect(input).toBeTruthy() | ||||||
}) | ||||||
|
||||||
it('can be checked', () => { | ||||||
const control = mount(<CheckboxBase checked={true} />) | ||||||
const inputChecked = control | ||||||
.find('input') | ||||||
.first() | ||||||
.prop('checked') | ||||||
expect(inputChecked).toBe(true) | ||||||
}) | ||||||
|
||||||
it('can be unchecked', () => { | ||||||
const control = mount(<CheckboxBase />) | ||||||
const inputChecked = control | ||||||
.find('input') | ||||||
.first() | ||||||
.prop('checked') | ||||||
expect(inputChecked).toBe(false) | ||||||
}) | ||||||
|
||||||
it('can switch check state', () => { | ||||||
const control = mount(<CheckboxBase />) | ||||||
const inputChecked = control | ||||||
.find('input') | ||||||
.first() | ||||||
.prop('checked') | ||||||
expect(inputChecked).toBe(false) | ||||||
control.setProps({ checked: true }) | ||||||
const inputCheckedAfter = control | ||||||
.find('input') | ||||||
.first() | ||||||
.prop('checked') | ||||||
expect(inputCheckedAfter).toBe(true) | ||||||
}) | ||||||
|
||||||
it('changes state when clicked', () => { | ||||||
const control = mount(<CheckboxBase />) | ||||||
expect( | ||||||
control | ||||||
.find('input') | ||||||
.first() | ||||||
.prop('checked'), | ||||||
).toBe(false) | ||||||
control.simulate('click') | ||||||
expect( | ||||||
control | ||||||
.find('input') | ||||||
.first() | ||||||
.prop('checked'), | ||||||
).toBe(true) | ||||||
control.simulate('click') | ||||||
expect( | ||||||
control | ||||||
.find('input') | ||||||
.first() | ||||||
.prop('checked'), | ||||||
).toBe(false) | ||||||
}) | ||||||
|
||||||
it('calls onChange when checked state changes', () => { | ||||||
const change = jest.fn() | ||||||
const control = mount(<CheckboxBase onChange={change} />) // let, the only constant is change | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's with the comment here? I don't understand it very well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Event handlers are tested as part of the baseline import { isConformant } from 'test/specs/commonTests'
describe('CheckboxBase', () => {
isConformant(CheckboxBase)
}) See |
||||||
control.simulate('click') | ||||||
expect(change).toHaveBeenCalled() | ||||||
}) | ||||||
|
||||||
it('does not change value when onChange prevents default', () => { | ||||||
const change = jest.fn((e: any) => e.preventDefault()) | ||||||
const control = mount(<CheckboxBase onChange={change} />) | ||||||
expect( | ||||||
control | ||||||
.find('input') | ||||||
.first() | ||||||
.prop('checked'), | ||||||
).toBe(false) | ||||||
control.simulate('click') | ||||||
expect( | ||||||
control | ||||||
.find('input') | ||||||
.first() | ||||||
.prop('checked'), | ||||||
).toBe(false) | ||||||
}) | ||||||
|
||||||
it('does not call onChange value when onClick prevents default', () => { | ||||||
const click = jest.fn((e: any) => e.preventDefault()) | ||||||
const change = jest.fn() | ||||||
const control = mount(<CheckboxBase onClick={click} onChange={change} />) | ||||||
control.simulate('click') | ||||||
expect(change).not.toHaveBeenCalled() | ||||||
}) | ||||||
|
||||||
it('calls onClick', () => { | ||||||
const click = jest.fn() | ||||||
const control = mount(<CheckboxBase onClick={click} />) | ||||||
control.simulate('click') | ||||||
expect(click).toHaveBeenCalled() | ||||||
}) | ||||||
|
||||||
it('renders content', () => { | ||||||
const control = mount( | ||||||
<CheckboxBase> | ||||||
<label>foo</label> | ||||||
</CheckboxBase>, | ||||||
) | ||||||
expect(control.find('label').length).toBe(1) | ||||||
}) | ||||||
|
||||||
it('can render with a different root type', () => { | ||||||
const control = mount(<CheckboxBase slots={{ root: 'span' }} />) | ||||||
expect(control.find('span').length).toBe(1) | ||||||
}) | ||||||
|
||||||
it('can render with a different input type', () => { | ||||||
const MyInput = () => ( | ||||||
<a> | ||||||
<input type="hidden" value="I don't even know why anymore..." /> | ||||||
</a> | ||||||
) | ||||||
const control = mount(<CheckboxBase slots={{ input: MyInput }} />) | ||||||
expect(control.find('a').length).toBe(1) | ||||||
}) | ||||||
|
||||||
it('changes on enter press', () => { | ||||||
const control = mount(<CheckboxBase />) | ||||||
control.simulate('keydown', { | ||||||
keyCode: 13, | ||||||
key: 'Enter', | ||||||
Comment on lines
+143
to
+144
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @khmakoto see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode, it's deprecated. Only There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @levithomason sure, what I meant is that having both was redundant, I don't actually have a preference for one over the other. |
||||||
}) | ||||||
expect(control.find('input').prop('checked')).toBe(true) | ||||||
}) | ||||||
|
||||||
it('changes on space press', () => { | ||||||
const control = mount(<CheckboxBase />) | ||||||
control.simulate('keydown', { | ||||||
keyCode: 32, | ||||||
}) | ||||||
expect(control.find('input').prop('checked')).toBe(true) | ||||||
}) | ||||||
|
||||||
describe('class handling', () => { | ||||||
it('renders classes', () => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
const control = mount(<CheckboxBase classes={{ root: 'foo' }} />) | ||||||
expect(control.find('div.foo').length).toBe(1) | ||||||
}) | ||||||
|
||||||
it('renders classes for input', () => { | ||||||
const control = mount(<CheckboxBase classes={{ input: 'foo' }} />) | ||||||
expect(control.find('input.foo').length).toBe(1) | ||||||
}) | ||||||
}) | ||||||
|
||||||
describe('slotProps', () => { | ||||||
it('renders slotProps for input', () => { | ||||||
const control = mount(<CheckboxBase slotProps={{ input: { 'data-foo': 'bar' } }} />) | ||||||
expect(control.find('input[data-foo="bar"]').length).toBe(1) | ||||||
}) | ||||||
|
||||||
it('renders slotProps for root', () => { | ||||||
const control = mount(<CheckboxBase slotProps={{ root: { 'data-foo': 'bar' } }} />) | ||||||
expect(control.find('div[data-foo="bar"]').length).toBe(1) | ||||||
}) | ||||||
|
||||||
it('applies unused props to the root element', () => { | ||||||
const control = mount(<CheckboxBase data-foo="bar" />) | ||||||
expect(control.find('div[data-foo="bar"]').length).toBe(1) | ||||||
}) | ||||||
}) | ||||||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might want to pull this out of the render function in a
const
object so that this isn't recomputed on every render.