@openedx/paragon 23.23.2 → 23.23.3

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 (123) hide show
  1. package/dist/Card/CardFooter.d.ts +19 -0
  2. package/dist/Card/CardFooter.js +5 -31
  3. package/dist/Card/CardFooter.js.map +1 -1
  4. package/dist/Card/CardSection.d.ts +19 -0
  5. package/dist/Card/CardSection.js +5 -31
  6. package/dist/Card/CardSection.js.map +1 -1
  7. package/dist/Card/CardStatus.d.ts +17 -0
  8. package/dist/Card/CardStatus.js +3 -25
  9. package/dist/Card/CardStatus.js.map +1 -1
  10. package/dist/Collapsible/CollapsibleAdvanced.d.ts +41 -0
  11. package/dist/Collapsible/CollapsibleAdvanced.js +8 -38
  12. package/dist/Collapsible/CollapsibleAdvanced.js.map +1 -1
  13. package/dist/Collapsible/CollapsibleBody.d.ts +11 -0
  14. package/dist/Collapsible/CollapsibleBody.js +6 -20
  15. package/dist/Collapsible/CollapsibleBody.js.map +1 -1
  16. package/dist/Collapsible/CollapsibleTrigger.d.ts +17 -0
  17. package/dist/Collapsible/CollapsibleTrigger.js +16 -33
  18. package/dist/Collapsible/CollapsibleTrigger.js.map +1 -1
  19. package/dist/Collapsible/CollapsibleVisible.d.ts +11 -0
  20. package/dist/Collapsible/CollapsibleVisible.js +6 -17
  21. package/dist/Collapsible/CollapsibleVisible.js.map +1 -1
  22. package/dist/Collapsible/index.d.ts +40 -0
  23. package/dist/Collapsible/index.js +23 -73
  24. package/dist/Collapsible/index.js.map +1 -1
  25. package/dist/DataTable/filters/DropdownFilter.d.ts +28 -0
  26. package/dist/DataTable/filters/DropdownFilter.js +0 -24
  27. package/dist/DataTable/filters/DropdownFilter.js.map +1 -1
  28. package/dist/Form/FormAutosuggestOption.d.ts +11 -0
  29. package/dist/Form/FormAutosuggestOption.js +1 -15
  30. package/dist/Form/FormAutosuggestOption.js.map +1 -1
  31. package/dist/Form/FormControlDecorator.d.ts +7 -0
  32. package/dist/Form/FormControlDecorator.js +1 -9
  33. package/dist/Form/FormControlDecorator.js.map +1 -1
  34. package/dist/Form/FormControlFloatingLabel.d.ts +6 -0
  35. package/dist/Form/FormControlFloatingLabel.js +0 -4
  36. package/dist/Form/FormControlFloatingLabel.js.map +1 -1
  37. package/dist/Form/index.d.ts +1 -1
  38. package/dist/Form/index.js +0 -1
  39. package/dist/Form/index.js.map +1 -1
  40. package/dist/Modal/ModalCloseButton.d.ts +15 -0
  41. package/dist/Modal/ModalCloseButton.js +11 -25
  42. package/dist/Modal/ModalCloseButton.js.map +1 -1
  43. package/dist/Modal/ModalDialog.d.ts +1 -1
  44. package/dist/Modal/ModalDialog.js +0 -1
  45. package/dist/Modal/ModalDialog.js.map +1 -1
  46. package/dist/ProductTour/CheckpointBody.d.ts +6 -0
  47. package/dist/ProductTour/CheckpointBody.js +3 -10
  48. package/dist/ProductTour/CheckpointBody.js.map +1 -1
  49. package/dist/ProductTour/CheckpointTitle.d.ts +6 -0
  50. package/dist/ProductTour/CheckpointTitle.js +3 -10
  51. package/dist/ProductTour/CheckpointTitle.js.map +1 -1
  52. package/dist/StatefulButton/index.d.ts +45 -0
  53. package/dist/StatefulButton/index.js +15 -61
  54. package/dist/StatefulButton/index.js.map +1 -1
  55. package/dist/Stepper/Stepper.d.ts +22 -0
  56. package/dist/Stepper/Stepper.js +1 -12
  57. package/dist/Stepper/Stepper.js.map +1 -1
  58. package/dist/Stepper/StepperActionRow.d.ts +19 -0
  59. package/dist/Stepper/StepperActionRow.js +10 -21
  60. package/dist/Stepper/StepperActionRow.js.map +1 -1
  61. package/dist/Stepper/StepperContext.d.ts +33 -0
  62. package/dist/Stepper/StepperContext.js +7 -10
  63. package/dist/Stepper/StepperContext.js.map +1 -1
  64. package/dist/Stepper/StepperHeader.d.ts +27 -0
  65. package/dist/Stepper/StepperHeader.js +17 -43
  66. package/dist/Stepper/StepperHeader.js.map +1 -1
  67. package/dist/Stepper/StepperHeaderStep.d.ts +16 -0
  68. package/dist/Stepper/StepperHeaderStep.js +2 -23
  69. package/dist/Stepper/StepperHeaderStep.js.map +1 -1
  70. package/dist/Stepper/StepperStep.d.ts +29 -0
  71. package/dist/Stepper/StepperStep.js +1 -36
  72. package/dist/Stepper/StepperStep.js.map +1 -1
  73. package/dist/Stepper/index.js +1 -0
  74. package/dist/Stepper/index.js.map +1 -0
  75. package/dist/TransitionReplace/index.d.ts +54 -0
  76. package/dist/TransitionReplace/index.js +9 -60
  77. package/dist/TransitionReplace/index.js.map +1 -1
  78. package/dist/index.d.ts +4 -4
  79. package/dist/index.js +4 -8
  80. package/dist/index.js.map +1 -1
  81. package/package.json +1 -1
  82. package/src/Card/{CardFooter.jsx → CardFooter.tsx} +25 -34
  83. package/src/Card/{CardSection.jsx → CardSection.tsx} +25 -34
  84. package/src/Card/{CardStatus.jsx → CardStatus.tsx} +21 -33
  85. package/src/Collapsible/{Collapsible.test.jsx → Collapsible.test.tsx} +88 -21
  86. package/src/Collapsible/{CollapsibleAdvanced.jsx → CollapsibleAdvanced.tsx} +42 -43
  87. package/src/Collapsible/{CollapsibleBody.jsx → CollapsibleBody.tsx} +13 -22
  88. package/src/Collapsible/{CollapsibleTrigger.jsx → CollapsibleTrigger.tsx} +29 -36
  89. package/src/Collapsible/{CollapsibleVisible.jsx → CollapsibleVisible.tsx} +14 -20
  90. package/src/Collapsible/__snapshots__/{Collapsible.test.jsx.snap → Collapsible.test.tsx.snap} +0 -1
  91. package/src/Collapsible/index.tsx +91 -0
  92. package/src/DataTable/filters/{DropdownFilter.jsx → DropdownFilter.tsx} +27 -28
  93. package/src/Form/{FormAutosuggestOption.jsx → FormAutosuggestOption.tsx} +12 -19
  94. package/src/Form/FormControlDecorator.tsx +16 -0
  95. package/src/Form/{FormControlFloatingLabel.jsx → FormControlFloatingLabel.tsx} +6 -7
  96. package/src/Form/index.tsx +0 -1
  97. package/src/Modal/ModalCloseButton.tsx +48 -0
  98. package/src/Modal/ModalDialog.tsx +0 -1
  99. package/src/Modal/tests/ModalDialog.test.tsx +1 -1
  100. package/src/ProductTour/{Checkpoint.test.jsx → Checkpoint.test.tsx} +11 -1
  101. package/src/ProductTour/CheckpointBody.tsx +21 -0
  102. package/src/ProductTour/CheckpointTitle.tsx +15 -0
  103. package/src/StatefulButton/{StatefulButtontest.test.jsx → StatefulButtontest.test.tsx} +4 -4
  104. package/src/StatefulButton/{index.jsx → index.tsx} +47 -57
  105. package/src/Stepper/{Stepper.jsx → Stepper.tsx} +18 -12
  106. package/src/Stepper/StepperActionRow.tsx +41 -0
  107. package/src/Stepper/{StepperContext.jsx → StepperContext.tsx} +41 -17
  108. package/src/Stepper/{StepperHeader.jsx → StepperHeader.tsx} +47 -44
  109. package/src/Stepper/{StepperHeaderStep.jsx → StepperHeaderStep.tsx} +18 -26
  110. package/src/Stepper/{StepperStep.jsx → StepperStep.tsx} +30 -39
  111. package/src/Stepper/index.ts +1 -0
  112. package/src/Stepper/tests/{Stepper.test.jsx → Stepper.test.tsx} +156 -14
  113. package/src/TransitionReplace/{TransitionReplace.test.jsx → TransitionReplace.test.tsx} +12 -10
  114. package/src/TransitionReplace/{index.jsx → index.tsx} +63 -72
  115. package/src/index.ts +4 -8
  116. package/src/Collapsible/index.jsx +0 -99
  117. package/src/Form/FormControlDecorator.jsx +0 -21
  118. package/src/Modal/ModalCloseButton.jsx +0 -45
  119. package/src/ProductTour/CheckpointBody.jsx +0 -24
  120. package/src/ProductTour/CheckpointTitle.jsx +0 -18
  121. package/src/Stepper/StepperActionRow.jsx +0 -40
  122. /package/{src/Stepper/index.js → dist/Stepper/index.d.ts} +0 -0
  123. /package/src/StatefulButton/__snapshots__/{StatefulButtontest.test.jsx.snap → StatefulButtontest.test.tsx.snap} +0 -0
