UNPKG

lucid-ui

Version:

A UI component library from Xandr.

283 lines 12.4 kB
"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) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); 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 }); var react_1 = __importDefault(require("react")); var prop_types_1 = __importDefault(require("prop-types")); var lodash_1 = __importDefault(require("lodash")); var style_helpers_1 = require("../../util/style-helpers"); 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; /** TODO: Remove the nonPassThroughs when the component is converted to a functional component */ var nonPassThroughs = [ 'style', 'isMultiLine', 'isDisabled', 'rows', 'className', 'onChange', 'onBlur', 'onChangeDebounced', 'onKeyDown', 'onSubmit', 'value', 'debounceLevel', 'lazyLevel', 'initialState', 'callbackId', 'children', ]; 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 (options) { /* istanbul ignore next */ _this.nativeElement.current.focus(options); }; 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({}, lodash_1.default.omit(passThroughs, nonPassThroughs)), { 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: "`TextField` should cover all your text input needs. It is able to handle single- and multi-line inputs.", categories: ['controls', 'text'], }; TextField.propTypes = { /** Styles that are passed through to native control. */ style: object, /** Set the TextField to multi line mode. Under the hood this will use a \`textarea\` instead of an \`input\` if set to \`true\`. */ isMultiLine: bool, /** Disables the TextField by greying it out. */ isDisabled: bool, /** Initial number of rows a multi line TextField should have. Ignored when not in multi-line mode. */ rows: number, /** Class names that are appended to the defaults. */ className: string, /** Fires an event every time the user types text into the TextField. Signature: \`(value, { event, props }) => {}\` */ onChange: func, /** Fires an on the \`input\`'s onBlur. Signature: \`(currentValue, { event, props }) => {}\` */ onBlur: func, /** Fires an event, debounced by \`debounceLevel\`, when the user types text into the TextField. Signature: \`(value, { event, props }) => {}\` */ onChangeDebounced: func, /** Fires an event on every keydown Signature: \`({ event, props }) => {}\` */ onKeyDown: func, /** Fires an event when the user hits "enter" from the TextField. You shouldn't use it if you're using \`isMultiLine\`. Signature: \`(value, { event, props }) => {}\` */ onSubmit: func, /** Set the value of the input. */ value: oneOfType([number, string]), /** Number of milliseconds to debounce the \`onChangeDebounced\` callback. Only useful if you provide an \`onChangeDebounced\` handler. */ debounceLevel: number, /** Set the holding time, in milliseconds, that the component will wait if the user is typing and the component gets a new \`value\` prop. Any time the user hits a key, it starts a timer that prevents state changes from flowing in to the component until the timer has elapsed. This was heavily inspired by the [lazy-input](https:/docs.npmjs.com/package/lazy-input) component. */ lazyLevel: number, }; 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; //# sourceMappingURL=TextField.js.map