lucid-ui
Version:
A UI component library from AppNexus.
227 lines • 15.2 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
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 __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var prop_types_1 = __importDefault(require("react-peek/prop-types"));
var lodash_1 = __importDefault(require("lodash"));
var style_helpers_1 = require("../../util/style-helpers");
var component_types_1 = require("../../util/component-types");
var TextField_reducers_1 = __importDefault(require("./TextField.reducers"));
var KEYCODE = __importStar(require("../../constants/key-code"));
var cx = style_helpers_1.lucidClassNames.bind('&-TextField');
var bool = prop_types_1.default.bool, string = prop_types_1.default.string, func = prop_types_1.default.func, number = prop_types_1.default.number, object = prop_types_1.default.object, oneOfType = prop_types_1.default.oneOfType;
var TextField = /** @class */ (function (_super) {
__extends(TextField, _super);
function TextField() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.state = {
value: _this.props.value,
isHolding: false,
isMounted: false,
};
_this.textareaElement = react_1.default.createRef();
_this.inputElement = react_1.default.createRef();
_this.nativeElement = _this.props.isMultiLine
? _this.textareaElement
: _this.inputElement;
_this.handleChangeDebounced = lodash_1.default.debounce(function (value, _a) {
var event = _a.event, props = _a.props;
_this.props.onChangeDebounced &&
_this.props.onChangeDebounced(value, { event: event, props: props });
}, _this.props.debounceLevel);
_this.releaseHold = lodash_1.default.debounce(function () {
if (!_this.state.isMounted) {
return;
}
_this.setState({ isHolding: false });
}, _this.props.lazyLevel);
_this.updateWhenReady = lodash_1.default.debounce(function (newValue) {
if (!_this.state.isMounted) {
return;
}
if (_this.state.isHolding) {
_this.updateWhenReady(newValue);
}
else if (newValue !== _this.state.value) {
_this.setState({ value: newValue });
}
}, _this.props.lazyLevel);
_this.handleChange = function (event) {
var _a = _this.props, onChange = _a.onChange, onChangeDebounced = _a.onChangeDebounced;
var value = lodash_1.default.get(event, 'target.value', '');
_this.setState({ value: value, isHolding: true });
_this.releaseHold();
onChange && onChange(value, { event: event, props: _this.props });
// Also call the debounced handler in case the user wants debounced change
// events.
if (onChangeDebounced !== lodash_1.default.noop) {
event.persist(); // https://facebook.github.io/react/docs/events.html#event-pooling
_this.handleChangeDebounced(value, { event: event, props: _this.props });
}
};
_this.handleBlur = function (event) {
var _a = _this.props, onBlur = _a.onBlur, onChangeDebounced = _a.onChangeDebounced;
var value = lodash_1.default.get(event, 'target.value', '');
if (onChangeDebounced !== lodash_1.default.noop) {
_this.handleChangeDebounced.flush();
}
onBlur && onBlur(value, { event: event, props: _this.props });
};
_this.handleKeyDown = function (event) {
var _a = _this, props = _a.props, _b = _a.props, onSubmit = _b.onSubmit, onKeyDown = _b.onKeyDown, onChangeDebounced = _b.onChangeDebounced;
var value = lodash_1.default.get(event, 'target.value', '');
// If the consumer passed an onKeyDown, we call it
if (onKeyDown) {
onKeyDown({ event: event, props: props });
}
if (event.keyCode === KEYCODE.Enter) {
if (onChangeDebounced !== lodash_1.default.noop) {
_this.handleChangeDebounced.flush();
}
onSubmit && onSubmit(value, { event: event, props: _this.props });
}
};
_this.focus = function () {
/* istanbul ignore next */
_this.nativeElement.current.focus();
};
return _this;
}
TextField.prototype.UNSAFE_componentWillMount = function () {
this.setState({ isMounted: true });
};
TextField.prototype.componentWillUnmount = function () {
this.setState({ isMounted: false });
};
TextField.prototype.UNSAFE_componentWillReceiveProps = function (nextProps) {
// Allow consumer to optionally control state
if (lodash_1.default.has(nextProps, 'value')) {
if (this.state.isHolding) {
this.updateWhenReady(nextProps.value);
}
else {
this.setState({ value: nextProps.value });
}
}
};
TextField.prototype.render = function () {
var _a = this.props, className = _a.className, isDisabled = _a.isDisabled, isMultiLine = _a.isMultiLine, rows = _a.rows, style = _a.style, passThroughs = __rest(_a, ["className", "isDisabled", "isMultiLine", "rows", "style"]);
var value = this.state.value;
var finalProps = __assign(__assign({}, component_types_1.omitProps(passThroughs, undefined, __spreadArrays(Object.keys(TextField.propTypes), [
'children',
]))), { className: cx('&', {
'&-is-disabled': isDisabled,
'&-is-multi-line': isMultiLine,
'&-is-single-line': !isMultiLine,
}, className), disabled: isDisabled, onChange: this.handleChange, onBlur: this.handleBlur, onKeyDown: this.handleKeyDown, style: style,
rows: rows,
value: value });
return isMultiLine ? (react_1.default.createElement("textarea", __assign({ ref: this.textareaElement }, finalProps))) : (react_1.default.createElement("input", __assign({ type: 'text', ref: this.inputElement }, finalProps)));
};
TextField.displayName = 'TextField';
TextField.peek = {
description: "\n\t\t\tTextField should cover all your text input needs. It is able to handle\n\t\t\tsingle and multi line inputs.\n\t\t",
categories: ['controls', 'text'],
};
TextField.propTypes = {
style: object(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n\t\t\tStyles that are passed through to native control.\n\t\t"], ["\n\t\t\tStyles that are passed through to native control.\n\t\t"]))),
isMultiLine: bool(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n\t\t\tSet the TextField to multi line mode. Under the hood this will use a\n\t\t\t`textarea` instead of an `input` if set to `true`.\n\t\t"], ["\n\t\t\tSet the TextField to multi line mode. Under the hood this will use a\n\t\t\t\\`textarea\\` instead of an \\`input\\` if set to \\`true\\`.\n\t\t"]))),
isDisabled: bool(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n\t\t\tDisables the TextField by greying it out.\n\t\t"], ["\n\t\t\tDisables the TextField by greying it out.\n\t\t"]))),
rows: number(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n\t\t\tInitial number of rows a multi line TextField should have. Ignored when\n\t\t\tnot in multi-line mode.\n\t\t"], ["\n\t\t\tInitial number of rows a multi line TextField should have. Ignored when\n\t\t\tnot in multi-line mode.\n\t\t"]))),
className: string(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n\t\t\tClass names that are appended to the defaults.\n\t\t"], ["\n\t\t\tClass names that are appended to the defaults.\n\t\t"]))),
onChange: func(templateObject_6 || (templateObject_6 = __makeTemplateObject(["\n\t\t\tFires an event every time the user types text into the TextField.\n\t\t\tSignature: `(value, { event, props }) => {}`\n\t\t"], ["\n\t\t\tFires an event every time the user types text into the TextField.\n\t\t\tSignature: \\`(value, { event, props }) => {}\\`\n\t\t"]))),
onBlur: func(templateObject_7 || (templateObject_7 = __makeTemplateObject(["\n\t\t\tFires an on the `input`'s onBlur. Signature:\n\t\t\t`(currentValue, { event, props }) => {}`\n\t\t"], ["\n\t\t\tFires an on the \\`input\\`'s onBlur. Signature:\n\t\t\t\\`(currentValue, { event, props }) => {}\\`\n\t\t"]))),
onChangeDebounced: func(templateObject_8 || (templateObject_8 = __makeTemplateObject(["\n\t\t\tFires an event, debounced by `debounceLevel`, when the user types text\n\t\t\tinto the TextField. Signature: `(value, { event, props }) => {}`\n\t\t"], ["\n\t\t\tFires an event, debounced by \\`debounceLevel\\`, when the user types text\n\t\t\tinto the TextField. Signature: \\`(value, { event, props }) => {}\\`\n\t\t"]))),
onKeyDown: func(templateObject_9 || (templateObject_9 = __makeTemplateObject(["\n\t\t\tFires an event on every keydown Signature: `({ event, props }) => {}`\n\t\t"], ["\n\t\t\tFires an event on every keydown Signature: \\`({ event, props }) => {}\\`\n\t\t"]))),
onSubmit: func(templateObject_10 || (templateObject_10 = __makeTemplateObject(["\n\t\t\tFires an event when the user hits \"enter\" from the TextField. You\n\t\t\tshouldn't use it if you're using `isMultiLine`. Signature:\n\t\t\t`(value, { event, props }) => {}`\n\t\t"], ["\n\t\t\tFires an event when the user hits \"enter\" from the TextField. You\n\t\t\tshouldn't use it if you're using \\`isMultiLine\\`. Signature:\n\t\t\t\\`(value, { event, props }) => {}\\`\n\t\t"]))),
value: oneOfType([number, string])(templateObject_11 || (templateObject_11 = __makeTemplateObject(["\n\t\t\tSet the value of the input.\n\t\t"], ["\n\t\t\tSet the value of the input.\n\t\t"]))),
debounceLevel: number(templateObject_12 || (templateObject_12 = __makeTemplateObject(["\n\t\t\tNumber of milliseconds to debounce the `onChangeDebounced` callback.\n\t\t\tOnly useful if you provide an `onChangeDebounced` handler.\n\t\t"], ["\n\t\t\tNumber of milliseconds to debounce the \\`onChangeDebounced\\` callback.\n\t\t\tOnly useful if you provide an \\`onChangeDebounced\\` handler.\n\t\t"]))),
lazyLevel: number(templateObject_13 || (templateObject_13 = __makeTemplateObject(["\n\t\t\tSet the holding time, in milliseconds, that the component will wait if\n\t\t\tthe user is typing and the component gets a new `value` prop. Any time\n\t\t\tthe user hits a key, it starts a timer that prevents state changes from\n\t\t\tflowing in to the component until the timer has elapsed. This was\n\t\t\theavily inspired by the\n\t\t\t[lazy-input](https:/docs.npmjs.com/package/lazy-input) component.\n\t\t"], ["\n\t\t\tSet the holding time, in milliseconds, that the component will wait if\n\t\t\tthe user is typing and the component gets a new \\`value\\` prop. Any time\n\t\t\tthe user hits a key, it starts a timer that prevents state changes from\n\t\t\tflowing in to the component until the timer has elapsed. This was\n\t\t\theavily inspired by the\n\t\t\t[lazy-input](https:/docs.npmjs.com/package/lazy-input) component.\n\t\t"]))),
};
TextField.defaultProps = {
style: undefined,
isDisabled: false,
isMultiLine: false,
onBlur: lodash_1.default.noop,
onChange: lodash_1.default.noop,
onChangeDebounced: lodash_1.default.noop,
onSubmit: lodash_1.default.noop,
onKeyDown: lodash_1.default.noop,
rows: 5,
debounceLevel: 500,
lazyLevel: 1000,
value: '',
};
TextField.reducers = TextField_reducers_1.default;
return TextField;
}(react_1.default.Component));
exports.default = TextField;
var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7, templateObject_8, templateObject_9, templateObject_10, templateObject_11, templateObject_12, templateObject_13;
//# sourceMappingURL=TextField.js.map