@aarhus-university/au-lib-react-components 10.3.3 → 10.6.0

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 (113) hide show
  1. package/.eslintrc.js +34 -44
  2. package/.storybook/main.js +8 -0
  3. package/{types/common/payloads.d.ts → .storybook/preview-head.html} +0 -0
  4. package/.storybook/preview.js +11 -0
  5. package/README.md +19 -19
  6. package/__tests__/jest/AUButtonComponent.test.tsx +163 -0
  7. package/__tests__/jest/AUSpinnerComponent.test.tsx +57 -0
  8. package/__tests__/jest/helpers.test.ts +15 -0
  9. package/__tests__/jest/setupTests.ts +2 -0
  10. package/babel.config.js +8 -7
  11. package/build/umd/all.css +2 -2
  12. package/build/umd/all.js +1 -1
  13. package/build/umd/alphabox.js +1 -1
  14. package/build/umd/alphabox.js.map +1 -1
  15. package/build/umd/databox.js +1 -1
  16. package/build/umd/databox.js.map +1 -1
  17. package/build/umd/diagramme.js +1 -1
  18. package/build/umd/diagramme.js.map +1 -1
  19. package/build/umd/flowbox.js +1 -1
  20. package/build/umd/flowbox.js.map +1 -1
  21. package/build/umd/universe.js +1 -1
  22. package/build/umd/universe.js.map +1 -1
  23. package/package.json +107 -66
  24. package/src/components/AUAlertComponent.tsx +70 -70
  25. package/src/components/AUAutoSuggestComponent.js +158 -158
  26. package/src/components/AUButtonComponent.tsx +77 -0
  27. package/src/components/AUCalendarComponent.tsx +493 -493
  28. package/src/components/AUContentToggleComponent.tsx +33 -33
  29. package/src/components/AUDatepickerComponent.tsx +117 -117
  30. package/src/components/AUMobilePrefixComponent.tsx +15 -15
  31. package/src/components/AUModalComponent.tsx +80 -80
  32. package/src/components/AUReceiptComponent.tsx +33 -33
  33. package/src/components/AUSpinnerComponent.tsx +33 -91
  34. package/src/components/AUSubNavComponent.tsx +48 -48
  35. package/src/components/AUSubmitButtonContainerComponent.tsx +31 -31
  36. package/src/components/AUTabbedContentComponent.tsx +145 -145
  37. package/src/components/AUTableComponent.tsx +24 -24
  38. package/src/components/AUToastComponent.tsx +103 -103
  39. package/src/components/AUToolbarComponent.tsx +45 -45
  40. package/src/components/profile/AUProfileActions.js +128 -128
  41. package/src/components/profile/AUProfileAvatarComponent.js +83 -83
  42. package/src/components/profile/AUProfileAvatarV2Component.js +91 -91
  43. package/src/components/profile/AUProfileAvatarV3Component.tsx +42 -42
  44. package/src/components/profile/AUProfileContainerComponent.js +283 -283
  45. package/src/components/profile/AUProfileHooks.js +30 -30
  46. package/src/components/profile/AUProfileItemComponent.js +54 -54
  47. package/src/components/profile/AUProfileLanguageComponent.js +131 -131
  48. package/src/components/profile/AUProfileLoginComponent.tsx +26 -26
  49. package/src/components/profile/AUProfileMailComponent.js +307 -307
  50. package/src/components/profile/AUProfileMobileComponent.js +164 -164
  51. package/src/components/profile/AUProfileNameComponent.js +253 -253
  52. package/src/components/profile/AUProfileNextOfKinComponent.js +216 -216
  53. package/src/components/profile/AUProfileReducer.js +230 -230
  54. package/src/components/profile/AUProfileWidgetComponent.js +95 -95
  55. package/src/components/profile/AUProfileWidgetV2Component.js +116 -116
  56. package/src/components/profile/AUProfileWidgetV3Component.tsx +122 -122
  57. package/src/components/wrapping/AUEmbedComponent.js +47 -47
  58. package/src/layout-2016/components/alphabox/AlphaBoxComponent.js +143 -143
  59. package/src/layout-2016/components/alphabox/AlphaBoxContentComponent.js +136 -136
  60. package/src/layout-2016/components/common/AUCollapsibleComponent.js +152 -152
  61. package/src/layout-2016/components/common/AUSpinnerComponent.js +103 -103
  62. package/src/layout-2016/components/databox/DataBoxAlphabetComponent.js +144 -144
  63. package/src/layout-2016/components/databox/DataBoxAssociationComponent.js +122 -122
  64. package/src/layout-2016/components/databox/DataBoxButtonComponent.js +157 -157
  65. package/src/layout-2016/components/databox/DataBoxComponent.js +297 -297
  66. package/src/layout-2016/components/databox/DataBoxGroupingComponent.js +64 -64
  67. package/src/layout-2016/components/databox/DataBoxSearchResultComponent.js +36 -36
  68. package/src/layout-2016/components/databox/DataBoxStackedAssociationComponent.js +54 -54
  69. package/src/layout-2016/components/databox/DataBoxSuggestionComponent.js +39 -39
  70. package/src/layout-2016/components/diagramme/AUDiagrammeComponent.js +309 -309
  71. package/src/layout-2016/components/flowbox/FlowBoxComponent.js +126 -126
  72. package/src/layout-2016/components/flowbox/FlowBoxPhoneComponent.js +104 -104
  73. package/src/layout-2016/components/profile/AUProfileAvatar2016Component.js +103 -103
  74. package/src/layout-2016/components/universe/StaffTopComponent.js +363 -363
  75. package/src/layout-2016/components/universe/StudentTopComponent.js +137 -137
  76. package/src/layout-2016/components/universe/UniverseContainerComponent.js +65 -65
  77. package/src/layout-2016/lib/all.js +3 -3
  78. package/src/layout-2016/lib/au-alphabox.js +100 -100
  79. package/src/layout-2016/lib/au-databox.js +400 -400
  80. package/src/layout-2016/lib/au-diagramme.js +85 -85
  81. package/src/layout-2016/lib/au-flowbox.js +93 -93
  82. package/src/layout-2016/lib/universe.js +9 -9
  83. package/src/lib/helpers.ts +194 -194
  84. package/src/lib/hooks.ts +33 -33
  85. package/src/lib/i18n.ts +600 -600
  86. package/src/lib/tracking.ts +69 -69
  87. package/src/lib/wrapping.ts +21 -21
  88. package/src/styles/_settings.scss +10 -10
  89. package/src/styles/alphabox.scss +222 -222
  90. package/src/styles/app.scss +7 -7
  91. package/src/styles/autosuggest.scss +57 -57
  92. package/src/styles/databox.scss +563 -563
  93. package/src/styles/diagramme.scss +119 -119
  94. package/src/styles/flowbox.scss +72 -72
  95. package/src/styles/maps.scss +395 -395
  96. package/stories/AUButtonComponent.stories.tsx +86 -0
  97. package/stories/AUSpinnerComponent.stories.tsx +41 -0
  98. package/stories/assets/code-brackets.svg +1 -0
  99. package/stories/assets/colors.svg +1 -0
  100. package/stories/assets/comments.svg +1 -0
  101. package/stories/assets/direction.svg +1 -0
  102. package/stories/assets/flow.svg +1 -0
  103. package/stories/assets/plugin.svg +1 -0
  104. package/stories/assets/repo.svg +1 -0
  105. package/stories/assets/stackalt.svg +1 -0
  106. package/tsconfig.json +46 -46
  107. package/webpack.config.js +89 -89
  108. package/types/common/delphinus-duplicates.d.ts +0 -40
  109. package/types/common/interfaces/gui.d.ts +0 -52
  110. package/types/common/interfaces/model.d.ts +0 -29
  111. package/types/common/main.d.ts +0 -6
  112. package/types/common/package.json +0 -5
  113. package/types/common/props.d.ts +0 -166
