newspack-components 2.2.1 → 3.0.0-alpha.2

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 (308) hide show
  1. package/babel.config.js +25 -0
  2. package/copy-styles.js +2 -10
  3. package/dist/{cjs/accordion → accordion}/index.js +17 -13
  4. package/dist/{cjs/accordion → accordion}/style.scss +7 -3
  5. package/dist/{cjs/action-card → action-card}/index.js +39 -43
  6. package/dist/{esm/action-card → action-card}/style.scss +9 -9
  7. package/dist/{cjs/autocomplete-tokenfield → autocomplete-tokenfield}/index.js +67 -29
  8. package/dist/{esm/autocomplete-tokenfield → autocomplete-tokenfield}/style.scss +14 -8
  9. package/dist/{cjs/autocomplete-with-suggestions → autocomplete-with-suggestions}/index.js +61 -73
  10. package/dist/{esm/autocomplete-with-suggestions → autocomplete-with-suggestions}/style.scss +9 -17
  11. package/dist/button/index.js +51 -0
  12. package/{src/button/index.js → dist/button/index.tsx} +12 -5
  13. package/dist/{cjs/button → button}/style.scss +1 -1
  14. package/dist/{cjs/button-card → button-card}/index.js +9 -10
  15. package/dist/{cjs/button-card → button-card}/style.scss +19 -13
  16. package/dist/{cjs/card → card}/index.js +4 -5
  17. package/dist/{esm/card → card}/style.scss +1 -1
  18. package/dist/{cjs/category-autocomplete → category-autocomplete}/index.js +51 -53
  19. package/dist/{esm/category-autocomplete → category-autocomplete}/style.scss +2 -2
  20. package/dist/{cjs/color-picker → color-picker}/index.js +14 -16
  21. package/dist/{cjs/color-picker → color-picker}/style.scss +3 -3
  22. package/dist/consts.js +7 -0
  23. package/dist/{cjs/custom-select-control → custom-select-control}/index.js +4 -5
  24. package/dist/{cjs/custom-select-control → custom-select-control}/style.scss +11 -10
  25. package/dist/{cjs/date-range-picker → date-range-picker}/index.js +9 -11
  26. package/dist/{cjs/date-range-picker → date-range-picker}/style.scss +6 -6
  27. package/dist/{cjs/footer → footer}/index.js +33 -36
  28. package/dist/{cjs/footer → footer}/style.scss +1 -1
  29. package/dist/{cjs/form-token-field → form-token-field}/index.js +6 -7
  30. package/dist/{esm/form-token-field → form-token-field}/style.scss +1 -1
  31. package/dist/{cjs/global-notices → global-notices}/index.js +4 -5
  32. package/dist/grid/index.js +44 -0
  33. package/dist/grid/index.tsx +34 -0
  34. package/dist/{cjs/grid → grid}/style.scss +9 -9
  35. package/dist/{cjs/handoff → handoff}/index.js +73 -71
  36. package/dist/{cjs/handoff-message → handoff-message}/index.js +5 -3
  37. package/dist/hooks/index.js +29 -0
  38. package/{src/hooks/index.js → dist/hooks/index.ts} +1 -1
  39. package/dist/{cjs/hooks → hooks}/useObjectState.js +2 -3
  40. package/dist/{cjs/hooks → hooks}/useObjectState.test.js +9 -9
  41. package/dist/{cjs/hooks → hooks}/useOnClickOutside.js +1 -2
  42. package/dist/hooks/usePrompt.js +47 -0
  43. package/{src/hooks/usePrompt.js → dist/hooks/usePrompt.ts} +4 -8
  44. package/dist/image-upload/index.js +148 -0
  45. package/dist/{cjs/image-upload → image-upload}/index.test.js +13 -6
  46. package/dist/{esm/image-upload → image-upload}/style.scss +5 -5
  47. package/dist/{cjs/index.js → index.js} +1 -1
  48. package/dist/{cjs/info-button → info-button}/index.js +6 -7
  49. package/dist/{cjs/modal → modal}/index.js +4 -5
  50. package/dist/{esm/modal → modal}/style.scss +5 -5
  51. package/dist/{cjs/newspack-icon → newspack-icon}/index.js +5 -6
  52. package/dist/{cjs/newspack-icon → newspack-icon}/style.scss +1 -1
  53. package/dist/{cjs/newspack-logo → newspack-logo}/index.js +5 -6
  54. package/dist/{cjs/newspack-logo → newspack-logo}/style.scss +1 -1
  55. package/dist/{cjs/notice → notice}/index.js +12 -10
  56. package/dist/{cjs/notice → notice}/style.scss +11 -11
  57. package/dist/patrons-logo/index.js +77 -0
  58. package/dist/plugin-installer/index.js +297 -0
  59. package/dist/{cjs/plugin-installer → plugin-installer}/style.scss +1 -1
  60. package/dist/{cjs/plugin-settings → plugin-settings}/SettingsSection.js +13 -13
  61. package/dist/plugin-settings/index.js +228 -0
  62. package/dist/{cjs/plugin-settings → plugin-settings}/style.scss +1 -1
  63. package/dist/plugin-toggle/README.md +53 -0
  64. package/dist/plugin-toggle/index.js +222 -0
  65. package/dist/{esm/plugin-toggle → plugin-toggle}/style.scss +1 -1
  66. package/dist/{cjs/popover → popover}/index.js +4 -5
  67. package/dist/{cjs/popover → popover}/style.scss +7 -7
  68. package/dist/{cjs/position-control → position-control}/index.js +33 -32
  69. package/dist/{cjs/position-control → position-control}/style.scss +8 -8
  70. package/dist/{cjs/progress-bar → progress-bar}/index.js +9 -10
  71. package/dist/{cjs/progress-bar → progress-bar}/index.test.js +16 -16
  72. package/dist/{cjs/progress-bar → progress-bar}/style.scss +2 -2
  73. package/dist/proxied-imports/router.js +15 -0
  74. package/dist/{cjs/radio-control → radio-control}/index.js +4 -5
  75. package/dist/{cjs/radio-control → radio-control}/style.scss +3 -3
  76. package/dist/{cjs/section-header → section-header}/index.js +20 -21
  77. package/dist/{esm/section-header → section-header}/style.scss +5 -1
  78. package/dist/{cjs/select-control → select-control}/ButtonGroupControl.js +17 -19
  79. package/dist/{cjs/select-control → select-control}/GroupedSelectControl.js +32 -35
  80. package/dist/{cjs/select-control → select-control}/index.js +7 -8
  81. package/dist/{cjs/select-control → select-control}/style.scss +4 -4
  82. package/dist/settings/MinMaxSetting.js +60 -0
  83. package/dist/{cjs/settings → settings}/SettingsCard.js +14 -16
  84. package/dist/settings/SettingsSection.js +33 -0
  85. package/dist/{cjs/settings → settings}/index.js +3 -4
  86. package/dist/{cjs/settings → settings}/style.scss +15 -2
  87. package/dist/sortable-newsletter-list-control/index.js +135 -0
  88. package/dist/sortable-newsletter-list-control/style.scss +45 -0
  89. package/dist/{cjs/steps-list → steps-list}/index.js +5 -6
  90. package/dist/{esm/steps-list → steps-list}/style.scss +2 -2
  91. package/dist/{cjs/steps-list-item → steps-list-item}/index.js +6 -7
  92. package/dist/{cjs/steps-list-item → steps-list-item}/style.scss +5 -5
  93. package/dist/{cjs/style-card → style-card}/index.js +21 -19
  94. package/dist/{esm/style-card → style-card}/style.scss +4 -4
  95. package/dist/{esm/style.scss → style.scss} +14 -2
  96. package/dist/{cjs/tabbed-navigation → tabbed-navigation}/index.js +16 -19
  97. package/dist/{cjs/tabbed-navigation → tabbed-navigation}/style.scss +4 -4
  98. package/dist/text-control/index.js +76 -0
  99. package/dist/text-control/index.tsx +71 -0
  100. package/dist/utils/index.js +36 -0
  101. package/dist/{cjs/waiting → waiting}/index.js +4 -5
  102. package/dist/web-preview/index.js +191 -0
  103. package/dist/{cjs/web-preview → web-preview}/style.scss +5 -5
  104. package/dist/with-wizard/index.js +328 -0
  105. package/dist/{esm/with-wizard → with-wizard}/style.scss +44 -22
  106. package/dist/{cjs/with-wizard-screen → with-wizard-screen}/index.js +40 -43
  107. package/dist/{esm/with-wizard-screen → with-wizard-screen}/style.scss +7 -7
  108. package/dist/{cjs/wizard → wizard}/components/WizardError.js +16 -17
  109. package/dist/{cjs/wizard → wizard}/index.js +44 -46
  110. package/dist/{cjs/wizard → wizard}/store/index.js +37 -38
  111. package/dist/{cjs/wizard → wizard}/store/utils.js +1 -4
  112. package/package.json +16 -14
  113. package/shared/scss/_mixins.scss +2 -2
  114. package/src/accordion/index.js +4 -1
  115. package/src/accordion/style.scss +7 -3
  116. package/src/action-card/style.scss +9 -9
  117. package/src/autocomplete-tokenfield/index.js +48 -15
  118. package/src/autocomplete-tokenfield/style.scss +14 -8
  119. package/src/autocomplete-with-suggestions/index.js +59 -62
  120. package/src/autocomplete-with-suggestions/style.scss +9 -17
  121. package/src/button/index.tsx +48 -0
  122. package/src/button/style.scss +1 -1
  123. package/src/button-card/style.scss +19 -13
  124. package/src/card/style.scss +1 -1
  125. package/src/category-autocomplete/style.scss +2 -2
  126. package/src/color-picker/style.scss +3 -3
  127. package/src/consts.js +0 -2
  128. package/src/custom-select-control/style.scss +11 -10
  129. package/src/date-range-picker/style.scss +6 -6
  130. package/src/footer/index.js +7 -7
  131. package/src/footer/style.scss +1 -1
  132. package/src/form-token-field/style.scss +1 -1
  133. package/src/grid/index.tsx +34 -0
  134. package/src/grid/style.scss +9 -9
  135. package/src/handoff/index.js +7 -6
  136. package/src/handoff-message/index.js +3 -1
  137. package/src/hooks/index.ts +15 -0
  138. package/src/hooks/usePrompt.ts +46 -0
  139. package/src/image-upload/index.js +5 -5
  140. package/src/image-upload/index.test.js +6 -1
  141. package/src/image-upload/style.scss +5 -5
  142. package/src/modal/style.scss +5 -5
  143. package/src/newspack-icon/style.scss +1 -1
  144. package/src/newspack-logo/style.scss +1 -1
  145. package/src/notice/index.js +1 -1
  146. package/src/notice/style.scss +11 -11
  147. package/src/patrons-logo/index.js +4 -1
  148. package/src/plugin-installer/index.js +7 -7
  149. package/src/plugin-installer/style.scss +1 -1
  150. package/src/plugin-settings/index.js +1 -1
  151. package/src/plugin-settings/style.scss +1 -1
  152. package/src/plugin-toggle/index.js +13 -12
  153. package/src/plugin-toggle/style.scss +1 -1
  154. package/src/popover/style.scss +7 -7
  155. package/src/position-control/index.js +63 -64
  156. package/src/position-control/style.scss +8 -8
  157. package/src/progress-bar/style.scss +2 -2
  158. package/src/radio-control/style.scss +3 -3
  159. package/src/section-header/index.js +3 -3
  160. package/src/section-header/style.scss +5 -1
  161. package/src/select-control/GroupedSelectControl.js +1 -1
  162. package/src/select-control/style.scss +4 -4
  163. package/src/settings/MinMaxSetting.js +8 -6
  164. package/src/settings/style.scss +15 -2
  165. package/src/sortable-newsletter-list-control/index.js +137 -0
  166. package/src/sortable-newsletter-list-control/style.scss +45 -0
  167. package/src/steps-list/style.scss +2 -2
  168. package/src/steps-list-item/style.scss +5 -5
  169. package/src/style-card/index.js +11 -5
  170. package/src/style-card/style.scss +4 -4
  171. package/src/style.scss +14 -2
  172. package/src/tabbed-navigation/style.scss +4 -4
  173. package/src/text-control/index.tsx +65 -0
  174. package/src/web-preview/index.js +6 -6
  175. package/src/web-preview/style.scss +5 -5
  176. package/src/with-wizard/index.js +4 -4
  177. package/src/with-wizard/style.scss +44 -22
  178. package/src/with-wizard-screen/index.js +1 -1
  179. package/src/with-wizard-screen/style.scss +7 -7
  180. package/src/wizard/components/WizardError.js +1 -1
  181. package/src/wizard/index.js +3 -3
  182. package/src/wizard/store/index.js +9 -2
  183. package/dist/cjs/action-card/style.scss +0 -483
  184. package/dist/cjs/autocomplete-tokenfield/style.scss +0 -43
  185. package/dist/cjs/autocomplete-with-suggestions/style.scss +0 -171
  186. package/dist/cjs/button/index.js +0 -57
  187. package/dist/cjs/card/style.scss +0 -168
  188. package/dist/cjs/category-autocomplete/style.scss +0 -34
  189. package/dist/cjs/consts.js +0 -12
  190. package/dist/cjs/form-token-field/style.scss +0 -37
  191. package/dist/cjs/grid/index.js +0 -54
  192. package/dist/cjs/hooks/index.js +0 -26
  193. package/dist/cjs/hooks/usePrompt.js +0 -52
  194. package/dist/cjs/image-upload/index.js +0 -144
  195. package/dist/cjs/image-upload/style.scss +0 -103
  196. package/dist/cjs/modal/style.scss +0 -56
  197. package/dist/cjs/patrons-logo/index.js +0 -82
  198. package/dist/cjs/plugin-installer/index.js +0 -299
  199. package/dist/cjs/plugin-settings/index.js +0 -222
  200. package/dist/cjs/plugin-toggle/index.js +0 -222
  201. package/dist/cjs/plugin-toggle/style.scss +0 -20
  202. package/dist/cjs/proxied-imports/router.js +0 -15
  203. package/dist/cjs/section-header/style.scss +0 -41
  204. package/dist/cjs/settings/MinMaxSetting.js +0 -57
  205. package/dist/cjs/settings/SettingsSection.js +0 -37
  206. package/dist/cjs/steps-list/style.scss +0 -11
  207. package/dist/cjs/style-card/style.scss +0 -94
  208. package/dist/cjs/style.scss +0 -43
  209. package/dist/cjs/text-control/index.js +0 -71
  210. package/dist/cjs/utils/index.js +0 -40
  211. package/dist/cjs/web-preview/index.js +0 -189
  212. package/dist/cjs/with-wizard/index.js +0 -318
  213. package/dist/cjs/with-wizard/style.scss +0 -260
  214. package/dist/cjs/with-wizard-screen/style.scss +0 -160
  215. package/dist/esm/accordion/index.js +0 -38
  216. package/dist/esm/accordion/style.scss +0 -27
  217. package/dist/esm/action-card/index.js +0 -222
  218. package/dist/esm/autocomplete-tokenfield/index.js +0 -223
  219. package/dist/esm/autocomplete-with-suggestions/index.js +0 -289
  220. package/dist/esm/button/index.js +0 -48
  221. package/dist/esm/button/style.scss +0 -11
  222. package/dist/esm/button-card/index.js +0 -65
  223. package/dist/esm/button-card/style.scss +0 -174
  224. package/dist/esm/card/index.js +0 -43
  225. package/dist/esm/category-autocomplete/index.js +0 -185
  226. package/dist/esm/color-picker/index.js +0 -61
  227. package/dist/esm/color-picker/style.scss +0 -37
  228. package/dist/esm/consts.js +0 -3
  229. package/dist/esm/custom-select-control/index.js +0 -37
  230. package/dist/esm/custom-select-control/style.scss +0 -92
  231. package/dist/esm/date-range-picker/index.js +0 -32
  232. package/dist/esm/date-range-picker/style.scss +0 -148
  233. package/dist/esm/footer/index.js +0 -105
  234. package/dist/esm/footer/style.scss +0 -82
  235. package/dist/esm/form-token-field/index.js +0 -50
  236. package/dist/esm/global-notices/index.js +0 -30
  237. package/dist/esm/grid/index.js +0 -45
  238. package/dist/esm/grid/style.scss +0 -166
  239. package/dist/esm/handoff/index.js +0 -177
  240. package/dist/esm/handoff-message/index.js +0 -45
  241. package/dist/esm/hooks/index.js +0 -17
  242. package/dist/esm/hooks/useObjectState.js +0 -31
  243. package/dist/esm/hooks/useObjectState.test.js +0 -83
  244. package/dist/esm/hooks/useOnClickOutside.js +0 -31
  245. package/dist/esm/hooks/usePrompt.js +0 -43
  246. package/dist/esm/image-upload/index.js +0 -141
  247. package/dist/esm/image-upload/index.test.js +0 -28
  248. package/dist/esm/index.js +0 -47
  249. package/dist/esm/info-button/index.js +0 -42
  250. package/dist/esm/info-button/style.scss +0 -18
  251. package/dist/esm/modal/index.js +0 -39
  252. package/dist/esm/newspack-icon/index.js +0 -50
  253. package/dist/esm/newspack-icon/style.scss +0 -26
  254. package/dist/esm/newspack-logo/index.js +0 -46
  255. package/dist/esm/newspack-logo/style.scss +0 -30
  256. package/dist/esm/notice/index.js +0 -67
  257. package/dist/esm/notice/style.scss +0 -111
  258. package/dist/esm/patrons-logo/index.js +0 -76
  259. package/dist/esm/plugin-installer/index.js +0 -295
  260. package/dist/esm/plugin-installer/style.scss +0 -72
  261. package/dist/esm/plugin-settings/SettingsSection.js +0 -136
  262. package/dist/esm/plugin-settings/index.js +0 -217
  263. package/dist/esm/plugin-settings/style.scss +0 -7
  264. package/dist/esm/plugin-toggle/index.js +0 -217
  265. package/dist/esm/popover/index.js +0 -41
  266. package/dist/esm/popover/style.scss +0 -161
  267. package/dist/esm/position-control/index.js +0 -109
  268. package/dist/esm/position-control/style.scss +0 -66
  269. package/dist/esm/progress-bar/index.js +0 -69
  270. package/dist/esm/progress-bar/index.test.js +0 -110
  271. package/dist/esm/progress-bar/style.scss +0 -42
  272. package/dist/esm/proxied-imports/router.js +0 -8
  273. package/dist/esm/radio-control/index.js +0 -37
  274. package/dist/esm/radio-control/style.scss +0 -92
  275. package/dist/esm/section-header/index.js +0 -68
  276. package/dist/esm/select-control/ButtonGroupControl.js +0 -54
  277. package/dist/esm/select-control/GroupedSelectControl.js +0 -84
  278. package/dist/esm/select-control/index.js +0 -51
  279. package/dist/esm/select-control/style.scss +0 -108
  280. package/dist/esm/settings/MinMaxSetting.js +0 -53
  281. package/dist/esm/settings/SettingsCard.js +0 -39
  282. package/dist/esm/settings/SettingsSection.js +0 -30
  283. package/dist/esm/settings/index.js +0 -9
  284. package/dist/esm/settings/style.scss +0 -106
  285. package/dist/esm/steps-list/index.js +0 -43
  286. package/dist/esm/steps-list-item/index.js +0 -48
  287. package/dist/esm/steps-list-item/style.scss +0 -47
  288. package/dist/esm/style-card/index.js +0 -76
  289. package/dist/esm/tabbed-navigation/index.js +0 -56
  290. package/dist/esm/tabbed-navigation/style.scss +0 -86
  291. package/dist/esm/text-control/index.js +0 -66
  292. package/dist/esm/text-control/style.scss +0 -32
  293. package/dist/esm/utils/index.js +0 -32
  294. package/dist/esm/waiting/index.js +0 -44
  295. package/dist/esm/waiting/style.scss +0 -27
  296. package/dist/esm/web-preview/index.js +0 -184
  297. package/dist/esm/web-preview/style.scss +0 -155
  298. package/dist/esm/with-wizard/index.js +0 -313
  299. package/dist/esm/with-wizard-screen/index.js +0 -116
  300. package/dist/esm/wizard/components/WizardError.js +0 -69
  301. package/dist/esm/wizard/index.js +0 -126
  302. package/dist/esm/wizard/store/index.js +0 -155
  303. package/dist/esm/wizard/store/utils.js +0 -17
  304. package/src/grid/index.js +0 -44
  305. package/src/text-control/index.js +0 -56
  306. /package/dist/{cjs/info-button → info-button}/style.scss +0 -0
  307. /package/dist/{cjs/text-control → text-control}/style.scss +0 -0
  308. /package/dist/{cjs/waiting → waiting}/style.scss +0 -0
