Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow checkbox to be "untracked" #66

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@inkonchain/ink-kit",
"version": "0.3.3",
"version": "0.3.6",
"description": "",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
Expand Down
25 changes: 25 additions & 0 deletions src/components/Checkbox/Checkbox.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Meta, StoryObj } from "@storybook/react";
import { Checkbox, CheckboxProps, CheckboxLabel } from "./index";
import { fn } from "@storybook/test";
import { useEffect, useMemo, useState } from "react";
import { ListItem } from "../ListItem";
const meta: Meta<CheckboxProps> = {
title: "Components/Checkbox",
component: Checkbox,
Expand Down Expand Up @@ -118,3 +119,27 @@ export const NestingWithIndeterminateState: Story = {
);
},
};

/** If you want to use the Checkbox without it's own managed state, don't set `onChange` and simply pass `checked={value}`. */
export const ManagedByAParentItem: Story = {
argTypes: {
onChange: {
control: false,
},
},
args: {},
render: (args) => {
const [checked, setChecked] = useState(args.checked);
useEffect(() => {
setChecked(args.checked);
}, [args.checked]);
return (
<ListItem
onClick={() => setChecked(!checked)}
iconLeft={<Checkbox {...args} checked={checked} onChange={undefined} />}
>
<div>Checkbox</div>
</ListItem>
);
},
};
24 changes: 17 additions & 7 deletions src/components/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,41 @@ import { classNames } from "../../util/classes";
import { InkIcon } from "../..";

export interface CheckboxProps {
checked: boolean;
defaultChecked?: boolean;
checked?: boolean;
indeterminate?: boolean;
onChange: (enabled: boolean) => void;
onChange?: (enabled: boolean) => void;
}

export const Checkbox: React.FC<CheckboxProps> = ({
checked,
indeterminate,
onChange,
}) => {
const Component = onChange ? HeadlessCheckbox : "span";
return (
<HeadlessCheckbox
<Component
checked={checked}
onChange={onChange}
indeterminate={!checked && indeterminate}
onChange={(eventOrValue) => {
/* Only happens when we're using the HeadlessCheckbox component. If we are not, then we don't track changes. */
if (typeof eventOrValue === "boolean") {
onChange?.(eventOrValue);
}
}}
className={classNames(
"ink:group ink:relative ink:flex ink:items-center ink:justify-center ink:size-3 ink:shrink-0 ink:cursor-pointer ink:rounded-xs ink:box-border",
"ink:group ink:relative ink:flex ink:items-center ink:justify-center ink:size-3 ink:shrink-0 ink:rounded-xs ink:box-border",
"ink:transition-colors ink:transition-default-animation",
"ink:bg-background-container ink:shadow-xs",
"ink:ring-text-on-secondary ink:focus-visible:outline-none ink:focus-visible:text-on-primary ink:focus-visible:ring-2 ink:focus-visible:ring-offset-2",
"ink:data-checked:bg-button-primary",
"ink:data-indeterminate:bg-button-primary",
"ink:flex ink:items-center",
"ink:text-button-primary ink:data-checked:text-text-on-primary ink:data-indeterminate:text-text-on-primary"
"ink:text-button-primary ink:data-checked:text-text-on-primary ink:data-indeterminate:text-text-on-primary",
"ink:cursor-pointer"
)}
data-checked={checked ? "true" : undefined}
data-indeterminate={indeterminate ? "true" : undefined}
>
<div className="ink:absolute ink:inset-0 ink:flex ink:items-center ink:justify-center ink:box-border">
<InkIcon.Check
Expand All @@ -44,6 +54,6 @@ export const Checkbox: React.FC<CheckboxProps> = ({
"ink:animate-svg-path ink:in-data-indeterminate:animate-svg-path-start"
)}
/>
</HeadlessCheckbox>
</Component>
);
};
2 changes: 1 addition & 1 deletion src/components/Typography/Typography.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const meta: Meta<TypographyProps> = {
args={{
children: (
<div>
ink-text-{variant}: The quick brown fox jumps over the lazy
ink:text-{variant} - The quick brown fox jumps over the lazy
dog
</div>
),
Expand Down
Loading