Projekt

Obecné

Profil

Stáhnout (9.76 KB) Statistiky
| Větev: | Revize:
1
import { Router, __RouterContext, matchPath } from 'react-router';
2
export * from 'react-router';
3
import _inheritsLoose from '@babel/runtime/helpers/esm/inheritsLoose';
4
import React from 'react';
5
import { createBrowserHistory, createHashHistory, createLocation } from 'history';
6
import PropTypes from 'prop-types';
7
import warning from 'tiny-warning';
8
import _extends from '@babel/runtime/helpers/esm/extends';
9
import _objectWithoutPropertiesLoose from '@babel/runtime/helpers/esm/objectWithoutPropertiesLoose';
10
import invariant from 'tiny-invariant';
11

    
12
/**
13
 * The public API for a <Router> that uses HTML5 history.
14
 */
15

    
16
var BrowserRouter =
17
/*#__PURE__*/
18
function (_React$Component) {
19
  _inheritsLoose(BrowserRouter, _React$Component);
20

    
21
  function BrowserRouter() {
22
    var _this;
23

    
24
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
25
      args[_key] = arguments[_key];
26
    }
27

    
28
    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
29
    _this.history = createBrowserHistory(_this.props);
30
    return _this;
31
  }
32

    
33
  var _proto = BrowserRouter.prototype;
34

    
35
  _proto.render = function render() {
36
    return React.createElement(Router, {
37
      history: this.history,
38
      children: this.props.children
39
    });
40
  };
41

    
42
  return BrowserRouter;
43
}(React.Component);
44

    
45
if (process.env.NODE_ENV !== "production") {
46
  BrowserRouter.propTypes = {
47
    basename: PropTypes.string,
48
    children: PropTypes.node,
49
    forceRefresh: PropTypes.bool,
50
    getUserConfirmation: PropTypes.func,
51
    keyLength: PropTypes.number
52
  };
53

    
54
  BrowserRouter.prototype.componentDidMount = function () {
55
    process.env.NODE_ENV !== "production" ? warning(!this.props.history, "<BrowserRouter> ignores the history prop. To use a custom history, " + "use `import { Router }` instead of `import { BrowserRouter as Router }`.") : void 0;
56
  };
57
}
58

    
59
/**
60
 * The public API for a <Router> that uses window.location.hash.
61
 */
62

    
63
var HashRouter =
64
/*#__PURE__*/
65
function (_React$Component) {
66
  _inheritsLoose(HashRouter, _React$Component);
67

    
68
  function HashRouter() {
69
    var _this;
70

    
71
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
72
      args[_key] = arguments[_key];
73
    }
74

    
75
    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
76
    _this.history = createHashHistory(_this.props);
77
    return _this;
78
  }
79

    
80
  var _proto = HashRouter.prototype;
81

    
82
  _proto.render = function render() {
83
    return React.createElement(Router, {
84
      history: this.history,
85
      children: this.props.children
86
    });
87
  };
88

    
89
  return HashRouter;
