Skip to content

Commit 563f913

Browse files
committed
add CheckboxInput component
1 parent 329e565 commit 563f913

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License.AGPL.txt in the project root for license information.
5+
*/
6+
7+
import { FC, useCallback } from "react";
8+
import { useId } from "../../hooks/useId";
9+
10+
type Props = {
11+
id?: string;
12+
value: string;
13+
checked: boolean;
14+
label: string;
15+
onChange: (checked: boolean) => void;
16+
};
17+
export const CheckboxInput: FC<Props> = ({ id, value, label, checked, onChange }) => {
18+
const maybeId = useId();
19+
const elementId = id || maybeId;
20+
21+
const handleChange = useCallback(
22+
(e) => {
23+
onChange(e.target.checked);
24+
},
25+
[onChange],
26+
);
27+
28+
return (
29+
<label className="flex space-x-2 justify-start items-center" htmlFor={elementId}>
30+
<input
31+
type="checkbox"
32+
className="rounded"
33+
value={value}
34+
id={elementId}
35+
checked={checked}
36+
onChange={handleChange}
37+
/>
38+
<span className="text-sm dark:text-gray-400 text-gray-600">{label}</span>
39+
</label>
40+
);
41+
};

0 commit comments

Comments
 (0)