-
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
Conversation
className={classes.root} | ||
<CheckboxBase | ||
slots={{ root: ElementType }} | ||
slotProps={{ input: { style: { visibility: 'hidden', display: 'none' } } }} |
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.
slots?: { | ||
root?: any | ||
input?: any | ||
} | ||
slotProps?: { | ||
root?: any | ||
input?: any | ||
} | ||
classes?: any |
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.
Will these types be more refined in the final version?
classes?: any | ||
} | ||
|
||
const Testhelper = (prop?: Function, propName?: string) => { |
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.
Maybe TestHelper
?
if (!ev.defaultPrevented) { | ||
if (onChange) { | ||
onChange(ev) | ||
} | ||
} | ||
if (!ev.defaultPrevented) { | ||
setIsChecked(!realIsChecked) | ||
} |
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 could fuse this two ifs into one.
setIsChecked(!realIsChecked) | ||
} | ||
}, | ||
[setIsChecked, realIsChecked, onChange], |
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.
Do we need to add setIsChecked
here? From my understanding, since the function will never change its value, it is a variable that will never trigger a different callback.
const { root: rootProps, input: inputProps } = slotProps | ||
|
||
const [isChecked, setIsChecked] = React.useState(!!checked) | ||
const realIsChecked = checked === undefined ? isChecked : !!checked |
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.
I'm having a really hard time figuring out why we need this instead of using the regular isChecked
.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
describe("Baby's first test", () => { | |
describe("CheckboxBase", () => { |
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.
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('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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Event handlers are tested as part of the baseline isConformant()
test. This test checks every synthetic event type and asserts more things:
import { isConformant } from 'test/specs/commonTests'
describe('CheckboxBase', () => {
isConformant(CheckboxBase)
})
See .github/test-a-feature.md
for more.
keyCode: 13, | ||
key: 'Enter', |
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.
Wouldn't the keyCode
have been enough here?
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.
@khmakoto see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode, it's deprecated.
Only key
is the valid check moving forward:
https://github.com/levithomason/keyboard-key#why
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.
@levithomason sure, what I meant is that having both was redundant, I don't actually have a preference for one over the other.
}) | ||
|
||
describe('class handling', () => { | ||
it('renders classes', () => { |
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.
it('renders classes', () => { | |
it('renders classes for root', () => { |
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 comment
The 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?
console.log(ev.keyCode) | ||
switch (ev.keyCode) { | ||
case 13: | ||
case 32: { |
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.
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?
switch (ev.keyCode) { | ||
case 13: | ||
case 32: { | ||
setIsChecked(!realIsChecked) |
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.
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 comment
The 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 <input />
, will automatically control their value when the user inputs values. However, if the dev controls the value
prop, then it will defer to the dev's props. If the dev removes the prop, then React resumes control of the <input />
.
The principles that follow from this is that:
- Components should work without requiring the consumer to wire them up. Dropdowns should open on click, inputs should update their value on input, etc.
- Components should defer to controlled mode if the dev provides props
- Components should resume control if the developer "unsets" the prop value (i.e.
undefined
).
return ( | ||
<RootSlot | ||
onKeyDown={onKeyDown} | ||
onClick={onChangeHandler} |
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.
should this be called onClickHandlefr?
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.
Convention is handleClick
, this is a common community convention based on popular libs and the React docs:
https://reactjs.org/docs/handling-events.html
In React, this could instead be:
function ActionLink() { function handleClick(e) { e.preventDefault(); console.log('The link was clicked.'); } return ( <a href="#" onClick={handleClick}> Click me </a> ); }```
Base Component Prototype
Purpose
The purpose of this PR is to act as a proof of concept for implementing base components which will underly the full "fluent" components. The goal is to be as framework agnostic as possible. It provides an api surface that is decoupled from styling, theming, and composition concerns while still allowing those to be implemented.