-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathCheckbox.tsx
More file actions
83 lines (79 loc) · 1.75 KB
/
Checkbox.tsx
File metadata and controls
83 lines (79 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import React, {
FC,
InputHTMLAttributes,
Ref,
useId,
useContext,
ReactNode,
} from 'react';
import classnames from 'classnames';
import { FormElement } from './FormElement';
import { CheckboxGroupContext, CheckboxValueType } from './CheckboxGroup';
/**
*
*/
export type CheckboxProps = {
label?: string;
required?: boolean;
cols?: number;
name?: string;
value?: CheckboxValueType;
checked?: boolean;
defaultChecked?: boolean;
tooltip?: ReactNode;
tooltipIcon?: string;
elementRef?: Ref<HTMLDivElement>;
inputRef?: Ref<HTMLInputElement>;
} & InputHTMLAttributes<HTMLInputElement>;
/**
*
*/
export const Checkbox: FC<CheckboxProps> = (props) => {
const {
type, // eslint-disable-line @typescript-eslint/no-unused-vars
id: id_,
className,
label,
required,
cols,
tooltip,
tooltipIcon,
elementRef,
inputRef,
children,
...rprops
} = props;
const prefix = useId();
const id = id_ ?? `${prefix}-id`;
const { grouped, error, errorId } = useContext(CheckboxGroupContext);
const formElemProps = {
required,
error,
errorId,
cols,
tooltip,
tooltipIcon,
elementRef,
};
const checkClassNames = classnames(className, 'slds-checkbox');
const check = (
<div className={checkClassNames}>
<input
ref={inputRef}
type='checkbox'
{...rprops}
id={id}
aria-describedby={error ? errorId : undefined}
/>
<label className='slds-checkbox__label' htmlFor={id}>
<span className='slds-checkbox_faux' />
<span className='slds-form-element__label'>{label || children}</span>
</label>
</div>
);
return grouped ? (
check
) : (
<FormElement {...formElemProps}>{check}</FormElement>
);
};