package/package.json CHANGED
@@ -1,66 +1,107 @@
1
- {
2
- "sideEffects": false,
3
- "name": "@aarhus-university/au-lib-react-components",
4
- "version": "10.3.3",
5
- "description": "Library for shared React components for various applications on au.dk",
6
- "scripts": {
7
- "build": "webpack --config ./webpack.config.js"
8
- },
9
- "repository": {
10
- "type": "git",
11
- "url": "https://gitlab.au.dk/au-web-og-digital/au-lib-react-components"
12
- },
13
- "author": {
14
- "name": "Thomas Riis Hansen",
15
- "email": "trh@au.dk"
16
- },
17
- "devDependencies": {
18
- "@babel/core": "^7.17.0",
19
- "@babel/plugin-proposal-class-properties": "^7.16.7",
20
- "@babel/plugin-proposal-object-rest-spread": "^7.16.7",
21
- "@babel/plugin-proposal-optional-chaining": "^7.16.7",
22
- "@babel/preset-env": "^7.16.11",
23
- "@babel/preset-react": "^7.16.7",
24
- "@babel/preset-typescript": "^7.16.7",
25
- "@typescript-eslint/eslint-plugin": "^5.10.2",
26
- "@typescript-eslint/parser": "^5.10.2",
27
- "babel-eslint": "^10.1.0",
28
- "babel-loader": "^8.2.3",
29
- "css-loader": "^6.6.0",
30
- "eslint": "^8.8.0",
31
- "eslint-config-airbnb": "^19.0.4",
32
- "eslint-import-resolver-typescript": "^2.5.0",
33
- "eslint-plugin-import": "^2.25.4",
34
- "eslint-plugin-jsx-a11y": "^6.5.1",
35
- "eslint-plugin-react": "^7.28.0",
36
- "eslint-plugin-react-hooks": "^4.3.0",
37
- "eslint-webpack-plugin": "^3.1.1",
38
- "fork-ts-checker-webpack-plugin": "^7.0.0",
39
- "mini-css-extract-plugin": "^2.5.3",
40
- "sass": "^1.49.7",
41
- "sass-loader": "^12.4.0",
42
- "style-loader": "^3.3.1",
43
- "ts-loader": "^9.2.6",
44
- "typescript": "^4.5.5",
45
- "webpack-cli": "^4.9.2"
46
- },
47
- "dependencies": {
48
- "@aarhus-university/au-designsystem-delphinus": "^0.26.8",
49
- "@types/google.analytics": "^0.0.42",
50
- "@types/history": "^5.0.0",
51
- "@types/react": "^18.0.14",
52
- "@types/react-dom": "^18.0.5",
53
- "@types/react-router-dom": "^5.3.3",
54
- "dayjs": "^1.10.7",
55
- "lodash.debounce": "^4.0.8",
56
- "prop-types": "^15.8.1",
57
- "query-string": "^7.1.0",
58
- "react-autosuggest": "^10.1.0",
59
- "react-router-dom": "^6.2.1",
60
- "webpack": "^5.68.0"
61
- },
62
- "peerDependencies": {
63
- "react": "18.2.0",
64
- "react-dom": "^18.2.0"
65
- }
66
- }
1
+ {
2
+ "sideEffects": false,
3
+ "name": "@aarhus-university/au-lib-react-components",
4
+ "version": "10.6.0",
5
+ "description": "Library for shared React components for various applications on au.dk",
6
+ "scripts": {
7
+ "test": "jest",
8
+ "build": "jest && webpack --config ./webpack.config.js",
9
+ "storybook": "start-storybook -p 6006",
10
+ "build-storybook": "build-storybook"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://gitlab.au.dk/au-web-og-digital/au-lib-react-components"
15
+ },
16
+ "author": {
17
+ "name": "Thomas Riis Hansen",
18
+ "email": "trh@au.dk"
19
+ },
20
+ "devDependencies": {
21
+ "@babel/core": "^7.17.0",
22
+ "@babel/plugin-proposal-class-properties": "^7.16.7",
23
+ "@babel/plugin-proposal-object-rest-spread": "^7.16.7",
24
+ "@babel/plugin-proposal-optional-chaining": "^7.16.7",
25
+ "@babel/preset-env": "^7.16.11",
26
+ "@babel/preset-react": "^7.16.7",
27
+ "@babel/preset-typescript": "^7.16.7",
28
+ "@mdx-js/react": "^2.1.2",
29
+ "@storybook/addon-actions": "^6.5.4",
30
+ "@storybook/addon-essentials": "^6.5.4",
31
+ "@storybook/addon-interactions": "^6.5.4",
32
+ "@storybook/addon-links": "^6.5.4",
33
+ "@storybook/builder-webpack4": "^6.5.4",
34
+ "@storybook/builder-webpack5": "^6.5.4",
35
+ "@storybook/manager-webpack4": "^6.5.4",
36
+ "@storybook/manager-webpack5": "^6.5.4",
37
+ "@storybook/react": "^6.5.4",
38
+ "@storybook/testing-library": "^0.0.11",
39
+ "@testing-library/jest-dom": "5.0.0",
40
+ "@testing-library/react": "13.0.0",
41
+ "@testing-library/user-event": "14.0.0",
42
+ "@types/jest": "^27.4.0",
43
+ "@types/testing-library__jest-dom": "5.0.0",
44
+ "@typescript-eslint/eslint-plugin": "^5.10.2",
45
+ "@typescript-eslint/parser": "^5.10.2",
46
+ "babel-eslint": "^10.1.0",
47
+ "babel-jest": "^27.5.1",
48
+ "babel-loader": "^8.2.3",
49
+ "css-loader": "^6.6.0",
50
+ "eslint": "^8.8.0",
51
+ "eslint-config-airbnb": "^19.0.4",
52
+ "eslint-import-resolver-typescript": "^2.5.0",
53
+ "eslint-plugin-import": "^2.25.4",
54
+ "eslint-plugin-jsx-a11y": "^6.5.1",
55
+ "eslint-plugin-react": "^7.28.0",
56
+ "eslint-plugin-react-hooks": "^4.3.0",
57
+ "eslint-plugin-storybook": "^0.5.12",
58
+ "eslint-webpack-plugin": "^3.1.1",
59
+ "fork-ts-checker-webpack-plugin": "^7.0.0",
60
+ "jest": "^27.5.1",
61
+ "mini-css-extract-plugin": "^2.5.3",
62
+ "react-test-renderer": "18.1.0",
63
+ "sass": "^1.49.7",
64
+ "sass-loader": "^12.4.0",
65
+ "style-loader": "^3.3.1",
66
+ "ts-loader": "^9.2.6",
67
+ "typescript": "^4.5.5",
68
+ "webpack-cli": "^4.9.2"
69
+ },
70
+ "dependencies": {
71
+ "@aarhus-university/au-designsystem-delphinus": "0.28.0",
72
+ "@aarhus-university/types": "^0.2.0",
73
+ "@types/google.analytics": "^0.0.42",
74
+ "@types/history": "^5.0.0",
75
+ "@types/react": "^18.0.14",
76
+ "@types/react-dom": "^18.0.5",
77
+ "@types/react-router-dom": "^5.3.3",
78
+ "dayjs": "^1.10.7",
79
+ "lodash.debounce": "^4.0.8",
80
+ "prop-types": "^15.8.1",
81
+ "query-string": "^7.1.0",
82
+ "react": "18.2.0",
83
+ "react-autosuggest": "^10.1.0",
84
+ "react-router-dom": "^6.2.1",
85
+ "regenerator-runtime": "^0.13.9",
86
+ "webpack": "^5.68.0",
87
+ "whatwg-fetch": "^3.6.2"
88
+ },
89
+ "peerDependencies": {
90
+ "react": "18.2.0",
91
+ "react-dom": "^18.2.0"
92
+ },
93
+ "jest": {
94
+ "rootDir": "__tests__/jest",
95
+ "setupFilesAfterEnv": [
96
+ "./setupTests.ts"
97
+ ],
98
+ "testEnvironment": "jsdom",
99
+ "transformIgnorePatterns": [
100
+ "node_modules/(?!(@aarhus-university/au-lib-react-components|@aarhus-university/au-designsystem-delphinus|node-fetch|data-uri-to-buffer|fetch-blob|formdata-polyfill)/)"
101
+ ],
102
+ "testPathIgnorePatterns": [
103
+ "factory.ts",
104
+ "setupTests.ts"
105
+ ]
106
+ }
107
+ }
@@ -1,70 +1,70 @@
1
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
- /* eslint-env browser */
3
- import React, { FC } from 'react';
4
- import ReactDOM from 'react-dom';
5
-
6
- const AUAlertComponent: FC<AUAlertComponentProps> = ({
7
- message,
8
- children,
9
- alert,
10
- buttons,
11
- }: AUAlertComponentProps) => {
12
- const renderButtons = (buttons || []).map((button) => (
13
- <button
14
- key={button.text}
15
- type="button"
16
- className={button.className}
17
- data-icon={button.dataIcon}
18
- onClick={() => {
19
- button.onClick();
20
- }}
21
- >
22
- {button.text}
23
- </button>
24
- ));
25
-
26
- if (alert) {
27
- return (
28
- <>
29
- {
30
- ReactDOM.createPortal(
31
- <div key="0" className="theme--normal toast-notification toast-notification--attention toast-notification--persistent">
32
- <div className="toast-notification__content">
33
- <p dangerouslySetInnerHTML={{ __html: message }} />
34
- </div>
35
- {renderButtons}
36
- </div>,
37
- document.querySelector('body')!,
38
- )
39
- }
40
- {children}
41
- </>
42
- );
43
- }
44
-
45
- if (!children) {
46
- return null;
47
- }
48
-
49
- return children;
50
- };
51
-
52
- AUAlertComponent.defaultProps = {
53
- buttons: [{
54
- className: 'button',
55
- text: 'OK',
56
- dataIcon: null,
57
- // eslint-disable-next-line no-console
58
- onClick: () => console.log('clicked on OK'),
59
- }, {
60
- className: 'button',
61
- text: 'Annuller',
62
- dataIcon: null,
63
- // eslint-disable-next-line no-console
64
- onClick: () => console.log('clicked on cancel'),
65
- },
66
- ],
67
- };
68
-
69
- AUAlertComponent.displayName = 'AUAlertComponent';
70
- export default AUAlertComponent;
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
+ /* eslint-env browser */
3
+ import React, { FC } from 'react';
4
+ import ReactDOM from 'react-dom';
5
+
6
+ const AUAlertComponent: FC<AUAlertComponentProps> = ({
7
+ message,
8
+ children,
9
+ alert,
10
+ buttons,
11
+ }: AUAlertComponentProps) => {
12
+ const renderButtons = (buttons || []).map((button) => (
13
+ <button
14
+ key={button.text}
15
+ type="button"
16
+ className={button.className}
17
+ data-icon={button.dataIcon}
18
+ onClick={() => {
19
+ button.onClick();
20
+ }}
21
+ >
22
+ {button.text}
23
+ </button>
24
+ ));
25
+
26
+ if (alert) {
27
+ return (
28
+ <>
29
+ {
30
+ ReactDOM.createPortal(
31
+ <div key="0" className="theme--normal toast-notification toast-notification--attention toast-notification--persistent">
32
+ <div className="toast-notification__content">
33
+ <p dangerouslySetInnerHTML={{ __html: message }} />
34
+ </div>
35
+ {renderButtons}
36
+ </div>,
37
+ document.querySelector('body')!,
38
+ )
39
+ }
40
+ {children}
41
+ </>
42
+ );
43
+ }
44
+
45
+ if (!children) {
46
+ return null;
47
+ }
48
+
49
+ return children;
50
+ };
51
+
52
+ AUAlertComponent.defaultProps = {
53
+ buttons: [{
54
+ className: 'button',
55
+ text: 'OK',
56
+ dataIcon: null,
57
+ // eslint-disable-next-line no-console
58
+ onClick: () => console.log('clicked on OK'),
59
+ }, {
60
+ className: 'button',
61
+ text: 'Annuller',
62
+ dataIcon: null,
63
+ // eslint-disable-next-line no-console
64
+ onClick: () => console.log('clicked on cancel'),
65
+ },
66
+ ],
67
+ };
68
+
69
+ AUAlertComponent.displayName = 'AUAlertComponent';
70
+ export default AUAlertComponent;
@@ -1,158 +1,158 @@
1
- /* eslint-disable @typescript-eslint/no-empty-function */
2
- import React from 'react';
3
- import PropTypes from 'prop-types';
4
- import Autosuggest from 'react-autosuggest';
5
- import debounce from 'lodash.debounce';
6
-
7
- class AUAutoSuggestComponent extends React.Component {
8
- constructor(props) {
9
- super(props);
10
- this.state = {
11
- value: '',
12
- suggestions: [],
13
- noSuggestions: false,
14
- };
15
-
16
- this.onChange = this.onChange.bind(this);
17
- this.onEnter = this.onEnter.bind(this);
18
- this.onSuggestionsFetchRequested = debounce(this.onSuggestionsFetchRequested.bind(this), 400);
19
- this.onSuggestionsClearRequested = this.onSuggestionsClearRequested.bind(this);
20
- this.onSuggestionSelected = this.onSuggestionSelected.bind(this);
21
- }
22
-
23
- onChange(_, { newValue }) {
24
- const { setQuery } = this.props;
25
- this.setState({
26
- value: newValue,
27
- }, () => {
28
- setQuery(newValue);
29
- });
30
- }
31
-
32
- onEnter(event) {
33
- const { suggestions } = this.state;
34
- const { setResults } = this.props;
35
- if (event.key === 'Enter') {
36
- setResults(suggestions);
37
- }
38
- }
39
-
40
- onSuggestionsFetchRequested({ value }) {
41
- const { getSuggestions, collection } = this.props;
42
- getSuggestions(value, collection, (suggestions) => {
43
- this.setState({
44
- suggestions,
45
- noSuggestions: suggestions.length === 0 && value.trim() !== '' && value.trim().length > 1,
46
- });
47
- });
48
- }
49
-
50
- onSuggestionsClearRequested() {
51
- this.setState({
52
- suggestions: [],
53
- });
54
- }
55
-
56
- onSuggestionSelected(event, { suggestion }) {
57
- const { setResults, clearInput } = this.props;
58
- setResults([suggestion]);
59
- if (clearInput) {
60
- this.setState({
61
- value: '',
62
- });
63
- }
64
- }
65
-
66
- render() {
67
- const { value, suggestions, noSuggestions } = this.state;
68
- const {
69
- placeholder,
70
- getSuggestionValue,
71
- renderSuggestion,
72
- id,
73
- theme,
74
- type,
75
- initialValue,
76
- disabled,
77
- noResultsText,
78
- } = this.props;
79
- const inputProps = {
80
- placeholder,
81
- value: initialValue || value,
82
- id,
83
- type,
84
- disabled,
85
- 'aria-label': placeholder,
86
- onChange: this.onChange,
87
- onKeyDown: this.onEnter,
88
- };
89
-
90
- return (
91
- <>
92
- <Autosuggest
93
- suggestions={suggestions}
94
- onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
95
- onSuggestionsClearRequested={this.onSuggestionsClearRequested}
96
- onSuggestionSelected={this.onSuggestionSelected}
97
- getSuggestionValue={getSuggestionValue}
98
- renderSuggestion={renderSuggestion}
99
- inputProps={inputProps}
100
- theme={theme}
101
- />
102
- {
103
- (noSuggestions && noResultsText) && (
104
- <p className="no-results">
105
- <em>{noResultsText}</em>
106
- </p>
107
- )
108
- }
109
- </>
110
- );
111
- }
112
- }
113
-
114
- AUAutoSuggestComponent.displayName = 'AUAutoSuggestComponent';
115
-
116
- AUAutoSuggestComponent.defaultProps = {
117
- id: 'autosuggest1',
118
- theme: {
119
- container: 'react-autosuggest__container',
120
- containerOpen: 'react-autosuggest__container--open',
121
- input: 'react-autosuggest__input',
122
- inputOpen: 'react-autosuggest__input--open',
123
- inputFocused: 'react-autosuggest__input--focused',
124
- suggestionsContainer: 'react-autosuggest__suggestions-container',
125
- suggestionsContainerOpen: 'react-autosuggest__suggestions-container--open',
126
- suggestionsList: 'react-autosuggest__suggestions-list',
127
- suggestion: 'react-autosuggest__suggestion',
128
- suggestionFirst: 'react-autosuggest__suggestion--first',
129
- suggestionHighlighted: 'react-autosuggest__suggestion--highlighted',
130
- sectionContainer: 'react-autosuggest__section-container',
131
- sectionContainerFirst: 'react-autosuggest__section-container--first',
132
- sectionTitle: 'react-autosuggest__section-title',
133
- },
134
- clearInput: false,
135
- type: 'text',
136
- initialValue: '',
137
- disabled: false,
138
- setQuery: () => { },
139
- noResultsText: '',
140
- };
141
-
142
- AUAutoSuggestComponent.propTypes = {
143
- setQuery: PropTypes.func,
144
- getSuggestions: PropTypes.func.isRequired,
145
- getSuggestionValue: PropTypes.func.isRequired,
146
- renderSuggestion: PropTypes.func.isRequired,
147
- setResults: PropTypes.func.isRequired,
148
- collection: PropTypes.arrayOf(PropTypes.shape({})).isRequired,
149
- placeholder: PropTypes.string.isRequired,
150
- id: PropTypes.string,
151
- theme: PropTypes.shape({}),
152
- clearInput: PropTypes.bool,
153
- type: PropTypes.string,
154
- initialValue: PropTypes.string,
155
- disabled: PropTypes.bool,
156
- noResultsText: PropTypes.string,
157
- };
158
- export default AUAutoSuggestComponent;
1
+ /* eslint-disable @typescript-eslint/no-empty-function */
2
+ import React from 'react';
3
+ import PropTypes from 'prop-types';
4
+ import Autosuggest from 'react-autosuggest';
5
+ import debounce from 'lodash.debounce';
6
+
7
+ class AUAutoSuggestComponent extends React.Component {
8
+ constructor(props) {
9
+ super(props);
10
+ this.state = {
11
+ value: '',
12
+ suggestions: [],
13
+ noSuggestions: false,
14
+ };
15
+
16
+ this.onChange = this.onChange.bind(this);
17
+ this.onEnter = this.onEnter.bind(this);
18
+ this.onSuggestionsFetchRequested = debounce(this.onSuggestionsFetchRequested.bind(this), 400);
19
+ this.onSuggestionsClearRequested = this.onSuggestionsClearRequested.bind(this);
20
+ this.onSuggestionSelected = this.onSuggestionSelected.bind(this);
21
+ }
22
+
23
+ onChange(_, { newValue }) {
24
+ const { setQuery } = this.props;
25
+ this.setState({
26
+ value: newValue,
27
+ }, () => {
28
+ setQuery(newValue);
29
+ });
30
+ }
31
+
32
+ onEnter(event) {
33
+ const { suggestions } = this.state;
34
+ const { setResults } = this.props;
35
+ if (event.key === 'Enter') {
36
+ setResults(suggestions);
37
+ }
38
+ }
39
+
40
+ onSuggestionsFetchRequested({ value }) {
41
+ const { getSuggestions, collection } = this.props;
42
+ getSuggestions(value, collection, (suggestions) => {
43
+ this.setState({
44
+ suggestions,
45
+ noSuggestions: suggestions.length === 0 && value.trim() !== '' && value.trim().length > 1,
46
+ });
47
+ });
48
+ }
49
+
50
+ onSuggestionsClearRequested() {
51
+ this.setState({
52
+ suggestions: [],
53
+ });
54
+ }
55
+
56
+ onSuggestionSelected(event, { suggestion }) {
57
+ const { setResults, clearInput } = this.props;
58
+ setResults([suggestion]);
59
+ if (clearInput) {
60
+ this.setState({
61
+ value: '',
62
+ });
63
+ }
64
+ }
65
+
66
+ render() {
67
+ const { value, suggestions, noSuggestions } = this.state;
68
+ const {
69
+ placeholder,
70
+ getSuggestionValue,
71
+ renderSuggestion,
72
+ id,
73
+ theme,
74
+ type,
75
+ initialValue,
76
+ disabled,
77
+ noResultsText,
78
+ } = this.props;
79
+ const inputProps = {
80
+ placeholder,
81
+ value: initialValue || value,
82
+ id,
83
+ type,
84
+ disabled,
85
+ 'aria-label': placeholder,
86
+ onChange: this.onChange,
87
+ onKeyDown: this.onEnter,
88
+ };
89
+
90
+ return (
91
+ <>
92
+ <Autosuggest
93
+ suggestions={suggestions}
94
+ onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
95
+ onSuggestionsClearRequested={this.onSuggestionsClearRequested}
96
+ onSuggestionSelected={this.onSuggestionSelected}
97
+ getSuggestionValue={getSuggestionValue}
98
+ renderSuggestion={renderSuggestion}
99
+ inputProps={inputProps}
100
+ theme={theme}
101
+ />
102
+ {
103
+ (noSuggestions && noResultsText) && (
104
+ <p className="no-results">
105
+ <em>{noResultsText}</em>
106
+ </p>
107
+ )
108
+ }
109
+ </>
110
+ );
111
+ }
112
+ }
113
+
114
+ AUAutoSuggestComponent.displayName = 'AUAutoSuggestComponent';
115
+
116
+ AUAutoSuggestComponent.defaultProps = {
117
+ id: 'autosuggest1',
118
+ theme: {
119
+ container: 'react-autosuggest__container',
120
+ containerOpen: 'react-autosuggest__container--open',
121
+ input: 'react-autosuggest__input',
122
+ inputOpen: 'react-autosuggest__input--open',
123
+ inputFocused: 'react-autosuggest__input--focused',
124
+ suggestionsContainer: 'react-autosuggest__suggestions-container',
125
+ suggestionsContainerOpen: 'react-autosuggest__suggestions-container--open',
126
+ suggestionsList: 'react-autosuggest__suggestions-list',
127
+ suggestion: 'react-autosuggest__suggestion',
128
+ suggestionFirst: 'react-autosuggest__suggestion--first',
129
+ suggestionHighlighted: 'react-autosuggest__suggestion--highlighted',
130
+ sectionContainer: 'react-autosuggest__section-container',
131
+ sectionContainerFirst: 'react-autosuggest__section-container--first',
132
+ sectionTitle: 'react-autosuggest__section-title',
133
+ },
134
+ clearInput: false,
135
+ type: 'text',
136
+ initialValue: '',
137
+ disabled: false,
138
+ setQuery: () => { },
139
+ noResultsText: '',
140
+ };
141
+
142
+ AUAutoSuggestComponent.propTypes = {
143
+ setQuery: PropTypes.func,
144
+ getSuggestions: PropTypes.func.isRequired,
145
+ getSuggestionValue: PropTypes.func.isRequired,
146
+ renderSuggestion: PropTypes.func.isRequired,
147
+ setResults: PropTypes.func.isRequired,
148
+ collection: PropTypes.arrayOf(PropTypes.shape({})).isRequired,
149
+ placeholder: PropTypes.string.isRequired,
150
+ id: PropTypes.string,
151
+ theme: PropTypes.shape({}),
152
+ clearInput: PropTypes.bool,
153
+ type: PropTypes.string,
154
+ initialValue: PropTypes.string,
155
+ disabled: PropTypes.bool,
156
+ noResultsText: PropTypes.string,
157
+ };
158
+ export default AUAutoSuggestComponent;