@panneau/display-link 3.0.142

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 (3) hide show
  1. package/es/index.js +55 -0
  2. package/lib/index.js +339 -0
  3. package/package.json +60 -0
package/es/index.js ADDED
@@ -0,0 +1,55 @@
1
+ import get from 'lodash/get';
2
+ import PropTypes from 'prop-types';
3
+ import React from 'react';
4
+ import { FormattedMessage } from 'react-intl';
5
+ import { Link as Link$2 } from 'wouter';
6
+
7
+ var propTypes = {
8
+ item: PropTypes.shape({
9
+ id: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
10
+ }),
11
+ value: PropTypes.string,
12
+ label: PropTypes.string,
13
+ labelPath: PropTypes.string,
14
+ external: PropTypes.bool,
15
+ target: PropTypes.string,
16
+ placeholder: PropTypes.oneOfType([PropTypes.node, PropTypes.string])
17
+ };
18
+ var defaultProps = {
19
+ item: null,
20
+ value: null,
21
+ label: null,
22
+ labelPath: null,
23
+ external: false,
24
+ target: null,
25
+ placeholder: null
26
+ };
27
+ var Link = function Link(_ref) {
28
+ var item = _ref.item,
29
+ label = _ref.label,
30
+ labelPath = _ref.labelPath,
31
+ value = _ref.value,
32
+ external = _ref.external,
33
+ target = _ref.target,
34
+ placeholder = _ref.placeholder;
35
+ var itemLabel = get(item, labelPath);
36
+ var finalValue = itemLabel || label || placeholder || /*#__PURE__*/React.createElement(FormattedMessage, {
37
+ id: "e0xuLo",
38
+ defaultMessage: [{
39
+ "type": 0,
40
+ "value": "Link"
41
+ }]
42
+ });
43
+ return external ? /*#__PURE__*/React.createElement("a", {
44
+ href: value,
45
+ target: target || '_blank',
46
+ rel: "noopener noreferrer"
47
+ }, finalValue) : /*#__PURE__*/React.createElement(Link$2, {
48
+ href: value
49
+ }, finalValue);
50
+ };
51
+ Link.propTypes = propTypes;
52
+ Link.defaultProps = defaultProps;
53
+ var Link$1 = Link;
54
+
55
+ export { Link$1 as default };
package/lib/index.js ADDED
@@ -0,0 +1,339 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var get = require('lodash/get');
6
+ var PropTypes = require('prop-types');
7
+ var React = require('react');
8
+ var reactIntl = require('react-intl');
9
+ var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
10
+ var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
11
+ var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
12
+ var regexparam = require('regexparam');
13
+ var index_js = require('use-sync-external-store/shim/index.js');
14
+
15
+ function _interopNamespaceDefault(e) {
16
+ var n = Object.create(null);
17
+ if (e) {
18
+ Object.keys(e).forEach(function (k) {
19
+ if (k !== 'default') {
20
+ var d = Object.getOwnPropertyDescriptor(e, k);
21
+ Object.defineProperty(n, k, d.get ? d : {
22
+ enumerable: true,
23
+ get: function () { return e[k]; }
24
+ });
25
+ }
26
+ });
27
+ }
28
+ n.default = e;
29
+ return Object.freeze(n);
30
+ }
31
+
32
+ var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
33
+
34
+ // React.useInsertionEffect is not available in React <18
35
+ var useEffect = React__namespace.useEffect,
36
+ useLayoutEffect = React__namespace.useLayoutEffect,
37
+ useRef = React__namespace.useRef,
38
+ useBuiltinInsertionEffect = React__namespace.useInsertionEffect;
39
+
40
+ // Copied from:
41
+ // https://github.com/facebook/react/blob/main/packages/shared/ExecutionEnvironment.js
42
+ var canUseDOM = !!(typeof window !== "undefined" && typeof window.document !== "undefined" && typeof window.document.createElement !== "undefined");
43
+
44
+ // Copied from:
45
+ // https://github.com/reduxjs/react-redux/blob/master/src/utils/useIsomorphicLayoutEffect.ts
46
+ // "React currently throws a warning when using useLayoutEffect on the server.
47
+ // To get around it, we can conditionally useEffect on the server (no-op) and
48
+ // useLayoutEffect in the browser."
49
+ var useIsomorphicLayoutEffect = canUseDOM ? useLayoutEffect : useEffect;
50
+
51
+ // useInsertionEffect is already a noop on the server.
52
+ // See: https://github.com/facebook/react/blob/main/packages/react-server/src/ReactFizzHooks.js
53
+ var useInsertionEffect = useBuiltinInsertionEffect || useIsomorphicLayoutEffect;
54
+
55
+ // Userland polyfill while we wait for the forthcoming
56
+ // https://github.com/reactjs/rfcs/blob/useevent/text/0000-useevent.md
57
+ // Note: "A high-fidelity polyfill for useEvent is not possible because
58
+ // there is no lifecycle or Hook in React that we can use to switch
59
+ // .current at the right timing."
60
+ // So we will have to make do with this "close enough" approach for now.
61
+ var useEvent = function useEvent(fn) {
62
+ var ref = useRef([fn, function () {
63
+ return ref[0].apply(ref, arguments);
64
+ }]).current;
65
+ // Per Dan Abramov: useInsertionEffect executes marginally closer to the
66
+ // correct timing for ref synchronization than useLayoutEffect on React 18.
67
+ // See: https://github.com/facebook/react/pull/25881#issuecomment-1356244360
68
+ useInsertionEffect(function () {
69
+ ref[0] = fn;
70
+ });
71
+ return ref[1];
72
+ };
73
+
74
+ /**
75
+ * History API docs @see https://developer.mozilla.org/en-US/docs/Web/API/History
76
+ */
77
+ var eventPopstate = "popstate";
78
+ var eventPushState = "pushState";
79
+ var eventReplaceState = "replaceState";
80
+ var eventHashchange = "hashchange";
81
+ var events = [eventPopstate, eventPushState, eventReplaceState, eventHashchange];
82
+ var subscribeToLocationUpdates = function subscribeToLocationUpdates(callback) {
83
+ for (var _i = 0, _events = events; _i < _events.length; _i++) {
84
+ var event = _events[_i];
85
+ addEventListener(event, callback);
86
+ }
87
+ return function () {
88
+ for (var _i2 = 0, _events2 = events; _i2 < _events2.length; _i2++) {
89
+ var _event = _events2[_i2];
90
+ removeEventListener(_event, callback);
91
+ }
92
+ };
93
+ };
94
+ var useLocationProperty = function useLocationProperty(fn, ssrFn) {
95
+ return index_js.useSyncExternalStore(subscribeToLocationUpdates, fn, ssrFn);
96
+ };
97
+ var currentSearch = function currentSearch() {
98
+ return location.search;
99
+ };
100
+ var useSearch = function useSearch() {
101
+ var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
102
+ _ref$ssrSearch = _ref.ssrSearch,
103
+ ssrSearch = _ref$ssrSearch === void 0 ? "" : _ref$ssrSearch;
104
+ return useLocationProperty(currentSearch, function () {
105
+ return ssrSearch;
106
+ });
107
+ };
108
+ var currentPathname = function currentPathname() {
109
+ return location.pathname;
110
+ };
111
+ var usePathname = function usePathname() {
112
+ var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
113
+ ssrPath = _ref2.ssrPath;
114
+ return useLocationProperty(currentPathname, ssrPath ? function () {
115
+ return ssrPath;
116
+ } : currentPathname);
117
+ };
118
+ var navigate = function navigate(to) {
119
+ var _ref3 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
120
+ _ref3$replace = _ref3.replace,
121
+ replace = _ref3$replace === void 0 ? false : _ref3$replace,
122
+ _ref3$state = _ref3.state,
123
+ state = _ref3$state === void 0 ? null : _ref3$state;
124
+ return history[replace ? eventReplaceState : eventPushState](state, "", to);
125
+ };
126
+
127
+ // the 2nd argument of the `useBrowserLocation` return value is a function
128
+ // that allows to perform a navigation.
129
+ var useBrowserLocation = function useBrowserLocation() {
130
+ var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
131
+ return [usePathname(opts), navigate];
132
+ };
133
+ var patchKey = Symbol["for"]("wouter_v3");
134
+
135
+ // While History API does have `popstate` event, the only
136
+ // proper way to listen to changes via `push/replaceState`
137
+ // is to monkey-patch these methods.
138
+ //
139
+ // See https://stackoverflow.com/a/4585031
140
+ if (typeof history !== "undefined" && typeof window[patchKey] === "undefined") {
141
+ var _loop = function _loop() {
142
+ var type = _arr[_i3];
143
+ var original = history[type];
144
+ // TODO: we should be using unstable_batchedUpdates to avoid multiple re-renders,
145
+ // however that will require an additional peer dependency on react-dom.
146
+ // See: https://github.com/reactwg/react-18/discussions/86#discussioncomment-1567149
147
+ history[type] = function () {
148
+ var result = original.apply(this, arguments);
149
+ var event = new Event(type);
150
+ event.arguments = arguments;
151
+ dispatchEvent(event);
152
+ return result;
153
+ };
154
+ };
155
+ for (var _i3 = 0, _arr = [eventPushState, eventReplaceState]; _i3 < _arr.length; _i3++) {
156
+ _loop();
157
+ }
158
+
159
+ // patch history object only once
160
+ // See: https://github.com/molefrog/wouter/issues/167
161
+ Object.defineProperty(window, patchKey, {
162
+ value: true
163
+ });
164
+ }
165
+
166
+ var _excluded3 = ["to", "href", "onClick", "asChild", "children", "className", "replace", "state"];
167
+
168
+ /*
169
+ * Transforms `path` into its relative `base` version
170
+ * If base isn't part of the path provided returns absolute path e.g. `~/app`
171
+ */
172
+ var relativePath = function relativePath() {
173
+ var base = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
174
+ var path = arguments.length > 1 ? arguments[1] : undefined;
175
+ return !path.toLowerCase().indexOf(base.toLowerCase()) ? path.slice(base.length) || "/" : "~" + path;
176
+ };
177
+ var absolutePath = function absolutePath(to) {
178
+ var base = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
179
+ return to[0] === "~" ? to.slice(1) : base + to;
180
+ };
181
+
182
+ /*
183
+ * decodes escape sequences such as %20
184
+ */
185
+ var unescape = function unescape(str) {
186
+ try {
187
+ return decodeURI(str);
188
+ } catch (_e) {
189
+ // fail-safe mode: if string can't be decoded do nothing
190
+ return str;
191
+ }
192
+ };
193
+
194
+ /*
195
+ * Router and router context. Router is a lightweight object that represents the current
196
+ * routing options: how location is managed, base path etc.
197
+ *
198
+ * There is a default router present for most of the use cases, however it can be overridden
199
+ * via the <Router /> component.
200
+ */
201
+
202
+ var defaultRouter = {
203
+ hook: useBrowserLocation,
204
+ searchHook: useSearch,
205
+ parser: regexparam.parse,
206
+ base: "",
207
+ // this option is used to override the current location during SSR
208
+ ssrPath: undefined,
209
+ ssrSearch: undefined,
210
+ // customizes how `href` props are transformed for <Link />
211
+ hrefs: function hrefs(x) {
212
+ return x;
213
+ }
214
+ };
215
+ var RouterCtx = React.createContext(defaultRouter);
216
+
217
+ // gets the closest parent router from the context
218
+ var useRouter = function useRouter() {
219
+ return React.useContext(RouterCtx);
220
+ };
221
+
222
+ /**
223
+ * Parameters context. Used by `useParams()` to get the
224
+ * matched params from the innermost `Route` component.
225
+ */
226
+
227
+ React.createContext({});
228
+
229
+ /*
230
+ * Part 1, Hooks API: useRoute and useLocation
231
+ */
232
+
233
+ // Internal version of useLocation to avoid redundant useRouter calls
234
+
235
+ var useLocationFromRouter = function useLocationFromRouter(router) {
236
+ var _router$hook = router.hook(router),
237
+ _router$hook2 = _slicedToArray(_router$hook, 2),
238
+ location = _router$hook2[0],
239
+ navigate = _router$hook2[1];
240
+
241
+ // the function reference should stay the same between re-renders, so that
242
+ // it can be passed down as an element prop without any performance concerns.
243
+ // (This is achieved via `useEvent`.)
244
+ return [unescape(relativePath(router.base, location)), useEvent(function (to, navOpts) {
245
+ return navigate(absolutePath(to, router.base), navOpts);
246
+ })];
247
+ };
248
+ var Link$2 = React.forwardRef(function (props, ref) {
249
+ var router = useRouter();
250
+ var _useLocationFromRoute3 = useLocationFromRouter(router),
251
+ _useLocationFromRoute4 = _slicedToArray(_useLocationFromRoute3, 2),
252
+ path = _useLocationFromRoute4[0],
253
+ navigate = _useLocationFromRoute4[1];
254
+ var to = props.to,
255
+ _props$href = props.href,
256
+ _href = _props$href === void 0 ? to : _props$href,
257
+ _onClick = props.onClick,
258
+ asChild = props.asChild,
259
+ children = props.children,
260
+ cls = props.className;
261
+ props.replace;
262
+ props.state;
263
+ var restProps = _objectWithoutProperties(props, _excluded3);
264
+ var onClick = useEvent(function (event) {
265
+ // ignores the navigation when clicked using right mouse button or
266
+ // by holding a special modifier key: ctrl, command, win, alt, shift
267
+ if (event.ctrlKey || event.metaKey || event.altKey || event.shiftKey || event.button !== 0) return;
268
+ _onClick === null || _onClick === void 0 || _onClick(event);
269
+ if (!event.defaultPrevented) {
270
+ event.preventDefault();
271
+ navigate(_href, props);
272
+ }
273
+ });
274
+
275
+ // handle nested routers and absolute paths
276
+ var href = router.hrefs(_href[0] === "~" ? _href.slice(1) : router.base + _href, router // pass router as a second argument for convinience
277
+ );
278
+ return asChild && React.isValidElement(children) ? React.cloneElement(children, {
279
+ onClick: onClick,
280
+ href: href
281
+ }) : React.createElement("a", _objectSpread(_objectSpread({}, restProps), {}, {
282
+ onClick: onClick,
283
+ href: href,
284
+ // `className` can be a function to apply the class if this link is active
285
+ className: cls !== null && cls !== void 0 && cls.call ? cls(path === href) : cls,
286
+ children: children,
287
+ ref: ref
288
+ }));
289
+ });
290
+
291
+ var propTypes = {
292
+ item: PropTypes.shape({
293
+ id: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
294
+ }),
295
+ value: PropTypes.string,
296
+ label: PropTypes.string,
297
+ labelPath: PropTypes.string,
298
+ external: PropTypes.bool,
299
+ target: PropTypes.string,
300
+ placeholder: PropTypes.oneOfType([PropTypes.node, PropTypes.string])
301
+ };
302
+ var defaultProps = {
303
+ item: null,
304
+ value: null,
305
+ label: null,
306
+ labelPath: null,
307
+ external: false,
308
+ target: null,
309
+ placeholder: null
310
+ };
311
+ var Link = function Link(_ref) {
312
+ var item = _ref.item,
313
+ label = _ref.label,
314
+ labelPath = _ref.labelPath,
315
+ value = _ref.value,
316
+ external = _ref.external,
317
+ target = _ref.target,
318
+ placeholder = _ref.placeholder;
319
+ var itemLabel = get(item, labelPath);
320
+ var finalValue = itemLabel || label || placeholder || /*#__PURE__*/React.createElement(reactIntl.FormattedMessage, {
321
+ id: "e0xuLo",
322
+ defaultMessage: [{
323
+ "type": 0,
324
+ "value": "Link"
325
+ }]
326
+ });
327
+ return external ? /*#__PURE__*/React.createElement("a", {
328
+ href: value,
329
+ target: target || '_blank',
330
+ rel: "noopener noreferrer"
331
+ }, finalValue) : /*#__PURE__*/React.createElement(Link$2, {
332
+ href: value
333
+ }, finalValue);
334
+ };
335
+ Link.propTypes = propTypes;
336
+ Link.defaultProps = defaultProps;
337
+ var Link$1 = Link;
338
+
339
+ exports.default = Link$1;
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@panneau/display-link",
3
+ "version": "3.0.142",
4
+ "description": "Link item value in list",
5
+ "keywords": [
6
+ "javascript"
7
+ ],
8
+ "homepage": "https://github.com/folkloreinc/panneau-js",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/folkloreinc/panneau-js.git"
12
+ },
13
+ "author": {
14
+ "name": "Folklore",
15
+ "email": "info@folklore.email"
16
+ },
17
+ "contributors": [
18
+ {
19
+ "name": "David Mongeau-Petitpas",
20
+ "email": "dmp@folklore.email"
21
+ },
22
+ {
23
+ "name": "Nicolas Roy-Bourdages",
24
+ "email": "nrb@folklore.email"
25
+ }
26
+ ],
27
+ "license": "ISC",
28
+ "main": "lib/index.js",
29
+ "module": "es/index.js",
30
+ "files": [
31
+ "lib",
32
+ "es",
33
+ "assets"
34
+ ],
35
+ "scripts": {
36
+ "prepublishOnly": "npm run build",
37
+ "build": "../../scripts/prepare-package.sh"
38
+ },
39
+ "devDependencies": {
40
+ "react": "^16.8.0 || ^17.0.0 || ^18.0.0",
41
+ "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0"
42
+ },
43
+ "peerDependencies": {
44
+ "react": "^16.8.0 || ^17.0.0 || ^18.0.0",
45
+ "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0"
46
+ },
47
+ "dependencies": {
48
+ "@babel/runtime": "^7.12.5",
49
+ "@panneau/core": "^3.0.142",
50
+ "@panneau/themes": "^3.0.142",
51
+ "lodash": "^4.17.21",
52
+ "prop-types": "^15.7.2",
53
+ "react-intl": "^5.15.8||^6.0.0",
54
+ "wouter": "^3.1.0"
55
+ },
56
+ "publishConfig": {
57
+ "access": "public"
58
+ },
59
+ "gitHead": "293308a0e1b7175dcc56128f6dbc1416845d0b15"
60
+ }