@openedx/paragon 23.0.0-alpha.2 → 23.0.0-alpha.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 (74) hide show
  1. package/dist/Button/index.d.ts +35 -0
  2. package/dist/Button/index.js +37 -15
  3. package/dist/Button/index.js.map +1 -1
  4. package/dist/Chip/ChipIcon.d.ts +13 -8
  5. package/dist/Chip/ChipIcon.js +0 -2
  6. package/dist/Chip/ChipIcon.js.map +1 -1
  7. package/dist/Chip/constants.d.ts +4 -0
  8. package/dist/Chip/constants.js +3 -2
  9. package/dist/Chip/constants.js.map +1 -0
  10. package/dist/Chip/index.d.ts +4 -3
  11. package/dist/Chip/index.js +2 -4
  12. package/dist/Chip/index.js.map +1 -1
  13. package/dist/ChipCarousel/index.js +0 -2
  14. package/dist/ChipCarousel/index.js.map +1 -1
  15. package/dist/Hyperlink/index.d.ts +24 -0
  16. package/dist/Hyperlink/index.js +20 -32
  17. package/dist/Hyperlink/index.js.map +1 -1
  18. package/dist/Icon/index.d.ts +4 -2
  19. package/dist/Icon/index.js +1 -1
  20. package/dist/Icon/index.js.map +1 -1
  21. package/dist/IconButton/index.d.ts +342 -0
  22. package/dist/IconButton/index.js +18 -26
  23. package/dist/IconButton/index.js.map +1 -1
  24. package/dist/Modal/ModalPopup.js +7 -1
  25. package/dist/Modal/ModalPopup.js.map +1 -1
  26. package/dist/Modal/_ModalDialog.scss +4 -0
  27. package/dist/Overlay/index.d.ts +128 -0
  28. package/dist/Overlay/index.js +8 -2
  29. package/dist/Overlay/index.js.map +1 -1
  30. package/dist/Tooltip/index.d.ts +7 -0
  31. package/dist/Tooltip/index.js.map +1 -1
  32. package/dist/core.css +3 -0
  33. package/dist/core.css.map +1 -1
  34. package/dist/core.min.css +1 -1
  35. package/dist/index.d.ts +5 -5
  36. package/dist/index.js +7 -7
  37. package/dist/setupTest.d.ts +2 -0
  38. package/dist/setupTest.js.map +1 -0
  39. package/dist/utils/types/bootstrap.d.ts +39 -0
  40. package/dist/utils/types/bootstrap.js +2 -0
  41. package/dist/utils/types/bootstrap.js.map +1 -0
  42. package/package.json +5 -5
  43. package/src/Button/{Button.test.jsx → Button.test.tsx} +14 -2
  44. package/src/Button/__snapshots__/{Button.test.jsx.snap → Button.test.tsx.snap} +19 -2
  45. package/src/Button/{index.jsx → index.tsx} +58 -16
  46. package/src/Chip/{Chip.test.jsx → Chip.test.tsx} +5 -7
  47. package/src/Chip/ChipIcon.tsx +8 -8
  48. package/src/Chip/{constants.js → constants.ts} +1 -1
  49. package/src/Chip/index.tsx +6 -8
  50. package/src/ChipCarousel/index.tsx +0 -2
  51. package/src/Hyperlink/{Hyperlink.test.jsx → Hyperlink.test.tsx} +21 -10
  52. package/src/Hyperlink/{index.jsx → index.tsx} +41 -37
  53. package/src/Icon/index.d.ts +4 -2
  54. package/src/Icon/index.jsx +1 -1
  55. package/src/IconButton/{IconButton.test.jsx → IconButton.test.tsx} +24 -3
  56. package/src/IconButton/__snapshots__/IconButton.test.tsx.snap +90 -0
  57. package/src/IconButton/{index.jsx → index.tsx} +66 -26
  58. package/src/Modal/ModalPopup.jsx +9 -1
  59. package/src/Modal/_ModalDialog.scss +4 -0
  60. package/src/Modal/tests/ModalPopupNoMock.test.jsx +29 -0
  61. package/src/Overlay/{index.jsx → index.tsx} +13 -8
  62. package/src/Tooltip/{index.jsx → index.tsx} +9 -3
  63. package/src/index.d.ts +5 -5
  64. package/src/index.js +7 -7
  65. package/src/{setupTest.js → setupTest.ts} +1 -0
  66. package/src/utils/types/bootstrap.test.tsx +86 -0
  67. package/src/utils/types/bootstrap.ts +43 -0
  68. package/src/IconButton/__snapshots__/IconButton.test.jsx.snap +0 -20
  69. /package/src/Button/{ButtonGroup.test.jsx → ButtonGroup.test.tsx} +0 -0
  70. /package/src/Button/{ButtonToolbar.test.jsx → ButtonToolbar.test.tsx} +0 -0
  71. /package/src/Button/__snapshots__/{ButtonGroup.test.jsx.snap → ButtonGroup.test.tsx.snap} +0 -0
  72. /package/src/Button/__snapshots__/{ButtonToolbar.test.jsx.snap → ButtonToolbar.test.tsx.snap} +0 -0
  73. /package/src/Chip/__snapshots__/{Chip.test.jsx.snap → Chip.test.tsx.snap} +0 -0
  74. /package/src/Tooltip/{Tooltip.test.jsx → Tooltip.test.tsx} +0 -0
