auth0-lock
Version:
Auth0 Lock
254 lines (250 loc) • 12.1 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = _interopRequireDefault(require("react"));
var _reactDom = _interopRequireDefault(require("react-dom"));
var _button = require("./box/button");
var _text_input = _interopRequireDefault(require("./input/text_input"));
var _media_utils = require("../utils/media_utils");
var su = _interopRequireWildcard(require("../utils/string_utils"));
var l = _interopRequireWildcard(require("../core/index"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
var cycle = function cycle(xs, x) {
return xs.skipWhile(function (y) {
return y !== x;
}).get(1, xs.get(0));
};
var FiltrableList = exports.default = /*#__PURE__*/function (_React$Component) {
function FiltrableList(props) {
var _this;
_classCallCheck(this, FiltrableList);
_this = _callSuper(this, FiltrableList, [props]);
_this.state = {
filteredItems: props.items,
highlighted: props.defaultItem
};
return _this;
}
_inherits(FiltrableList, _React$Component);
return _createClass(FiltrableList, [{
key: "filter",
value: function filter(str) {
var filteredItems = this.props.items.filter(function (x) {
return su.matches(str, x.get('label'));
});
var highlighted = filteredItems.size === 1 && filteredItems.get(0) || filteredItems.includes(this.state.highlighted) && this.state.highlighted || null;
return {
filteredItems: filteredItems,
highlighted: highlighted
};
}
}, {
key: "select",
value: function select(x) {
this.props.onSelect(x);
}
}, {
key: "handleChange",
value: function handleChange(e) {
this.setState(this.filter(e.target.value));
}
}, {
key: "handleKeyDown",
value: function handleKeyDown(e) {
var _this$state = this.state,
filteredItems = _this$state.filteredItems,
highlighted = _this$state.highlighted;
switch (e.key) {
case 'ArrowDown':
e.preventDefault();
this.setState({
highlighted: cycle(filteredItems, highlighted)
});
break;
case 'ArrowUp':
e.preventDefault();
this.setState({
highlighted: cycle(filteredItems.reverse(), highlighted)
});
break;
case 'Enter':
e.preventDefault();
highlighted && this.select(highlighted);
break;
case 'Escape':
e.preventDefault();
this.props.onCancel();
default:
// no-op
}
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$props = this.props,
icon = _this$props.icon,
iconUrl = _this$props.iconUrl,
onCancel = _this$props.onCancel,
model = _this$props.model;
return /*#__PURE__*/_react.default.createElement("div", {
className: "auth0-lock-select-country"
}, /*#__PURE__*/_react.default.createElement("div", {
className: "auth0-lock-search"
}, /*#__PURE__*/_react.default.createElement(_button.BackButton, {
lockId: l.id(model),
onClick: onCancel
}), /*#__PURE__*/_react.default.createElement(_text_input.default, {
lockId: l.id(model),
name: "search",
icon: icon,
iconUrl: iconUrl,
isValid: true,
onChange: this.handleChange.bind(this),
onKeyDown: this.handleKeyDown.bind(this)
})), /*#__PURE__*/_react.default.createElement(List, {
highlighted: this.state.highlighted,
items: this.state.filteredItems,
onClick: this.select.bind(this),
onMouseMove: function onMouseMove(x) {
return _this2.setState({
highlighted: x
});
}
}));
}
}]);
}(_react.default.Component);
var List = /*#__PURE__*/function (_React$Component2) {
function List(props) {
var _this3;
_classCallCheck(this, List);
_this3 = _callSuper(this, List, [props]);
_this3.highlightedRef = /*#__PURE__*/_react.default.createRef();
_this3.listRef = /*#__PURE__*/_react.default.createRef();
return _this3;
}
_inherits(List, _React$Component2);
return _createClass(List, [{
key: "componentDidUpdate",
value: function componentDidUpdate() {
var _this4 = this;
// Ensure that highlighted item is entirely visible
// NOTE: I've spent very little time on this. It works, but it
// surely can be more clearly.
var highlighted = this.highlightedRef.current;
if (highlighted) {
var scrollableNode = this.listRef.current;
var highlightedNode = highlighted;
var relativeOffsetTop = highlightedNode.offsetTop - scrollableNode.scrollTop;
var scrollTopDelta = 0;
if (relativeOffsetTop + highlightedNode.offsetHeight > scrollableNode.clientHeight) {
scrollTopDelta = relativeOffsetTop + highlightedNode.offsetHeight - scrollableNode.clientHeight;
} else if (relativeOffsetTop < 0) {
scrollTopDelta = relativeOffsetTop;
}
if (scrollTopDelta) {
this.preventHighlight = true;
scrollableNode.scrollTop += scrollTopDelta;
if (this.timeout) clearTimeout(this.timeout);
this.timeout = setTimeout(function () {
return _this4.preventHighlight = false;
}, 100);
}
}
}
}, {
key: "mouseMoveHandler",
value: function mouseMoveHandler(x) {
// TODO: This is an ugly hack to avoid highlighting the element under the
// mouse when an arrow key trigger a scroll of the list (which in turn
// triggers a mousemove event).
!this.preventHighlight && this.props.onMouseMove(x);
}
}, {
key: "mouseLeaveHandler",
value: function mouseLeaveHandler() {
// TODO: clear highlighted?
}
}, {
key: "render",
value: function render() {
var _this5 = this;
var items = this.props.items.map(function (x) {
var highlighted = x === _this5.props.highlighted;
var props = {
highlighted: highlighted,
label: x.get('label'),
onClick: function onClick() {
return _this5.props.onClick(x);
},
onMouseMove: function onMouseMove() {
return _this5.mouseMoveHandler(x);
}
};
if (highlighted) props.ref = _this5.highlightedRef;
return /*#__PURE__*/_react.default.createElement(Item, _extends({
key: x.get('label')
}, props));
});
return /*#__PURE__*/_react.default.createElement("div", {
ref: this.listRef,
className: "auth0-lock-list-code",
onMouseLeave: this.mouseLeaveHandler.bind(this)
}, /*#__PURE__*/_react.default.createElement("ul", null, items));
}
}]);
}(_react.default.Component);
var Item = /*#__PURE__*/function (_React$Component3) {
function Item() {
_classCallCheck(this, Item);
return _callSuper(this, Item, arguments);
}
_inherits(Item, _React$Component3);
return _createClass(Item, [{
key: "shouldComponentUpdate",
value: function shouldComponentUpdate(nextProps) {
return this.props.highlighted != nextProps.highlighted;
}
}, {
key: "render",
value: function render() {
var _this$props2 = this.props,
highlighted = _this$props2.highlighted,
label = _this$props2.label,
onClick = _this$props2.onClick,
onMouseMove = _this$props2.onMouseMove;
var className = highlighted ? 'auth0-lock-list-code-highlighted' : '';
return /*#__PURE__*/_react.default.createElement("li", {
className: className,
onClick: onClick,
onMouseMove: onMouseMove
}, label);
}
}]);
}(_react.default.Component);
_defineProperty(Item, "propTypes", {
highlighted: _propTypes.default.bool.isRequired,
label: _propTypes.default.string.isRequired,
onClick: _propTypes.default.func.isRequired,
onMouseMove: _propTypes.default.func.isRequired
});