forked from CredenceOrg/Credence-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToggle.tsx
More file actions
41 lines (38 loc) · 965 Bytes
/
Copy pathToggle.tsx
File metadata and controls
41 lines (38 loc) · 965 Bytes
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
import './controls.css'
interface ToggleProps {
id?: string
checked: boolean
onChange: (next: boolean) => void
ariaLabel?: string
disabled?: boolean
isLoading?: boolean
error?: string
}
export default function Toggle({
id,
checked,
onChange,
ariaLabel,
disabled,
isLoading,
error,
}: ToggleProps) {
const isDisabled = disabled || isLoading
const isInvalid = !!error
return (
<div className={`control-toggle-wrapper ${isLoading ? 'control-toggle-wrapper--loading' : ''}`}>
<button
id={id}
className={`control-toggle ${isInvalid ? 'control-toggle--error' : ''}`}
role="switch"
aria-checked={checked}
aria-label={ariaLabel}
aria-invalid={isInvalid}
disabled={isDisabled}
onClick={() => onChange(!checked)}
>
{isLoading ? <span className="control-toggle-spinner" aria-hidden="true" /> : checked ? 'On' : 'Off'}
</button>
</div>
)
}