Skip to content

Commit 482560f

Browse files
committed
refactor(CFormCheck): update indeterminate state
1 parent 94f54b0 commit 482560f

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

packages/coreui-react/src/components/form/CFormCheck.tsx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import React, { forwardRef, InputHTMLAttributes, ReactNode } from 'react'
1+
import React, { forwardRef, InputHTMLAttributes, ReactNode, useEffect, useRef } from 'react'
22
import PropTypes from 'prop-types'
33
import classNames from 'classnames'
44

5+
import { useForkedRef } from '../../utils/hooks'
56
import { Colors, Shapes } from '../Types'
67

78
import { CFormLabel } from './CFormLabel'
@@ -46,6 +47,10 @@ export interface CFormCheckProps extends InputHTMLAttributes<HTMLInputElement> {
4647
* The id global attribute defines an identifier (ID) that must be unique in the whole document.
4748
*/
4849
id?: string
50+
/**
51+
* Input Checkbox indeterminate Property.
52+
*/
53+
indeterminate?: boolean
4954
/**
5055
* Group checkboxes or radios on the same horizontal row by adding.
5156
*/
@@ -70,9 +75,30 @@ export interface CFormCheckProps extends InputHTMLAttributes<HTMLInputElement> {
7075

7176
export const CFormCheck = forwardRef<HTMLInputElement, CFormCheckProps>(
7277
(
73-
{ className, button, hitArea, id, inline, invalid, label, type = 'checkbox', valid, ...rest },
78+
{
79+
className,
80+
button,
81+
hitArea,
82+
id,
83+
indeterminate,
84+
inline,
85+
invalid,
86+
label,
87+
type = 'checkbox',
88+
valid,
89+
...rest
90+
},
7491
ref,
7592
) => {
93+
const inputRef = useRef<HTMLInputElement>(null)
94+
const forkedRef = useForkedRef(ref, inputRef)
95+
96+
useEffect(() => {
97+
if (inputRef.current && indeterminate) {
98+
inputRef.current.indeterminate = indeterminate
99+
}
100+
}, [indeterminate])
101+
76102
const _className = classNames(
77103
'form-check',
78104
{
@@ -102,7 +128,7 @@ export const CFormCheck = forwardRef<HTMLInputElement, CFormCheckProps>(
102128
)
103129

104130
const formControl = () => {
105-
return <input type={type} className={inputClassName} id={id} {...rest} ref={ref} />
131+
return <input type={type} className={inputClassName} id={id} {...rest} ref={forkedRef} />
106132
}
107133

108134
const formLabel = () => {
@@ -141,6 +167,7 @@ CFormCheck.propTypes = {
141167
className: PropTypes.string,
142168
hitArea: PropTypes.oneOf(['full']),
143169
id: PropTypes.string,
170+
indeterminate: PropTypes.bool,
144171
inline: PropTypes.bool,
145172
invalid: PropTypes.bool,
146173
label: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),

packages/docs/content/4.0/api/CFormCheck.api.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import CFormCheck from '@coreui/react/src/components/form/CFormCheck'
1111
| **className** | A string of all className you want applied to the component. | `string` | - |
1212
| **hitArea** | Sets hit area to the full area of the component. | `'full'` | - |
1313
| **id** | The id global attribute defines an identifier (ID) that must be unique in the whole document. | `string` | - |
14+
| **indeterminate** | Input Checkbox indeterminate Property. | `boolean` | - |
1415
| **inline** | Group checkboxes or radios on the same horizontal row by adding. | `boolean` | - |
1516
| **invalid** | Set component validation state to invalid. | `boolean` | - |
1617
| **label** | The element represents a caption for a component. | `ReactNode` | - |

packages/docs/content/4.0/forms/checks-radios.mdx

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,14 @@ Browser default checkboxes and radios are replaced with the help of `<CFormCheck
3434

3535
## Indeterminate
3636

37-
Checkboxes can utilize the `:indeterminate` pseudo class when manually set via JavaScript (there is no available HTML attribute for specifying it).
38-
39-
export const IndeterminateExample = () => {
40-
const checkboxRef = useRef(null)
41-
useEffect(() => {
42-
checkboxRef.current.indeterminate = true
43-
})
44-
return <CFormCheck id="flexCheckIndeterminate" label="Indeterminate checkbox" ref={checkboxRef} />
45-
}
37+
Checkboxes can utilize the `:indeterminate` pseudo-class when manually set via `indeterminate` property.
4638

4739
<Example>
48-
<IndeterminateExample />
40+
<CFormCheck id="flexCheckIndeterminate" label="Indeterminate checkbox" indeterminate />
4941
</Example>
5042

5143
```jsx
52-
const checkboxRef = useRef(null)
53-
useEffect(() => {
54-
checkboxRef.current.indeterminate = true
55-
})
56-
return <CFormCheck id="flexCheckIndeterminate" label="Indeterminate checkbox" ref={checkboxRef} />
44+
<CFormCheck id="flexCheckIndeterminate" label="Indeterminate checkbox" indeterminate />
5745
```
5846

5947
### Disabled

0 commit comments

Comments
 (0)