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

Checkbox: add indeterminate state. #2241

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Checkbox } from '@fluentui/react'
import * as React from 'react'

const CheckboxExampleIndeterminate = () => {
return (
<>
<Checkbox indeterminate={true} label="Indeterminate Checked" />
</>
)
}

export default CheckboxExampleIndeterminate
5 changes: 5 additions & 0 deletions docs/src/examples/components/Checkbox/States/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ const States = () => (
description="A checkbox can be read-only and unable to change states."
examplePath="components/Checkbox/States/CheckboxExampleDisabled"
/>
<ComponentExample
title="Indeterminate"
description="A checkbox can be indeterminate."
examplePath="components/Checkbox/States/CheckboxExampleIndeterminate"
/>
</ExampleSection>
)

Expand Down
27 changes: 19 additions & 8 deletions packages/react/src/components/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export interface CheckboxProps extends UIComponentProps, ChildrenComponentProps
/** A checkbox's checked state can be controlled. */
checked?: SupportedIntrinsicInputProps['checked']

/** A checkbox can be indetermiante by default - uncontrolled state. */
defaultIndeterminate?: SupportedIntrinsicInputProps['defaultIndeterminate']

/** A checkbox's indeterminate state can be controlled. */
indeterminate?: SupportedIntrinsicInputProps['indeterminate']

/** A checkbox can appear disabled and be unable to change states. */
disabled?: SupportedIntrinsicInputProps['disabled']

Expand Down Expand Up @@ -65,6 +71,7 @@ export interface CheckboxProps extends UIComponentProps, ChildrenComponentProps

export interface CheckboxState {
checked: CheckboxProps['checked']
indeterminate: CheckboxProps['indeterminate']
}

class Checkbox extends AutoControlledComponent<WithAsProp<CheckboxProps>, CheckboxState> {
Expand All @@ -82,8 +89,10 @@ class Checkbox extends AutoControlledComponent<WithAsProp<CheckboxProps>, Checkb
}),
checked: PropTypes.bool,
defaultChecked: PropTypes.bool,
defaultIndeterminate: PropTypes.bool,
disabled: PropTypes.bool,
icon: customPropTypes.itemShorthandWithoutJSX,
indeterminate: customPropTypes.itemShorthandWithoutJSX,
label: customPropTypes.itemShorthand,
labelPosition: PropTypes.oneOf(['start', 'end']),
onChange: PropTypes.func,
Expand All @@ -107,30 +116,32 @@ class Checkbox extends AutoControlledComponent<WithAsProp<CheckboxProps>, Checkb
}

getInitialAutoControlledState(): CheckboxState {
return { checked: false }
return { checked: false, indeterminate: false }
}

handleChange = (e: React.ChangeEvent) => {
// Checkbox component doesn't present any `input` component in markup, however all of our
// components should handle events transparently.
const { disabled } = this.props
const { disabled, indeterminate } = this.props
const checked = !this.state.checked
const isIndeterminate = !this.state.indeterminate

if (!disabled) {
this.setState({ checked })
_.invoke(this.props, 'onChange', e, { ...this.props, checked })
this.setState({ checked, indeterminate })
_.invoke(this.props, 'onChange', e, { ...this.props, checked, isIndeterminate })
}
}

handleClick = (e: React.MouseEvent | React.KeyboardEvent) => {
const { disabled } = this.props
const { disabled, indeterminate } = this.props
const checked = !this.state.checked
const isIndeterminate = !this.state.indeterminate

if (!disabled) {
this.setState({ checked })
this.setState({ checked, indeterminate })

_.invoke(this.props, 'onClick', e, { ...this.props, checked })
_.invoke(this.props, 'onChange', e, { ...this.props, checked })
_.invoke(this.props, 'onClick', e, { ...this.props, checked, isIndeterminate })
_.invoke(this.props, 'onChange', e, { ...this.props, checked, isIndeterminate })
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/utils/htmlPropsUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ export type HtmlInputAttrs =
| 'autoCorrect'
| 'autoFocus'
| 'checked'
| 'defaultIndeterminate'
| 'disabled'
| 'form'
| 'id'
| 'indeterminate'
| 'list'
| 'max'
| 'maxLength'
Expand Down Expand Up @@ -92,9 +94,11 @@ export const htmlInputAttrs: HtmlInputAttrs[] = [
'autoCorrect',
'autoFocus',
'checked',
'defaultIndeterminate',
'disabled',
'form',
'id',
'indeterminate',
'list',
'max',
'maxLength',
Expand Down