90
}(React.Component);
91

    
92
if (process.env.NODE_ENV !== "production") {
93
  HashRouter.propTypes = {
94
    basename: PropTypes.string,
95
    children: PropTypes.node,
96
    getUserConfirmation: PropTypes.func,
97
    hashType: PropTypes.oneOf(["hashbang", "noslash", "slash"])
98
  };
99

    
100
  HashRouter.prototype.componentDidMount = function () {
101
    process.env.NODE_ENV !== "production" ? warning(!this.props.history, "<HashRouter> ignores the history prop. To use a custom history, " + "use `import { Router }` instead of `import { HashRouter as Router }`.") : void 0;
102
  };
103
}
104

    
105
var resolveToLocation = function resolveToLocation(to, currentLocation) {
106
  return typeof to === "function" ? to(currentLocation) : to;
107
};
108
var normalizeToLocation = function normalizeToLocation(to, currentLocation) {
109
  return typeof to === "string" ? createLocation(to, null, null, currentLocation) : to;
110
};
111

    
112
var forwardRefShim = function forwardRefShim(C) {
113
  return C;
114
};
115

    
116
var forwardRef = React.forwardRef;
117

    
118
if (typeof forwardRef === "undefined") {
119
  forwardRef = forwardRefShim;
120
}
121

    
122
function isModifiedEvent(event) {
123
  return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
124
}
125

    
126
var LinkAnchor = forwardRef(function (_ref, forwardedRef) {
127
  var innerRef = _ref.innerRef,
128
      navigate = _ref.navigate,
129
      _onClick = _ref.onClick,
130
      rest = _objectWithoutPropertiesLoose(_ref, ["innerRef", "navigate", "onClick"]);
131

    
132
  var target = rest.target;
133

    
134
  var props = _extends({}, rest, {
135
    onClick: function onClick(event) {
136
      try {
137
        if (_onClick) _onClick(event);
138
      } catch (ex) {
139
        event.preventDefault();
140
        throw ex;
141
      }
142

    
143
      if (!event.defaultPrevented && // onClick prevented default
144
      event.button === 0 && ( // ignore everything but left clicks
145
      !target || target === "_self") && // let browser handle "target=_blank" etc.
146
      !isModifiedEvent(event) // ignore clicks with modifier keys
147
      ) {
148
          event.preventDefault();
149
          navigate();
150
        }
151
    }
152
  }); // React 15 compat
153

    
154

    
155
  if (forwardRefShim !== forwardRef) {
156
    props.ref = forwardedRef || innerRef;
157
  } else {
158
    props.ref = innerRef;
159
  }
160

    
161
  return React.createElement("a", props);
162
});
163

    
164
if (process.env.NODE_ENV !== "production") {
165
  LinkAnchor.displayName = "LinkAnchor";
166
}
167
/**
168
 * The public API for rendering a history-aware <a>.
169
 */
170

    
171

    
172
var Link = forwardRef(function (_ref2, forwardedRef) {
173
  var _ref2$component = _ref2.component,
174
      component = _ref2$component === void 0 ? LinkAnchor : _ref2$component,
175
      replace = _ref2.replace,
176
      to = _ref2.to,
177
      innerRef = _ref2.innerRef,
178
      rest = _objectWithoutPropertiesLoose(_ref2, ["component", "replace", "to", "innerRef"]);
179

    
180
  return React.createElement(__RouterContext.Consumer, null, function (context) {
181
    !context ? process.env.NODE_ENV !== "production" ? invariant(false, "You should not use <Link> outside a <Router>") : invariant(false) : void 0;
182
    var history = context.history;
183
    var location = normalizeToLocation(resolveToLocation(to, context.location), context.location);
184
    var href = location ? history.createHref(location) : "";
185

    
186
    var props = _extends({}, rest, {
187
      href: href,
188
      navigate: function navigate() {
189
        var location = resolveToLocation(to, context.location);
190
        var method = replace ? history.replace : history.push;
191
        method(location);
192
      }
193
    }); // React 15 compat
194

    
195

    
196
    if (forwardRefShim !== forwardRef) {
197
      props.ref = forwardedRef || innerRef;
198
    } else {
199
      props.innerRef = innerRef;
200
    }
201

    
202
    return React.createElement(component, props);
203
  });
204
});
205

    
206
if (process.env.NODE_ENV !== "production") {
207
  var toType = PropTypes.oneOfType([PropTypes.string, PropTypes.object, PropTypes.func]);
208
  var refType = PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.shape({
209
    current: PropTypes.any
210
  })]);
211
  Link.displayName = "Link";
212
  Link.propTypes = {
213
    innerRef: refType,
214
    onClick: PropTypes.func,
215
    replace: PropTypes.bool,
216
    target: PropTypes.string,
217
    to: toType.isRequired
218
  };
