newspack-components 4.3.0 → 4.4.0-alpha.1
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.
- package/CHANGELOG.md +11 -0
- package/DEVELOPMENT.md +868 -0
- package/colors/colors.module.scss +52 -2
- package/copy-styles.js +3 -2
- package/dist/cjs/accordion/index.js +17 -12
- package/dist/cjs/action-card/index.js +176 -139
- package/dist/cjs/autocomplete-tokenfield/index.js +254 -234
- package/dist/cjs/autocomplete-with-latest-posts/index.js +172 -110
- package/dist/cjs/autocomplete-with-suggestions/index.js +229 -126
- package/dist/cjs/badge/index.js +7 -7
- package/dist/cjs/box-contrast/index.js +19 -15
- package/dist/cjs/button/index.js +41 -26
- package/dist/cjs/button-props/button-props.js +5 -5
- package/dist/cjs/button-props/index.js +2 -2
- package/dist/cjs/card/core-card.js +110 -52
- package/dist/cjs/card/index.js +74 -67
- package/dist/cjs/card/style-core.scss +93 -25
- package/dist/cjs/card/style.scss +2 -2
- package/dist/cjs/card-feature/index.js +141 -0
- package/dist/cjs/card-feature/style.scss +75 -0
- package/dist/cjs/card-form/index.js +144 -0
- package/dist/cjs/card-form/style.scss +20 -0
- package/dist/cjs/card-settings-group/index.js +36 -16
- package/dist/cjs/card-sortable-list/index.js +176 -140
- package/dist/cjs/category-autocomplete/index.js +175 -155
- package/dist/cjs/color-picker/index.js +41 -23
- package/dist/cjs/color-picker/index.test.js +82 -0
- package/dist/cjs/color-picker/style.scss +11 -13
- package/dist/cjs/confirm-dialog/index.js +86 -22
- package/dist/cjs/consts.js +1 -1
- package/dist/cjs/custom-select-control/index.js +39 -28
- package/dist/cjs/dataviews/index.js +38 -0
- package/dist/cjs/dataviews/style.scss +9 -0
- package/dist/cjs/divider/index.js +26 -21
- package/dist/cjs/footer/index.js +36 -30
- package/dist/cjs/form-token-field/index.js +51 -40
- package/dist/cjs/global-notices/index.js +5 -5
- package/dist/cjs/grid/index.js +25 -19
- package/dist/cjs/grid/style.scss +6 -1
- package/dist/cjs/handoff/index.js +204 -156
- package/dist/cjs/handoff-message/index.js +12 -7
- package/dist/cjs/hooks/index.js +10 -8
- package/dist/cjs/hooks/use-confirm-dialog.js +79 -0
- package/dist/cjs/hooks/use-unsaved-changes-dialog.js +144 -0
- package/dist/cjs/hooks/useObjectState.js +21 -12
- package/dist/cjs/hooks/useObjectState.test.js +55 -33
- package/dist/cjs/hooks/useOnClickOutside.js +6 -6
- package/dist/cjs/hooks/usePrompt.js +10 -13
- package/dist/cjs/image-upload/index.js +114 -123
- package/dist/cjs/image-upload/index.test.js +11 -13
- package/dist/cjs/image-upload/style.scss +12 -40
- package/dist/cjs/index.js +136 -104
- package/dist/cjs/info-button/index.js +43 -32
- package/dist/cjs/integration-icons/active-campaign.js +22 -0
- package/dist/cjs/integration-icons/constant-contact.js +28 -0
- package/dist/cjs/integration-icons/fundraise-up.js +25 -0
- package/dist/cjs/integration-icons/index.js +48 -0
- package/dist/cjs/integration-icons/mailchimp.js +19 -0
- package/dist/cjs/integration-icons/salesforce.js +17 -0
- package/dist/cjs/integration-icons/wisepops.js +20 -0
- package/dist/cjs/modal/index.js +22 -19
- package/dist/cjs/modal/style.scss +4 -0
- package/dist/cjs/newspack-icon/index.js +45 -38
- package/dist/cjs/notice/index.js +64 -55
- package/dist/cjs/plugin-installer/index.js +259 -253
- package/dist/cjs/plugin-settings/SettingsSection.js +66 -60
- package/dist/cjs/plugin-settings/index.js +217 -200
- package/dist/cjs/plugin-toggle/index.js +192 -199
- package/dist/cjs/popover/index.js +41 -28
- package/dist/cjs/position-control/index.js +24 -23
- package/dist/cjs/progress-bar/index.js +71 -59
- package/dist/cjs/progress-bar/index.test.js +62 -68
- package/dist/cjs/proxied-imports/router.js +3 -3
- package/dist/cjs/radio-control/index.js +40 -27
- package/dist/cjs/section-header/index.js +103 -58
- package/dist/cjs/section-header/style.scss +31 -11
- package/dist/cjs/select-control/ButtonGroupControl.js +26 -21
- package/dist/cjs/select-control/GroupedSelectControl.js +41 -35
- package/dist/cjs/select-control/index.js +52 -40
- package/dist/cjs/settings/MinMaxSetting.js +30 -20
- package/dist/cjs/settings/SettingsCard.js +20 -17
- package/dist/cjs/settings/SettingsSection.js +22 -21
- package/dist/cjs/settings/index.js +5 -5
- package/dist/cjs/sortable-newsletter-list-control/index.js +78 -57
- package/dist/cjs/steps-list/index.js +44 -34
- package/dist/cjs/steps-list-item/index.js +47 -39
- package/dist/cjs/style-card/index.js +70 -63
- package/dist/cjs/tabbed-navigation/index.js +38 -36
- package/dist/cjs/text-control/index.js +27 -25
- package/dist/cjs/utils/color.js +4 -4
- package/dist/cjs/utils/editor-toolbar-back-button.js +61 -0
- package/dist/cjs/utils/index.js +31 -19
- package/dist/cjs/utils/style.scss +8 -0
- package/dist/cjs/waiting/index.js +48 -37
- package/dist/cjs/web-preview/index.js +217 -211
- package/dist/cjs/with-wizard/index.js +362 -364
- package/dist/cjs/with-wizard/style.scss +4 -54
- package/dist/cjs/with-wizard-screen/index.js +54 -58
- package/dist/cjs/with-wizard-screen/style.scss +41 -3
- package/dist/cjs/wizard/components/WizardError.js +20 -18
- package/dist/cjs/wizard/components/WizardSnackbar.js +26 -23
- package/dist/cjs/wizard/index.js +176 -114
- package/dist/cjs/wizard/store/index.js +171 -123
- package/dist/cjs/wizard/store/utils.js +13 -8
- package/dist/esm/accordion/index.js +14 -9
- package/dist/esm/action-card/index.js +168 -123
- package/dist/esm/autocomplete-tokenfield/index.js +248 -222
- package/dist/esm/autocomplete-with-latest-posts/index.js +169 -103
- package/dist/esm/autocomplete-with-suggestions/index.js +226 -119
- package/dist/esm/badge/index.js +5 -5
- package/dist/esm/box-contrast/index.js +15 -11
- package/dist/esm/button/index.js +39 -21
- package/dist/esm/button-props/button-props.js +1 -1
- package/dist/esm/card/core-card.js +109 -47
- package/dist/esm/card/index.js +69 -54
- package/dist/esm/card/style-core.scss +93 -25
- package/dist/esm/card/style.scss +2 -2
- package/dist/esm/card-feature/index.js +131 -0
- package/dist/esm/card-feature/style.scss +75 -0
- package/dist/esm/card-form/index.js +134 -0
- package/dist/esm/card-form/style.scss +20 -0
- package/dist/esm/card-settings-group/index.js +33 -14
- package/dist/esm/card-sortable-list/index.js +170 -126
- package/dist/esm/category-autocomplete/index.js +168 -140
- package/dist/esm/color-picker/index.js +39 -21
- package/dist/esm/color-picker/index.test.js +78 -0
- package/dist/esm/color-picker/style.scss +11 -13
- package/dist/esm/confirm-dialog/index.js +85 -18
- package/dist/esm/consts.js +1 -1
- package/dist/esm/custom-select-control/index.js +34 -15
- package/dist/esm/dataviews/index.js +34 -0
- package/dist/esm/dataviews/style.scss +9 -0
- package/dist/esm/divider/index.js +24 -16
- package/dist/esm/footer/index.js +34 -28
- package/dist/esm/form-token-field/index.js +46 -27
- package/dist/esm/global-notices/index.js +3 -3
- package/dist/esm/grid/index.js +23 -14
- package/dist/esm/grid/style.scss +6 -1
- package/dist/esm/handoff/index.js +199 -143
- package/dist/esm/handoff-message/index.js +10 -6
- package/dist/esm/hooks/index.js +8 -6
- package/dist/esm/hooks/use-confirm-dialog.js +73 -0
- package/dist/esm/hooks/use-unsaved-changes-dialog.js +136 -0
- package/dist/esm/hooks/useObjectState.js +19 -9
- package/dist/esm/hooks/useObjectState.test.js +54 -32
- package/dist/esm/hooks/useOnClickOutside.js +4 -4
- package/dist/esm/hooks/usePrompt.js +9 -11
- package/dist/esm/image-upload/index.js +110 -111
- package/dist/esm/image-upload/index.test.js +11 -13
- package/dist/esm/image-upload/style.scss +12 -40
- package/dist/esm/index.js +7 -1
- package/dist/esm/info-button/index.js +38 -19
- package/dist/esm/integration-icons/active-campaign.js +16 -0
- package/dist/esm/integration-icons/constant-contact.js +22 -0
- package/dist/esm/integration-icons/fundraise-up.js +19 -0
- package/dist/esm/integration-icons/index.js +6 -0
- package/dist/esm/integration-icons/mailchimp.js +13 -0
- package/dist/esm/integration-icons/salesforce.js +11 -0
- package/dist/esm/integration-icons/wisepops.js +14 -0
- package/dist/esm/modal/index.js +20 -13
- package/dist/esm/modal/style.scss +4 -0
- package/dist/esm/newspack-icon/index.js +40 -25
- package/dist/esm/notice/index.js +59 -42
- package/dist/esm/plugin-installer/index.js +255 -243
- package/dist/esm/plugin-settings/SettingsSection.js +61 -50
- package/dist/esm/plugin-settings/index.js +212 -189
- package/dist/esm/plugin-toggle/index.js +187 -190
- package/dist/esm/popover/index.js +35 -16
- package/dist/esm/position-control/index.js +22 -18
- package/dist/esm/progress-bar/index.js +66 -49
- package/dist/esm/progress-bar/index.test.js +61 -67
- package/dist/esm/radio-control/index.js +34 -15
- package/dist/esm/section-header/index.js +102 -57
- package/dist/esm/section-header/style.scss +31 -11
- package/dist/esm/select-control/ButtonGroupControl.js +23 -18
- package/dist/esm/select-control/GroupedSelectControl.js +38 -30
- package/dist/esm/select-control/index.js +47 -27
- package/dist/esm/settings/MinMaxSetting.js +27 -16
- package/dist/esm/settings/SettingsCard.js +18 -13
- package/dist/esm/settings/SettingsSection.js +20 -19
- package/dist/esm/settings/index.js +3 -3
- package/dist/esm/sortable-newsletter-list-control/index.js +74 -53
- package/dist/esm/steps-list/index.js +39 -21
- package/dist/esm/steps-list-item/index.js +42 -26
- package/dist/esm/style-card/index.js +65 -50
- package/dist/esm/tabbed-navigation/index.js +35 -33
- package/dist/esm/text-control/index.js +25 -19
- package/dist/esm/utils/color.js +4 -4
- package/dist/esm/utils/editor-toolbar-back-button.js +53 -0
- package/dist/esm/utils/index.js +29 -16
- package/dist/esm/utils/style.scss +8 -0
- package/dist/esm/waiting/index.js +43 -24
- package/dist/esm/web-preview/index.js +213 -201
- package/dist/esm/with-wizard/index.js +360 -358
- package/dist/esm/with-wizard/style.scss +4 -54
- package/dist/esm/with-wizard-screen/index.js +48 -46
- package/dist/esm/with-wizard-screen/style.scss +41 -3
- package/dist/esm/wizard/components/WizardError.js +18 -16
- package/dist/esm/wizard/components/WizardSnackbar.js +23 -17
- package/dist/esm/wizard/index.js +171 -103
- package/dist/esm/wizard/store/index.js +169 -116
- package/dist/esm/wizard/store/utils.js +13 -6
- package/package.json +30 -16
- package/release.config.js +1 -0
- package/shared/hooks/use-coauthors.js +195 -27
- package/shared/hooks/use-coauthors.test.js +232 -6
- package/shared/scss/_mixins.scss +13 -5
- package/src/accordion/index.js +8 -7
- package/src/action-card/index.js +15 -1
- package/src/badge/index.tsx +3 -1
- package/src/card/core-card.js +95 -18
- package/src/card/index.js +1 -0
- package/src/card/style-core.scss +93 -25
- package/src/card/style.scss +2 -2
- package/src/card-feature/README.md +188 -0
- package/src/card-feature/index.tsx +186 -0
- package/src/card-feature/style.scss +75 -0
- package/src/card-form/README.md +113 -0
- package/src/card-form/index.tsx +128 -0
- package/src/card-form/style.scss +20 -0
- package/src/card-settings-group/index.tsx +28 -3
- package/src/card-sortable-list/index.tsx +27 -4
- package/src/color-picker/index.js +15 -8
- package/src/color-picker/index.test.js +52 -0
- package/src/color-picker/style.scss +11 -13
- package/src/confirm-dialog/README.md +128 -0
- package/src/confirm-dialog/index.tsx +74 -6
- package/src/dataviews/index.tsx +34 -0
- package/src/dataviews/style.scss +9 -0
- package/src/grid/style.scss +6 -1
- package/src/handoff/README.md +93 -0
- package/src/handoff/index.js +46 -8
- package/src/hooks/index.ts +2 -1
- package/src/hooks/use-confirm-dialog.tsx +80 -0
- package/src/hooks/use-unsaved-changes-dialog.tsx +140 -0
- package/src/image-upload/index.js +10 -15
- package/src/image-upload/style.scss +12 -40
- package/src/index.js +7 -1
- package/src/integration-icons/active-campaign.js +14 -0
- package/src/integration-icons/constant-contact.js +22 -0
- package/src/integration-icons/fundraise-up.js +18 -0
- package/src/integration-icons/index.js +6 -0
- package/src/integration-icons/mailchimp.js +10 -0
- package/src/integration-icons/salesforce.js +10 -0
- package/src/integration-icons/wisepops.js +14 -0
- package/src/modal/style.scss +4 -0
- package/src/plugin-installer/index.js +30 -19
- package/src/section-header/index.js +56 -24
- package/src/section-header/style.scss +31 -11
- package/src/select-control/index.js +4 -0
- package/src/utils/editor-toolbar-back-button.tsx +48 -0
- package/src/utils/index.js +6 -0
- package/src/utils/style.scss +8 -0
- package/src/with-wizard/style.scss +4 -54
- package/src/with-wizard-screen/style.scss +41 -3
- package/src/wizard/index.js +90 -53
- package/src/wizard/store/index.js +2 -2
- package/dist/cjs/button-card/index.js +0 -72
- package/dist/cjs/button-card/style.scss +0 -180
- package/dist/esm/button-card/index.js +0 -64
- package/dist/esm/button-card/style.scss +0 -180
- package/src/button-card/index.js +0 -52
- package/src/button-card/style.scss +0 -180
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports["default"] = void 0;
|
|
8
|
+
var _element = require("@wordpress/element");
|
|
9
|
+
var _i18n = require("@wordpress/i18n");
|
|
10
|
+
var _useConfirmDialog2 = _interopRequireDefault(require("./use-confirm-dialog"));
|
|
11
|
+
/**
|
|
12
|
+
* WordPress dependencies.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Internal dependencies.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// Stack of mounted guards; the last entry owns the document-level handler so
|
|
20
|
+
// only the most-recently-mounted guard prompts when several are active. Using a
|
|
21
|
+
// stack keeps ownership correct under out-of-order unmounts.
|
|
22
|
+
var activeGuards = [];
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Returns true when `href` resolves to the same origin as the current page —
|
|
26
|
+
* i.e. it is an internal navigation that would unload the wizard. External
|
|
27
|
+
* `https://...` links, `mailto:`, `tel:`, and other schemes navigate to a new
|
|
28
|
+
* context (new tab, mail client, dialer) without unloading the wizard, and
|
|
29
|
+
* must not trigger the discard-changes prompt.
|
|
30
|
+
*/
|
|
31
|
+
function isSameOriginNavigation(link) {
|
|
32
|
+
try {
|
|
33
|
+
var url = new URL(link.href, window.location.href);
|
|
34
|
+
return url.origin === window.location.origin && (url.protocol === 'http:' || url.protocol === 'https:');
|
|
35
|
+
} catch (e) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Shared unsaved-changes guard. Wraps `useConfirmDialog` with standardized
|
|
42
|
+
* messaging, intercepts same-origin link clicks so the dialog fires instead
|
|
43
|
+
* of a silent navigation, and adds a `beforeunload` listener as the last-resort
|
|
44
|
+
* guard for refresh / tab-close (browser-native, cannot be styled). The
|
|
45
|
+
* returned `confirmDialog` element must be rendered in JSX.
|
|
46
|
+
*
|
|
47
|
+
* Single-consumer constraint: the click handler is attached at the document
|
|
48
|
+
* level in capture phase. Two simultaneously-active instances will both fire
|
|
49
|
+
* a dialog. A development-only warning surfaces this.
|
|
50
|
+
*/
|
|
51
|
+
function useUnsavedChangesDialog(_ref) {
|
|
52
|
+
var when = _ref.when;
|
|
53
|
+
var _useConfirmDialog = (0, _useConfirmDialog2["default"])({
|
|
54
|
+
when: when,
|
|
55
|
+
message: (0, _i18n.__)('You have unsaved changes that will be lost. Discard changes?', 'newspack-plugin'),
|
|
56
|
+
confirmButtonText: (0, _i18n.__)('Discard changes', 'newspack-plugin'),
|
|
57
|
+
hideTitle: true
|
|
58
|
+
}),
|
|
59
|
+
confirmDialog = _useConfirmDialog.confirmDialog,
|
|
60
|
+
requestConfirm = _useConfirmDialog.requestConfirm;
|
|
61
|
+
|
|
62
|
+
// Tracks navigation the user has already approved via our custom dialog so
|
|
63
|
+
// the beforeunload guard doesn't fire a second native prompt on top of it.
|
|
64
|
+
var isNavigatingRef = (0, _element.useRef)(false);
|
|
65
|
+
(0, _element.useEffect)(function () {
|
|
66
|
+
if (!when) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
var ownerId = Symbol('unsaved-changes-guard');
|
|
70
|
+
activeGuards.push(ownerId);
|
|
71
|
+
if (process.env.NODE_ENV !== 'production' && activeGuards.length > 1) {
|
|
72
|
+
// eslint-disable-next-line no-console
|
|
73
|
+
console.warn('useUnsavedChangesDialog: more than one active instance detected. ' + 'Document-level click capture will fire a dialog per instance — ensure only one guard is active at a time.');
|
|
74
|
+
}
|
|
75
|
+
var handler = function handler(e) {
|
|
76
|
+
// Only the top-of-stack guard prompts, so concurrent guards can't stack dialogs.
|
|
77
|
+
if (activeGuards[activeGuards.length - 1] !== ownerId) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (e.defaultPrevented || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || e.button !== 0) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
var target = e.target;
|
|
84
|
+
var link = target === null || target === void 0 ? void 0 : target.closest('a[href]');
|
|
85
|
+
if (!link) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
var href = link.getAttribute('href');
|
|
89
|
+
if (!href || href.startsWith('#') || href.startsWith('javascript:')) {
|
|
90
|
+
// All `#`-prefixed links are skipped here: plain anchors
|
|
91
|
+
// (`#section`) are scroll targets, and HashRouter paths
|
|
92
|
+
// (`#/route`) are intercepted by `ConfirmDialog`'s built-in
|
|
93
|
+
// `history.block` listener instead. Routing them through
|
|
94
|
+
// `window.location.href` here would trigger a hashchange
|
|
95
|
+
// that the still-active block re-catches, double-prompting.
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (link.target && link.target !== '_self') {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
// Skip mailto:, tel:, external origins, and any non-http(s) scheme —
|
|
102
|
+
// they don't unload the wizard, so a discard prompt would be wrong.
|
|
103
|
+
if (!isSameOriginNavigation(link)) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
e.preventDefault();
|
|
107
|
+
e.stopPropagation();
|
|
108
|
+
var destination = link.href;
|
|
109
|
+
requestConfirm(function () {
|
|
110
|
+
isNavigatingRef.current = true;
|
|
111
|
+
window.location.href = destination;
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
document.addEventListener('click', handler, true);
|
|
115
|
+
return function () {
|
|
116
|
+
document.removeEventListener('click', handler, true);
|
|
117
|
+
var idx = activeGuards.indexOf(ownerId);
|
|
118
|
+
if (idx !== -1) {
|
|
119
|
+
activeGuards.splice(idx, 1);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}, [when, requestConfirm]);
|
|
123
|
+
(0, _element.useEffect)(function () {
|
|
124
|
+
if (!when) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
var handler = function handler(e) {
|
|
128
|
+
if (isNavigatingRef.current) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
e.preventDefault();
|
|
132
|
+
e.returnValue = '';
|
|
133
|
+
};
|
|
134
|
+
window.addEventListener('beforeunload', handler);
|
|
135
|
+
return function () {
|
|
136
|
+
return window.removeEventListener('beforeunload', handler);
|
|
137
|
+
};
|
|
138
|
+
}, [when]);
|
|
139
|
+
return {
|
|
140
|
+
confirmDialog: confirmDialog,
|
|
141
|
+
requestConfirm: requestConfirm
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
var _default = exports["default"] = useUnsavedChangesDialog;
|
|
@@ -4,7 +4,9 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
4
4
|
Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
|
-
exports
|
|
7
|
+
exports["default"] = void 0;
|
|
8
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
9
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
8
10
|
var _element = require("@wordpress/element");
|
|
9
11
|
var _isArray = _interopRequireDefault(require("lodash/isArray"));
|
|
10
12
|
var _mergeWith = _interopRequireDefault(require("lodash/mergeWith"));
|
|
@@ -17,8 +19,8 @@ var _mergeWith = _interopRequireDefault(require("lodash/mergeWith"));
|
|
|
17
19
|
* @property {Object.<string, any>} [update] The update object with key-value pairs to merge into the state.
|
|
18
20
|
*/
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
if ((0, _isArray
|
|
22
|
+
var mergeCustomizer = function mergeCustomizer(objValue, srcValue) {
|
|
23
|
+
if ((0, _isArray["default"])(objValue)) {
|
|
22
24
|
// If it's an array, replace it (instead of concatenating).
|
|
23
25
|
return srcValue;
|
|
24
26
|
}
|
|
@@ -32,17 +34,24 @@ const mergeCustomizer = (objValue, srcValue) => {
|
|
|
32
34
|
* @param {T} initial Initial state object.
|
|
33
35
|
* @return {[T, (keyOrUpdate: string | Partial<T>) => (value?: any) => void]} The state object and a function to update it.
|
|
34
36
|
*/
|
|
35
|
-
var _default =
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
var _default = exports["default"] = function _default() {
|
|
38
|
+
var initial = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
39
|
+
var _useState = (0, _element.useState)(initial),
|
|
40
|
+
_useState2 = (0, _slicedToArray2["default"])(_useState, 2),
|
|
41
|
+
stateObject = _useState2[0],
|
|
42
|
+
setStateObject = _useState2[1];
|
|
43
|
+
var runUpdate = function runUpdate(update) {
|
|
44
|
+
return setStateObject(function (_stateObject) {
|
|
45
|
+
return (0, _mergeWith["default"])({}, _stateObject, update, mergeCustomizer);
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
var updateStateObject = function updateStateObject(keyOrUpdate) {
|
|
39
49
|
if (typeof keyOrUpdate === 'string') {
|
|
40
|
-
return value
|
|
41
|
-
[
|
|
42
|
-
}
|
|
50
|
+
return function (value) {
|
|
51
|
+
return runUpdate((0, _defineProperty2["default"])({}, keyOrUpdate, value));
|
|
52
|
+
};
|
|
43
53
|
}
|
|
44
54
|
runUpdate(keyOrUpdate);
|
|
45
55
|
};
|
|
46
56
|
return [stateObject, updateStateObject];
|
|
47
|
-
};
|
|
48
|
-
exports.default = _default;
|
|
57
|
+
};
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
4
5
|
var _react = _interopRequireDefault(require("react"));
|
|
5
6
|
var _react2 = require("@testing-library/react");
|
|
6
|
-
var
|
|
7
|
+
var _useObjectState3 = _interopRequireDefault(require("./useObjectState"));
|
|
7
8
|
var _jsxRuntime = require("react/jsx-runtime");
|
|
8
|
-
|
|
9
|
+
var INIT_STATE = {
|
|
9
10
|
name: 'Foo',
|
|
10
11
|
widgets: [1],
|
|
11
12
|
attributes: {
|
|
@@ -13,68 +14,89 @@ const INIT_STATE = {
|
|
|
13
14
|
baz: 1
|
|
14
15
|
}
|
|
15
16
|
};
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
var TestComponent = function TestComponent() {
|
|
18
|
+
var _useObjectState = (0, _useObjectState3["default"])(INIT_STATE),
|
|
19
|
+
_useObjectState2 = (0, _slicedToArray2["default"])(_useObjectState, 2),
|
|
20
|
+
state = _useObjectState2[0],
|
|
21
|
+
updateState = _useObjectState2[1];
|
|
18
22
|
return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
|
|
19
23
|
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
|
|
20
24
|
placeholder: "state",
|
|
21
|
-
onChange: ()
|
|
25
|
+
onChange: function onChange() {},
|
|
22
26
|
value: JSON.stringify(state)
|
|
23
27
|
}), /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
|
|
24
|
-
onClick: ()
|
|
25
|
-
|
|
26
|
-
|
|
28
|
+
onClick: function onClick() {
|
|
29
|
+
return updateState({
|
|
30
|
+
widgets: []
|
|
31
|
+
});
|
|
32
|
+
},
|
|
27
33
|
children: "Remove widgets"
|
|
28
34
|
}), /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
|
|
29
|
-
onClick: ()
|
|
30
|
-
|
|
31
|
-
|
|
35
|
+
onClick: function onClick() {
|
|
36
|
+
return updateState({
|
|
37
|
+
widgets: [1]
|
|
38
|
+
});
|
|
39
|
+
},
|
|
32
40
|
children: "Add widget"
|
|
33
41
|
}), /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
|
|
34
|
-
onClick: ()
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
42
|
+
onClick: function onClick() {
|
|
43
|
+
return updateState({
|
|
44
|
+
attributes: {
|
|
45
|
+
bar: 2
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
},
|
|
39
49
|
children: "Nested update"
|
|
40
50
|
}), /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
|
|
41
51
|
type: "text",
|
|
42
52
|
value: state.name,
|
|
43
|
-
onChange: e
|
|
44
|
-
|
|
45
|
-
|
|
53
|
+
onChange: function onChange(e) {
|
|
54
|
+
return updateState({
|
|
55
|
+
name: e.target.value
|
|
56
|
+
});
|
|
57
|
+
},
|
|
46
58
|
placeholder: "name"
|
|
47
59
|
})]
|
|
48
60
|
});
|
|
49
61
|
};
|
|
50
|
-
describe('useObjectState', ()
|
|
51
|
-
|
|
52
|
-
|
|
62
|
+
describe('useObjectState', function () {
|
|
63
|
+
var getState = function getState() {
|
|
64
|
+
return JSON.parse(_react2.screen.getByPlaceholderText('state').getAttribute('value'));
|
|
65
|
+
};
|
|
66
|
+
beforeEach(function () {
|
|
53
67
|
(0, _react2.render)(/*#__PURE__*/(0, _jsxRuntime.jsx)(TestComponent, {}));
|
|
54
68
|
});
|
|
55
|
-
it('updates arrays', ()
|
|
69
|
+
it('updates arrays', function () {
|
|
56
70
|
expect(getState()).toStrictEqual(INIT_STATE);
|
|
57
|
-
(0, _react2.act)(()
|
|
71
|
+
(0, _react2.act)(function () {
|
|
72
|
+
return _react2.screen.getByText('Remove widgets').click();
|
|
73
|
+
});
|
|
58
74
|
expect(getState()).toMatchObject({
|
|
59
75
|
widgets: []
|
|
60
76
|
});
|
|
61
|
-
(0, _react2.act)(()
|
|
77
|
+
(0, _react2.act)(function () {
|
|
78
|
+
return _react2.screen.getByText('Add widget').click();
|
|
79
|
+
});
|
|
62
80
|
expect(getState()).toMatchObject({
|
|
63
81
|
widgets: [1]
|
|
64
82
|
});
|
|
65
83
|
});
|
|
66
|
-
it('updates a simple value', ()
|
|
67
|
-
(0, _react2.act)(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
84
|
+
it('updates a simple value', function () {
|
|
85
|
+
(0, _react2.act)(function () {
|
|
86
|
+
return _react2.fireEvent.change(_react2.screen.getByPlaceholderText('name'), {
|
|
87
|
+
target: {
|
|
88
|
+
value: 'Ramon'
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
});
|
|
72
92
|
expect(getState()).toMatchObject({
|
|
73
93
|
name: 'Ramon'
|
|
74
94
|
});
|
|
75
95
|
});
|
|
76
|
-
it('updates a nested object', ()
|
|
77
|
-
(0, _react2.act)(()
|
|
96
|
+
it('updates a nested object', function () {
|
|
97
|
+
(0, _react2.act)(function () {
|
|
98
|
+
return _react2.screen.getByText('Nested update').click();
|
|
99
|
+
});
|
|
78
100
|
expect(getState()).toMatchObject({
|
|
79
101
|
attributes: {
|
|
80
102
|
bar: 2,
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
exports["default"] = void 0;
|
|
7
7
|
var _element = require("@wordpress/element");
|
|
8
8
|
// https://usehooks.com/useOnClickOutside/
|
|
9
9
|
|
|
@@ -11,9 +11,9 @@ var _element = require("@wordpress/element");
|
|
|
11
11
|
* External dependencies.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
(0, _element.useEffect)(()
|
|
16
|
-
|
|
14
|
+
var useOnClickOutside = function useOnClickOutside(ref, handler) {
|
|
15
|
+
(0, _element.useEffect)(function () {
|
|
16
|
+
var listener = function listener(event) {
|
|
17
17
|
// Do nothing if clicking ref's element or descendent elements
|
|
18
18
|
if (!ref.current || ref.current.contains(event.target)) {
|
|
19
19
|
return;
|
|
@@ -22,7 +22,7 @@ const useOnClickOutside = (ref, handler) => {
|
|
|
22
22
|
};
|
|
23
23
|
document.addEventListener('mousedown', listener);
|
|
24
24
|
document.addEventListener('touchstart', listener);
|
|
25
|
-
return ()
|
|
25
|
+
return function () {
|
|
26
26
|
document.removeEventListener('mousedown', listener);
|
|
27
27
|
document.removeEventListener('touchstart', listener);
|
|
28
28
|
};
|
|
@@ -35,4 +35,4 @@ const useOnClickOutside = (ref, handler) => {
|
|
|
35
35
|
// ... passing it into this hook.
|
|
36
36
|
[ref, handler]);
|
|
37
37
|
};
|
|
38
|
-
var _default = exports
|
|
38
|
+
var _default = exports["default"] = useOnClickOutside;
|
|
@@ -4,7 +4,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
4
4
|
Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
|
-
exports
|
|
7
|
+
exports["default"] = void 0;
|
|
8
8
|
var _element = require("@wordpress/element");
|
|
9
9
|
var _router = _interopRequireDefault(require("../proxied-imports/router"));
|
|
10
10
|
// https://gist.github.com/sibelius/60a4e11da1f826b8d60dc3975a1ac805
|
|
@@ -17,13 +17,11 @@ var _router = _interopRequireDefault(require("../proxied-imports/router"));
|
|
|
17
17
|
* Internal dependencies.
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
var
|
|
24
|
-
|
|
25
|
-
const self = (0, _element.useRef)(null);
|
|
26
|
-
const onWindowOrTabClose = event => {
|
|
20
|
+
var useHistory = _router["default"].useHistory;
|
|
21
|
+
var _default = exports["default"] = function _default(when, message) {
|
|
22
|
+
var history = useHistory();
|
|
23
|
+
var self = (0, _element.useRef)(null);
|
|
24
|
+
var onWindowOrTabClose = function onWindowOrTabClose(event) {
|
|
27
25
|
if (!when) {
|
|
28
26
|
return;
|
|
29
27
|
}
|
|
@@ -32,18 +30,17 @@ var _default = (when, message) => {
|
|
|
32
30
|
}
|
|
33
31
|
return message;
|
|
34
32
|
};
|
|
35
|
-
(0, _element.useEffect)(()
|
|
33
|
+
(0, _element.useEffect)(function () {
|
|
36
34
|
if (when) {
|
|
37
35
|
self.current = history.block(message);
|
|
38
36
|
window.addEventListener('beforeunload', onWindowOrTabClose);
|
|
39
37
|
}
|
|
40
|
-
return ()
|
|
38
|
+
return function () {
|
|
41
39
|
if (self.current) {
|
|
42
40
|
self.current();
|
|
43
41
|
}
|
|
44
42
|
window.removeEventListener('beforeunload', onWindowOrTabClose);
|
|
45
43
|
};
|
|
46
44
|
}, [message, when]);
|
|
47
|
-
return self.current || (
|
|
48
|
-
};
|
|
49
|
-
exports.default = _default;
|
|
45
|
+
return self.current || function () {};
|
|
46
|
+
};
|