package/dist/index.d.ts CHANGED
@@ -5,9 +5,14 @@
5
5
  // Things that have types
6
6
  // // // // // // // // // // // // // // // // // // // // // // // // // // //
7
7
  export { default as Bubble } from './Bubble';
8
+ export { default as Button, ButtonGroup, ButtonToolbar } from './Button';
8
9
  export { default as Chip, CHIP_PGN_CLASS } from './Chip';
9
10
  export { default as ChipCarousel } from './ChipCarousel';
11
+ export { default as Hyperlink, HYPER_LINK_EXTERNAL_LINK_ALT_TEXT, HYPER_LINK_EXTERNAL_LINK_TITLE } from './Hyperlink';
10
12
  export { default as Icon } from './Icon';
13
+ export { default as IconButton, IconButtonWithTooltip } from './IconButton';
14
+ export { default as Overlay, OverlayTrigger } from './Overlay';
15
+ export { default as Tooltip } from './Tooltip';
11
16
 
12
17
  // // // // // // // // // // // // // // // // // // // // // // // // // // //
13
18
  // Things that don't have types
@@ -20,7 +25,6 @@ export const Avatar: any; // from './Avatar';
20
25
  export const AvatarButton: any; // from './AvatarButton';
21
26
  export const Badge: any; // from './Badge';
22
27
  export const Breadcrumb: any; // from './Breadcrumb';
23
- export const Button: any, ButtonGroup: any, ButtonToolbar: any; // from './Button';
24
28
  export const
25
29
  Card: any,
26
30
  CardColumns: any,
@@ -72,8 +76,6 @@ export const
72
76
  FormAutosuggestOption: any,
73
77
  InputGroup: any;
74
78
  // from './Form';
75
- export const Hyperlink: any, HYPER_LINK_EXTERNAL_LINK_ALT_TEXT: string, HYPER_LINK_EXTERNAL_LINK_TITLE: string; // from './Hyperlink';
76
- export const IconButton: any, IconButtonWithTooltip: any; // from './IconButton';
77
79
  export const IconButtonToggle: any; // from './IconButtonToggle';
78
80
  export const Input: any; // from './Input';
79
81
  export const InputSelect: any; // from './InputSelect';
@@ -106,7 +108,6 @@ export const
106
108
  NavLink: any;
107
109
  // from './Nav';
108
110
  export const Navbar: any, NavbarBrand: any, NAVBAR_LABEL: string; // from './Navbar';
109
- export const Overlay: any, OverlayTrigger: any; // from './Overlay';
110
111
  export const PageBanner: any, PAGE_BANNER_DISMISS_ALT_TEXT: string; // from './PageBanner';
111
112
  export const
112
113
  Pagination: any,
@@ -145,7 +146,6 @@ export const
145
146
  // from './Tabs';
146
147
  export const TextArea: any; // from './TextArea';
147
148
  export const Toast: any, TOAST_CLOSE_LABEL_TEXT: string, TOAST_DELAY: number; // from './Toast';
148
- export const Tooltip: any; // from './Tooltip';
149
149
  export const ValidationFormGroup: any; // from './ValidationFormGroup';
150
150
  export const TransitionReplace: any; // from './TransitionReplace';
151
151
  export const ValidationMessage: any; // from './ValidationMessage';
package/dist/index.js CHANGED
@@ -1,13 +1,18 @@
1
- // To keep this file in sync with the .d.ts file, it's in the same order
2
- // and each line number is the same
1
+ // Keep this file in sync with the .d.ts file (manually). It's in the same order
2
+ // and each line number is the same, to make it easier.
3
3
 
4
4
  // // // // // // // // // // // // // // // // // // // // // // // // // // //
5
5
  // Things that have types
6
6
  // // // // // // // // // // // // // // // // // // // // // // // // // // //
7
7
  export { default as Bubble } from './Bubble';
8
+ export { default as Button, ButtonGroup, ButtonToolbar } from './Button';
8
9
  export { default as Chip, CHIP_PGN_CLASS } from './Chip';
9
10
  export { default as ChipCarousel } from './ChipCarousel';
11
+ export { default as Hyperlink, HYPER_LINK_EXTERNAL_LINK_ALT_TEXT, HYPER_LINK_EXTERNAL_LINK_TITLE } from './Hyperlink';
10
12
  export { default as Icon } from './Icon';
13
+ export { default as IconButton, IconButtonWithTooltip } from './IconButton';
14
+ export { default as Overlay, OverlayTrigger } from './Overlay';
15
+ export { default as Tooltip } from './Tooltip';
11
16
 
