Repository navigation
CORE-2901: Add an indeterminate state to Checkbox #163
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
Changes from 4 commits
44302a8
49d0db4
b03aedc
48e041f
4eddd79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| import type React from "react"; | ||
| import { LabelHTMLAttributes, PropsWithChildren } from "react"; | ||
| import React, { LabelHTMLAttributes, PropsWithChildren, useEffect, useRef } from "react"; | ||
| import { checkboxVariants, CheckboxVariant, CheckboxSize } from "./sharedCheckboxStyles"; | ||
| import { InputHTMLAttributes } from "react"; | ||
| import classNames from "classnames"; | ||
|
|
@@ -8,12 +7,23 @@ import "./Checkbox.css"; | |
| type CheckboxProps = PropsWithChildren< | ||
| Omit<InputHTMLAttributes<HTMLInputElement>, 'type'> & { | ||
| variant?: CheckboxVariant; | ||
| /** Exposes the mixed state, for a checkbox that summarises a set of others. Does not change `checked`. */ | ||
| indeterminate?: boolean; | ||
| size?: CheckboxSize; | ||
| bold?: boolean; | ||
| labelProps?: LabelHTMLAttributes<HTMLLabelElement>; | ||
| }>; | ||
|
|
||
| export const Checkbox = ({ children, disabled, variant = 'primary', bold = false, size = 1.6, labelProps, className, style, ...props }: CheckboxProps) => { | ||
| export const Checkbox = ({ children, disabled, variant = 'primary', bold = false, size = 1.6, labelProps, className, style, indeterminate = false, onClick, ...props }: CheckboxProps) => { | ||
| const inputRef = useRef<HTMLInputElement>(null); | ||
|
|
||
| // `indeterminate` is a DOM property with no attribute, so it is applied after every render. | ||
| useEffect(() => { | ||
|
Member
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. Apparently this might have to be a
Contributor
Author
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. Done in 4eddd79. The effect is now a layout effect, so the property is set before paint. It falls back to |
||
| if (inputRef.current) { | ||
| inputRef.current.indeterminate = indeterminate; | ||
| } | ||
| }); | ||
|
RoyEJohnson marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Get variant styles for CSS variables | ||
| const variantStyles = disabled ? checkboxVariants.disabled : checkboxVariants[variant]; | ||
|
|
||
|
|
@@ -46,13 +56,26 @@ export const Checkbox = ({ children, disabled, variant = 'primary', bold = false | |
| '--checkbox-border-checked': variantStyles.checkedBorder, | ||
| '--checkbox-checkmark': variantStyles.backgroundImage === 'none' ? 'none' : `url('${variantStyles.backgroundImage}')`, | ||
| '--checkbox-opacity': disabled ? '0.4' : '1', | ||
| ...(indeterminate ? { | ||
| '--checkbox-indeterminate-bg': variantStyles.backgroundColor, | ||
| '--checkbox-indeterminate-icon': variantStyles.indeterminateImage === 'none' | ||
| ? 'none' | ||
| : `url('${variantStyles.indeterminateImage}')`, | ||
| } : {}), | ||
| ...style | ||
| } as unknown as React.CSSProperties; | ||
|
|
||
| return ( | ||
| <label {...labelProps} className={labelClassName} style={labelStyle}> | ||
| <input | ||
| {...props} | ||
| ref={inputRef} | ||
| onClick={indeterminate ? (event) => { | ||
| onClick?.(event); | ||
| // The browser clears it on click, before any render. Put it back so it follows the prop | ||
| // even when the click does not cause one. | ||
| event.currentTarget.indeterminate = true; | ||
| } : onClick} | ||
| type="checkbox" | ||
| className={inputClassName} | ||
| style={inputStyle} | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.