lucid-ui
Version:
A UI component library from AppNexus.
216 lines • 13 kB
JavaScript
"use strict";
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
return cooked;
};
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AutocompleteDumb = void 0;
var react_1 = __importDefault(require("react"));
var prop_types_1 = __importDefault(require("react-peek/prop-types"));
var lodash_1 = __importDefault(require("lodash"));
var component_types_1 = require("../../util/component-types");
var style_helpers_1 = require("../../util/style-helpers");
var state_management_1 = require("../../util/state-management");
var text_manipulation_1 = require("../../util/text-manipulation");
var reducers = __importStar(require("./Autocomplete.reducers"));
var KEYCODE = __importStar(require("../../constants/key-code"));
var DropMenu_1 = require("../DropMenu/DropMenu");
var cx = style_helpers_1.lucidClassNames.bind('&-Autocomplete');
var arrayOf = prop_types_1.default.arrayOf, bool = prop_types_1.default.bool, func = prop_types_1.default.func, object = prop_types_1.default.object, shape = prop_types_1.default.shape, string = prop_types_1.default.string;
var Autocomplete = component_types_1.createClass({
statics: {
peek: {
description: "\n\t\t\t\tA text input with suggested values displayed in an attached menu.\n\t\t\t",
categories: ['controls', 'text'],
madeFrom: ['DropMenu'],
},
},
displayName: 'Autocomplete',
reducers: reducers,
propTypes: {
className: string(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n\t\t\tAppended to the component-specific class names set on the root elements.\n\t\t"], ["\n\t\t\tAppended to the component-specific class names set on the root elements.\n\t\t"]))),
style: object(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n\t\t\tStyles that are passed through to root element.\n\t\t"], ["\n\t\t\tStyles that are passed through to root element.\n\t\t"]))),
isDisabled: bool(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n\t\t\tDisables the Autocomplete from being clicked or focused.\n\t\t"], ["\n\t\t\tDisables the Autocomplete from being clicked or focused.\n\t\t"]))),
suggestions: arrayOf(string)(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n\t\t\tArray of suggested text input values shown in drop menu.\n\t\t"], ["\n\t\t\tArray of suggested text input values shown in drop menu.\n\t\t"]))),
value: string(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n\t\t\tText value of the input.\n\t\t"], ["\n\t\t\tText value of the input.\n\t\t"]))),
DropMenu: shape(DropMenu_1.DropMenuDumb.propTypes)(templateObject_6 || (templateObject_6 = __makeTemplateObject(["\n\t\t\tObject of DropMenu props which are passed thru to the underlying DropMenu\n\t\t\tcomponent.\n\t\t"], ["\n\t\t\tObject of DropMenu props which are passed thru to the underlying DropMenu\n\t\t\tcomponent.\n\t\t"]))),
onChange: func(templateObject_7 || (templateObject_7 = __makeTemplateObject(["\n\t\t\tCalled when the input value changes. Has the signature\n\t\t\t`(value, {props, event}) => {}` where value is a string.\n\t\t"], ["\n\t\t\tCalled when the input value changes. Has the signature\n\t\t\t\\`(value, {props, event}) => {}\\` where value is a string.\n\t\t"]))),
onSelect: func(templateObject_8 || (templateObject_8 = __makeTemplateObject(["\n\t\t\tCalled when a suggstion is selected from the menu. Has the signature\n\t\t\t`(optionIndex, {props, event}) => {}` where optionIndex is a number.\n\t\t"], ["\n\t\t\tCalled when a suggstion is selected from the menu. Has the signature\n\t\t\t\\`(optionIndex, {props, event}) => {}\\` where optionIndex is a number.\n\t\t"]))),
onExpand: func(templateObject_9 || (templateObject_9 = __makeTemplateObject(["\n\t\t\tCalled when menu is expected to expand. Has the signature\n\t\t\t`({props, event}) => {}`.\n\t\t"], ["\n\t\t\tCalled when menu is expected to expand. Has the signature\n\t\t\t\\`({props, event}) => {}\\`.\n\t\t"]))),
},
getDefaultProps: function () {
return {
isDisabled: false,
suggestions: [],
value: '',
onChange: lodash_1.default.noop,
onSelect: lodash_1.default.noop,
onExpand: lodash_1.default.noop,
DropMenu: DropMenu_1.DropMenuDumb.defaultProps,
}; // TODO: typescript hack that should be removed
},
handleSelect: function (optionIndex, _a) {
var event = _a.event;
var _b = this.props, suggestions = _b.suggestions, onChange = _b.onChange, onSelect = _b.onSelect;
onChange(suggestions[optionIndex], { event: event, props: this.props });
onSelect(optionIndex, { event: event, props: this.props });
},
handleInput: function (event) {
var _a = this.props, onChange = _a.onChange, onExpand = _a.onExpand, onCollapse = _a.DropMenu.onCollapse;
onChange(event.target.value, { event: event, props: this.props });
if (!lodash_1.default.isEmpty(event.target.value)) {
onExpand({ event: event, props: this.props });
}
else {
onCollapse();
}
},
getInputValue: function () {
return lodash_1.default.get(this, 'inputRef.value', this.props.value);
},
setInputValue: function (value) {
if (this.inputRef) {
this.inputRef.value = value;
}
},
handleInputKeydown: function (event) {
var _a = this.props, onExpand = _a.onExpand, _b = _a.DropMenu, isExpanded = _b.isExpanded, focusedIndex = _b.focusedIndex, onCollapse = _b.onCollapse;
var value = this.getInputValue();
if (event.keyCode === KEYCODE.Tab && isExpanded && focusedIndex !== null) {
this.handleSelect(focusedIndex, { event: event, props: this.props });
event.preventDefault();
}
if (event.keyCode === KEYCODE.ArrowDown && !isExpanded) {
event.stopPropagation();
if (lodash_1.default.isEmpty(value)) {
onExpand({ event: event, props: this.props });
}
}
if (event.keyCode === KEYCODE.Escape) {
event.stopPropagation();
onCollapse(event);
}
if (event.keyCode === KEYCODE.Enter && focusedIndex === null) {
event.stopPropagation();
onCollapse(event);
}
},
handleControlClick: function (event) {
var _a = this.props, onExpand = _a.onExpand, _b = _a.DropMenu, isExpanded = _b.isExpanded, onCollapse = _b.onCollapse;
if (event.target === this.inputRef) {
onExpand({ event: event, props: this.props });
}
else {
if (isExpanded) {
onCollapse(event);
}
else {
onExpand({ event: event, props: this.props });
}
this.inputRef.focus();
}
},
componentDidMount: function () {
var value = this.props.value;
this.inputRef.addEventListener('input', this.handleInput);
this.setInputValue(value);
},
UNSAFE_componentWillReceiveProps: function (nextProps) {
// TODO: typescript hack that should be removed
var value = nextProps.value;
if (value !== this.getInputValue()) {
this.setInputValue(value);
}
},
componentWillUnmount: function () {
if (this.inputRef) {
this.inputRef.removeEventListener('input', this.handleInput);
}
},
render: function () {
var _this = this;
var _a = this.props, style = _a.style, className = _a.className, isDisabled = _a.isDisabled, dropMenuProps = _a.DropMenu, suggestions = _a.suggestions, passThroughs = __rest(_a, ["style", "className", "isDisabled", "DropMenu", "suggestions"]); // TODO: typescript hack that should be removed
var isExpanded = dropMenuProps.isExpanded;
var value = this.getInputValue();
var valuePattern = new RegExp(lodash_1.default.escapeRegExp(value), 'i');
return (react_1.default.createElement(DropMenu_1.DropMenuDumb, __assign({}, dropMenuProps, { isDisabled: isDisabled, selectedIndices: [], className: cx('&', className), onSelect: this.handleSelect, style: style }),
react_1.default.createElement(DropMenu_1.DropMenuDumb.Control, __assign({}, {
onClick: this.handleControlClick,
} /* TODO: typescript hack that should be removed */),
react_1.default.createElement("div", { className: cx('&-Control', {
'&-Control-is-expanded': isExpanded,
'&-Control-is-disabled': isDisabled,
}) },
react_1.default.createElement("input", __assign({}, lodash_1.default.omit(passThroughs, [
'onChange',
'onSelect',
'onExpand',
'value',
'children',
]), { type: 'text', className: cx('&-Control-input'), ref: function (ref) { return (_this.inputRef = ref); }, onKeyDown: this.handleInputKeydown, disabled: isDisabled })))),
value
? lodash_1.default.map(suggestions, function (suggestion) { return (react_1.default.createElement(DropMenu_1.DropMenuDumb.Option, { key: 'AutocompleteOption' + suggestion }, (function () {
var _a = text_manipulation_1.partitionText(suggestion, valuePattern, value.length), pre = _a[0], match = _a[1], post = _a[2];
var formattedSuggestion = [];
if (pre) {
formattedSuggestion.push(react_1.default.createElement("span", { key: "AutocompleteOption-suggestion-pre-" + suggestion, className: cx('&-Option-suggestion-pre') }, pre));
}
if (match) {
formattedSuggestion.push(react_1.default.createElement("span", { key: "AutocompleteOption-suggestion-match-" + suggestion, className: cx('&-Option-suggestion-match') }, match));
}
if (post) {
formattedSuggestion.push(react_1.default.createElement("span", { key: "AutocompleteOption-suggestion-post-" + suggestion, className: cx('&-Option-suggestion-post') }, post));
}
return formattedSuggestion;
})())); })
: lodash_1.default.map(suggestions, function (suggestion) { return (react_1.default.createElement(DropMenu_1.DropMenuDumb.Option, { key: 'AutocompleteOption' + suggestion }, suggestion)); })));
},
});
exports.AutocompleteDumb = Autocomplete;
exports.default = state_management_1.buildHybridComponent(Autocomplete);
var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7, templateObject_8, templateObject_9;
//# sourceMappingURL=Autocomplete.js.map