12
17
  // // // // // // // // // // // // // // // // // // // // // // // // // // //
13
18
  // Things that don't have types
@@ -20,7 +25,6 @@ export { default as Avatar } from './Avatar';
20
25
  export { default as AvatarButton } from './AvatarButton';
21
26
  export { default as Badge } from './Badge';
22
27
  export { default as Breadcrumb } from './Breadcrumb';
23
- export { default as Button, ButtonGroup, ButtonToolbar } from './Button';
24
28
  export {
25
29
  default as Card,
26
30
  CardColumns,
@@ -69,8 +73,6 @@ export {
69
73
  FormAutosuggestOption,
70
74
  InputGroup,
71
75
  } from './Form';
72
- export { default as Hyperlink, HYPER_LINK_EXTERNAL_LINK_ALT_TEXT, HYPER_LINK_EXTERNAL_LINK_TITLE } from './Hyperlink';
73
- export { default as IconButton, IconButtonWithTooltip } from './IconButton';
74
76
  export { default as IconButtonToggle } from './IconButtonToggle';
75
77
  export { default as Image, Figure } from './Image';
76
78
  export { default as MailtoLink, MAIL_TO_LINK_EXTERNAL_LINK_ALTERNATIVE_TEXT, MAIL_TO_LINK_EXTERNAL_LINK_TITLE } from './MailtoLink';
@@ -97,7 +99,6 @@ export {
97
99
  NavLink,
98
100
  } from './Nav';
99
101
  export { default as Navbar, NavbarBrand, NAVBAR_LABEL } from './Navbar';
100
- export { default as Overlay, OverlayTrigger } from './Overlay';
101
102
  export { default as PageBanner, PAGE_BANNER_DISMISS_ALT_TEXT } from './PageBanner';
102
103
  export {
103
104
  default as Pagination,
@@ -132,7 +133,6 @@ export {
132
133
  TabPane,
133
134
  } from './Tabs';
134
135
  export { default as Toast, TOAST_CLOSE_LABEL_TEXT, TOAST_DELAY } from './Toast';
135
- export { default as Tooltip } from './Tooltip';
136
136
  export { default as TransitionReplace } from './TransitionReplace';
137
137
  export { default as ValidationMessage } from './ValidationMessage';
138
138
  export { default as DataTable } from './DataTable';
@@ -0,0 +1,2 @@
1
+ import 'regenerator-runtime/runtime';
2
+ import '@testing-library/jest-dom';
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setupTest.js","names":["ResizeObserver","_classCallCheck","_createClass","key","value","observe","unobserve","disconnect","window"],"sources":["../src/setupTest.ts"],"sourcesContent":["/* eslint-disable import/no-extraneous-dependencies */\nimport 'regenerator-runtime/runtime';\n\nimport '@testing-library/jest-dom';\n\nclass ResizeObserver {\n observe() {\n // do nothing\n }\n\n unobserve() {\n // do nothing\n }\n\n disconnect() {\n // do nothing\n }\n}\n\nwindow.ResizeObserver = ResizeObserver;\n"],"mappings":";;;;;;AAAA;AACA,OAAO,6BAA6B;AAEpC,OAAO,2BAA2B;AAAC,IAE7BA,cAAc;EAAA,SAAAA,eAAA;IAAAC,eAAA,OAAAD,cAAA;EAAA;EAAAE,YAAA,CAAAF,cAAA;IAAAG,GAAA;IAAAC,KAAA,EAClB,SAAAC,QAAA,EAAU;MACR;IAAA;EACD;IAAAF,GAAA;IAAAC,KAAA,EAED,SAAAE,UAAA,EAAY;MACV;IAAA;EACD;IAAAH,GAAA;IAAAC,KAAA,EAED,SAAAG,WAAA,EAAa;MACX;IAAA;EACD;EAAA,OAAAP,cAAA;AAAA;AAGHQ,MAAM,CAACR,cAAc,GAAGA,cAAc","ignoreList":[]}
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Types related to bootstrap components
3
+ */
4
+ import React from 'react';
5
+ import type { BsPrefixProps, BsPrefixRefForwardingComponent } from 'react-bootstrap/esm/helpers';
6
+ /**
7
+ * Type helper for defining props of a component that wraps a bootstrap
8
+ * component. This type defines three props:
9
+ * 1. `className`: this component accepts additional CSS classes.
10
+ * 2. `bsPrefix`: locally change the class name prefix used for this component.
11
+ * 3. `as`: optionally specify which HTML element or Component is used, e.g. `"div"`
12
+ *
13
+ * This type assumes no `children` are allowed, but you can extend it to allow children.
14
+ */
15
+ export type BsPropsWithAs<As extends React.ElementType = React.ElementType> = BsPrefixProps<As>;
16
+ /**
17
+ * This is a helper that can be used to define the type of a Paragon component
18
+ * that accepts an `as` prop.
19
+ *
20
+ * It:
21
+ * - assumes you are using `forwardRef`, and sets the type of the `ref` prop
22
+ * to match the type of the component passed in the `as` prop.
23
+ * - assumes you are passing all unused props to the component, so adds any
24
+ * props from the `as` component type to the props you specify as `Props`.
25
+ *
26
+ * Example;
27
+ * ```
28
+ * interface MyProps extends BsPropsWithAs {
29
+ * customProp?: string;
30
+ * }
31
+ * export const MyComponent: ComponentWithAsProp<'div', MyProps> = (
32
+ * React.forwardRef<HTMLDivElement, MyProps>(
33
+ * ({ as: Inner = 'div', ...props }, ref) => <Inner {...props} ref={ref} />,
34
+ * )
35
+ * );
36
+ * ```
37
+ * Note that you need to define the default (e.g. `'div'`) in three different places.
38
+ */
39
+ export type ComponentWithAsProp<DefaultElementType extends React.ElementType, Props> = BsPrefixRefForwardingComponent<DefaultElementType, Props>;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=bootstrap.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bootstrap.js","names":[],"sources":["../../../src/utils/types/bootstrap.ts"],"sourcesContent":["/**\n * Types related to bootstrap components\n */\nimport React from 'react';\n\nimport type { BsPrefixProps, BsPrefixRefForwardingComponent } from 'react-bootstrap/esm/helpers';\n\n/**\n * Type helper for defining props of a component that wraps a bootstrap\n * component. This type defines three props:\n * 1. `className`: this component accepts additional CSS classes.\n * 2. `bsPrefix`: locally change the class name prefix used for this component.\n * 3. `as`: optionally specify which HTML element or Component is used, e.g. `\"div\"`\n *\n * This type assumes no `children` are allowed, but you can extend it to allow children.\n */\nexport type BsPropsWithAs<As extends React.ElementType = React.ElementType> = BsPrefixProps<As>;\n\n/**\n * This is a helper that can be used to define the type of a Paragon component\n * that accepts an `as` prop.\n *\n * It:\n * - assumes you are using `forwardRef`, and sets the type of the `ref` prop\n * to match the type of the component passed in the `as` prop.\n * - assumes you are passing all unused props to the component, so adds any\n * props from the `as` component type to the props you specify as `Props`.\n *\n * Example;\n * ```\n * interface MyProps extends BsPropsWithAs {\n * customProp?: string;\n * }\n * export const MyComponent: ComponentWithAsProp<'div', MyProps> = (\n * React.forwardRef<HTMLDivElement, MyProps>(\n * ({ as: Inner = 'div', ...props }, ref) => <Inner {...props} ref={ref} />,\n * )\n * );\n * ```\n * Note that you need to define the default (e.g. `'div'`) in three different places.\n */\nexport type ComponentWithAsProp<DefaultElementType extends React.ElementType, Props>\n = BsPrefixRefForwardingComponent<DefaultElementType, Props>;\n"],"mappings":"","ignoreList":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openedx/paragon",
3
- "version": "23.0.0-alpha.2",
3
+ "version": "23.0.0-alpha.3",
4
4
  "description": "Accessible, responsive UI component library based on Bootstrap.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -123,8 +123,8 @@
123
123
  "@testing-library/react-hooks": "^8.0.1",
124
124
  "@testing-library/user-event": "^13.5.0",
125
125
  "@types/jest": "^29.5.10",
126
- "@types/react": "17.0.0",
127
- "@types/react-dom": "17.0.11",
126
+ "@types/react": "^17.0.80",
127
+ "@types/react-dom": "^17.0.11",
128
128
  "@types/react-responsive": "^8.0.8",
129
129
  "@types/react-table": "^7.7.19",
130
130
  "@types/react-test-renderer": "^18.0.0",
@@ -169,7 +169,7 @@
169
169
  "^.+\\.tsx?$": "ts-jest"
170
170
  },
171
171
  "setupFilesAfterEnv": [
172
- "./src/setupTest.js"
172
+ "./src/setupTest.ts"
173
173
  ],
174
174
  "moduleNameMapper": {
175
175
  "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
@@ -180,7 +180,7 @@
180
180
  ],
181
181
  "coveragePathIgnorePatterns": [
182
182
  "/node_modules/",
183
- "src/setupTest.js",
183
+ "src/setupTest.ts",
184
184
  "src/index.js",
185
185
  "/tests/",
186
186
  "/www/",
@@ -30,7 +30,7 @@ describe('<Button />', () => {
30
30
 
31
31
  it('renders with props iconAfter and size', () => {
32
32
  const tree = renderer.create((
33
- <Button iconAfter={Close} size="md">Button</Button>
33
+ <Button iconAfter={Close} size="sm">Button</Button>
34
34
  )).toJSON();
35
35
  expect(tree).toMatchSnapshot();
36
36
  });
@@ -94,9 +94,21 @@ describe('<Button />', () => {
94
94
  });
95
95
 
96
96
  test('test button as hyperlink', () => {
97
- render(<Button as={Hyperlink} destination="https://www.poop.com/💩">Button</Button>);
97
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
98
+ const ref = (_current: HTMLAnchorElement) => {}; // Check typing of a ref - should not show type errors.
99
+ render(<Button as={Hyperlink} ref={ref} destination="https://www.poop.com/💩">Button</Button>);
98
100
  expect(screen.getByRole('link').getAttribute('href')).toEqual('https://www.poop.com/💩');
99
101
  });
100
102
  });
103
+
104
+ test('with size="inline"', () => {
105
+ const tree = renderer.create((
106
+ <p>
107
+ <span className="mr-1">2 items selected.</span>
108
+ <Button variant="link" size="inline">Clear</Button>
109
+ </p>
110
+ )).toJSON();
111
+ expect(tree).toMatchSnapshot();
112
+ });
101
113
  });
102
114
  });
