Skip to content

Commit

Permalink
UI kit: Checkbox (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpierre authored May 2, 2024
1 parent 2606e03 commit a42bb35
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 111 deletions.
51 changes: 51 additions & 0 deletions frontend/uikit-gallery/src/Checkbox/1. Default.fixture.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"use client";

import { Checkbox } from "@liquity2/uikit";
import { useState } from "react";
import type { ReactNode } from "react";

const options = ["Option 1", "Option 2", "Option 3"];

export default function CheckboxFixture() {
return (
<div>
{options.map((label, index) => (
<CheckboxField
key={index}
label={label}
/>
))}
</div>
);
}

function CheckboxField({ label }: { label: string }) {
const [checked, setChecked] = useState(false);
const toggle = () => setChecked((c) => !c);
return (
<Label>
<Checkbox
checked={checked}
onChange={toggle}
/>
{label}
</Label>
);
}

function Label({ children }: { children: ReactNode }) {
return (
<label
style={{
display: "flex",
alignItems: "center",
height: 32,
gap: 8,
cursor: "pointer",
userSelect: "none",
}}
>
{children}
</label>
);
}
52 changes: 52 additions & 0 deletions frontend/uikit-gallery/src/Checkbox/2. Disabled.fixture.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"use client";

import { Checkbox } from "@liquity2/uikit";
import { useState } from "react";
import type { ReactNode } from "react";

const options = ["Option 1", "Option 2", "Option 3"];

export default function CheckboxFixture() {
return (
<div>
{options.map((label, index) => (
<CheckboxField
key={index}
label={label}
/>
))}
</div>
);
}

function CheckboxField({ label }: { label: string }) {
const [checked, setChecked] = useState(false);
const toggle = () => setChecked((c) => !c);
return (
<Label>
<Checkbox
disabled={true}
checked={checked}
onChange={toggle}
/>
{label}
</Label>
);
}

function Label({ children }: { children: ReactNode }) {
return (
<label
style={{
display: "flex",
alignItems: "center",
height: 32,
gap: 8,
cursor: "pointer",
userSelect: "none",
}}
>
{children}
</label>
);
}
23 changes: 23 additions & 0 deletions frontend/uikit/src/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { ComponentPropsWithoutRef } from "react";

import { Radio } from "../Radio/Radio";

type RadioProps = ComponentPropsWithoutRef<typeof Radio>;

export function Checkbox({
appearance = "checkbox",
...props
}:
& Omit<RadioProps, "onChange">
& {
checked: NonNullable<RadioProps["checked"]>;
onChange: NonNullable<RadioProps["onChange"]>;
})
{
return (
<Radio
appearance={appearance}
{...props}
/>
);
}
Loading

0 comments on commit a42bb35

Please sign in to comment.