@@ -1,223 +0,0 @@
1
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
- /**
3
- * External dependencies
4
- */
5
- import debounce from 'lodash/debounce';
6
-
7
- /**
8
- * WordPress dependencies
9
- */
10
- import { Component } from '@wordpress/element';
11
- import { FormTokenField, Spinner } from '@wordpress/components';
12
-
13
- /**
14
- * Internal dependencies
15
- */
16
- import './style.scss';
17
-
18
- /**
19
- * An multi-selecting, api-driven autocomplete input suitable for use in block attributes.
20
- */
21
- import { jsx as _jsx } from "@emotion/react/jsx-runtime";
22
- import { jsxs as _jsxs } from "@emotion/react/jsx-runtime";
23
- class AutocompleteTokenField extends Component {
24
- constructor(props) {
25
- super(props);
26
- /**
27
- * If the component has tokens passed in props, it should fetch info after it mounts.
28
- */
29
- _defineProperty(this, "isFetchingInfoOnLoad", () => {
30
- const {
31
- tokens,
32
- fetchSavedInfo
33
- } = this.props;
34
- return Boolean(tokens.length && fetchSavedInfo);
35
- });
36
- this.state = {
37
- suggestions: [],
38
- validValues: {},
39
- loading: this.isFetchingInfoOnLoad()
40
- };
41
- this.debouncedUpdateSuggestions = debounce(this.updateSuggestions, 500);
42
- }
43
- /**
44
- * When the component loads, fetch information about the tokens so we can populate
45
- * the tokens with the correct labels.
46
- */
47
- componentDidMount() {
48
- if (this.isFetchingInfoOnLoad()) {
49
- const {
50
- tokens,
51
- fetchSavedInfo
52
- } = this.props;
53
- fetchSavedInfo(tokens).then(results => {
54
- const {
55
- validValues
56
- } = this.state;
57
- results.forEach(suggestion => {
58
- validValues[suggestion.value] = suggestion.label;
59
- });
60
- this.setState({
61
- validValues,
62
- loading: false
63
- });
64
- });
65
- }
66
- }
67
-
68
- /**
69
- * Clean up any unfinished autocomplete api call requests.
70
- */
71
- componentWillUnmount() {
72
- delete this.suggestionsRequest;
73
- this.debouncedUpdateSuggestions.cancel();
74
- }
75
-
76
- /**
77
- * Get a list of labels for input values.
78
- *
79
- * @param {Array} values Array of values (ids, etc.).
80
- * @return {Array} array of valid labels corresponding to the values.
81
- */
82
- getLabelsForValues(values) {
83
- const {
84
- validValues
85
- } = this.state;
86
- return values.reduce((accumulator, value) => {
87
- if (!value) {
88
- return accumulator;
89
- }
90
- if (value.label) {
91
- return [...accumulator, value.label];
92
- }
93
- return validValues[value] ? [...accumulator, validValues[value]] : accumulator;
94
- }, []);
95
- }
96
-
97
- /**
98
- * Get a list of values for input labels.
99
- *
100
- * @param {Array} labels Array of labels from the tokens.
101
- * @return {Array} Array of valid values corresponding to the labels.
102
- */
103
- getValuesForLabels(labels) {
104
- const {
105
- returnFullObjects
106
- } = this.props; // If this prop is passed, return both the value and label. Otherwise, return just the label.
107
- const {
108
- validValues
109
- } = this.state;
110
- if (returnFullObjects) {
111
- return labels.reduce((acc, label) => {
112
- Object.keys(validValues).forEach(key => {
113
- if (validValues[key] === label) {
114
- acc.push({
115
- value: key,
116
- label
117
- });
118
- }
119
- });
120
- return acc;
121
- }, []);
122
- }
123
- return labels.map(label => Object.keys(validValues).find(key => validValues[key] === label));
124
- }
125
-
126
- /**
127
- * Refresh the autocomplete dropdown.
128
- *
129
- * @param {string} input Input to fetch suggestions for
130
- */
131
- updateSuggestions(input) {
132
- const {
133
- fetchSuggestions
134
- } = this.props;
135
- if (!fetchSuggestions) {
136
- return;
137
- }
138
- this.setState({
139
- loading: true
140
- }, () => {
141
- const request = fetchSuggestions(input);
142
- request.then(suggestions => {
143
- // A fetch Promise doesn't have an abort option. It's mimicked by
144
- // comparing the request reference in on the instance, which is
145
- // reset or deleted on subsequent requests or unmounting.
146
- if (this.suggestionsRequest !== request) {
147
- return;
148
- }
149
- const currentSuggestions = [...suggestions];
150
- const currentValidValues = {};
151
- suggestions.forEach(suggestion => {
152
- currentValidValues[suggestion.value] = suggestion.label;
153
- });
154
- this.setState({
155
- suggestions: currentSuggestions,
156
- validValues: currentValidValues,
157
- loading: false
158
- });
159
- }).catch(() => {
160
- if (this.suggestionsRequest === request) {
161
- this.setState({
162
- loading: false
163
- });
164
- }
165
- });
166
- this.suggestionsRequest = request;
167
- });
168
- }
169
-
170
- /**
171
- * When a token is selected, we need to convert the string label into a recognized value suitable for saving as an attribute.
172
- *
173
- * @param {Array} tokenStrings An array of token label strings.
174
- */
175
- handleOnChange(tokenStrings) {
176
- const {
177
- onChange
178
- } = this.props;
179
- onChange(this.getValuesForLabels(tokenStrings));
180
- }
181
-
182
- /**
183
- * To populate the tokens, we need to convert the values into a human-readable label.
184
- *
185
- * @return {Array} An array of token label strings.
186
- */
187
- getTokens() {
188
- const {
189
- tokens
190
- } = this.props;
191
- return this.getLabelsForValues(tokens);
192
- }
193
-
194
- /**
195
- * Render.
196
- */
197
- render() {
198
- const {
199
- help,
200
- label = '',
201
- maxLength
202
- } = this.props;
203
- const {
204
- suggestions,
205
- loading
206
- } = this.state;
207
- return _jsxs("div", {
208
- className: "newspack-autocomplete-tokenfield",
209
- children: [_jsx(FormTokenField, {
210
- value: this.getTokens(),
211
- suggestions: suggestions.map(suggestion => suggestion.label),
212
- onChange: tokens => this.handleOnChange(tokens),
213
- onInputChange: input => this.debouncedUpdateSuggestions(input),
214
- label: label,
215
- maxLength: maxLength
216
- }), loading && _jsx(Spinner, {}), help && _jsx("p", {
217
- className: "newspack-autocomplete-tokenfield__help",
218
- children: help
219
- })]
220
- });
221
- }
222
- }
223
- export default AutocompleteTokenField;
@@ -1,289 +0,0 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import apiFetch from '@wordpress/api-fetch';
5
- import { Button, CheckboxControl, SelectControl, Spinner } from '@wordpress/components';
6
- import { decodeEntities } from '@wordpress/html-entities';
7
- import { useEffect, useState } from '@wordpress/element';
8
- import { __ as alias__, _x as alias_x, sprintf } from '@wordpress/i18n';
9
- const __ = alias__,
10
- _x = alias_x;
11
- import { addQueryArgs } from '@wordpress/url';
12
-
13
- /**
14
- * External dependencies.
15
- */
16
- import { AutocompleteTokenField } from '../';
17
- import './style.scss';
18
- import { jsx as _jsx } from "@emotion/react/jsx-runtime";
19
- import { jsxs as _jsxs } from "@emotion/react/jsx-runtime";
20
- import { Fragment as _Fragment } from "@emotion/react/jsx-runtime";
21
- const AutocompleteWithSuggestions = _ref => {
22
- let {
23
- fetchSavedPosts = false,
24
- // If passed, will use this function to fetch saved data.
25
- fetchSuggestions = false,
26
- // If passed, will use this function to fetch suggestions data.
27
- help = __('Begin typing search term, click autocomplete result to select.', 'newspack'),
28
- hideHelp = false,
29
- // If true, all help text will be hidden.
30
- label = __('Search', 'newspack'),
31
- maxItemsToSuggest = 0,
32
- // If passed, will be used to determine "load more" state. Necessary if you want "load more" functionality when using a custom `fetchSuggestions` function.
33
- multiSelect = false,
34
- // If true, component can select multiple values at once.
35
- onChange = false,
36
- // Function to call when selections change.
37
- onPostTypeChange = false,
38
- // Funciton to call when the post type selector is changed.
39
- postTypes = [{
40
- slug: 'post',
41
- label: 'Post'
42
- }],
43
- // If passed, will render a selector to change the post type queried for suggestions.
44
- postTypeLabel = __('item', 'newspack'),
45
- // String to describe the data being shown.
46
- postTypeLabelPlural = __('items', 'newspack'),
47
- // Plural string to describe the data being shown.
48
- selectedItems = [],
49
- // Array of saved items.
50
- selectedPost = 0,
51
- // Legacy prop when single-select was the only option.
52
- suggestionsToFetch = 10 // Number of suggestions to fetch per query.
53
- } = _ref;
54
- const [isLoading, setIsLoading] = useState(true);
55
- const [isLoadingMore, setIsLoadingMore] = useState(false);
56
- const [suggestions, setSuggestions] = useState([]);
57
- const [maxSuggestions, setMaxSuggestions] = useState(0);
58
- const [postTypeToSearch, setPostTypeToSearch] = useState(postTypes[0].slug);
59
- const classNames = ['newspack-autocomplete-with-suggestions'];
60
- if (hideHelp) {
61
- classNames.push('hide-help');
62
- }
63
-
64
- /**
65
- * Fetch recent posts to show as suggestions.
66
- */
67
- useEffect(() => {
68
- if (onPostTypeChange) {
69
- onPostTypeChange(postTypeToSearch);
70
- }
71
- setIsLoading(true);
72
- handleFetchSuggestions(null, 0, postTypeToSearch).then(_suggestions => {
73
- if (0 < _suggestions.length) {
74
- setSuggestions(_suggestions);
75
- }
76
- }).finally(() => setIsLoading(false));
77
- }, [postTypeToSearch]);
78
-
79
- /**
80
- * Fetch more suggestions.
81
- */
82
- useEffect(() => {
83
- if (isLoadingMore) {
84
- handleFetchSuggestions(null, suggestions.length, postTypeToSearch).then(_suggestions => {
85
- if (0 < _suggestions.length) {
86
- setSuggestions(suggestions.concat(_suggestions));
87
- }
88
- }).finally(() => setIsLoadingMore(false));
89
- }
90
- }, [isLoadingMore]);
91
-
92
- /**
93
- * If passed a `fetchSavedPosts` prop, use that, otherwise, build it based on the selected post type.
94
- */
95
- const handleFetchSaved = fetchSavedPosts ? fetchSavedPosts : async function () {
96
- let postIds = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
97
- let searchSlug = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
98
- const postTypeSlug = searchSlug || postTypeToSearch;
99
- const endpoint = 'post' === postTypeSlug || 'page' === postTypeSlug ? postTypeSlug + 's' // Default post type endpoints are plural.
100
- : postTypeSlug; // Custom post type endpoints are singular.
101
- const posts = await apiFetch({
102
- path: addQueryArgs('/wp/v2/' + endpoint, {
103
- per_page: 100,
104
- include: postIds.join(','),
105
- _fields: 'id,title'
106
- })
107
- });
108
- return posts.map(post => ({
109
- value: parseInt(post.id),
110
- label: decodeEntities(post.title) || __('(no title)', 'newspack')
111
- }));
112
- };
113
-
114
- /**
115
- * If passed a `fetchSuggestions` prop, use that, otherwise, build it based on the selected post type.
116
- */
117
- const handleFetchSuggestions = fetchSuggestions ? fetchSuggestions : async function () {
118
- let search = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
119
- let offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
120
- let searchSlug = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
121
- const postTypeSlug = searchSlug || postTypeToSearch;
122
- const endpoint = 'post' === postTypeSlug || 'page' === postTypeSlug ? postTypeSlug + 's' // Default post type endpoints are plural.
123
- : postTypeSlug; // Custom post type endpoints are singular.
124
- const response = await apiFetch({
125
- parse: false,
126
- path: addQueryArgs('/wp/v2/' + endpoint, {
127
- search,
128
- offset,
129
- per_page: suggestionsToFetch,
130
- _fields: 'id,title'
131
- })
132
- });
133
- const total = parseInt(response.headers.get('x-wp-total') || 0);
134
- const posts = await response.json();
135
- setMaxSuggestions(total);
136
-
137
- // Format suggestions for FormTokenField display.
138
- return posts.reduce((acc, post) => {
139
- acc.push({
140
- value: parseInt(post.id),
141
- label: decodeEntities(post?.title.rendered) || __('(no title)', 'newspack')
142
- });
143
- return acc;
144
- }, []);
145
- };
146
-
147
- /**
148
- * Intercept onChange callback so we can decide whether to allow multiple selections.
149
- */
150
- const handleOnChange = _selections => {
151
- // If only allowing one selection, just return the one selected item.
152
- if (!multiSelect) {
153
- return onChange(_selections);
154
- }
155
-
156
- // Handle legacy `selectedPost` prop.
157
- const selections = selectedPost ? [...selectedItems, selectedPost] : [...selectedItems];
158
-
159
- // Loop through new selections to determine whether to add or remove them.
160
- _selections.forEach(_selection => {
161
- const existingSelection = selections.findIndex(selection => parseInt(selection.value) === parseInt(_selection.value));
162
- if (-1 < existingSelection) {
163
- // If the selection is already selected, remove it.
164
- selections.splice(existingSelection, 1);
165
- } else {
166
- // Otherwise, add it.
167
- selections.push(_selection);
168
- }
169
- });
170
-
171
- // Include currently selected post type in selection results.
172
- onChange(selections.map(selection => ({
173
- ...selection,
174
- postType: postTypeToSearch
175
- })));
176
- };
177
-
178
- /**
179
- * Render selected item(s) for this component. Clicking on a selection deselects it.
180
- */
181
- const renderSelections = () => {
182
- // Handle legacy `selectedPost` prop.
183
- const selections = selectedPost ? [...selectedItems, selectedPost] : selectedItems;
184
- const selectedMessage = multiSelect ? sprintf(
185
- // Translators: %1: the length of selections. %2: the selection leabel.
186
- __('%1$s %2$s selected', 'newspack'), selections.length, selections.length > 1 ? postTypeLabelPlural : postTypeLabel) :
187
- // Translators: %s: The label for the selection.
188
- sprintf(__('Selected %s', 'newspack'), postTypeLabel);
189
- return _jsxs("div", {
190
- className: "newspack-autocomplete-with-suggestions__selected-items",
191
- children: [_jsxs("p", {
192
- className: "newspack-autocomplete-with-suggestions__label",
193
- children: [selectedMessage, 1 < selections.length && _x(' – ', 'separator character', 'newspack'), 1 < selections.length && _jsx(Button, {
194
- onClick: () => onChange([]),
195
- isLink: true,
196
- isDestructive: true,
197
- children: __('Clear all', 'newspack')
198
- })]
199
- }), selections.map(selection => _jsx(Button, {
200
- className: "newspack-autocomplete-with-suggestions__selected-item-button",
201
- isTertiary: true,
202
- onClick: () => onChange(selections.filter(_selection => _selection.value !== selection.value)),
203
- children: selection.label
204
- }, selection.value))]
205
- });
206
- };
207
-
208
- /**
209
- * Render post type select dropdown.
210
- */
211
- const renderPostTypeSelect = () => {
212
- return _jsx(SelectControl, {
213
- label: sprintf(
214
- // Translators: %s: The name of the type.
215
- __('%s type', 'newspack'), postTypeLabel.charAt(0).toUpperCase() + postTypeLabel.slice(1)),
216
- value: postTypeToSearch,
217
- options: postTypes.map(postTypeOption => ({
218
- label: postTypeOption.label,
219
- value: postTypeOption.slug
220
- })),
221
- onChange: _postType => setPostTypeToSearch(_postType)
222
- });
223
- };
224
-
225
- /**
226
- * Render a single suggestion object that can be clicked to select it immediately.
227
- *
228
- * @param {Object} suggestion Suggestion object with value and label keys.
229
- */
230
- const renderSuggestion = suggestion => {
231
- if (multiSelect) {
232
- const selections = selectedPost ? [...selectedItems, selectedPost] : [...selectedItems];
233
- const isSelected = !!selections.find(_selection => parseInt(_selection.value) === parseInt(suggestion.value) && _selection.label === suggestion.label);
234
- return _jsx(CheckboxControl, {
235
- checked: isSelected,
236
- onChange: () => handleOnChange([suggestion]),
237
- label: suggestion.label
238
- }, suggestion.value);
239
- }
240
- return _jsx(Button, {
241
- isLink: true,
242
- onClick: () => handleOnChange([suggestion]),
243
- children: suggestion.label
244
- }, suggestion.value);
245
- };
246
-
247
- /**
248
- * Render a list of suggestions that can be clicked to select instead of searching by title.
249
- */
250
- const renderSuggestions = () => {
251
- if (isLoading) {
252
- return _jsx("div", {
253
- className: "newspack-autocomplete-with-suggestions__suggestions-spinner",
254
- children: _jsx(Spinner, {})
255
- });
256
- }
257
- if (0 === suggestions.length) {
258
- return null;
259
- }
260
- const className = multiSelect ? 'newspack-autocomplete-with-suggestions__search-suggestions-multiselect' : 'newspack-autocomplete-with-suggestions__search-suggestions';
261
- return _jsxs(_Fragment, {
262
- children: [!hideHelp && _jsx("p", {
263
- className: "newspack-autocomplete-with-suggestions__label",
264
- children: /* Translators: %s: the name of a post type. */sprintf(__('Or, select a recent %s:', 'newspack'), postTypeLabel)
265
- }), _jsxs("div", {
266
- className: className,
267
- children: [suggestions.map(renderSuggestion), suggestions.length < (maxItemsToSuggest || maxSuggestions) && _jsx(Button, {
268
- disabled: isLoadingMore,
269
- isSecondary: true,
270
- onClick: () => setIsLoadingMore(true),
271
- children: isLoadingMore ? __('Loading…', 'newspack') : __('Load more', 'newspack')
272
- })]
273
- })]
274
- });
275
- };
276
- return _jsxs("div", {
277
- className: classNames.join(' '),
278
- children: [0 < selectedItems.length && renderSelections(), 1 < postTypes.length && renderPostTypeSelect(), _jsx(AutocompleteTokenField, {
279
- tokens: [],
280
- onChange: handleOnChange,
281
- fetchSuggestions: async search => handleFetchSuggestions(search, 0, postTypeToSearch),
282
- fetchSavedInfo: postIds => handleFetchSaved(postIds),
283
- label: label,
284
- help: !hideHelp && help,
285
- returnFullObjects: true
286
- }), renderSuggestions()]
287
- });
288
- };
289
- export default AutocompleteWithSuggestions;
@@ -1,48 +0,0 @@
1
- /**
2
- * Button
3
- */
4
-
5
- /**
6
- * WordPress dependencies.
7
- */
8
- import { Button as BaseComponent } from '@wordpress/components';
9
- import { useState } from '@wordpress/element';
10
-
11
- /**
12
- * Internal dependencies
13
- */
14
- import Router from '../proxied-imports/router';
15
- import './style.scss';
16
- import { jsx as _jsx } from "@emotion/react/jsx-runtime";
17
- const {
18
- useHistory
19
- } = Router;
20
- const Button = _ref => {
21
- let {
22
- href,
23
- onClick,
24
- ...otherProps
25
- } = _ref;
26
- const history = useHistory();
27
- const [isAwaitingOnClick, setIsAwaitingOnClick] = useState(false);
28
-
29
- // If both onClick and href are present, await the onClick action an then redirect.
30
- if (href && onClick) {
31
- otherProps.onClick = async () => {
32
- setIsAwaitingOnClick(true);
33
- await onClick();
34
- setIsAwaitingOnClick(false);
35
- history.push(href.replace('#', ''));
36
- };
37
- } else {
38
- otherProps.href = href;
39
- otherProps.onClick = onClick;
40
- }
41
- if (isAwaitingOnClick) {
42
- otherProps.disabled = true;
43
- }
44
- return _jsx(BaseComponent, {
45
- ...otherProps
46
- });
47
- };
48
- export default Button;
@@ -1,11 +0,0 @@
1
- /**
2
- * Button
3
- */
4
-
5
- .components-button {
6
- &.is-destructive {
7
- &:hover:not( :disabled ) {
8
- box-shadow: none;
9
- }
10
- }
11
- }
@@ -1,65 +0,0 @@
1
- /**
2
- * Button Card
3
- */
4
-
5
- /**
6
- * WordPress dependencies
7
- */
8
- import { Component } from '@wordpress/element';
9
- import { Icon, chevronRight } from '@wordpress/icons';
10
-
11
- /**
12
- * Internal dependencies
13
- */
14
- import { Grid } from '../';
15
- import './style.scss';
16
-
17
- /**
18
- * External dependencies
19
- */
20
- import classnames from 'classnames';
21
- import { jsx as _jsx } from "@emotion/react/jsx-runtime";
22
- import { jsxs as _jsxs } from "@emotion/react/jsx-runtime";
23
- class ButtonCard extends Component {
24
- /**
25
- * Render
26
- */
27
- render() {
28
- const {
29
- chevron,
30
- className,
31
- desc,
32
- grouped,
33
- icon,
34
- isDestructive,
35
- isPressed,
36
- isSmall,
37
- title,
38
- ...otherProps
39
- } = this.props;
40
- const classes = classnames('newspack-button-card', className, chevron && 'has-chevron', grouped && 'grouped', icon && 'has-icon', isDestructive && 'is-destructive', isPressed && 'is-pressed', isSmall && 'is-small');
41
- return _jsxs("a", {
42
- className: classes,
43
- ...otherProps,
44
- children: [icon && _jsx(Icon, {
45
- icon: icon,
46
- height: 48,
47
- width: 48
48
- }), _jsxs(Grid, {
49
- noMargin: true,
50
- columns: 1,
51
- gutter: 8,
52
- children: [title && _jsx("h3", {
53
- children: title
54
- }), desc && _jsx("p", {
55
- children: desc
56
- })]
57
- }), chevron && _jsx(Icon, {
58
- icon: chevronRight,
59
- height: 24,
60
- width: 24
61
- })]
62
- });
63
- }
64
- }
65
- export default ButtonCard;