lucid-ui
Version:
A UI component library from Xandr.
242 lines • 11.4 kB
JavaScript
"use strict";
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("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 = (0, component_types_1.createClass)({
statics: {
peek: {
description: "A text input with suggested values displayed in an attached menu.",
categories: ['controls', 'text'],
madeFrom: ['DropMenu'],
},
},
displayName: 'Autocomplete',
reducers: reducers,
propTypes: {
/**
Appended to the component-specific class names set on the root elements.
*/
className: string,
/**
Styles that are passed through to root element.
*/
style: object,
/**
Disables the Autocomplete from being clicked or focused.
*/
isDisabled: bool,
/**
Array of suggested text input values shown in drop menu.
*/
suggestions: arrayOf(string),
/**
Text value of the input.
*/
value: string,
/**
Object of DropMenu props which are passed thru to the underlying DropMenu
component.
*/
DropMenu: shape(DropMenu_1.DropMenuDumb.propTypes),
/**
Called when the input value changes. Has the signature
\`(value, {props, event}) => {}\` where value is a string.
*/
onChange: func,
/**
Called when a suggstion is selected from the menu. Has the signature
\`(optionIndex, {props, event}) => {}\` where optionIndex is a number.
*/
onSelect: func,
/**
Called when menu is expected to expand. Has the signature
\`({props, event}) => {}\`.
*/
onExpand: func,
},
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 = (0, 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-".concat(suggestion), className: cx('&-Option-suggestion-pre') }, pre));
}
if (match) {
formattedSuggestion.push(react_1.default.createElement("span", { key: "AutocompleteOption-suggestion-match-".concat(suggestion), className: cx('&-Option-suggestion-match') }, match));
}
if (post) {
formattedSuggestion.push(react_1.default.createElement("span", { key: "AutocompleteOption-suggestion-post-".concat(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 = (0, state_management_1.buildHybridComponent)(Autocomplete);
//# sourceMappingURL=Autocomplete.js.map