Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

Checkbox base prototype #2263

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft

Checkbox base prototype #2263

wants to merge 3 commits into from

Conversation

joschect
Copy link
Contributor

@joschect joschect commented Jan 18, 2020

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.

className={classes.root}
<CheckboxBase
slots={{ root: ElementType }}
slotProps={{ input: { style: { visibility: 'hidden', display: 'none' } } }}
Copy link
Member

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.

Comment on lines +7 to +15
slots?: {
root?: any
input?: any
}
slotProps?: {
root?: any
input?: any
}
classes?: any
Copy link
Member

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) => {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe TestHelper?

Comment on lines +41 to +48
if (!ev.defaultPrevented) {
if (onChange) {
onChange(ev)
}
}
if (!ev.defaultPrevented) {
setIsChecked(!realIsChecked)
}
Copy link
Member

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],
Copy link
Member

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
Copy link
Member

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", () => {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
describe("Baby's first test", () => {
describe("CheckboxBase", () => {

Copy link
Member

@levithomason levithomason Jan 23, 2020

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
Copy link
Member

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.

Copy link
Member

@levithomason levithomason Jan 23, 2020

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.

Comment on lines +143 to +144
keyCode: 13,
key: 'Enter',
Copy link
Member

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?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

@khmakoto khmakoto Jan 23, 2020

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', () => {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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)
Copy link
Contributor

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: {
Copy link
Contributor

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)
Copy link
Contributor

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.

Copy link
Member

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:

  1. Components should work without requiring the consumer to wire them up. Dropdowns should open on click, inputs should update their value on input, etc.
  2. Components should defer to controlled mode if the dev provides props
  3. Components should resume control if the developer "unsets" the prop value (i.e. undefined).

return (
<RootSlot
onKeyDown={onKeyDown}
onClick={onChangeHandler}
Copy link
Contributor

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?

Copy link
Member

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>
  );
}```

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants