| 1 |
import { Fragment } from 'react'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import styles from '@patternfly/react-styles/css/components/DataList/data-list'; |
| 4 |
import { Checkbox, CheckboxProps } from '../Checkbox'; |
| 5 |
import { useSSRSafeId } from '../../helpers'; |
| 6 |
|
| 7 |
export interface DataListCheckProps extends Omit<CheckboxProps, 'ref' | 'id'> { |
| 8 |
/** Id of the DataList checkbox. */ |
| 9 |
id?: string; |
| 10 |
/** Additional classes added to the DataList item checkbox */ |
| 11 |
className?: string; |
| 12 |
/** Flag to show if the DataList checkbox selection is valid or invalid */ |
| 13 |
isValid?: boolean; |
| 14 |
/** Flag to show if the DataList checkbox is disabled */ |
| 15 |
isDisabled?: boolean; |
| 16 |
/** Flag to show if the DataList checkbox is checked when it is controlled by React state. Both isChecked and checked are valid, |
| 17 |
* but only use one. |
| 18 |
* To make the DataList checkbox uncontrolled, instead use the defaultChecked prop, but do not use both. |
| 19 |
*/ |
| 20 |
isChecked?: boolean; |
| 21 |
/** Flag to show if the DataList checkbox is checked when it is controlled by React state. Both isChecked and checked are valid, |
| 22 |
* but only use one. |
| 23 |
* To make the DataList checkbox uncontrolled, instead use the defaultChecked prop, but do not use both. |
| 24 |
*/ |
| 25 |
checked?: boolean; |
| 26 |
/** Flag to set default value of DataList checkbox when it is uncontrolled by React state. |
| 27 |
* To make the DataList checkbox controlled, instead use the isChecked prop, but do not use both. |
| 28 |
*/ |
| 29 |
defaultChecked?: boolean; |
| 30 |
/** A callback for when the DataList checkbox selection changes */ |
| 31 |
onChange?: (event: React.FormEvent<HTMLInputElement>, checked: boolean) => void; |
| 32 |
/** Aria-labelledby of the DataList checkbox */ |
| 33 |
'aria-labelledby': string; |
| 34 |
/** Flag to indicate if other controls are used in the DataListItem */ |
| 35 |
otherControls?: boolean; |
| 36 |
} |
| 37 |
|
| 38 |
export const DataListCheck: React.FunctionComponent<DataListCheckProps> = ({ |
| 39 |
id, |
| 40 |
className, |
| 41 |
onChange, |
| 42 |
isValid = true, |
| 43 |
isDisabled = false, |
| 44 |
isChecked, |
| 45 |
checked, |
| 46 |
defaultChecked, |
| 47 |
otherControls = false, |
| 48 |
...props |
| 49 |
}: DataListCheckProps) => { |
| 50 |
const randomId = useSSRSafeId(); |
| 51 |
const check = ( |
| 52 |
<div className={css(styles.dataListCheck)}> |
| 53 |
<Checkbox |
| 54 |
id={id ?? `datalist-check-${randomId}`} |
| 55 |
isChecked={isChecked} |
| 56 |
checked={checked} |
| 57 |
defaultChecked={defaultChecked} |
| 58 |
onChange={onChange} |
| 59 |
aria-invalid={!isValid} |
| 60 |
isDisabled={isDisabled} |
| 61 |
isLabelWrapped |
| 62 |
{...props} |
| 63 |
/> |
| 64 |
</div> |
| 65 |
); |
| 66 |
return ( |
| 67 |
<Fragment> |
| 68 |
{!otherControls && <div className={css(styles.dataListItemControl, className)}>{check}</div>} |
| 69 |
{otherControls && check} |
| 70 |
</Fragment> |
| 71 |
); |
| 72 |
}; |
| 73 |
DataListCheck.displayName = 'DataListCheck'; |