@@ -55,13 +55,13 @@ exports[`<Button /> correct rendering renders with props iconAfter 1`] = `
55
55
 
56
56
  exports[`<Button /> correct rendering renders with props iconAfter and size 1`] = `
57
57
  <button
58
- className="btn btn-primary btn-md"
58
+ className="btn btn-primary btn-sm"
59
59
  disabled={false}
60
60
  type="button"
61
61
  >
62
62
  Button
63
63
  <span
64
- className="pgn__icon pgn__icon__md btn-icon-after"
64
+ className="pgn__icon pgn__icon__sm btn-icon-after"
65
65
  >
66
66
  <svg
67
67
  aria-hidden={true}
@@ -197,3 +197,20 @@ exports[`<Button /> correct rendering renders without props 1`] = `
197
197
  Button
198
198
  </button>
199
199
  `;
200
+
201
+ exports[`<Button /> correct rendering with size="inline" 1`] = `
202
+ <p>
203
+ <span
204
+ className="mr-1"
205
+ >
206
+ 2 items selected.
207
+ </span>
208
+ <button
209
+ className="btn btn-link btn-inline"
210
+ disabled={false}
211
+ type="button"
212
+ >
213
+ Clear
214
+ </button>
215
+ </p>
216
+ `;
@@ -1,31 +1,55 @@
1
1
  import React from 'react';
