@solexllc/usx-react 0.1.5 → 0.1.7

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.
Files changed (33) hide show
  1. package/dist/index.cjs +116 -1292
  2. package/dist/index.js +118 -1294
  3. package/package.json +4 -4
  4. package/src/components/accordion/Accordion.tsx +105 -0
  5. package/src/components/accordion/accordion.django.html +1 -1
  6. package/src/components/accordion/config.json +6 -1
  7. package/src/components/banner/Banner.tsx +35 -27
  8. package/src/components/banner/banner.django.html +1 -1
  9. package/src/components/banner/config.json +4 -0
  10. package/src/components/date-picker/{DatePicker.jsx → DatePicker.tsx} +12 -17
  11. package/src/components/date-range-picker/{DateRangePicker.jsx → DateRangePicker.tsx} +21 -21
  12. package/src/components/fieldset/Fieldset.tsx +1 -1
  13. package/src/components/file-input/{FileInput.jsx → FileInput.tsx} +31 -34
  14. package/src/components/file-list/{FileList.jsx → FileList.tsx} +24 -24
  15. package/src/components/header/{HeaderBranding.jsx → HeaderBranding.tsx} +0 -1
  16. package/src/components/hero/Hero.tsx +49 -7
  17. package/src/components/hero/config.json +45 -0
  18. package/src/components/hero/hero.django.html +16 -5
  19. package/src/components/hero/hero.html +19 -0
  20. package/src/components/identifier/Identifier.tsx +26 -40
  21. package/src/components/identifier/config.json +6 -6
  22. package/src/components/identifier/identifier.django.html +9 -13
  23. package/src/components/identifier/identifier.html +10 -10
  24. package/src/components/in-page-nav/{InPageNav.jsx → InPageNav.tsx} +14 -18
  25. package/src/components/language-selector/{LanguageSelector.jsx → LanguageSelector.tsx} +40 -69
  26. package/src/components/legend/Legend.tsx +1 -1
  27. package/src/components/memorable-date/{MemorableDate.jsx → MemorableDate.tsx} +27 -27
  28. package/src/components/page/{Page.jsx → Page.tsx} +7 -11
  29. package/src/components/range-slider/{RangeSlider.jsx → RangeSlider.tsx} +13 -21
  30. package/src/components/select/Select.tsx +2 -2
  31. package/src/components/time-picker/{TimePicker.jsx → TimePicker.tsx} +14 -19
  32. package/src/index.js +11 -11
  33. package/src/components/accordion/Accordion.jsx +0 -170
@@ -1,7 +1,36 @@
1
1
  import React from 'react';
2
- import PropTypes from 'prop-types';
2
+
3
3
  import classnames from 'classnames';
4
4
 
5
+ export interface LanguageOption {
6
+ code?: string;
7
+ label?: React.ReactNode;
8
+ description?: string;
9
+ href?: string;
10
+ current?: boolean;
11
+ plain?: boolean;
12
+ }
13
+
14
+ export interface LanguageSelectorProps {
15
+ languages?: LanguageOption[];
16
+ variant?: 'two' | 'menu' | null;
17
+ unstyled?: boolean;
18
+ small?: boolean;
19
+ label?: string;
20
+ id?: string;
21
+ navAriaLabel?: string;
22
+ className?: string;
23
+ onSelect?: ((language: LanguageOption, event: React.MouseEvent<HTMLAnchorElement | HTMLButtonElement>) => void) | null;
24
+ }
25
+
26
+ interface TwoLanguageSelectorProps extends Pick<LanguageSelectorProps, 'onSelect' | 'className' | 'navAriaLabel'> {
27
+ target: LanguageOption;
28
+ }
29
+
30
+ interface MenuLanguageSelectorProps extends Omit<LanguageSelectorProps, 'variant' | 'languages'> {
31
+ languages: LanguageOption[];
32
+ }
33
+
5
34
  /**
6
35
  * Renders a single language link/submenu-item's accessible label.
7
36
  * Real USWDS wraps the native-language name in <strong> and appends an
@@ -9,35 +38,29 @@ import classnames from 'classnames';
9
38
  * whole thing carries `lang`/`xml:lang` so assistive tech announces it in
10
39
  * the correct language (WCAG H58).
11
40
  */