@@ -1,18 +1,41 @@
1
- import React, { useRef } from 'react';
2
- import PropTypes from 'prop-types';
1
+ import React, { useRef, ReactNode } from 'react';
3
2
  import Form from '../../Form';
4
3
  import { newId } from '../../utils';
5
4
 
6
5
  const DEFAULT_VALUE = '';
7
6
 
7
+ interface DropdownFilterProps {
8
+ /**
9
+ * Specifies a column object.
10
+ *
11
+ * `setFilter`: Function to set the filter value.
12
+ *
13
+ * `Header`: Column header used for labels and placeholders.
14
+ *
15
+ * `filterChoices`: Specifies array of choices.
16
+ *
17
+ * `getHeaderProps`: Generates a key unique to the column being filtered.
18
+ */
19
+ column: {
20
+ setFilter: (value: string | number | undefined) => void;
21
+ Header: ReactNode;
22
+ filterChoices: {
23
+ name: string;
24
+ number?: number;
25
+ value: string | number;
26
+ }[];
27
+ getHeaderProps: () => { key: string };
28
+ };
29
+ }
30
+
8
31
  function DropdownFilter({
9
32
  column: {
10
33
  setFilter, Header, filterChoices, getHeaderProps,
11
34
  },
12
- }) {
35
+ }: DropdownFilterProps) {
13
36
  // creates a unique label that does not change on re-render in case there are multiple checkbox filters in the dom
14
37
  const ariaLabel = useRef(newId(`dropdown-filter-label-${getHeaderProps().key}-`));
15
- const onChange = (e) => {
38
+ const onChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
16
39
  if (e.target.value === DEFAULT_VALUE) {
17
40
  // setting undefined resets the filter
18
41
  return setFilter(undefined);
@@ -36,28 +59,4 @@ function DropdownFilter({
36
59
  );
37
60
  }
38
61
 
39
- DropdownFilter.propTypes = {
40
- /**
41
- * Specifies a column object.
42
- *
43
- * `setFilter`: Function to set the filter value.
44
- *
45
- * `Header`: Column header used for labels and placeholders.
46
- *
47
- * `filterChoices`: Specifies array of choices.
48
- *
49
- * `getHeaderProps`: Generates a key unique to the column being filtered.
50
- */
51
- column: PropTypes.shape({
52
- setFilter: PropTypes.func.isRequired,
53
- Header: PropTypes.oneOfType([PropTypes.elementType, PropTypes.node]).isRequired,
54
- filterChoices: PropTypes.arrayOf(PropTypes.shape({
55
- name: PropTypes.string.isRequired,
56
- number: PropTypes.number,
57
- value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
58
- })).isRequired,
59
- getHeaderProps: PropTypes.func.isRequired,
60
- }).isRequired,
61
- };
62
-
63
62
  export default DropdownFilter;
@@ -1,20 +1,28 @@
1
- import React from 'react';
2
- import PropTypes from 'prop-types';
1
+ import React, { ComponentPropsWithoutRef, MouseEventHandler } from 'react';
3
2
  import classNames from 'classnames';
4
3
  import MenuItem from '../Menu/MenuItem';
5
4
 
5
+ interface FormAutosuggestOptionProps extends Omit<ComponentPropsWithoutRef<'li'>, 'children' | 'className' | 'onClick'> {
6
+ /** Specifies class name to append to the base element. */
7
+ className?: string;
8
+ /** Specifies the text-content of the `FormAutosuggestOption`. */
9
+ children?: string;
10
+ /** A click handler for the `FormAutosuggestOption` */
11
+ onClick?: MouseEventHandler<HTMLLIElement>;
12
+ }
13
+
6
14
  function FormAutosuggestOption({
7
15
  children,
8
16
  className,
9
17
  onClick,
10
18
  ...props
11
- }) {
19
+ }: FormAutosuggestOptionProps) {
12
20
  return (
13
21
  <MenuItem
14
22
  as="li"
15
23
  data-testid="autosuggest-optionitem"
16
24
  role="option"
17
- tabIndex="-1"
25
+ tabIndex={-1}
18
26
  onClick={onClick}
19
27
  className={classNames(className, 'dropdown-item')}
20
28
  {...props}
@@ -24,19 +32,4 @@ function FormAutosuggestOption({
24
32
  );
25
33
  }
26
34
 
27
- FormAutosuggestOption.defaultProps = {
28
- className: null,
29
- children: null,
30
- onClick: null,
31
- };
32
-
33
- FormAutosuggestOption.propTypes = {
34
- /** Specifies class name to append to the base element. */
35
- className: PropTypes.string,
36
- /** Specifies the text-content of the `FormAutosuggestOption`. */
37
- children: PropTypes.string,
38
- /** A click handler for the `FormAutosuggestOption` */
39
- onClick: PropTypes.func,
40
- };
41
-
42
35
  export default FormAutosuggestOption;
@@ -0,0 +1,16 @@
1
+ import React, { ReactNode } from 'react';
2
+
3
+ interface FormControlDecoratorProps {
4
+ children: ReactNode;
5
+ location?: 'leading' | 'trailing';
6
+ }
7
+
8
+ function FormControlDecorator({ children, location = 'leading' }: FormControlDecoratorProps) {
9
+ return (
10
+ <div className={`pgn__form-control-decorator pgn__form-control-decorator-${location}`}>
11
+ {children}
12
+ </div>
13
+ );
14
+ }
15
+
16
+ export default FormControlDecorator;
@@ -1,8 +1,11 @@
1
- import React from 'react';
2
- import PropTypes from 'prop-types';
1
+ import React, { ReactNode } from 'react';
3
2
  import { useFormGroupContext } from './FormGroupContext';
4
3
 
5
- function FormControlFloatingLabel({ children }) {
4
+ interface FormControlFloatingLabelProps {
5
+ children: ReactNode;
6
+ }
7
+
8
+ function FormControlFloatingLabel({ children }: FormControlFloatingLabelProps) {
6
9
  const { controlId } = useFormGroupContext();
7
10
  return (
8
11
  <div className="pgn__form-control-floating-label">
@@ -18,8 +21,4 @@ function FormControlFloatingLabel({ children }) {
18
21
  );
19
22
  }
20
23
 
21
- FormControlFloatingLabel.propTypes = {
22
- children: PropTypes.node.isRequired,
23
- };
24
-
25
24
  export default FormControlFloatingLabel;
@@ -19,7 +19,6 @@ import FormRadioSet from './FormRadioSet';
19
19
  import FormRadioSetContext from './FormRadioSetContext';
20
20
  // @ts-ignore
21
21
  import FormAutosuggest from './FormAutosuggest';
22
- // @ts-ignore
23
22
  import FormAutosuggestOption from './FormAutosuggestOption';
24
23
  // @ts-ignore
25
24
  import FormCheckbox, { CheckboxControl } from './FormCheckbox';
@@ -0,0 +1,48 @@
1
+ import {
2
+ useContext, ReactNode, ElementType, forwardRef, createElement,
3
+ } from 'react';
4
+ import classNames from 'classnames';
5
+ import ModalContext from './ModalContext';
6
+ import Button, { type ButtonProps } from '../Button';
7
+ import type { ComponentWithAsProp } from '../utils/types/bootstrap';
8
+
9
+ export interface ModalCloseButtonProps extends Omit<ButtonProps, 'children' | 'onClick'> {
10
+ /** Specifies the base element */
11
+ as?: ElementType;
12
+ /** Specifies the content of the button */
13
+ children?: ReactNode;
14
+ /** Specifies class name to append to the base element */
15
+ className?: string;
16
+ /** Specifies the callback function when the close button is clicked */
17
+ onClick?: () => void;
18
+ }
19
+
20
+ const ModalCloseButton: ComponentWithAsProp<'button', ModalCloseButtonProps> = forwardRef(({
21
+ as = Button,
22
+ children = null,
23
+ className,
24
+ onClick,
25
+ ...props
26
+ }: ModalCloseButtonProps, ref: React.ForwardedRef<HTMLButtonElement>) => {
27
+ const { onClose } = useContext(ModalContext);
28
+ const type = as;
29
+ const componentProps = {
30
+ ...props,
31
+ className: classNames('pgn__modal-close-button', className),
32
+ onClick: () => {
33
+ onClose();
34
+ if (onClick) {
35
+ onClick();
36
+ }
37
+ },
38
+ ref,
39
+ };
40
+
41
+ // Use the non-jsx syntax to create this element so we can more
42
+ // finely control the component type (defaulted to Button via defaultProps)
43
+ return createElement(type, componentProps, children);
44
+ });
45
+
46
+ ModalCloseButton.displayName = 'ModalCloseButton';
47
+
48
+ export default ModalCloseButton;
@@ -3,7 +3,6 @@ import classNames from 'classnames';
3
3
  import { useMediaQuery } from 'react-responsive';
4
4
  import { useIntl } from 'react-intl';
5
5
  import ModalLayer from './ModalLayer';
6
- // @ts-ignore for now - this needs to be converted to TypeScript
7
6
  import ModalCloseButton from './ModalCloseButton';
8
7
  import ModalDialogHeader from './ModalDialogHeader';
9
8
  // @ts-ignore for now - this needs to be converted to TypeScript
@@ -51,7 +51,7 @@ describe('ModalDialog', () => {
51
51
  >
52
52
  <ModalDialog.Header><ModalDialog.Title>The title</ModalDialog.Title></ModalDialog.Header>
53
53
  <ModalDialog.Body><p>The hidden content</p></ModalDialog.Body>
54
- <ModalDialog.Footer><ModalDialog.CloseButton>Cancel</ModalDialog.CloseButton></ModalDialog.Footer>
54
+ <ModalDialog.Footer><ModalDialog.CloseButton variant="tertiary">Cancel</ModalDialog.CloseButton></ModalDialog.Footer>
55
55
  </ModalDialog>
56
56
  </IntlProvider>,
57
57
  );
@@ -1,11 +1,12 @@
1
- import React from 'react';
2
1
  import { render, screen } from '@testing-library/react';
3
2
  import userEvent, { PointerEventsCheckLevel } from '@testing-library/user-event';
4
3
  import { IntlProvider } from 'react-intl';
5
4
 
6
5
  import * as popper from '@popperjs/core';
7
6
 
7
+ // @ts-ignore
8
8
  import Checkpoint from './Checkpoint';
9
+ import CheckpointTitle from './CheckpointTitle';
9
10
 
10
11
  const popperMock = jest.spyOn(popper, 'createPopper');
11
12
 
@@ -131,4 +132,13 @@ describe('Checkpoint', () => {
131
132
  expect(screen.getByText('End', { selector: 'button' })).toBeInTheDocument();
132
133
  });
133
134
  });
135
+
136
+ // This test is based on the existing behavior, but it doesn't necessarily make sense.
137
+ // It would probably be better to render nothing at all when no heading text is provided.
138
+ describe('CheckpointTitle', () => {
139
+ it('renders an empty heading when no children are provided', () => {
140
+ render(<CheckpointTitle />);
141
+ expect(screen.getByRole('heading', { level: 2 })).toBeEmptyDOMElement();
142
+ });
143
+ });
134
144
  });
@@ -0,0 +1,21 @@
1
+ import React, { forwardRef, ForwardedRef, ReactNode } from 'react';
2
+
3
+ interface CheckpointBodyProps {
4
+ children?: ReactNode;
5
+ }
6
+
7
+ const CheckpointBody = forwardRef(({
8
+ children = null,
9
+ }: CheckpointBodyProps, ref: ForwardedRef<HTMLDivElement>) => {
10
+ if (!children) {
11
+ return null;
12
+ }
13
+
14
+ return (
15
+ <div className="pgn__checkpoint-body" ref={ref}>
16
+ {children}
17
+ </div>
18
+ );
19
+ });
20
+
21
+ export default CheckpointBody;
@@ -0,0 +1,15 @@
1
+ import React, { forwardRef, ForwardedRef, ReactNode } from 'react';
2
+
3
+ interface CheckpointTitleProps {
4
+ children?: ReactNode;
5
+ }
6
+
7
+ const CheckpointTitle = forwardRef(({
8
+ children = null,
9
+ }: CheckpointTitleProps, ref: ForwardedRef<HTMLHeadingElement>) => (
10
+ <h2 id="pgn__checkpoint-title" ref={ref}>
11
+ {children}
12
+ </h2>
13
+ ));
14
+
15
+ export default CheckpointTitle;
@@ -1,7 +1,7 @@
1
1
  import React from 'react';
2
2
  import renderer from 'react-test-renderer';
3
3
 
4
- import StatefulButton from './index';
4
+ import StatefulButton from '.';
5
5
  import Icon from '../Icon';
6
6
 
7
7
  describe('StatefulButton', () => {
@@ -13,7 +13,7 @@ describe('StatefulButton', () => {
13
13
  complete: 'Saved',
14
14
  },
15
15
  className: 'mr-2',
16
- variant: 'primary',
16
+ variant: 'primary' as const,
17
17
  };
18
18
  const tree = renderer.create((
19
19
  <div>
@@ -39,7 +39,7 @@ describe('StatefulButton', () => {
39
39
  },
40
40
  disabledStates: ['pending'],
41
41
  className: 'mr-2',
42
- variant: 'primary',
42
+ variant: 'primary' as const,
43
43
  };
44
44
  const tree = renderer.create((
45
45
  <>
@@ -63,7 +63,7 @@ describe('StatefulButton', () => {
63
63
  },
64
64
  disabledStates: ['unedited'],
65
65
  className: 'mr-2',
66
- variant: 'primary',
66
+ variant: 'primary' as const,
67
67
  };
68
68
  const tree = renderer.create((
69
69
  <>
@@ -1,19 +1,61 @@
1
1
  import React from 'react';
2
- import PropTypes from 'prop-types';
3
2
  import classNames from 'classnames';
4
3
  import { Cancel, CheckCircleOutline, SpinnerSimple } from '../../icons';
5
4
  import Button from '../Button';
6
5
  import Icon from '../Icon';
7
6
 
7
+ export interface StatefulButtonProps extends React.ComponentPropsWithoutRef<typeof Button> {
8
+ /** Optionally specify additional CSS classes to give this button. */
9
+ className?: string;
10
+ /**
11
+ * Each state has:
12
+ * - A label (required)
13
+ * - An icon
14
+ * - an option to be disabled
15
+ *
16
+ * Control the state with the `state` prop. Example usage:
17
+ *
18
+ * ```jsx
19
+ * <StatefulButton
20
+ * state="pending"
21
+ * labels={{
22
+ * default: 'Download',
23
+ * pending: 'Downloading',
24
+ * complete: 'Downloaded',
25
+ * }}
26
+ * icons={{
27
+ * default: <Icon className="fa fa-download" />,
28
+ * pending: <Icon className="fa fa-spinner fa-spin" />,
29
+ * complete: <Icon className="fa fa-check" />,
30
+ * }}
31
+ * disabledStates=['pending']
32
+ * className='btn-primary mr-2'
33
+ * />
34
+ * ```
35
+ */
36
+ state?: string;
37
+ /** Each state has a `label`. Required. */
38
+ labels: { [key: string]: React.ReactNode };
39
+ /** Each state has an `icon`. */
40
+ icons?: { [key: string]: React.ReactNode };
41
+ /** States listed in `disabledStates` will not be clickable */
42
+ disabledStates?: string[];
43
+ }
44
+
8
45
  function StatefulButton({
9
46
  className,
10
- state,
47
+ state = 'default',
11
48
  labels,
12
- icons,
13
- disabledStates,
49
+ icons = {
50
+ default: undefined,
51
+ pending: <Icon src={SpinnerSimple} className={classNames('icon-spin')} />,
52
+ complete: <Icon src={CheckCircleOutline} />,
53
+ error: <Icon src={Cancel} />,
54
+ },
55
+ disabledStates = ['pending', 'complete'],
14
56
  onClick,
15
57
  ...attributes
16
- }) {
58
+ }: StatefulButtonProps) {
17
59
  const isDisabled = disabledStates.indexOf(state) !== -1;
18
60
  const icon = icons[state] !== undefined ? icons[state] : icons.default;
19
61
  const label = labels[state] !== undefined ? labels[state] : labels.default;
@@ -51,56 +93,4 @@ function StatefulButton({
51
93
  );
52
94
  }
53
95
 
54
- StatefulButton.propTypes = {
55
- className: PropTypes.string,
56
- /**
57
- * Each state has:
58
- * - A label (required)
59
- * - An icon
60
- * - an option to be disabled
61
- *
62
- * Control the state with the `state` prop. Example usage:
63
- *
64
- * ```jsx
65
- * <StatefulButton
66
- * state="pending"
67
- * labels={{
68
- * default: 'Download',
69
- * pending: 'Downloading',
70
- * complete: 'Downloaded',
71
- * }}
72
- * icons={{
73
- * default: <Icon className="fa fa-download" />,
74
- * pending: <Icon className="fa fa-spinner fa-spin" />,
75
- * complete: <Icon className="fa fa-check" />,
76
- * }}
77
- * disabledStates=['pending']
78
- * className='btn-primary mr-2'
79
- * />
80
- * ```
81
- */
82
- state: PropTypes.string,
83
- /** Required. Each state has a `label`. */
84
- labels: PropTypes.objectOf(PropTypes.node).isRequired,
85
- /** Required. Each state has an `icon`. */
86
- icons: PropTypes.objectOf(PropTypes.node),
87
- /** Required. Each state has a `disabledState` */
88
- disabledStates: PropTypes.arrayOf(PropTypes.string),
89
- /** Specifies the callback function when the button is clicked */
90
- onClick: PropTypes.func,
91
- };
92
-
93
- StatefulButton.defaultProps = {
94
- className: undefined,
95
- state: 'default',
96
- icons: {
97
- default: undefined,
98
- pending: <Icon src={SpinnerSimple} className={classNames('icon-spin')} />,
99
- complete: <Icon src={CheckCircleOutline} />,
100
- error: <Icon src={Cancel} />,
101
- },
102
- disabledStates: ['pending', 'complete'],
103
- onClick: undefined,
104
- };
105
-
106
96
  export default StatefulButton;
@@ -1,27 +1,33 @@
1
1
  import React from 'react';
2
- import PropTypes from 'prop-types';
3
2
  import StepperStep from './StepperStep';
4
3
  import StepperHeader from './StepperHeader';
5
4
  import StepperActionRow from './StepperActionRow';
6
5
  import { StepperContextProvider } from './StepperContext';
7
6
 
8
- function Stepper({ children, activeKey }) {
9
- return (
10
- <StepperContextProvider activeKey={activeKey}>
11
- {children}
12
- </StepperContextProvider>
13
- );
14
- }
15
-
16
- Stepper.propTypes = {
7
+ export interface StepperProps {
17
8
  /**
18
9
  * Specifies the content of the `Stepper`.
19
10
  */
20
- children: PropTypes.node.isRequired,
11
+ children: React.ReactNode;
21
12
  /**
22
13
  * The eventKey of the step to display.
23
14
  */
24
- activeKey: PropTypes.string.isRequired,
15
+ activeKey: string;
16
+ }
17
+
18
+ interface StepperComponent {
19
+ (props: StepperProps): JSX.Element;
20
+ Step: typeof StepperStep;
21
+ Header: typeof StepperHeader;
22
+ ActionRow: typeof StepperActionRow;
23
+ }
24
+
25
+ const Stepper: StepperComponent = function Stepper({ children, activeKey }: StepperProps) {
26
+ return (
27
+ <StepperContextProvider activeKey={activeKey}>
28
+ {children}
29
+ </StepperContextProvider>
30
+ );
25
31
  };
26
32
 
27
33
  Stepper.Step = StepperStep;
@@ -0,0 +1,41 @@
1
+ import React, { useContext, forwardRef } from 'react';
2
+ import { StepperContext } from './StepperContext';
3
+ import ActionRow from '../ActionRow';
4
+ import type { ComponentWithAsProp } from '../utils/types/bootstrap';
5
+
6
+ export interface StepperActionRowProps {
7
+ /** Specifies the content of the `ActionRow`. */
8
+ children: React.ReactNode;
9
+ /**
10
+ * An identifier of the `ActionRow`. When `activeKey` on the
11
+ * `Stepper` equals to the `eventKey`, the `ActionRow` will be displayed.
12
+ */
13
+ eventKey: string;
14
+ /** Specifies the base element */
15
+ as?: React.ElementType;
16
+ }
17
+
18
+ type StepperActionRowComponent = ComponentWithAsProp<'div', StepperActionRowProps> & {
19
+ Spacer: typeof ActionRow.Spacer;
20
+ };
21
+
22
+ const StepperActionRow: ComponentWithAsProp<'div', StepperActionRowProps> = forwardRef(({
23
+ as = ActionRow,
24
+ children,
25
+ eventKey,
26
+ ...props
27
+ }: StepperActionRowProps, ref: React.ForwardedRef<HTMLDivElement>) => {
28
+ const { activeKey } = useContext(StepperContext);
29
+ const isActive = activeKey === eventKey;
30
+
31
+ if (!isActive) {
32
+ return null;
33
+ }
34
+
35
+ return React.createElement(as, { ...props, ref }, children);
36
+ });
37
+
38
+ (StepperActionRow as StepperActionRowComponent).Spacer = ActionRow.Spacer;
39
+ StepperActionRow.displayName = 'StepperActionRow';
40
+
41
+ export default StepperActionRow as StepperActionRowComponent;
@@ -1,14 +1,38 @@
1
1
  import React, {
2
2
  useCallback, useEffect, useReducer, useState,
3
3
  } from 'react';
4
- import PropTypes from 'prop-types';
5
4
 
6
- export const StepperContext = React.createContext({
5
+ export interface Step {
6
+ eventKey: string;
7
+ title: string;
8
+ description?: string;
9
+ hasError?: boolean;
10
+ index?: number;
11
+ onClick?: () => void;
12
+ }
13
+
14
+ export interface StepperContextValue {
15
+ activeKey: string;
16
+ registerStep: (step: Step) => void;
17
+ steps: Step[];
18
+ removeStep: (eventKey: string) => void;
19
+ getIsViewed: (index: number) => boolean;
20
+ }
21
+
22
+ export const StepperContext = React.createContext<StepperContextValue>({
7
23
  activeKey: '',
24
+ registerStep: () => {},
25
+ steps: [],
26
+ removeStep: () => {},
27
+ getIsViewed: () => false,
8
28
  });
9
29
 
10
- const stepsReducer = (stepsState, action) => {
11
- let newStepsState = [];
30
+ type StepsAction =
31
+ | { type: 'register'; step: Step }
32
+ | { type: 'remove'; eventKey: string };
33
+
34
+ const stepsReducer = (stepsState: Step[], action: StepsAction): Step[] => {
35
+ let newStepsState: Step[] = [];
12
36
  switch (action.type) {
13
37
  case 'remove':
14
38
  return stepsState.filter(step => step.eventKey !== action.eventKey);
@@ -27,22 +51,29 @@ const stepsReducer = (stepsState, action) => {
27
51
  }
28
52
 
29
53
  // If using the index prop
30
- if (stepsState.some(step => step.index)) {
54
+ if (stepsState.some(step => step.index !== undefined)) {
31
55
  return newStepsState.sort((a, b) => (
32
- a.index > b.index ? 1 : -1
56
+ (a.index || 0) > (b.index || 0) ? 1 : -1
33
57
  ));
34
58
  }
35
59
  return newStepsState;
36
60
  }
37
61
  };
38
62
 
39
- export function StepperContextProvider({ children, activeKey }) {
63
+ export interface StepperContextProviderProps {
64
+ /** Specifies the content of the `ContextProvider`. */
65
+ children: React.ReactNode;
66
+ /** Specifies the current step of the `Stepper`. */
67
+ activeKey: string;
68
+ }
69
+
70
+ export function StepperContextProvider({ children, activeKey }: StepperContextProviderProps) {
40
71
  const [steps, dispatch] = useReducer(stepsReducer, []);
41
72
  const [currentBoundary, setCurrentBoundary] = useState(0);
42
- const registerStep = useCallback((step) => dispatch({ step, type: 'register' }), []);
43
- const removeStep = useCallback((eventKey) => dispatch({ eventKey, type: 'remove' }), []);
73
+ const registerStep = useCallback((step: Step) => dispatch({ step, type: 'register' }), []);
74
+ const removeStep = useCallback((eventKey: string) => dispatch({ eventKey, type: 'remove' }), []);
44
75
 
45
- const getIsViewed = (index) => index <= currentBoundary;
76
+ const getIsViewed = (index: number) => index <= currentBoundary;
46
77
 
47
78
  useEffect(() => {
48
79
  const activeIndex = steps.findIndex(step => step.eventKey === activeKey);
@@ -64,11 +95,4 @@ export function StepperContextProvider({ children, activeKey }) {
64
95
  );
65
96
  }
66
97
 
67
- StepperContextProvider.propTypes = {
68
- /** Specifies the content of the `ContextProvider`. */
69
- children: PropTypes.node.isRequired,
70
- /** Specifies the current step of the `Stepper`. */
71
- activeKey: PropTypes.node.isRequired,
72
- };
73
-
74
98
  export { stepsReducer };