2
- import PropTypes from 'prop-types';
2
+ import PropTypes, { type Requireable } from 'prop-types';
3
3
  import classNames from 'classnames';
4
- import BaseButton from 'react-bootstrap/Button';
5
- import BaseButtonGroup from 'react-bootstrap/ButtonGroup';
6
- import BaseButtonToolbar from 'react-bootstrap/ButtonToolbar';
4
+ import BaseButton, { type ButtonProps as BaseButtonProps } from 'react-bootstrap/Button';
5
+ import BaseButtonGroup, { type ButtonGroupProps as BaseButtonGroupProps } from 'react-bootstrap/ButtonGroup';
6
+ import BaseButtonToolbar, { type ButtonToolbarProps } from 'react-bootstrap/ButtonToolbar';
7
+ import type { ComponentWithAsProp } from '../utils/types/bootstrap';
7
8
 
8
9
  import Icon from '../Icon';
9
10
 
10
- const Button = React.forwardRef(({
11
+ interface ButtonProps extends Omit<BaseButtonProps, 'size'> {
12
+ /**
13
+ * An icon component to render. Example:
14
+ * ```
15
+ * import { Close } from '@openedx/paragon/icons';
16
+ * <Button iconBefore={Close}>Close</Button>
17
+ * ```
18
+ */
19
+ iconBefore?: React.ComponentType;
20
+ /**
21
+ * An icon component to render. Example:
22
+ * ```
23
+ * import { Close } from '@openedx/paragon/icons';
24
+ * <Button iconAfter={Close}>Close</Button>
25
+ * ```
26
+ */
27
+ iconAfter?: React.ComponentType;
28
+ size?: 'sm' | 'md' | 'lg' | 'inline';
29
+ }
30
+
31
+ type ButtonType = ComponentWithAsProp<'button', ButtonProps> & { Deprecated?: any };
32
+
33
+ const Button: ButtonType = React.forwardRef<HTMLButtonElement, ButtonProps>(({
11
34
  children,
12
35
  iconAfter,
13
36
  iconBefore,
37
+ size,
14
38
  ...props
15
39
  }, ref) => (
16
40
  <BaseButton
41
+ size={size as 'sm' | 'lg' | undefined} // Bootstrap's <Button> types do not allow 'md' or 'inline', but we do.
17
42
  {...props}
18
43
  className={classNames(props.className)}
19
44
  ref={ref}
20
45
  >
21
- {iconBefore && <Icon className="btn-icon-before" size={props.size} src={iconBefore} />}
46
+ {iconBefore && <Icon className="btn-icon-before" size={size} src={iconBefore} />}
22
47
  {children}
23
- {iconAfter && <Icon className="btn-icon-after" size={props.size} src={iconAfter} />}
48
+ {iconAfter && <Icon className="btn-icon-after" size={size} src={iconAfter} />}
24
49
  </BaseButton>
25
50
  ));
26
51
 
27
52
  Button.propTypes = {
28
- ...Button.propTypes,
29
53
  /** Specifies class name to apply to the button */
30
54
  className: PropTypes.string,
31
55
  /** Disables the Button, preventing mouse events, even if the underlying component is an `<a>` element */
@@ -51,10 +75,13 @@ Button.propTypes = {
51
75
  variant: PropTypes.string,
52
76
  /** An icon component to render.
53
77
  * Example import of a Paragon icon component: `import { Check } from '@openedx/paragon/icons';` */
54
- iconBefore: PropTypes.oneOfType([PropTypes.elementType, PropTypes.node]),
78
+ iconBefore: PropTypes.elementType as Requireable<React.ComponentType>,
55
79
  /** An icon component to render.
56
80
  * Example import of a Paragon icon component: `import { Check } from '@openedx/paragon/icons';` */
57
- iconAfter: PropTypes.oneOfType([PropTypes.elementType, PropTypes.node]),
81
+ iconAfter: PropTypes.elementType as Requireable<React.ComponentType>,
82
+ // The 'as' type casting above is required for TypeScript checking, because the 'PropTypes.elementType' type normally
83
+ // allows strings as a value (for use cases like 'div') but we don't support that for <Icon />/iconBefore/iconAfter.
84
+ // The React TypeScript type definitions are more specific (React.ComponentType vs React.ElementType).
58
85
  };
59
86
 
60
87
  Button.defaultProps = {
@@ -66,20 +93,29 @@ Button.defaultProps = {
66
93
  disabled: false,
67
94
  };
68
95
 
69
- function ButtonGroup(props) {
70
- return <BaseButtonGroup {...props} />;
71
- }
72
- function ButtonToolbar(props) {
73
- return <BaseButtonToolbar {...props} />;
96
+ // We could just re-export 'ButtonGroup' and 'ButtonToolbar', but we currently
97
+ // override them to add propTypes validation at runtime, since most Paragon
98
+ // consumers aren't using TypeScript yet. We also force ButtonGroup's 'size'
99
+ // prop to accept our custom values of 'md' and 'inline' which are used in
100
+ // Paragon but not used in the base Bootstrap classes.
101
+
102
+ interface ButtonGroupProps extends Omit<BaseButtonGroupProps, 'size'> {
103
+ size?: 'sm' | 'md' | 'lg' | 'inline';
74
104
  }
75
105
 
106
+ const ButtonGroup: ComponentWithAsProp<'div', ButtonGroupProps> = (
107
+ React.forwardRef<HTMLButtonElement, ButtonGroupProps>(({ size, ...props }, ref) => (
108
+ <BaseButtonGroup size={size as 'sm' | 'lg'} {...props} ref={ref} />
109
+ ))
110
+ );
111
+
76
112
  ButtonGroup.propTypes = {
77
113
  /** Specifies element type for this component. */
78
114
  as: PropTypes.elementType,
79
115
  /** An ARIA role describing the button group. */
80
116
  role: PropTypes.string,
81
117
  /** Specifies the size for all Buttons in the group. */
82
- size: PropTypes.oneOf(['sm', 'md', 'lg']),
118
+ size: PropTypes.oneOf(['sm', 'md', 'lg', 'inline']),
83
119
  /** Display as a button toggle group. */
84
120
  toggle: PropTypes.bool,
85
121
  /** Specifies if the set of Buttons should appear vertically stacked. */
@@ -97,6 +133,12 @@ ButtonGroup.defaultProps = {
97
133
  size: 'md',
98
134
  };
99
135
 
136
+ const ButtonToolbar: ComponentWithAsProp<'div', ButtonToolbarProps> = (
137
+ React.forwardRef<HTMLButtonElement, ButtonToolbarProps>((props, ref) => (
138
+ <BaseButtonToolbar {...props} ref={ref} />
139
+ ))
140
+ );
141
+
100
142
  ButtonToolbar.propTypes = {
101
143
  /** An ARIA role describing the button group. */
102
144
  role: PropTypes.string,
@@ -7,7 +7,7 @@ import { Close } from '../../icons';
7
7
  import { STYLE_VARIANTS } from './constants';
8
8
  import Chip from '.';
9
9
 
10
- function TestChip(props) {
10
+ function TestChip(props: Omit<React.ComponentProps<typeof Chip>, 'children'>) {
11
11
  return (
12
12
  <Chip {...props}>
13
13
  Test
@@ -42,15 +42,13 @@ describe('<Chip />', () => {
42
42
  iconBeforeAlt="close icon"
43
43
  iconAfter={Close}
44
44
  iconAfterAlt="close icon"
45
- >
46
- Chip
47
- </TestChip>
45
+ />
48
46
  )).toJSON();
49
47
  expect(tree).toMatchSnapshot();
50
48
  });
51
49
  it('renders div with "button" role when onClick is provided', () => {
52
50
  const tree = renderer.create((
53
- <TestChip onClick={jest.fn}>Chip</TestChip>
51
+ <TestChip onClick={jest.fn} />
54
52
  )).toJSON();
55
53
  expect(tree).toMatchSnapshot();
56
54
  });
@@ -104,7 +102,7 @@ describe('<Chip />', () => {
104
102
  />,
105
103
  );
106
104
  const iconAfter = screen.getByLabelText('icon-after');
107
- await userEvent.click(iconAfter, '{enter}', { skipClick: true });
105
+ await userEvent.click(iconAfter);
108
106
  expect(func).toHaveBeenCalledTimes(1);
109
107
  });
110
108
  it('onIconBeforeClick is triggered', async () => {
@@ -130,7 +128,7 @@ describe('<Chip />', () => {
130
128
  />,
131
129
  );
132
130
  const iconBefore = screen.getByLabelText('icon-before');
133
- await userEvent.click(iconBefore, '{enter}', { skipClick: true });
131
+ await userEvent.click(iconBefore);
134
132
  expect(func).toHaveBeenCalledTimes(1);
135
133
  });
136
134
  it('checks the absence of the `selected` class in the chip', async () => {
@@ -1,19 +1,19 @@
1
1
  import React, { KeyboardEventHandler, MouseEventHandler } from 'react';
2
2
  import PropTypes from 'prop-types';
3
3
  import Icon from '../Icon';
4
- // @ts-ignore
5
4
  import IconButton from '../IconButton';
6
- // @ts-ignore
7
5
  import { STYLE_VARIANTS } from './constants';
8
6
 
9
- export interface ChipIconProps {
7
+ export type ChipIconProps = {
10
8
  className: string,
11
- src: React.ReactElement | Function,
12
- onClick?: KeyboardEventHandler & MouseEventHandler,
13
- alt?: string,
14
- variant: string,
9
+ src: React.ComponentType,
10
+ variant: typeof STYLE_VARIANTS[keyof typeof STYLE_VARIANTS],
15
11
  disabled?: boolean,
16
- }
12
+ } & (
13
+ // Either _both_ onClick and alt are provided, or neither is:
14
+ | { onClick: KeyboardEventHandler<HTMLButtonElement> & MouseEventHandler<HTMLButtonElement>, alt: string }
15
+ | { onClick?: undefined, alt?: undefined }
16
+ );
17
17
 
18
18
  function ChipIcon({
19
19
  className, src, onClick, alt, variant, disabled,
@@ -2,4 +2,4 @@
2
2
  export const STYLE_VARIANTS = {
3
3
  DARK: 'dark',
4
4
  LIGHT: 'light',
5
- };
5
+ } as const;
@@ -1,11 +1,9 @@
1
1
  import React, { ForwardedRef, KeyboardEventHandler, MouseEventHandler } from 'react';
2
- import PropTypes from 'prop-types';
2
+ import PropTypes, { type Requireable } from 'prop-types';
3
3
  import classNames from 'classnames';
4
4
  // @ts-ignore
5
5
  import { requiredWhen } from '../utils/propTypes';
6
- // @ts-ignore
7
6
  import { STYLE_VARIANTS } from './constants';
8
- // @ts-ignore
9
7
  import ChipIcon from './ChipIcon';
10
8
 
11
9
  export const CHIP_PGN_CLASS = 'pgn__chip';
@@ -14,10 +12,10 @@ export interface IChip {
14
12
  children: React.ReactNode,
15
13
  onClick?: KeyboardEventHandler & MouseEventHandler,
16
14
  className?: string,
17
- variant?: string,
18
- iconBefore?: React.ReactElement | Function,
15
+ variant?: typeof STYLE_VARIANTS[keyof typeof STYLE_VARIANTS],
16
+ iconBefore?: React.ComponentType,
19
17
  iconBeforeAlt?: string,
20
- iconAfter?: React.ReactElement | Function,
18
+ iconAfter?: React.ComponentType,
21
19
  iconAfterAlt?: string,
22
20
  onIconBeforeClick?: KeyboardEventHandler & MouseEventHandler,
23
21
  onIconAfterClick?: KeyboardEventHandler & MouseEventHandler,
@@ -111,7 +109,7 @@ Chip.propTypes = {
111
109
  *
112
110
  * `import { Check } from '@openedx/paragon/icons';`
113
111
  */
114
- iconBefore: PropTypes.oneOfType([PropTypes.element, PropTypes.func]),
112
+ iconBefore: PropTypes.elementType as Requireable<React.ComponentType>,
115
113
  /** Specifies icon alt text. */
116
114
  iconBeforeAlt: requiredWhen(PropTypes.string, ['iconBefore', 'onIconBeforeClick']),
117
115
  /** A click handler for the `Chip` icon before. */
@@ -122,7 +120,7 @@ Chip.propTypes = {
122
120
  *
123
121
  * `import { Check } from '@openedx/paragon/icons';`
124
122
  */
125
- iconAfter: PropTypes.oneOfType([PropTypes.element, PropTypes.func]),
123
+ iconAfter: PropTypes.elementType as Requireable<React.ComponentType>,
126
124
  /** Specifies icon alt text. */
127
125
  iconAfterAlt: requiredWhen(PropTypes.string, ['iconAfter', 'onIconAfterClick']),
128
126
  /** A click handler for the `Chip` icon after. */
@@ -4,9 +4,7 @@ import { useIntl } from 'react-intl';
4
4
  import classNames from 'classnames';
5
5
  // @ts-ignore
6
6
  import { OverflowScroll, OverflowScrollContext } from '../OverflowScroll';
7
- // @ts-ignore
8
7
  import IconButton from '../IconButton';
9
- // @ts-ignore
10
8
  import Icon from '../Icon';
11
9
  // @ts-ignore
12
10
  import { ArrowForward, ArrowBack } from '../../icons';
@@ -4,30 +4,34 @@ import userEvent from '@testing-library/user-event';
4
4
 
5
5
  import Hyperlink from '.';
6
6
 
7
- const content = 'content';
8
7
  const destination = 'destination';
8
+ const content = 'content';
9
9
  const onClick = jest.fn();
10
10
  const props = {
11
- content,
12
11
  destination,
13
12
  onClick,
14
13
  };
15
14
  const externalLinkAlternativeText = 'externalLinkAlternativeText';
16
15
  const externalLinkTitle = 'externalLinkTitle';
17
16
  const externalLinkProps = {
18
- target: '_blank',
17
+ target: '_blank' as const,
19
18
  externalLinkAlternativeText,
20
19
  externalLinkTitle,
21
20
  ...props,
22
21
  };
23
22
 
24
23
  describe('correct rendering', () => {
24
+ beforeEach(() => {
25
+ onClick.mockClear();
26
+ });
27
+
25
28
  it('renders Hyperlink', async () => {
26
- const { getByRole } = render(<Hyperlink {...props} />);
29
+ const { getByRole } = render(<Hyperlink {...props}>{content}</Hyperlink>);
27
30
  const wrapper = getByRole('link');
28
31
  expect(wrapper).toBeInTheDocument();
29
32
 
30
33
  expect(wrapper).toHaveClass('pgn__hyperlink');
34
+ expect(wrapper).toHaveClass('standalone-link');
31
35
  expect(wrapper).toHaveTextContent(content);
32
36
  expect(wrapper).toHaveAttribute('href', destination);
33
37
  expect(wrapper).toHaveAttribute('target', '_self');
@@ -36,8 +40,17 @@ describe('correct rendering', () => {
36
40
  expect(onClick).toHaveBeenCalledTimes(1);
37
41
  });
38
42
 
43
+ it('renders an underlined Hyperlink', async () => {
44
+ const { getByRole } = render(<Hyperlink isInline {...props}>{content}</Hyperlink>);
45
+ const wrapper = getByRole('link');
46
+ expect(wrapper).toBeInTheDocument();
47
+ expect(wrapper).toHaveClass('pgn__hyperlink');
48
+ expect(wrapper).not.toHaveClass('standalone-link');
49
+ expect(wrapper).toHaveClass('inline-link');
50
+ });
51
+
39
52
  it('renders external Hyperlink', () => {
40
- const { getByRole, getByTestId } = render(<Hyperlink {...externalLinkProps} />);
53
+ const { getByRole, getByTestId } = render(<Hyperlink {...externalLinkProps}>{content}</Hyperlink>);
41
54
  const wrapper = getByRole('link');
42
55
  const icon = getByTestId('hyperlink-icon');
43
56
  const iconSvg = icon.querySelector('svg');
@@ -53,18 +66,16 @@ describe('correct rendering', () => {
53
66
 
54
67
  describe('security', () => {
55
68
  it('prevents reverse tabnabbing for links with target="_blank"', () => {
56
- const { getByRole } = render(<Hyperlink {...externalLinkProps} />);
69
+ const { getByRole } = render(<Hyperlink {...externalLinkProps}>{content}</Hyperlink>);
57
70
  const wrapper = getByRole('link');
58
71
  expect(wrapper).toHaveAttribute('rel', 'noopener noreferrer');
59
72
  });
60
73
  });
61
74
 
62
75
  describe('event handlers are triggered correctly', () => {
63
- let spy;
64
- beforeEach(() => { spy = jest.fn(); });
65
-
66
76
  it('should fire onClick', async () => {
67
- const { getByRole } = render(<Hyperlink {...props} onClick={spy} />);
77
+ const spy = jest.fn();
78
+ const { getByRole } = render(<Hyperlink {...props} onClick={spy}>{content}</Hyperlink>);
68
79
  const wrapper = getByRole('link');
69
80
  expect(spy).toHaveBeenCalledTimes(0);
70
81
  await userEvent.click(wrapper);