219
}
220

    
221
var forwardRefShim$1 = function forwardRefShim(C) {
222
  return C;
223
};
224

    
225
var forwardRef$1 = React.forwardRef;
226

    
227
if (typeof forwardRef$1 === "undefined") {
228
  forwardRef$1 = forwardRefShim$1;
229
}
230

    
231
function joinClassnames() {
232
  for (var _len = arguments.length, classnames = new Array(_len), _key = 0; _key < _len; _key++) {
233
    classnames[_key] = arguments[_key];
234
  }
235

    
236
  return classnames.filter(function (i) {
237
    return i;
238
  }).join(" ");
239
}
240
/**
241
 * A <Link> wrapper that knows if it's "active" or not.
242
 */
243

    
244

    
245
var NavLink = forwardRef$1(function (_ref, forwardedRef) {
246
  var _ref$ariaCurrent = _ref["aria-current"],
247
      ariaCurrent = _ref$ariaCurrent === void 0 ? "page" : _ref$ariaCurrent,
248
      _ref$activeClassName = _ref.activeClassName,
249
      activeClassName = _ref$activeClassName === void 0 ? "active" : _ref$activeClassName,
250
      activeStyle = _ref.activeStyle,
251
      classNameProp = _ref.className,
252
      exact = _ref.exact,
253
      isActiveProp = _ref.isActive,
254
      locationProp = _ref.location,
255
      strict = _ref.strict,
256
      styleProp = _ref.style,
257
      to = _ref.to,
258
      innerRef = _ref.innerRef,
259
      rest = _objectWithoutPropertiesLoose(_ref, ["aria-current", "activeClassName", "activeStyle", "className", "exact", "isActive", "location", "strict", "style", "to", "innerRef"]);
260

    
261
  return React.createElement(__RouterContext.Consumer, null, function (context) {
262
    !context ? process.env.NODE_ENV !== "production" ? invariant(false, "You should not use <NavLink> outside a <Router>") : invariant(false) : void 0;
263
    var currentLocation = locationProp || context.location;
264
    var toLocation = normalizeToLocation(resolveToLocation(to, currentLocation), currentLocation);
265
    var path = toLocation.pathname; // Regex taken from: https://github.com/pillarjs/path-to-regexp/blob/master/index.js#L202
266

    
267
    var escapedPath = path && path.replace(/([.+*?=^!:${}()[\]|/\\])/g, "\\$1");
268
    var match = escapedPath ? matchPath(currentLocation.pathname, {
269
      path: escapedPath,
270
      exact: exact,
271
      strict: strict
272
    }) : null;
273
    var isActive = !!(isActiveProp ? isActiveProp(match, currentLocation) : match);
274
    var className = isActive ? joinClassnames(classNameProp, activeClassName) : classNameProp;
275
    var style = isActive ? _extends({}, styleProp, {}, activeStyle) : styleProp;
276

    
277
    var props = _extends({
278
      "aria-current": isActive && ariaCurrent || null,
279
      className: className,
280
      style: style,
281
      to: toLocation
282
    }, rest); // React 15 compat
283

    
284

    
285
    if (forwardRefShim$1 !== forwardRef$1) {
286
      props.ref = forwardedRef || innerRef;
287
    } else {
288
      props.innerRef = innerRef;
289
    }
290

    
291
    return React.createElement(Link, props);
292
  });
293
});
294

    
295
if (process.env.NODE_ENV !== "production") {
296
  NavLink.displayName = "NavLink";
297
  var ariaCurrentType = PropTypes.oneOf(["page", "step", "location", "date", "time", "true"]);
298
  NavLink.propTypes = _extends({}, Link.propTypes, {
299
    "aria-current": ariaCurrentType,
300
    activeClassName: PropTypes.string,
301
    activeStyle: PropTypes.object,
302
    className: PropTypes.string,
303
    exact: PropTypes.bool,
304
    isActive: PropTypes.func,
305
    location: PropTypes.object,
306
    strict: PropTypes.bool,
307
    style: PropTypes.object
308
  });
309
}
310

    
311
export { BrowserRouter, HashRouter, Link, NavLink };
312
//# sourceMappingURL=react-router-dom.js.map
(1-1/2)