UNPKG

igniteui-react-inputs

Version:

Ignite UI React input components.

568 lines (564 loc) 16.3 kB
import * as React from 'react'; import { XInput } from "./XInput"; import { ensureEnum, brushToString, stringToBrush, ensureBool, NamePatcher, isValidProp, getModifiedProps } from "igniteui-react-core"; import { FontInfo } from "igniteui-react-core"; import { ReactRenderer, PortalManager } from "igniteui-react-core"; import { delegateCombine, delegateRemove } from "igniteui-react-core"; import { IgrXInputGroupItem } from './igr-x-input-group-item'; import { IgrInputChangeEventArgs } from './igr-input-change-event-args'; import { ControlDisplayDensity_$type } from "igniteui-react-core"; import { IgrKeyEventArgs } from "igniteui-react-core"; import { BaseControlTheme_$type } from "igniteui-react-core"; import { HorizontalAlignment_$type } from "igniteui-react-core"; let requiredStyle = ` `; let pending = []; let checked = /*@__PURE__*/ new WeakMap(); // TODO why not WeakSet? function ensureCss() { if (!document) { return; } if (checked.has(document)) { return; } injectCss(requiredStyle, "data-ig-input-style", () => { let temp = pending; pending = null; temp.forEach((v, i, a) => { //v.styleUpdated(); }); }); checked.set(document, true); } function injectCss(style, attributeName, onload = null) { for (let i = 0; i < document.head.children.length; i++) { let child = document.head.children[i]; if (child.tagName && child.tagName.toLowerCase() == "style" && child.hasAttribute(attributeName)) { return; } } let styleEle = document.createElement("style"); styleEle.textContent = style; styleEle.setAttribute(attributeName, 'true'); if (onload) { const onloadResolved = () => { styleEle.onload = null; onload(); }; styleEle.onload = onloadResolved; } document.head.appendChild(styleEle); } export class IgrXInput extends IgrXInputGroupItem { _getMainRef(ref) { this._elRef = ref; } render() { let propChildren = this.props.children; let children = []; React.Children.forEach(propChildren, (ch) => { children.push(React.cloneElement(ch)); }); this._portalManager.onRender(children); let div = React.createElement("div", { className: "ig-x-input igr-x-input", ref: this._getMainRef, children: children }); return div; } requestRender() { if (this._initialized) { this.setState({}); } } constructor(props) { super(props); this._wrapper = null; this._keyDown = null; this._keyDown_wrapped = null; this._keyUp = null; this._keyUp_wrapped = null; this._keyPress = null; this._keyPress_wrapped = null; this._change = null; this._change_wrapped = null; this._changing = null; this._changing_wrapped = null; this._compositionEnd = null; this._compositionEnd_wrapped = null; ensureCss(); if (this._styling) { NamePatcher.ensureStylablePatched(Object.getPrototypeOf(this)); } this._implementation = this.createImplementation(); this._implementation.externalObject = this; this._getMainRef = this._getMainRef.bind(this); var container; if (document) { container = document.createElement("div"); } var root; root = container; if (container != null) { root = container; } this.requestRender = this.requestRender.bind(this); this._portalManager = new PortalManager("inputContent", this.requestRender); var ren = new ReactRenderer(root, document, true, {}, this._portalManager); this._wrapper = ren; this._container = this._wrapper.rootWrapper; this._container.setStyleProperty("display", "inline-block"); this._container.setStyleProperty("vertical-align", "middle"); var input = this.i; this._input = input; input.provideContainer(ren); if (props) { this.initializeProperties(); } } //private _getLabelPortal(element: DomWrapper, portalCallback: (portal: DomPortal) => void): void { // this._portalManager.getPortal(element, "TemplateContent", portalCallback); //} shouldComponentUpdate(nextProps, nextState) { const mod = getModifiedProps(this.props, nextProps); for (const p of Object.keys(mod)) { if (isValidProp(this, p)) { this[p] = mod[p]; } } return true; } initializeProperties() { for (const p of Object.keys(this.props)) { if (isValidProp(this, p)) { this[p] = this.props[p]; } } } // supports angular themes or custom properties set in CSS updateStyle() { this._styling(this._elRef, this); } destroy() { this._input.destroy(); this._wrapper.destroy(); } componentWillUnmount() { } componentDidMount() { this._elRef.appendChild(this._container.getNativeElement()); this.initializeContent(); } initializeContent() { this._styling(this._container.getNativeElement(), this); this.updateStyle(); } createImplementation() { return new XInput(); } /** * @hidden */ get i() { return this._implementation; } /** * Gets or sets the base built in theme to use for the button. */ get baseTheme() { return this.i.w; } set baseTheme(v) { this.i.w = ensureEnum(BaseControlTheme_$type, v); } /** * Gets or sets the display density to use for the input. */ get density() { return this.i.aa; } set density(v) { this.i.aa = ensureEnum(ControlDisplayDensity_$type, v); } /** * Gets the actual display density to use for the label. */ get actualDensity() { return this.i.z; } set actualDensity(v) { this.i.z = ensureEnum(ControlDisplayDensity_$type, v); } /** * Gets/Sets the input mask for the input. */ get mask() { return this.i.ce; } set mask(v) { this.i.ce = v; } /** * Gets/Sets the character representing a fillable spot in the input mask */ get promptChar() { return this.i.ci; } set promptChar(v) { this.i.ci = v; } /** * Specifies if the bound value includes the formatting symbols. */ get includeLiterals() { return this.i.au; } set includeLiterals(v) { this.i.au = ensureBool(v); } get showSpinner() { return this.i.a1; } set showSpinner(v) { this.i.a1 = ensureBool(v); } /** * Gets the actual color to use for the text color. */ get actualTextColor() { return brushToString(this.i.d4); } set actualTextColor(v) { this.i.d4 = stringToBrush(v); } /** * Gets the actual hover color to use for the text. */ get actualHoverTextColor() { return brushToString(this.i.d3); } set actualHoverTextColor(v) { this.i.d3 = stringToBrush(v); } /** * Gets or sets the color to use for the text. */ get textColor() { return brushToString(this.i.d8); } set textColor(v) { this.i.d8 = stringToBrush(v); } /** * Gets or sets the use for the button. */ get textStyle() { if (this.i.af == null) { return null; } return this.i.af.fontString; } set textStyle(v) { let fi = new FontInfo(); fi.fontString = v; this.i.af = fi; } /** * Gets or sets the type to use for the input. */ get inputType() { return this.i.b8; } set inputType(v) { this.i.b8 = v; } /** * Gets or sets the placeholder to use for the input. */ get placeholder() { return this.i.cg; } set placeholder(v) { this.i.cg = v; } /** * Gets or sets the color to use for the hovered text of the button regardless of type. */ get hoverTextColor() { return brushToString(this.i.d7); } set hoverTextColor(v) { this.i.d7 = stringToBrush(v); } /** * Gets or sets the id to use for the checkbox. */ get id() { return this.i.b5; } set id(v) { this.i.b5 = v; } /** * Gets or sets TabIndex to use for the checkbox. */ get tabIndex() { return this.i.be; } set tabIndex(v) { this.i.be = +v; } /** * Gets or sets the for attribute to use for the input. */ get for() { return this.i.b2; } set for(v) { this.i.b2 = v; } /** * Gets or sets the value of the aria-label attribute. */ get ariaLabel() { return this.i.bx; } set ariaLabel(v) { this.i.bx = v; } /** * Gets or sets whether the input is hovered. */ get isHover() { return this.i.aw; } set isHover(v) { this.i.aw = ensureBool(v); } /** * Gets or sets the value for the input. */ get value() { return this.i.cq; } set value(v) { this.i.cq = v; } get hasValue() { return this.i.at; } /** * Gets or sets whether the checkbox is disabled. */ get disabled() { return this.i.disabled; } set disabled(v) { this.i.disabled = ensureBool(v); } /** * Gets or sets whether the checkbox is readonly. */ get readonly() { return this.i.readonly; } set readonly(v) { this.i.readonly = ensureBool(v); } /** * Gets or sets the alignment of the text. */ get textAlignment() { return this.i.dy; } set textAlignment(v) { this.i.dy = ensureEnum(HorizontalAlignment_$type, v); } /** * Returns / Sets the beginning index of the selected text. When nothing is selected, this returns the position of the text input cursor (caret) inside of the input. */ get selectionStart() { return this.i.selectionStart; } set selectionStart(v) { this.i.selectionStart = +v; } /** * Returns / Sets the end index of the selected text. When nothing is selected, this returns the position of the text input cursor (caret) inside of the input. */ get selectionEnd() { return this.i.selectionEnd; } set selectionEnd(v) { this.i.selectionEnd = +v; } onDetachedFromUI() { this.i.onDetachedFromUI(); } onAttachedToUI() { this.i.onAttachedToUI(); } /** * Exports visual information about the current state of the grid. */ exportVisualModel() { let iv = this.i.bg(); return (iv); } /** * Returns a serialized copy of the exported visual model */ exportSerializedVisualModel() { let iv = this.i.b1(); return (iv); } setSelectionRange(selectionStart, selectionEnd) { this.i.dr(selectionStart, selectionEnd); } blur() { this.i.ct(); } select() { this.i.dp(); } get keyDown() { return this._keyDown; } set keyDown(ev) { if (this._keyDown_wrapped !== null) { this.i.keyDown = delegateRemove(this.i.keyDown, this._keyDown_wrapped); this._keyDown_wrapped = null; this._keyDown = null; } this._keyDown = ev; this._keyDown_wrapped = (o, e) => { let outerArgs = new IgrKeyEventArgs(); outerArgs._provideImplementation(e); if (this.beforeKeyDown) { this.beforeKeyDown(this, outerArgs); } if (this._keyDown) { this._keyDown(this, outerArgs); } }; this.i.keyDown = delegateCombine(this.i.keyDown, this._keyDown_wrapped); ; } get keyUp() { return this._keyUp; } set keyUp(ev) { if (this._keyUp_wrapped !== null) { this.i.keyUp = delegateRemove(this.i.keyUp, this._keyUp_wrapped); this._keyUp_wrapped = null; this._keyUp = null; } this._keyUp = ev; this._keyUp_wrapped = (o, e) => { let outerArgs = new IgrKeyEventArgs(); outerArgs._provideImplementation(e); if (this.beforeKeyUp) { this.beforeKeyUp(this, outerArgs); } if (this._keyUp) { this._keyUp(this, outerArgs); } }; this.i.keyUp = delegateCombine(this.i.keyUp, this._keyUp_wrapped); ; } get keyPress() { return this._keyPress; } set keyPress(ev) { if (this._keyPress_wrapped !== null) { this.i.keyPress = delegateRemove(this.i.keyPress, this._keyPress_wrapped); this._keyPress_wrapped = null; this._keyPress = null; } this._keyPress = ev; this._keyPress_wrapped = (o, e) => { let outerArgs = new IgrKeyEventArgs(); outerArgs._provideImplementation(e); if (this.beforeKeyPress) { this.beforeKeyPress(this, outerArgs); } if (this._keyPress) { this._keyPress(this, outerArgs); } }; this.i.keyPress = delegateCombine(this.i.keyPress, this._keyPress_wrapped); ; } get change() { return this._change; } set change(ev) { if (this._change_wrapped !== null) { this.i.change = delegateRemove(this.i.change, this._change_wrapped); this._change_wrapped = null; this._change = null; } this._change = ev; this._change_wrapped = (o, e) => { let outerArgs = new IgrInputChangeEventArgs(); outerArgs._provideImplementation(e); if (this.beforeChange) { this.beforeChange(this, outerArgs); } if (this._change) { this._change(this, outerArgs); } }; this.i.change = delegateCombine(this.i.change, this._change_wrapped); ; } get changing() { return this._changing; } set changing(ev) { if (this._changing_wrapped !== null) { this.i.changing = delegateRemove(this.i.changing, this._changing_wrapped); this._changing_wrapped = null; this._changing = null; } this._changing = ev; this._changing_wrapped = (o, e) => { let outerArgs = new IgrInputChangeEventArgs(); outerArgs._provideImplementation(e); if (this.beforeChanging) { this.beforeChanging(this, outerArgs); } if (this._changing) { this._changing(this, outerArgs); } }; this.i.changing = delegateCombine(this.i.changing, this._changing_wrapped); ; } get compositionEnd() { return this._compositionEnd; } set compositionEnd(ev) { if (this._compositionEnd_wrapped !== null) { this.i.compositionEnd = delegateRemove(this.i.compositionEnd, this._compositionEnd_wrapped); this._compositionEnd_wrapped = null; this._compositionEnd = null; } this._compositionEnd = ev; this._compositionEnd_wrapped = (o, e) => { let outerArgs = new IgrInputChangeEventArgs(); outerArgs._provideImplementation(e); if (this.beforeCompositionEnd) { this.beforeCompositionEnd(this, outerArgs); } if (this._compositionEnd) { this._compositionEnd(this, outerArgs); } }; this.i.compositionEnd = delegateCombine(this.i.compositionEnd, this._compositionEnd_wrapped); ; } }