-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
✨ #12 - feat: add boolean component
- Loading branch information
Showing
10 changed files
with
174 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
@import "../../settings/style"; | ||
|
||
.mykn-boolean { | ||
align-items: center; | ||
border-radius: var(--border-radus-m); | ||
display: inline-flex; | ||
font-size: var(--typography-font-size-body-s); | ||
justify-content: center; | ||
height: var(--typography-line-height-body-s); | ||
width: var(--typography-line-height-body-s); | ||
|
||
&--value-true { | ||
background-color: var(--theme-color-success-background); | ||
color: var(--theme-color-success-body); | ||
} | ||
|
||
&--explicit#{&}--value-false { | ||
background-color: var(--theme-color-danger-background); | ||
color: var(--theme-color-danger-body); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { expect, within } from "@storybook/test"; | ||
import React from "react"; | ||
|
||
import { Body, H1, H2, H3, P } from "../typography"; | ||
import { Boolean } from "./boolean"; | ||
|
||
const meta = { | ||
title: "Icon/Boolean", | ||
component: Boolean, | ||
} satisfies Meta<typeof Boolean>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const True: Story = { | ||
args: { | ||
value: true, | ||
labelTrue: "This value is true", | ||
labelFalse: "This value is false", | ||
}, | ||
play: ({ canvasElement }) => { | ||
const canvas = within(canvasElement); | ||
const boolean = canvas.getByLabelText("This value is true"); | ||
expect(boolean).toBeVisible(); | ||
expect(boolean.children).toHaveLength(1); | ||
}, | ||
}; | ||
|
||
export const False: Story = { | ||
args: { | ||
value: false, | ||
labelTrue: "This value is true", | ||
labelFalse: "This value is false", | ||
}, | ||
play: ({ canvasElement }) => { | ||
const canvas = within(canvasElement); | ||
const boolean = canvas.getByLabelText("This value is false"); | ||
expect(boolean).toBeVisible(); | ||
expect(boolean.children).toHaveLength(0); | ||
}, | ||
}; | ||
|
||
export const ExplicitFalse: Story = { | ||
args: { | ||
explicit: true, | ||
value: false, | ||
labelTrue: "This value is true", | ||
labelFalse: "This value is false", | ||
}, | ||
play: ({ canvasElement }) => { | ||
const canvas = within(canvasElement); | ||
const boolean = canvas.getByLabelText("This value is false"); | ||
expect(boolean.children).toHaveLength(1); | ||
}, | ||
}; | ||
|
||
export const BooleanInText: Story = { | ||
args: { | ||
value: true, | ||
labelTrue: "This value is true", | ||
labelFalse: "This value is false", | ||
}, | ||
render: (args) => ( | ||
<Body> | ||
<H1> | ||
The quick brown fox jumps over the lazy dog <Boolean {...args} /> | ||
</H1> | ||
<H2> | ||
The quick brown fox jumps over the lazy dog <Boolean {...args} /> | ||
</H2> | ||
<H3> | ||
The quick brown fox jumps over the lazy dog <Boolean {...args} /> | ||
</H3> | ||
<P size="s"> | ||
The quick brown fox jumps over the lazy dog{" "} | ||
<Boolean {...args} value={false} explicit /> | ||
</P> | ||
<P size="xs"> | ||
The quick brown fox jumps over the lazy dog <Boolean {...args} /> | ||
</P> | ||
</Body> | ||
), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import clsx from "clsx"; | ||
import React from "react"; | ||
|
||
import { Outline } from "../../components/icon"; | ||
import "./boolean.scss"; | ||
|
||
export type BooleanProps = { | ||
/** The accessible label when value is true. */ | ||
labelTrue: string; | ||
|
||
/** The accessible label when value is false. */ | ||
labelFalse: string; | ||
|
||
/** The value. */ | ||
value: boolean; | ||
|
||
/** Show an icon, even if value is false. */ | ||
explicit?: boolean; | ||
}; | ||
|
||
/** | ||
* Boolean component, shows a checkmark if `value` is `true`. If `value` is | ||
* `false` the icon is hidden, unless `explicit` is `true`. | ||
* @param children | ||
* @param props | ||
* @constructor | ||
*/ | ||
export const Boolean: React.FC<BooleanProps> = ({ | ||
explicit = false, | ||
value, | ||
labelTrue, | ||
labelFalse, | ||
...props | ||
}) => ( | ||
<div | ||
className={clsx("mykn-boolean", `mykn-boolean--value-${value}`, { | ||
"mykn-boolean--explicit": explicit, | ||
})} | ||
{...props} | ||
title={value ? labelTrue : labelFalse} | ||
aria-label={value ? labelTrue : labelFalse} | ||
> | ||
{value && <Outline.CheckIcon />} | ||
{!value && explicit && <Outline.XMarkIcon />} | ||
</div> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./boolean"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters