From a251805661024ff5b27f74a7ce07e413af1d9608 Mon Sep 17 00:00:00 2001 From: Michael Pande Date: Fri, 8 Mar 2024 12:33:14 +0100 Subject: [PATCH] Mixed checkbox, style fixes and example --- .../components/Checkbox/Checkbox.module.scss | 5 ++ .../components/Checkbox/Checkbox.stories.tsx | 77 +++++++++++++++--- .../src/lib/components/Checkbox/Checkbox.tsx | 80 +++++++++++++++++-- .../src/lib/components/Icon/Icons.tsx | 1 + 4 files changed, 142 insertions(+), 21 deletions(-) diff --git a/libs/pxweb2-ui/src/lib/components/Checkbox/Checkbox.module.scss b/libs/pxweb2-ui/src/lib/components/Checkbox/Checkbox.module.scss index 6d999919..7f789161 100644 --- a/libs/pxweb2-ui/src/lib/components/Checkbox/Checkbox.module.scss +++ b/libs/pxweb2-ui/src/lib/components/Checkbox/Checkbox.module.scss @@ -29,3 +29,8 @@ align-items: center; justify-content: center; } + +.checked { + background-color: var(--px-color-surface-action); + color: white; +} diff --git a/libs/pxweb2-ui/src/lib/components/Checkbox/Checkbox.stories.tsx b/libs/pxweb2-ui/src/lib/components/Checkbox/Checkbox.stories.tsx index 95085765..ba6c53b5 100644 --- a/libs/pxweb2-ui/src/lib/components/Checkbox/Checkbox.stories.tsx +++ b/libs/pxweb2-ui/src/lib/components/Checkbox/Checkbox.stories.tsx @@ -1,5 +1,6 @@ import type { Meta, StoryFn } from '@storybook/react'; -import { Checkbox } from './Checkbox'; +import { Checkbox, MixedCheckbox } from './Checkbox'; +import React from 'react'; /* import { within } from '@storybook/testing-library'; import { expect } from '@storybook/jest'; */ @@ -11,25 +12,75 @@ const meta: Meta = { export default meta; export const Variants: StoryFn = () => { + const [selectedMalamute, setSelectedMalamute] = React.useState(true); + const [selectedHusky, setSelectedHusky] = React.useState(true); + const [selectedYorkshireTerrier, setSelectedYorkshireTerrier] = + React.useState(false); + const [allSelected, setAllSelected] = React.useState< + 'mixed' | 'true' | 'false' + >('mixed'); + + React.useEffect(() => { + if (!selectedHusky && !selectedMalamute && !selectedYorkshireTerrier) { + setAllSelected('false'); + } else if (selectedHusky && selectedMalamute && selectedYorkshireTerrier) { + setAllSelected('true'); + } else { + setAllSelected('mixed'); + } + }, [selectedMalamute, selectedHusky, selectedYorkshireTerrier]); + return ( <> - { - console.log(val); - }} - value={true} - /> -
- { console.log(val); + if (selectedHusky && selectedMalamute && selectedYorkshireTerrier) { + setSelectedHusky(false); + setSelectedMalamute(false); + setSelectedYorkshireTerrier(false); + } + if ( + !selectedHusky || + !selectedMalamute || + !selectedYorkshireTerrier + ) { + setSelectedHusky(true); + setSelectedMalamute(true); + setSelectedYorkshireTerrier(true); + } }} - value={true} + ariaControls={['var1', 'var2', 'var3']} + value={allSelected} /> +
+ { + setSelectedHusky(val); + }} + value={selectedHusky} + /> + { + setSelectedMalamute(val); + }} + value={selectedMalamute} + /> + { + setSelectedYorkshireTerrier(val); + }} + value={selectedYorkshireTerrier} + /> +

); diff --git a/libs/pxweb2-ui/src/lib/components/Checkbox/Checkbox.tsx b/libs/pxweb2-ui/src/lib/components/Checkbox/Checkbox.tsx index b0bcae3b..47e4bd2b 100644 --- a/libs/pxweb2-ui/src/lib/components/Checkbox/Checkbox.tsx +++ b/libs/pxweb2-ui/src/lib/components/Checkbox/Checkbox.tsx @@ -1,4 +1,5 @@ import React from 'react'; +import cl from 'clsx'; import styles from './Checkbox.module.scss'; import { Icon } from '../Icon/Icon'; import Label from '../Typography/Label/Label'; @@ -18,29 +19,92 @@ export const Checkbox: React.FC = ({ onChange, tabIndex, }) => { - const [checked, setChecked] = React.useState(value); - return ( + ); +}; +interface MixedCheckboxProps { + id: string; + text: string; + value: 'mixed' | 'false' | 'true'; + onChange: (str: string) => void; + ariaControls: string[]; + tabIndex?: number; +} +export const MixedCheckbox: React.FC = ({ + id, + value, + text, + onChange, + ariaControls, + tabIndex, +}) => { + return ( +