12
- function LanguageLabel({ code, label, description }) {
41
+ function LanguageLabel({ code, label, description }: Pick<LanguageOption, 'code' | 'label' | 'description'>) {
13
42
  if (!code) {
14
43
  return <>{label}</>;
15
44
  }
16
45
 
17
46
  return (
18
- <span lang={code} xmlLang={code}>
47
+ <span {...{ lang: code, xmlLang: code }}>
19
48
  <strong>{label}</strong>
20
49
  {description ? ` (${description})` : null}
21
50
  </span>
22
51
  );
23
52
  }
24
53
 
25
- LanguageLabel.propTypes = {
26
- code: PropTypes.string,
27
- label: PropTypes.node.isRequired,
28
- description: PropTypes.string,
29
- };
30
-
31
- function TwoLanguageSelector({ target, onSelect, className, navAriaLabel }) {
32
- const handleClick = (e) => {
33
- if (onSelect) onSelect(target, e);
34
- if (!target.href) e.preventDefault();
54
+ function TwoLanguageSelector({ target, onSelect, className, navAriaLabel }: TwoLanguageSelectorProps) {
55
+ const handleClick = (event: React.MouseEvent<HTMLAnchorElement | HTMLButtonElement>) => {
56
+ if (onSelect) onSelect(target, event);
57
+ if (!target.href) event.preventDefault();
35
58
  };
36
59
 
37
60
  const Tag = target.href ? 'a' : 'button';
38
61
  const tagProps = target.href
39
62
  ? { href: target.href }
40
- : { type: 'button' };
63
+ : { type: 'button' as const };
41
64
 
42
65
  return (
43
66
  <nav aria-label={navAriaLabel} className={className}>
@@ -50,17 +73,6 @@ function TwoLanguageSelector({ target, onSelect, className, navAriaLabel }) {
50
73
  );
51
74
  }
52
75
 
53
- TwoLanguageSelector.propTypes = {
54
- target: PropTypes.shape({
55
- code: PropTypes.string,
56
- label: PropTypes.node,
57
- href: PropTypes.string,
58
- }).isRequired,
59
- onSelect: PropTypes.func,
60
- className: PropTypes.string,
61
- navAriaLabel: PropTypes.string,
62
- };
63
-
64
76
  function MenuLanguageSelector({
65
77
  languages,
66
78
  unstyled,
@@ -70,7 +82,7 @@ function MenuLanguageSelector({
70
82
  onSelect,
71
83
  className,
72
84
  navAriaLabel,
73
- }) {
85
+ }: MenuLanguageSelectorProps) {
74
86
  // Open/close (aria-expanded, hidden, outside-click, Escape, focusout) is
75
87
  // driven by USWDS's own JS, initialized via a decorator in the story/host
76
88
  // app — this component only renders the static USWDS markup.
@@ -101,7 +113,7 @@ function MenuLanguageSelector({
101
113
  </button>
102
114
  <ul id={id} className="usa-language__submenu" hidden>
103
115
  {languages.map((lang, idx) => (
104
- <li className="usa-language__submenu-item" key={lang.code || lang.label || idx}>
116
+ <li className="usa-language__submenu-item" key={lang.code || (typeof lang.label === 'string' || typeof lang.label === 'number' ? lang.label : undefined) || idx}>
105
117
  <a
106
118
  href={lang.href || 'javascript:void(0);'}
107
119
  aria-current={lang.current ? 'true' : undefined}
@@ -126,26 +138,6 @@ function MenuLanguageSelector({
126
138
  );
127
139
  }
128
140
 
129
- MenuLanguageSelector.propTypes = {
130
- languages: PropTypes.arrayOf(
131
- PropTypes.shape({
132
- code: PropTypes.string,
133
- label: PropTypes.node,
134
- description: PropTypes.string,
135
- href: PropTypes.string,
136
- current: PropTypes.bool,
137
- plain: PropTypes.bool,
138
- })
139
- ).isRequired,
140
- unstyled: PropTypes.bool,
141
- small: PropTypes.bool,
142
- label: PropTypes.string,
143
- id: PropTypes.string,
144
- onSelect: PropTypes.func,
145
- className: PropTypes.string,
146
- navAriaLabel: PropTypes.string,
147
- };
148
-
149
141
  /**
150
142
  * LanguageSelector — USWDS `usa-language-selector` with the USX treatment.
151
143
  *
@@ -166,7 +158,7 @@ export default function LanguageSelector({
166
158
  className = '',
167
159
  onSelect = null,
168
160
  ...props
169
- }) {
161
+ }: LanguageSelectorProps) {
170
162
  const resolvedVariant = variant || (languages.length > 2 ? 'menu' : 'two');
171
163
  const classes = classnames('usx-language-selector', className);
172
164
 
@@ -201,24 +193,3 @@ export default function LanguageSelector({
201
193
  />
202
194
  );
203
195
  }
204
-
205
- LanguageSelector.propTypes = {
206
- languages: PropTypes.arrayOf(
207
- PropTypes.shape({
208
- code: PropTypes.string,
209
- label: PropTypes.node,
210
- description: PropTypes.string,
211
- href: PropTypes.string,
212
- current: PropTypes.bool,
213
- plain: PropTypes.bool,
214
- })
215
- ),
216
- variant: PropTypes.oneOf(['two', 'menu']),
217
- unstyled: PropTypes.bool,
218
- small: PropTypes.bool,
219
- label: PropTypes.string,
220
- id: PropTypes.string,
221
- navAriaLabel: PropTypes.string,
222
- className: PropTypes.string,
223
- onSelect: PropTypes.func,
224
- };
@@ -3,7 +3,7 @@ import ClassNames from 'classnames';
3
3
  import Required from '../required/Required';
4
4
 
5
5
  export interface LegendProps extends HTMLAttributes<HTMLLegendElement> {
6
- text?: string | undefined;
6
+ text?: ReactNode;
7
7
  required?: boolean;
8
8
  large?: boolean;
9
9
  children?: ReactNode;
@@ -1,11 +1,34 @@
1
1
  import React from 'react';
2
- import PropTypes from 'prop-types';
2
+
3
3
  import Fieldset from '../fieldset/Fieldset';
4
4
  import FormGroup from '../form-group/FormGroup';
5
5
  import Hint from '../hint/Hint';
6
6
  import Select from '../select/Select';
7
7
  import Input from '../input/Input';
8
8
 
9
+ export interface MemorableDateProps {
10
+ id?: string;
11
+ legend?: React.ReactNode;
12
+ groupHint?: React.ReactNode;
13
+ monthLabel?: React.ReactNode;
14
+ dayLabel?: React.ReactNode;
15
+ yearLabel?: React.ReactNode;
16
+ monthHint?: React.ReactNode;
17
+ dayHint?: React.ReactNode;
18
+ yearHint?: React.ReactNode;
19
+ monthName?: string;
20
+ dayName?: string;
21
+ yearName?: string;
22
+ defaultMonth?: string;
23
+ defaultDay?: string;
24
+ defaultYear?: string;
25
+ disabled?: boolean;
26
+ ariaDisabled?: boolean;
27
+ required?: boolean;
28
+ error?: string | null;
29
+ className?: string;
30
+ }
31
+
9
32
  const MONTH_OPTIONS = [
10
33
  { value: '1', label: 'January' },
11
34
  { value: '2', label: 'February' },
@@ -42,7 +65,7 @@ export default function MemorableDate({
42
65
  required = false,
43
66
  error = null,
44
67
  className = '',
45
- }) {
68
+ }: MemorableDateProps) {
46
69
  const monthId = `${id}-month`;
47
70
  const dayId = `${id}-day`;
48
71
  const yearId = `${id}-year`;
@@ -53,10 +76,10 @@ export default function MemorableDate({
53
76
  const errorId = error ? `${id}-error` : undefined;
54
77
  const hasError = !!error;
55
78
 
56
- const describedBy = (hintId) => [hintId, errorId].filter(Boolean).join(' ') || undefined;
79
+ const describedBy = (hintId: string) => [hintId, errorId].filter(Boolean).join(' ') || undefined;
57
80
 
58
81
  // Native disabled and aria-disabled are mutually exclusive, matching real USWDS's variants.
59
- const stateProps = disabled
82
+ const stateProps: Pick<React.InputHTMLAttributes<HTMLInputElement>, 'disabled' | 'aria-disabled'> = disabled
60
83
  ? { disabled: true }
61
84
  : ariaDisabled
62
85
  ? { 'aria-disabled': 'true' }
@@ -142,26 +165,3 @@ export default function MemorableDate({
142
165
  </FormGroup>
143
166
  );
144
167
  }
145
-
146
- MemorableDate.propTypes = {
147
- id: PropTypes.string,
148
- legend: PropTypes.node,
149
- groupHint: PropTypes.node,
150
- monthLabel: PropTypes.node,
151
- dayLabel: PropTypes.node,
152
- yearLabel: PropTypes.node,
153
- monthHint: PropTypes.node,
154
- dayHint: PropTypes.node,
155
- yearHint: PropTypes.node,
156
- monthName: PropTypes.string,
157
- dayName: PropTypes.string,
158
- yearName: PropTypes.string,
159
- defaultMonth: PropTypes.string,
160
- defaultDay: PropTypes.string,
161
- defaultYear: PropTypes.string,
162
- disabled: PropTypes.bool,
163
- ariaDisabled: PropTypes.bool,
164
- required: PropTypes.bool,
165
- error: PropTypes.string,
166
- className: PropTypes.string,
167
- };
@@ -1,5 +1,10 @@
1
1
  import React from 'react';
2
- import PropTypes from 'prop-types';
2
+
3
+ export interface PageProps extends Omit<React.HTMLAttributes<HTMLElement>, 'title' | 'content'> {
4
+ title?: React.ReactNode;
5
+ eyebrow?: React.ReactNode;
6
+ content?: React.ReactNode;
7
+ }
3
8
 
4
9
  export default function Page({
5
10
  id = undefined,
@@ -9,7 +14,7 @@ export default function Page({
9
14
  content = null,
10
15
  className = '',
11
16
  ...props
12
- }) {
17
+ }: PageProps) {
13
18
  const classes = ['usx-page', className].filter(Boolean).join(' ');
14
19
  const pageContent = children || content;
15
20
 
@@ -23,12 +28,3 @@ export default function Page({
23
28
  </main>
24
29
  );
25
30
  }
26
-
27
- Page.propTypes = {
28
- id: PropTypes.string,
29
- title: PropTypes.string,
30
- eyebrow: PropTypes.node,
31
- children: PropTypes.node,
32
- content: PropTypes.node,
33
- className: PropTypes.string,
34
- };
@@ -1,10 +1,21 @@
1
1
  import React from 'react';
2
- import PropTypes from 'prop-types';
2
+
3
3
  import Label from '../label/Label';
4
4
  import FormGroup from '../form-group/FormGroup';
5
5
  import Hint from '../hint/Hint';
6
6
  import ErrorMessage from '../error-message/ErrorMessage';
7
7
 
8
+ export interface RangeSliderProps extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'value' | 'defaultValue'> {
9
+ label?: React.ReactNode;
10
+ hint?: React.ReactNode;
11
+ error?: React.ReactNode;
12
+ value?: number | string;
13
+ defaultValue?: number | string;
14
+ textUnit?: string | null;
15
+ textPreposition?: string | null;
16
+ formGroup?: boolean;
17
+ }
18
+
8
19
  // Enhanced by @uswds/uswds/js/usa-range at runtime (call ONLY range.init() in
9
20
  // the consuming app, never .on()/.off() — see Combobox.tsx for why) — that
10
21
  // script builds the value display span and keeps the aria-valuetext
@@ -27,7 +38,7 @@ export default function RangeSlider({
27
38
  formGroup = true,
28
39
  className = '',
29
40
  ...props
30
- }) {
41
+ }: RangeSliderProps) {
31
42
  const hintId = hint ? `${id}-hint` : undefined;
32
43
  const errorId = error ? `${id}-error` : undefined;
33
44
  const describedBy = [hintId, errorId].filter(Boolean).join(' ') || undefined;
@@ -63,22 +74,3 @@ export default function RangeSlider({
63
74
 
64
75
  return formGroup ? <FormGroup error={!!error}>{content}</FormGroup> : content;
65
76
  }
66
-
67
- RangeSlider.propTypes = {
68
- id: PropTypes.string,
69
- name: PropTypes.string,
70
- label: PropTypes.node,
71
- hint: PropTypes.node,
72
- error: PropTypes.node,
73
- disabled: PropTypes.bool,
74
- required: PropTypes.bool,
75
- min: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
76
- max: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
77
- step: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
78
- value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
79
- defaultValue: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
80
- textUnit: PropTypes.string,
81
- textPreposition: PropTypes.string,
82
- formGroup: PropTypes.bool,
83
- className: PropTypes.string,
84
- };
@@ -1,5 +1,5 @@
1
1
  // import React from 'react';
2
- import { SelectHTMLAttributes } from 'react';
2
+ import { ReactNode, SelectHTMLAttributes } from 'react';
3
3
  import ClassNames from 'classnames';
4
4
  import Label from '../label/Label';
5
5
  import Hint from '../hint/Hint';
@@ -12,7 +12,7 @@ export interface SelectOption {
12
12
  }
13
13
 
14
14
  export interface SelectProps extends Omit<SelectHTMLAttributes<HTMLSelectElement>, 'size'> {
15
- label: string;
15
+ label: ReactNode;
16
16
  id?: string;
17
17
  name?: string;
18
18
  options?: SelectOption[];
@@ -1,11 +1,23 @@
1
1
  import React from 'react';
2
- import PropTypes from 'prop-types';
2
+
3
3
  import classNames from 'classnames';
4
4
  import Label from '../label/Label';
5
5
  import Hint from '../hint/Hint';
6
6
  import ErrorMessage from '../error-message/ErrorMessage';
7
7
  import FormGroup from '../form-group/FormGroup';
8
8
 
9
+ export interface TimePickerProps extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'defaultValue' | 'step'> {
10
+ label?: React.ReactNode;
11
+ hint?: React.ReactNode;
12
+ error?: React.ReactNode;
13
+ ariaDisabled?: boolean;
14
+ defaultValue?: string | null;
15
+ minTime?: string | null;
16
+ maxTime?: string | null;
17
+ step?: string | number | null;
18
+ formGroup?: boolean;
19
+ }
20
+
9
21
  export default function TimePicker({
10
22
  id = 'appointment-time',
11
23
  name,
@@ -22,7 +34,7 @@ export default function TimePicker({
22
34
  formGroup = true,
23
35
  className = '',
24
36
  ...props
25
- }) {
37
+ }: TimePickerProps) {
26
38
  const labelId = `${id}-label`;
27
39
  const hintId = hint ? `${id}-hint` : undefined;
28
40
  const errorId = error ? `${id}-error` : undefined;
@@ -68,20 +80,3 @@ export default function TimePicker({
68
80
 
69
81
  return formGroup ? <FormGroup error={!!error}>{content}</FormGroup> : content;
70
82
  }
71
-
72
- TimePicker.propTypes = {
73
- id: PropTypes.string,
74
- name: PropTypes.string,
75
- label: PropTypes.node,
76
- hint: PropTypes.node,
77
- error: PropTypes.node,
78
- disabled: PropTypes.bool,
79
- ariaDisabled: PropTypes.bool,
80
- required: PropTypes.bool,
81
- defaultValue: PropTypes.string,
82
- minTime: PropTypes.string,
83
- maxTime: PropTypes.string,
84
- step: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
85
- formGroup: PropTypes.bool,
86
- className: PropTypes.string,
87
- };
package/src/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Auto-generated by scripts/generate-exports.js — do not edit by hand.
2
2
  // Run: pnpm generate:exports
3
- export { default as Accordion } from './components/accordion/Accordion.jsx';
3
+ export { default as Accordion } from './components/accordion/Accordion.tsx';
4
4
  export { default as Alert } from './components/alert/Alert.tsx';
5
5
  export { default as Attribution } from './components/attribution/Attribution.tsx';
6
6
  export { default as Avatar } from './components/avatar/Avatar.tsx';
@@ -23,13 +23,13 @@ export { default as Code } from './components/code/Code.tsx';
23
23
  export { default as Collection } from './components/collection/Collection.tsx';
24
24
  export { default as Combobox } from './components/combobox/Combobox.tsx';
25
25
  export { default as CopyToClipboard } from './components/copy-to-clipboard/CopyToClipboard.tsx';
26
- export { default as DatePicker } from './components/date-picker/DatePicker.jsx';
27
- export { default as DateRangePicker } from './components/date-range-picker/DateRangePicker.jsx';
26
+ export { default as DatePicker } from './components/date-picker/DatePicker.tsx';
27
+ export { default as DateRangePicker } from './components/date-range-picker/DateRangePicker.tsx';
28
28
  export { default as ErrorMessage } from './components/error-message/ErrorMessage.tsx';
29
29
  export { default as Eyebrow } from './components/eyebrow/Eyebrow.tsx';
30
30
  export { default as Fieldset } from './components/fieldset/Fieldset.tsx';
31
- export { default as FileInput } from './components/file-input/FileInput.jsx';
32
- export { default as FileList } from './components/file-list/FileList.jsx';
31
+ export { default as FileInput } from './components/file-input/FileInput.tsx';
32
+ export { default as FileList } from './components/file-list/FileList.tsx';
33
33
  export { default as Footer } from './components/footer/Footer.tsx';
34
34
  export { default as FormGroup } from './components/form-group/FormGroup.tsx';
35
35
  export { default as Header } from './components/header/Header.tsx';
@@ -39,24 +39,24 @@ export { default as Icon } from './components/icon/Icon.tsx';
39
39
  export { default as IconList } from './components/icon-list/IconList.tsx';
40
40
  export { default as Identifier } from './components/identifier/Identifier.tsx';
41
41
  export { default as Image } from './components/image/Image.tsx';
42
- export { default as InPageNav } from './components/in-page-nav/InPageNav.jsx';
42
+ export { default as InPageNav } from './components/in-page-nav/InPageNav.tsx';
43
43
  export { default as Indicator } from './components/indicator/Indicator.tsx';
44
44
  export { default as Input } from './components/input/Input.tsx';
45
45
  export { default as Label } from './components/label/Label.tsx';
46
- export { default as LanguageSelector } from './components/language-selector/LanguageSelector.jsx';
46
+ export { default as LanguageSelector } from './components/language-selector/LanguageSelector.tsx';
47
47
  export { default as Layout } from './components/layout/Layout.tsx';
48
48
  export { default as Legend } from './components/legend/Legend.tsx';
49
49
  export { default as Link } from './components/link/Link.tsx';
50
50
  export { default as List } from './components/list/List.tsx';
51
- export { default as MemorableDate } from './components/memorable-date/MemorableDate.jsx';
51
+ export { default as MemorableDate } from './components/memorable-date/MemorableDate.tsx';
52
52
  export { default as MiscBanner } from './components/misc-banner/MiscBanner.tsx';
53
53
  export { default as Modal } from './components/modal/Modal.tsx';
54
- export { default as Page } from './components/page/Page.jsx';
54
+ export { default as Page } from './components/page/Page.tsx';
55
55
  export { default as Pagination } from './components/pagination/Pagination.tsx';
56
56
  export { default as ProcessList } from './components/process-list/ProcessList.tsx';
57
57
  export { default as Prose } from './components/prose/Prose.tsx';
58
58
  export { default as RadioButtons } from './components/radio-buttons/RadioButtons.tsx';
59
- export { default as RangeSlider } from './components/range-slider/RangeSlider.jsx';
59
+ export { default as RangeSlider } from './components/range-slider/RangeSlider.tsx';
60
60
  export { default as Required } from './components/required/Required.tsx';
61
61
  export { default as Search } from './components/search/Search.tsx';
62
62
  export { default as Section } from './components/section/Section.tsx';
@@ -76,7 +76,7 @@ export { default as TagGroup } from './components/tag-group/TagGroup.tsx';
76
76
  export { default as TaskList } from './components/task-list/TaskList.tsx';
77
77
  export { default as TextArea } from './components/text-area/TextArea.tsx';
78
78
  export { default as ThemePicker } from './components/theme-picker/ThemePicker.tsx';
79
- export { default as TimePicker } from './components/time-picker/TimePicker.jsx';
79
+ export { default as TimePicker } from './components/time-picker/TimePicker.tsx';
80
80
  export { default as Toggle } from './components/toggle/Toggle.tsx';
81
81
  export { default as Tooltip } from './components/tooltip/Tooltip.tsx';
82
82
  export { default as Villain } from './components/villain/Villain.tsx';
@@ -1,170 +0,0 @@
1
- import React, { useEffect, useState } from 'react';
2
- import PropTypes from 'prop-types';
3
- import classnames from 'classnames';
4
-
5
- function AccordionItem({
6
- title,
7
- id,
8
- content,
9
- expanded,
10
- className,
11
- headingLevel,
12
- handleToggle,
13
- }) {
14
- const headingClasses = classnames('usa-accordion__heading', className)
15
- const contentClasses = classnames(
16
- 'usa-accordion__content',
17
- 'usa-prose',
18
- className
19
- )
20
-
21
- const Heading = headingLevel
22
-
23
- const handleClick = (e) => {
24
- // Prevent default navigation/behavior and stop other listeners
25
- // (including non-React handlers added by imported JS) from running.
26
- if (e && typeof e.preventDefault === 'function') e.preventDefault();
27
- if (e && typeof e.stopImmediatePropagation === 'function') e.stopImmediatePropagation();
28
- if (e && typeof e.stopPropagation === 'function') e.stopPropagation();
29
- handleToggle(e)
30
- }
31
-
32
- return (
33
- <>
34
- <Heading id={`${id}-heading`} className={headingClasses}>
35
- <button
36
- type="button"
37
- className="usa-accordion__button"
38
- aria-expanded={expanded}
39
- aria-controls={`${id}-content`}
40
- data-testid={`accordionButton_${id}`}
41
- onClickCapture={handleClick}>
42
- {title}
43
- </button>
44
- </Heading>
45
- <div
46
- id={`${id}-content`}
47
- aria-labelledby={`${id}-heading`}
48
- data-testid={`accordionItem_${id}`}
49
- className={contentClasses}
50
- hidden={!expanded}>
51
- {content}
52
- </div>
53
- </>
54
- )
55
- };
56
-
57
- function buildExpansions(
58
- items,
59
- multiselectable,
60
- savedExpansions = new Map()
61
- ) {
62
- const lastExpandedItem = multiselectable
63
- ? undefined
64
- : items.findLast((item) => item.expanded || savedExpansions.get(item.id))
65
- return items.reduce((map, item) => {
66
- map.set(
67
- item.id,
68
- multiselectable
69
- ? (savedExpansions.get(item.id) ?? item.expanded)
70
- : !!lastExpandedItem && item.id === lastExpandedItem.id
71
- )
72
- return map
73
- }, new Map())
74
- }
75
-
76
- AccordionItem.propTypes = {
77
- title: PropTypes.node.isRequired,
78
- id: PropTypes.string.isRequired,
79
- content: PropTypes.node.isRequired,
80
- expanded: PropTypes.bool,
81
- className: PropTypes.string,
82
- headingLevel: PropTypes.string,
83
- handleToggle: PropTypes.func,
84
- }
85
-
86
- Accordion.propTypes = {
87
- id: PropTypes.string,
88
- bordered: PropTypes.bool,
89
- items: PropTypes.arrayOf(PropTypes.shape(AccordionItem.propTypes)),
90
- multiselectable: PropTypes.bool,
91
- headingLevel: PropTypes.string,
92
- className: PropTypes.string,
93
- };
94
-
95
- export function Accordion({
96
- id,
97
- bordered = false,
98
- items = [],
99
- multiselectable = false,
100
- headingLevel = 'h3',
101
- className = '',
102
- ...props
103
- }) {
104
- const [savedExpansions, setSavedExpansions] = useState(() =>
105
- buildExpansions(items, multiselectable)
106
- )
107
-
108
- const [prevItems, setPrevItems] = useState(items)
109
- useEffect(() => {
110
- if (items !== prevItems) {
111
- setPrevItems(items)
112
- }
113
- }, [items, prevItems])
114
-
115
- useEffect(() => {
116
- if (items !== prevItems) {
117
- setSavedExpansions((prevExpansions) =>
118
- buildExpansions(items, multiselectable, prevExpansions)
119
- )
120
- }
121
- }, [items, multiselectable, prevItems])
122
-
123
- const accordionClasses = classnames(
124
- 'usa-accordion',
125
- 'usx-accordion',
126
- bordered && 'usa-accordion--bordered',
127
- className
128
- )
129
-
130
-
131
- const toggleItem = (itemId) => {
132
- setSavedExpansions((prevExpansions) => {
133
- const updatedExpansions = new Map(prevExpansions)
134
- if (updatedExpansions.get(itemId)) {
135
- updatedExpansions.set(itemId, false)
136
- } else {
137
- if (!multiselectable) {
138
- updatedExpansions.forEach((_val, key, map) => map.set(key, false))
139
- }
140
- updatedExpansions.set(itemId, true)
141
- }
142
- return updatedExpansions
143
- })
144
- }
145
-
146
- return (
147
- <div
148
- id={id}
149
- className={accordionClasses}
150
- data-allow-multiple={multiselectable}
151
- {...props}
152
- >
153
- {items.map((item) => (
154
- <AccordionItem
155
- key={item.id}
156
- {...item}
157
- expanded={savedExpansions.get(item.id) ?? false}
158
- handleToggle={(e) => {
159
- if (item.handleToggle) item.handleToggle(e)
160
- toggleItem(item.id)
161
- }}
162
- headingLevel={headingLevel}
163
- className={className}
164
- />
165
- ))}
166
- </div>
167
- );
168
- }
169
-
170
- export default Accordion;