@nypl/design-system-react-components 0.25.5 → 0.25.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +91 -1
- package/README.md +1 -1
- package/dist/components/Breadcrumbs/BreadcrumbsTypes.d.ts +1 -0
- package/dist/components/Button/Button.d.ts +6 -6
- package/dist/components/Button/ButtonTypes.d.ts +0 -1
- package/dist/components/Card/Card.d.ts +6 -4
- package/dist/components/Checkbox/Checkbox.d.ts +3 -2
- package/dist/components/CheckboxGroup/CheckboxGroup.d.ts +3 -2
- package/dist/components/ComponentWrapper/ComponentWrapper.d.ts +8 -2
- package/dist/components/DatePicker/DatePicker.d.ts +3 -2
- package/dist/components/Form/Form.d.ts +2 -2
- package/dist/components/Heading/Heading.d.ts +7 -3
- package/dist/components/Heading/HeadingTypes.d.ts +6 -6
- package/dist/components/HelperErrorText/HelperErrorText.d.ts +5 -2
- package/dist/components/HorizontalRule/HorizontalRule.d.ts +2 -2
- package/dist/components/Icons/Icon.d.ts +4 -4
- package/dist/components/Icons/IconSvgs.d.ts +1 -21
- package/dist/components/Icons/IconTypes.d.ts +1 -23
- package/dist/components/Image/Image.d.ts +11 -3
- package/dist/components/Image/ImageTypes.d.ts +3 -1
- package/dist/components/Link/LinkTypes.d.ts +1 -0
- package/dist/components/Logo/Logo.d.ts +28 -0
- package/dist/components/Logo/LogoSvgs.d.ts +18 -0
- package/dist/components/Logo/LogoTypes.d.ts +30 -0
- package/dist/components/Modal/Modal.d.ts +0 -4
- package/dist/components/Notification/Notification.d.ts +4 -2
- package/dist/components/Placeholder/Placeholder.d.ts +3 -5
- package/dist/components/Radio/Radio.d.ts +6 -5
- package/dist/components/RadioGroup/RadioGroup.d.ts +3 -2
- package/dist/components/SearchBar/SearchBar.d.ts +10 -2
- package/dist/components/Select/Select.d.ts +5 -2
- package/dist/components/SkeletonLoader/SkeletonLoader.d.ts +8 -4
- package/dist/components/Slider/Slider.d.ts +3 -2
- package/dist/components/StatusBadge/StatusBadge.d.ts +2 -1
- package/dist/components/StructuredContent/StructuredContent.d.ts +41 -0
- package/dist/components/StructuredContent/StructuredContentTypes.d.ts +5 -0
- package/dist/components/Table/Table.d.ts +23 -0
- package/dist/components/Template/Template.d.ts +8 -3
- package/dist/components/Text/Text.d.ts +2 -2
- package/dist/components/TextInput/TextInput.d.ts +4 -3
- package/dist/components/Toggle/Toggle.d.ts +48 -0
- package/dist/components/Toggle/ToggleSizes.d.ts +4 -0
- package/dist/components/VideoPlayer/VideoPlayer.d.ts +17 -6
- package/dist/design-system-react-components.cjs.development.js +5989 -5490
- package/dist/design-system-react-components.cjs.development.js.map +1 -1
- package/dist/design-system-react-components.cjs.production.min.js +1 -1
- package/dist/design-system-react-components.cjs.production.min.js.map +1 -1
- package/dist/design-system-react-components.esm.js +6057 -5551
- package/dist/design-system-react-components.esm.js.map +1 -1
- package/dist/index.d.ts +11 -4
- package/dist/styles.css +2 -2
- package/dist/theme/components/breadcrumb.d.ts +9 -0
- package/dist/theme/components/button.d.ts +0 -12
- package/dist/theme/components/card.d.ts +14 -2
- package/dist/theme/components/customTable.d.ts +47 -0
- package/dist/theme/components/fieldset.d.ts +2 -2
- package/dist/theme/components/global.d.ts +1 -1
- package/dist/theme/components/heading.d.ts +4 -0
- package/dist/theme/components/label.d.ts +1 -1
- package/dist/theme/components/link.d.ts +14 -1
- package/dist/theme/components/list.d.ts +0 -2
- package/dist/theme/components/logo.d.ts +4 -0
- package/dist/theme/components/notification.d.ts +8 -4
- package/dist/theme/components/searchBar.d.ts +7 -13
- package/dist/theme/components/select.d.ts +1 -0
- package/dist/theme/components/structuredContent.d.ts +33 -0
- package/dist/theme/components/template.d.ts +10 -10
- package/dist/theme/components/textInput.d.ts +2 -0
- package/dist/theme/components/toggle.d.ts +71 -0
- package/dist/theme/foundations/spacing.d.ts +2 -0
- package/dist/utils/utils.d.ts +10 -0
- package/package.json +2 -3
- package/src/__tests__/utils/utils.test.ts +23 -1
- package/src/components/Accordion/Accordion.stories.mdx +15 -14
- package/src/components/Autosuggest/Autosuggest.stories.mdx +2 -1
- package/src/components/Autosuggest/Autosuggest.stories.tsx +24 -48
- package/src/components/Autosuggest/_Autosuggest.scss +2 -6
- package/src/components/Breadcrumbs/Breadcrumbs.stories.mdx +50 -14
- package/src/components/Breadcrumbs/Breadcrumbs.test.tsx +20 -0
- package/src/components/Breadcrumbs/BreadcrumbsTypes.tsx +1 -0
- package/src/components/Breadcrumbs/__snapshots__/Breadcrumbs.test.tsx.snap +198 -0
- package/src/components/Button/Button.stories.mdx +93 -48
- package/src/components/Button/Button.test.tsx +0 -12
- package/src/components/Button/Button.tsx +7 -8
- package/src/components/Button/ButtonTypes.tsx +0 -1
- package/src/components/Button/__snapshots__/Button.test.tsx.snap +0 -12
- package/src/components/Card/Card.stories.mdx +246 -64
- package/src/components/Card/Card.test.tsx +45 -22
- package/src/components/Card/Card.tsx +26 -13
- package/src/components/Card/__snapshots__/Card.test.tsx.snap +75 -37
- package/src/components/Chakra/Box.stories.mdx +3 -3
- package/src/components/Chakra/Center.stories.mdx +5 -5
- package/src/components/Chakra/Grid.stories.mdx +14 -17
- package/src/components/Chakra/Stack.stories.mdx +2 -2
- package/src/components/Checkbox/Checkbox.stories.mdx +37 -15
- package/src/components/Checkbox/Checkbox.tsx +13 -8
- package/src/components/CheckboxGroup/CheckboxGroup.stories.mdx +48 -16
- package/src/components/CheckboxGroup/CheckboxGroup.tsx +14 -10
- package/src/components/CheckboxGroup/__snapshots__/CheckboxGroup.test.tsx.snap +6 -3
- package/src/components/ComponentWrapper/ComponentWrapper.test.tsx +151 -0
- package/src/components/ComponentWrapper/ComponentWrapper.tsx +36 -23
- package/src/components/ComponentWrapper/__snapshots__/ComponentWrapper.test.tsx.snap +85 -0
- package/src/components/DatePicker/DatePicker.stories.mdx +63 -18
- package/src/components/DatePicker/DatePicker.test.tsx +8 -6
- package/src/components/DatePicker/DatePicker.tsx +10 -6
- package/src/components/DatePicker/__snapshots__/DatePicker.test.tsx.snap +41 -22
- package/src/components/Fieldset/Fieldset.stories.mdx +19 -8
- package/src/components/Form/Form.stories.mdx +49 -41
- package/src/components/Form/Form.tsx +5 -4
- package/src/components/Grid/SimpleGrid.stories.mdx +26 -26
- package/src/components/Heading/Heading.stories.mdx +59 -23
- package/src/components/Heading/Heading.test.tsx +82 -18
- package/src/components/Heading/Heading.tsx +31 -31
- package/src/components/Heading/HeadingTypes.tsx +6 -6
- package/src/components/Heading/__snapshots__/Heading.test.tsx.snap +71 -0
- package/src/components/HelperErrorText/HelperErrorText.stories.mdx +55 -27
- package/src/components/HelperErrorText/HelperErrorText.test.tsx +42 -15
- package/src/components/HelperErrorText/HelperErrorText.tsx +24 -24
- package/src/components/HelperErrorText/__snapshots__/HelperErrorText.test.tsx.snap +41 -4
- package/src/components/Hero/Hero.stories.mdx +72 -53
- package/src/components/HorizontalRule/HorizontalRule.stories.mdx +9 -8
- package/src/components/HorizontalRule/HorizontalRule.tsx +2 -4
- package/src/components/Icons/Icon.stories.mdx +77 -75
- package/src/components/Icons/Icon.tsx +4 -5
- package/src/components/Icons/IconSvgs.tsx +2 -42
- package/src/components/Icons/IconTypes.tsx +1 -24
- package/src/components/Image/Image.stories.mdx +214 -104
- package/src/components/Image/Image.test.tsx +10 -0
- package/src/components/Image/Image.tsx +21 -10
- package/src/components/Image/ImageTypes.ts +2 -0
- package/src/components/Image/__snapshots__/Image.test.tsx.snap +24 -8
- package/src/components/Label/Label.stories.mdx +21 -20
- package/src/components/Link/Link.stories.mdx +103 -53
- package/src/components/Link/Link.test.tsx +108 -7
- package/src/components/Link/Link.tsx +58 -19
- package/src/components/Link/LinkTypes.tsx +1 -0
- package/src/components/Link/__snapshots__/Link.test.tsx.snap +261 -0
- package/src/components/List/List.stories.mdx +36 -24
- package/src/components/Logo/Logo.stories.mdx +220 -0
- package/src/components/Logo/Logo.test.tsx +98 -0
- package/src/components/Logo/Logo.tsx +97 -0
- package/src/components/Logo/LogoSvgs.tsx +34 -0
- package/src/components/Logo/LogoTypes.tsx +32 -0
- package/src/components/Logo/__snapshots__/Logo.test.tsx.snap +71 -0
- package/src/components/Modal/Modal.stories.mdx +20 -5
- package/src/components/Modal/Modal.tsx +2 -8
- package/src/components/Notification/Notification.stories.mdx +96 -40
- package/src/components/Notification/Notification.test.tsx +62 -16
- package/src/components/Notification/Notification.tsx +26 -9
- package/src/components/Notification/__snapshots__/Notification.test.tsx.snap +121 -0
- package/src/components/Pagination/Pagination.stories.mdx +18 -8
- package/src/components/Pagination/Pagination.tsx +1 -1
- package/src/components/Pagination/__snapshots__/Pagination.test.tsx.snap +42 -0
- package/src/components/Placeholder/Placeholder.tsx +7 -14
- package/src/components/ProgressIndicator/ProgressIndicator.stories.mdx +74 -46
- package/src/components/Radio/Radio.stories.mdx +39 -19
- package/src/components/Radio/Radio.tsx +13 -9
- package/src/components/RadioGroup/RadioGroup.stories.mdx +50 -16
- package/src/components/RadioGroup/RadioGroup.tsx +15 -11
- package/src/components/RadioGroup/__snapshots__/RadioGroup.test.tsx.snap +6 -3
- package/src/components/SearchBar/SearchBar.Test.tsx +114 -22
- package/src/components/SearchBar/SearchBar.stories.mdx +111 -33
- package/src/components/SearchBar/SearchBar.tsx +57 -33
- package/src/components/Select/Select.stories.mdx +177 -66
- package/src/components/Select/Select.test.tsx +2 -2
- package/src/components/Select/Select.tsx +22 -12
- package/src/components/SkeletonLoader/SkeletonLoader.stories.mdx +50 -16
- package/src/components/SkeletonLoader/SkeletonLoader.tsx +8 -4
- package/src/components/Slider/Slider.stories.mdx +74 -23
- package/src/components/Slider/Slider.test.tsx +35 -0
- package/src/components/Slider/Slider.tsx +22 -11
- package/src/components/Slider/__snapshots__/Slider.test.tsx.snap +60 -30
- package/src/components/StatusBadge/StatusBadge.stories.mdx +33 -18
- package/src/components/StatusBadge/StatusBadge.tsx +2 -1
- package/src/components/StructuredContent/StructuredContent.stories.mdx +427 -0
- package/src/components/StructuredContent/StructuredContent.test.tsx +376 -0
- package/src/components/StructuredContent/StructuredContent.tsx +153 -0
- package/src/components/StructuredContent/StructuredContentTypes.tsx +5 -0
- package/src/components/StructuredContent/__snapshots__/StructuredContent.test.tsx.snap +283 -0
- package/src/components/StyleGuide/Bidirectionality.stories.mdx +32 -83
- package/src/components/StyleGuide/Breakpoints.stories.mdx +1 -11
- package/src/components/StyleGuide/Buttons.stories.mdx +3 -18
- package/src/components/StyleGuide/ColorCard.tsx +1 -2
- package/src/components/StyleGuide/Colors.stories.mdx +3 -11
- package/src/components/StyleGuide/DesignTokens.stories.mdx +3 -8
- package/src/components/StyleGuide/Forms.stories.mdx +2 -10
- package/src/components/StyleGuide/Iconography.stories.mdx +8 -34
- package/src/components/StyleGuide/Spacing.stories.mdx +3 -14
- package/src/components/StyleGuide/Typography.stories.mdx +64 -76
- package/src/components/Table/Table.stories.mdx +66 -0
- package/src/components/Table/Table.test.tsx +60 -0
- package/src/components/Table/Table.tsx +116 -0
- package/src/components/Tabs/Tabs.stories.mdx +19 -13
- package/src/components/Tabs/Tabs.test.tsx +1 -1
- package/src/components/Template/Template.stories.mdx +57 -49
- package/src/components/Template/Template.test.tsx +65 -5
- package/src/components/Template/Template.tsx +35 -5
- package/src/components/Template/__snapshots__/Template.test.tsx.snap +76 -0
- package/src/components/Text/Text.stories.mdx +33 -11
- package/src/components/Text/Text.tsx +2 -2
- package/src/components/TextInput/TextInput.stories.mdx +84 -17
- package/src/components/TextInput/TextInput.test.tsx +96 -0
- package/src/components/TextInput/TextInput.tsx +12 -8
- package/src/components/TextInput/__snapshots__/TextInput.test.tsx.snap +240 -0
- package/src/components/Toggle/Toggle.stories.mdx +200 -0
- package/src/components/Toggle/Toggle.test.tsx +140 -0
- package/src/components/Toggle/Toggle.tsx +123 -0
- package/src/components/Toggle/ToggleSizes.tsx +4 -0
- package/src/components/Toggle/__snapshots__/Toggle.test.tsx.snap +255 -0
- package/src/components/VideoPlayer/VideoPlayer.stories.mdx +96 -25
- package/src/components/VideoPlayer/VideoPlayer.test.tsx +103 -1
- package/src/components/VideoPlayer/VideoPlayer.tsx +72 -22
- package/src/components/VideoPlayer/__snapshots__/VideoPlayer.test.tsx.snap +57 -3
- package/src/docs/Chakra.stories.mdx +5 -8
- package/src/docs/Intro.stories.mdx +2 -2
- package/src/index.ts +13 -4
- package/src/styles/base/_03-base.scss +1 -1
- package/src/styles/base/_place-holder.scss +7 -7
- package/src/styles.scss +1 -5
- package/src/theme/components/breadcrumb.ts +14 -4
- package/src/theme/components/button.ts +5 -12
- package/src/theme/components/card.ts +9 -7
- package/src/theme/components/checkbox.ts +1 -1
- package/src/theme/components/customTable.ts +50 -0
- package/src/theme/components/datePicker.ts +1 -1
- package/src/theme/components/global.ts +7 -7
- package/src/theme/components/heading.ts +13 -11
- package/src/theme/components/helperErrorText.ts +1 -1
- package/src/theme/components/icon.ts +2 -2
- package/src/theme/components/image.ts +9 -1
- package/src/theme/components/link.ts +17 -5
- package/src/theme/components/list.ts +1 -3
- package/src/theme/components/logo.ts +54 -0
- package/src/theme/components/notification.ts +9 -7
- package/src/theme/components/searchBar.ts +7 -13
- package/src/theme/components/select.ts +1 -0
- package/src/theme/components/statusBadge.ts +1 -1
- package/src/theme/components/structuredContent.ts +54 -0
- package/src/theme/components/template.ts +10 -10
- package/src/theme/components/text.ts +6 -6
- package/src/theme/components/textInput.ts +1 -0
- package/src/theme/components/toggle.ts +69 -0
- package/src/theme/components/videoPlayer.ts +0 -2
- package/src/theme/foundations/global.ts +2 -2
- package/src/theme/foundations/spacing.ts +3 -0
- package/src/theme/foundations/typography.ts +84 -12
- package/src/theme/index.ts +8 -0
- package/src/utils/componentCategories.ts +4 -2
- package/src/utils/utils.ts +17 -0
- package/dist/__tests__/utils/bem.test.d.ts +0 -1
- package/dist/components/CardEdition/CardEdition.d.ts +0 -21
- package/dist/components/CardEdition/CardEdition.stories.d.ts +0 -27
- package/dist/components/Input/Input.d.ts +0 -36
- package/dist/components/Input/Input.stories.d.ts +0 -29
- package/dist/components/Input/InputTypes.d.ts +0 -6
- package/dist/components/StyleGuide/UIDocCard.d.ts +0 -11
- package/dist/helpers/CSSVariablesHelper.d.ts +0 -3
- package/dist/helpers/getCSSVariable.d.ts +0 -1
- package/dist/interfaces.d.ts +0 -3
- package/dist/utils/bem.d.ts +0 -1
- package/src/__tests__/utils/bem.test.ts +0 -37
- package/src/components/CardEdition/CardEdition.stories.tsx +0 -122
- package/src/components/CardEdition/CardEdition.test.tsx +0 -395
- package/src/components/CardEdition/CardEdition.tsx +0 -60
- package/src/components/CardEdition/_CardEdition.scss +0 -137
- package/src/components/Input/Input.stories.tsx +0 -133
- package/src/components/Input/Input.test.tsx +0 -266
- package/src/components/Input/Input.tsx +0 -81
- package/src/components/Input/InputTypes.tsx +0 -8
- package/src/components/Input/_Input.scss +0 -78
- package/src/components/StyleGuide/UIDocCard.tsx +0 -36
- package/src/helpers/CSSVariablesHelper.tsx +0 -34
- package/src/helpers/getCSSVariable.tsx +0 -5
- package/src/interfaces.ts +0 -3
- package/src/utils/bem.ts +0 -44
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { HStack, VStack, Grid, Box } from "@chakra-ui/react";
|
|
2
|
+
import {
|
|
3
|
+
Meta,
|
|
4
|
+
Story,
|
|
5
|
+
Canvas,
|
|
6
|
+
ArgsTable,
|
|
7
|
+
Description,
|
|
8
|
+
} from "@storybook/addon-docs";
|
|
9
|
+
import { withDesign } from "storybook-addon-designs";
|
|
10
|
+
|
|
11
|
+
import Heading from "../Heading/Heading";
|
|
12
|
+
import SimpleGrid from "../Grid/SimpleGrid";
|
|
13
|
+
import Toggle from "./Toggle";
|
|
14
|
+
import { ToggleSizes } from "./ToggleSizes";
|
|
15
|
+
import { getCategory } from "../../utils/componentCategories";
|
|
16
|
+
import DSProvider from "../../theme/provider";
|
|
17
|
+
import { getStorybookEnumValues } from "../../utils/utils";
|
|
18
|
+
|
|
19
|
+
export const sizeEnumValues = getStorybookEnumValues(
|
|
20
|
+
ToggleSizes,
|
|
21
|
+
"ToggleSizes"
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
<Meta
|
|
25
|
+
title={getCategory("Toggle")}
|
|
26
|
+
component={Toggle}
|
|
27
|
+
decorators={[withDesign]}
|
|
28
|
+
parameters={{
|
|
29
|
+
design: {
|
|
30
|
+
type: "figma",
|
|
31
|
+
url: "https://www.figma.com/file/qShodlfNCJHb8n03IFyApM/Main?node-id=31581%3A302",
|
|
32
|
+
},
|
|
33
|
+
jest: ["Toggle.test.tsx"],
|
|
34
|
+
}}
|
|
35
|
+
argTypes={{
|
|
36
|
+
additionalStyles: { control: false },
|
|
37
|
+
defaultChecked: { control: false },
|
|
38
|
+
id: { control: false },
|
|
39
|
+
key: { table: { disable: true } },
|
|
40
|
+
name: { control: false },
|
|
41
|
+
onChange: { control: false },
|
|
42
|
+
ref: { table: { disable: true } },
|
|
43
|
+
size: {
|
|
44
|
+
control: {
|
|
45
|
+
type: "select",
|
|
46
|
+
table: { defaultValue: { summary: "ToggleSizes.Large" } },
|
|
47
|
+
options: sizeEnumValues.options,
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
}}
|
|
51
|
+
/>
|
|
52
|
+
|
|
53
|
+
# Toggle
|
|
54
|
+
|
|
55
|
+
| Component Version | DS Version |
|
|
56
|
+
| ----------------- | ---------- |
|
|
57
|
+
| Added | `0.25.8` |
|
|
58
|
+
| Latest | `0.25.9` |
|
|
59
|
+
|
|
60
|
+
<Description of={Toggle} />
|
|
61
|
+
|
|
62
|
+
The Toggle component is used as an alternative for the Checkbox component and returns a boolean response. Like the Checkbox component, the Toggle component can display a label to the right of the toggle and helper/error text below the toggle.
|
|
63
|
+
|
|
64
|
+
### Best Practices
|
|
65
|
+
|
|
66
|
+
Toggle components are commonly used for “on/off” switches to perform binary actions that occur immediately after a user “flips the switch.”
|
|
67
|
+
|
|
68
|
+
The Toggle component label should clarify the action being performed. Labels should be short and to the point, often three words or less.
|
|
69
|
+
|
|
70
|
+
<Canvas>
|
|
71
|
+
<Story
|
|
72
|
+
name="Toggle"
|
|
73
|
+
args={{
|
|
74
|
+
additionalStyles: undefined,
|
|
75
|
+
defaultChecked: false,
|
|
76
|
+
helperText: "This is the helper text!",
|
|
77
|
+
id: "toggle-1",
|
|
78
|
+
invalidText: "",
|
|
79
|
+
isChecked: undefined,
|
|
80
|
+
isDisabled: false,
|
|
81
|
+
isInvalid: false,
|
|
82
|
+
isRequired: false,
|
|
83
|
+
labelText: "Test Label",
|
|
84
|
+
name: "toggle-1",
|
|
85
|
+
onChange: undefined,
|
|
86
|
+
size: "ToogleSizes.Large",
|
|
87
|
+
}}
|
|
88
|
+
>
|
|
89
|
+
{(args) => <Toggle {...args} size={sizeEnumValues.getValue(args.size)} />}
|
|
90
|
+
</Story>
|
|
91
|
+
</Canvas>
|
|
92
|
+
|
|
93
|
+
<ArgsTable story="Toggle" />
|
|
94
|
+
|
|
95
|
+
## Sizes
|
|
96
|
+
|
|
97
|
+
<Canvas>
|
|
98
|
+
<DSProvider>
|
|
99
|
+
<SimpleGrid columns="2">
|
|
100
|
+
<VStack align="left" spacing="s">
|
|
101
|
+
<Heading level={3}>Large</Heading>
|
|
102
|
+
<Toggle size={ToggleSizes.Large} defaultChecked={true} labelText="On" />
|
|
103
|
+
<Toggle size={ToggleSizes.Large} labelText="Off" />
|
|
104
|
+
</VStack>
|
|
105
|
+
<VStack align="left" spacing="s">
|
|
106
|
+
<Heading level={3}>Small</Heading>
|
|
107
|
+
<Toggle size={ToggleSizes.Small} defaultChecked={true} labelText="On" />
|
|
108
|
+
<Toggle size={ToggleSizes.Small} labelText="Off" />
|
|
109
|
+
</VStack>
|
|
110
|
+
</SimpleGrid>
|
|
111
|
+
</DSProvider>
|
|
112
|
+
</Canvas>
|
|
113
|
+
|
|
114
|
+
## Controlled Toggle
|
|
115
|
+
|
|
116
|
+
Note that the `isChecked` property in this example is set to `true`. Clicking
|
|
117
|
+
on it won't change the toggle; The `isChecked` prop must now be controlled
|
|
118
|
+
and removed manually by the parent component that controls this state. The
|
|
119
|
+
`onChange` function will be called every time the `isChecked` prop changes.
|
|
120
|
+
|
|
121
|
+
```jsx
|
|
122
|
+
const isChecked = true;
|
|
123
|
+
const onChange = (e) => {
|
|
124
|
+
// This will return the value through the event object.
|
|
125
|
+
console.log(e.target.value);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
<Toggle isChecked={true} onChange={onChange} labelText="Controlled Toggle" />;
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
<Canvas>
|
|
132
|
+
<DSProvider>
|
|
133
|
+
<Toggle
|
|
134
|
+
isChecked={true}
|
|
135
|
+
onChange={(e) => {
|
|
136
|
+
console.log(e.target.value);
|
|
137
|
+
}}
|
|
138
|
+
labelText="Controlled Toggle"
|
|
139
|
+
/>
|
|
140
|
+
</DSProvider>
|
|
141
|
+
</Canvas>
|
|
142
|
+
|
|
143
|
+
## Browser States
|
|
144
|
+
|
|
145
|
+
<Canvas>
|
|
146
|
+
<DSProvider>
|
|
147
|
+
<SimpleGrid columns="3">
|
|
148
|
+
<VStack align="left" spacing="s">
|
|
149
|
+
<Heading level={3}>Default</Heading>
|
|
150
|
+
<Toggle size={ToggleSizes.Large} defaultChecked={true} labelText="On" />
|
|
151
|
+
<Toggle size={ToggleSizes.Large} labelText="Off" />
|
|
152
|
+
</VStack>
|
|
153
|
+
<VStack align="left" spacing="s">
|
|
154
|
+
<Heading level={3}>Disabled</Heading>
|
|
155
|
+
<Toggle
|
|
156
|
+
size={ToggleSizes.Large}
|
|
157
|
+
defaultChecked={true}
|
|
158
|
+
isDisabled={true}
|
|
159
|
+
labelText="On"
|
|
160
|
+
/>
|
|
161
|
+
<Toggle size={ToggleSizes.Large} labelText="Off" isDisabled={true} />
|
|
162
|
+
</VStack>
|
|
163
|
+
<VStack align="left" spacing="s">
|
|
164
|
+
<Heading level={3}>Error</Heading>
|
|
165
|
+
<Toggle
|
|
166
|
+
size={ToggleSizes.Large}
|
|
167
|
+
defaultChecked={true}
|
|
168
|
+
labelText="On"
|
|
169
|
+
isInvalid={true}
|
|
170
|
+
/>
|
|
171
|
+
<Toggle size={ToggleSizes.Large} labelText="Off" isInvalid={true} />
|
|
172
|
+
</VStack>
|
|
173
|
+
</SimpleGrid>
|
|
174
|
+
</DSProvider>
|
|
175
|
+
</Canvas>
|
|
176
|
+
|
|
177
|
+
## Layouts
|
|
178
|
+
|
|
179
|
+
<Canvas>
|
|
180
|
+
<DSProvider>
|
|
181
|
+
<SimpleGrid columns="2">
|
|
182
|
+
<VStack align="left" spacing="s">
|
|
183
|
+
<Heading level={3}>Grouped</Heading>
|
|
184
|
+
<Toggle defaultChecked={true} labelText="On" />
|
|
185
|
+
<Toggle defaultChecked={true} labelText="On" />
|
|
186
|
+
<Toggle defaultChecked={true} labelText="On" />
|
|
187
|
+
<Toggle defaultChecked={true} labelText="On" />
|
|
188
|
+
<Toggle defaultChecked={true} labelText="On" />
|
|
189
|
+
</VStack>
|
|
190
|
+
<VStack align="left" spacing="s">
|
|
191
|
+
<Heading level={3}>With Helper Text</Heading>
|
|
192
|
+
<Toggle
|
|
193
|
+
defaultChecked={true}
|
|
194
|
+
labelText="On"
|
|
195
|
+
helperText="Component Helper Text"
|
|
196
|
+
/>
|
|
197
|
+
</VStack>
|
|
198
|
+
</SimpleGrid>
|
|
199
|
+
</DSProvider>
|
|
200
|
+
</Canvas>
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { render, screen } from "@testing-library/react";
|
|
3
|
+
import userEvent from "@testing-library/user-event";
|
|
4
|
+
import { axe } from "jest-axe";
|
|
5
|
+
import renderer from "react-test-renderer";
|
|
6
|
+
import * as generateUUID from "../../helpers/generateUUID";
|
|
7
|
+
import Toggle from "./Toggle";
|
|
8
|
+
|
|
9
|
+
describe("Toggle Accessibility", () => {
|
|
10
|
+
it("Passes axe accessibility test", async () => {
|
|
11
|
+
const { container } = render(
|
|
12
|
+
<Toggle id="inputID" onChange={jest.fn()} labelText="Test Label" />
|
|
13
|
+
);
|
|
14
|
+
expect(await axe(container)).toHaveNoViolations();
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe("Toggle", () => {
|
|
19
|
+
let changeHandler;
|
|
20
|
+
let generateUUIDSpy;
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
changeHandler = jest.fn();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("Renders with a Toggle input and label", () => {
|
|
27
|
+
render(<Toggle id="inputID" labelText="Test Label" />);
|
|
28
|
+
expect(screen.getByLabelText("Test Label")).toBeInTheDocument();
|
|
29
|
+
expect(screen.getByRole("checkbox")).toBeInTheDocument();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("Renders visible helper or error text", () => {
|
|
33
|
+
const { rerender } = render(
|
|
34
|
+
<Toggle
|
|
35
|
+
id="inputID"
|
|
36
|
+
labelText="Test Label"
|
|
37
|
+
helperText="This is the helper text."
|
|
38
|
+
invalidText="This is the error text :("
|
|
39
|
+
/>
|
|
40
|
+
);
|
|
41
|
+
expect(screen.getByText("This is the helper text.")).toBeVisible();
|
|
42
|
+
expect(
|
|
43
|
+
screen.queryByText("This is the error text :(")
|
|
44
|
+
).not.toBeInTheDocument();
|
|
45
|
+
|
|
46
|
+
rerender(
|
|
47
|
+
<Toggle
|
|
48
|
+
id="inputID"
|
|
49
|
+
labelText="Test Label"
|
|
50
|
+
isInvalid
|
|
51
|
+
helperText="This is the helper text."
|
|
52
|
+
invalidText="This is the error text :("
|
|
53
|
+
/>
|
|
54
|
+
);
|
|
55
|
+
expect(screen.getByText("This is the error text :(")).toBeVisible();
|
|
56
|
+
expect(
|
|
57
|
+
screen.queryByText("This is the helper text.")
|
|
58
|
+
).not.toBeInTheDocument();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("Sets the Toggle's ID", () => {
|
|
62
|
+
render(<Toggle id="inputID" labelText="Test Label" />);
|
|
63
|
+
expect(screen.getByRole("checkbox")).toHaveAttribute("id", "inputID");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("Calls the UUID generation function if no id prop value is passed", () => {
|
|
67
|
+
generateUUIDSpy = jest.spyOn(generateUUID, "default");
|
|
68
|
+
expect(generateUUIDSpy).toHaveBeenCalledTimes(0);
|
|
69
|
+
render(<Toggle labelText="Test Label" />);
|
|
70
|
+
expect(generateUUIDSpy).toHaveBeenCalledTimes(1);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("Sets the 'checked' attribute", () => {
|
|
74
|
+
render(<Toggle id="inputID" labelText="Test Label" isChecked />);
|
|
75
|
+
expect(screen.getByRole("checkbox")).toHaveAttribute("checked");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("Sets the 'disabled' attribute", () => {
|
|
79
|
+
render(<Toggle id="inputID" labelText="Test Label" isDisabled />);
|
|
80
|
+
expect(screen.getByRole("checkbox")).toHaveAttribute("disabled");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("Sets the error state", () => {
|
|
84
|
+
render(
|
|
85
|
+
<Toggle
|
|
86
|
+
id="inputID-attributes"
|
|
87
|
+
labelText="onChange test"
|
|
88
|
+
invalidText="This is the error text!"
|
|
89
|
+
isInvalid
|
|
90
|
+
/>
|
|
91
|
+
);
|
|
92
|
+
expect(screen.getByRole("checkbox")).toHaveAttribute("aria-invalid");
|
|
93
|
+
expect(screen.getByText("This is the error text!")).toBeInTheDocument();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("does not render the error text when 'isInvalid' is true but 'invalidText' is not passed", () => {
|
|
97
|
+
render(
|
|
98
|
+
<Toggle id="inputID-attributes" labelText="onChange test" isInvalid />
|
|
99
|
+
);
|
|
100
|
+
expect(screen.getByRole("checkbox")).toHaveAttribute("aria-invalid");
|
|
101
|
+
expect(
|
|
102
|
+
screen.queryByText("This is the error text!")
|
|
103
|
+
).not.toBeInTheDocument();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("Changing the value calls the onChange handler", () => {
|
|
107
|
+
const utils = render(
|
|
108
|
+
<Toggle
|
|
109
|
+
id="onChangeTest"
|
|
110
|
+
onChange={changeHandler}
|
|
111
|
+
labelText="onChangeTest Lab"
|
|
112
|
+
isChecked
|
|
113
|
+
/>
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
expect(changeHandler).toHaveBeenCalledTimes(0);
|
|
117
|
+
userEvent.click(utils.getByText("onChangeTest Lab"));
|
|
118
|
+
expect(changeHandler).toHaveBeenCalledTimes(1);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("Renders the UI snapshot correctly", () => {
|
|
122
|
+
const primary = renderer
|
|
123
|
+
.create(<Toggle id="inputID" labelText="Test Label" />)
|
|
124
|
+
.toJSON();
|
|
125
|
+
const isChecked = renderer
|
|
126
|
+
.create(<Toggle id="Toggle-checked" labelText="Test Label" isChecked />)
|
|
127
|
+
.toJSON();
|
|
128
|
+
const isInvalid = renderer
|
|
129
|
+
.create(<Toggle id="Toggle-invalid" labelText="Test Label" isInvalid />)
|
|
130
|
+
.toJSON();
|
|
131
|
+
const isDisabled = renderer
|
|
132
|
+
.create(<Toggle id="Toggle-disabled" labelText="Test Label" isDisabled />)
|
|
133
|
+
.toJSON();
|
|
134
|
+
|
|
135
|
+
expect(primary).toMatchSnapshot();
|
|
136
|
+
expect(isChecked).toMatchSnapshot();
|
|
137
|
+
expect(isInvalid).toMatchSnapshot();
|
|
138
|
+
expect(isDisabled).toMatchSnapshot();
|
|
139
|
+
});
|
|
140
|
+
});
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Box,
|
|
3
|
+
Switch,
|
|
4
|
+
useMultiStyleConfig,
|
|
5
|
+
useStyleConfig,
|
|
6
|
+
} from "@chakra-ui/react";
|
|
7
|
+
import * as React from "react";
|
|
8
|
+
|
|
9
|
+
import HelperErrorText, {
|
|
10
|
+
HelperErrorTextType,
|
|
11
|
+
} from "../HelperErrorText/HelperErrorText";
|
|
12
|
+
import { ToggleSizes } from "./ToggleSizes";
|
|
13
|
+
import generateUUID from "../../helpers/generateUUID";
|
|
14
|
+
|
|
15
|
+
export interface ToggleProps {
|
|
16
|
+
/** Optionally pass in additional Chakra-based styles. */
|
|
17
|
+
additionalStyles?: { [key: string]: any };
|
|
18
|
+
/** Used for uncontrolled scenarios. Sets the state of the Toggle when the page first loads.
|
|
19
|
+
* If true, the toggle will be initially set to the "on" position. */
|
|
20
|
+
defaultChecked?: boolean;
|
|
21
|
+
/** Optional string to populate the HelperErrorText for standard state */
|
|
22
|
+
helperText?: HelperErrorTextType;
|
|
23
|
+
/** ID that other components can cross reference for accessibility purposes */
|
|
24
|
+
id?: string;
|
|
25
|
+
/** Optional string to populate the HelperErrorText for the error state
|
|
26
|
+
* when `isInvalid` is true. */
|
|
27
|
+
invalidText?: HelperErrorTextType;
|
|
28
|
+
/** When using the Toggle as a "controlled" form element, you can specify
|
|
29
|
+
* the Toggle's checked state using this prop.
|
|
30
|
+
* Learn more about controlled and uncontrolled form fields:
|
|
31
|
+
* https://goshakkk.name/controlled-vs-uncontrolled-inputs-react/ */
|
|
32
|
+
isChecked?: boolean;
|
|
33
|
+
/** Adds the 'disabled' and `aria-disabled` attributes to the input when true.
|
|
34
|
+
* This also makes the text italic and color scheme gray. */
|
|
35
|
+
isDisabled?: boolean;
|
|
36
|
+
/** Adds the 'aria-invalid' attribute to the input when true. This also makes
|
|
37
|
+
* the color theme "NYPL error" red for the button and text. */
|
|
38
|
+
isInvalid?: boolean;
|
|
39
|
+
/** Adds the 'required' attribute to the input when true. */
|
|
40
|
+
isRequired?: boolean;
|
|
41
|
+
/** The checkbox's label. This will serve as the text content for a `<label>`
|
|
42
|
+
* element if `showlabel` is true, or an "aria-label" if `showLabel` is false. */
|
|
43
|
+
labelText: string;
|
|
44
|
+
/** The name prop indicates into which group of checkboxes this checkbox
|
|
45
|
+
* belongs. If none is specified, 'default' will be used */
|
|
46
|
+
name?: string;
|
|
47
|
+
/** The action to perform on the `<input>`'s onChange function */
|
|
48
|
+
onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void;
|
|
49
|
+
/** Offers the ability to hide the helper/invalid text. */
|
|
50
|
+
size?: ToggleSizes;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const onChangeDefault = () => {
|
|
54
|
+
return;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Component that renders Chakra's `Switch` component along with NYPL defaults.
|
|
59
|
+
*/
|
|
60
|
+
const Toggle = React.forwardRef<HTMLInputElement, ToggleProps>(
|
|
61
|
+
(props, ref?) => {
|
|
62
|
+
const {
|
|
63
|
+
additionalStyles = {},
|
|
64
|
+
defaultChecked = false,
|
|
65
|
+
helperText,
|
|
66
|
+
id = generateUUID(),
|
|
67
|
+
invalidText,
|
|
68
|
+
isChecked,
|
|
69
|
+
isDisabled = false,
|
|
70
|
+
isInvalid = false,
|
|
71
|
+
isRequired = false,
|
|
72
|
+
labelText,
|
|
73
|
+
name,
|
|
74
|
+
onChange = onChangeDefault,
|
|
75
|
+
size = ToggleSizes.Large,
|
|
76
|
+
} = props;
|
|
77
|
+
const footnote: HelperErrorTextType = isInvalid ? invalidText : helperText;
|
|
78
|
+
const ariaAttributes = {};
|
|
79
|
+
const styles = useMultiStyleConfig("Toggle", {});
|
|
80
|
+
const switchStyles = useStyleConfig("Switch");
|
|
81
|
+
ariaAttributes["aria-label"] =
|
|
82
|
+
labelText && footnote ? `${labelText} - ${footnote}` : labelText;
|
|
83
|
+
|
|
84
|
+
return (
|
|
85
|
+
<>
|
|
86
|
+
<Box __css={{ ...styles, ...additionalStyles }}>
|
|
87
|
+
<Switch
|
|
88
|
+
id={id}
|
|
89
|
+
name={name || "default"}
|
|
90
|
+
isDisabled={isDisabled}
|
|
91
|
+
isInvalid={isInvalid}
|
|
92
|
+
isRequired={isRequired}
|
|
93
|
+
ref={ref}
|
|
94
|
+
size={size === ToggleSizes.Large ? "lg" : "sm"}
|
|
95
|
+
{...(isChecked !== undefined
|
|
96
|
+
? {
|
|
97
|
+
isChecked,
|
|
98
|
+
onChange,
|
|
99
|
+
}
|
|
100
|
+
: {
|
|
101
|
+
defaultChecked,
|
|
102
|
+
})}
|
|
103
|
+
{...ariaAttributes}
|
|
104
|
+
__css={switchStyles}
|
|
105
|
+
>
|
|
106
|
+
{labelText}
|
|
107
|
+
</Switch>
|
|
108
|
+
</Box>
|
|
109
|
+
{footnote && (
|
|
110
|
+
<Box __css={styles.helper}>
|
|
111
|
+
<HelperErrorText
|
|
112
|
+
id={`${id}-helperText`}
|
|
113
|
+
isInvalid={isInvalid}
|
|
114
|
+
text={footnote}
|
|
115
|
+
/>
|
|
116
|
+
</Box>
|
|
117
|
+
)}
|
|
118
|
+
</>
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
export default Toggle;
|