UNPKG

igniteui-react-grids

Version:

Ignite UI React grid components.

294 lines (288 loc) 8.79 kB
import * as React from 'react'; import { RowDirective } from "./RowDirective"; import { TypeRegistrar } from "igniteui-react-core"; import { ReactRenderer, PortalManager } from "igniteui-react-core"; import { NamePatcher, getModifiedProps, isValidProp, ensureBool, toSpinal, initializePropertiesFromCss } from "igniteui-react-core"; export class IgrRowDirective extends React.Component { createImplementation() { return new RowDirective(); } get nativeElement() { return this._implementation.nativeElement; } /** * @hidden */ get i() { return this._implementation; } /** * @hidden */ static _createFromInternal(internal) { if (!internal) { return null; } if (!internal.$type) { return null; } let name = internal.$type.name; let externalName = "Igr" + name; if (!TypeRegistrar.isRegistered(externalName)) { return null; } return TypeRegistrar.create(externalName); } onImplementationCreated() { } constructor(props) { super(props); this.mounted = false; this.__p = null; this._hasUserValues = new Set(); this._stylingContainer = null; this._stylingParent = null; this._inStyling = false; if (this._styling) { NamePatcher.ensureStylablePatched(Object.getPrototypeOf(this)); } this._implementation = this.createImplementation(); this._portalManager = new PortalManager("templates", () => { if (this.mounted) { this.setState({}); } }); if (typeof window !== 'undefined' && typeof document !== 'undefined') { this._renderer = new ReactRenderer(this._implementation.nativeElement, document, false, null, this._portalManager); } this._implementation.externalObject = this; this.onImplementationCreated(); if (this._initializeAdapters) { this._initializeAdapters(); } } componentDidMount() { this.mounted = true; for (const p of Object.keys(this.props)) { if (isValidProp(this, p)) { this[p] = this.props[p]; } } } 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; } render() { const nativePropsName = Object.keys(this.props).filter(prop => !isValidProp(this, prop) && prop !== "originalRef" && prop !== "className"); const nativeProps = {}; nativePropsName.forEach(propName => { nativeProps[propName] = this.props[propName]; }); let propChildren = this.props.children; let children = []; React.Children.forEach(propChildren, (ch) => { children.push(React.cloneElement(ch)); }); this._portalManager.onRender(children); let style = {}; if (this.props.style) { style = this.props.style; } let div = React.createElement("igc-row-directive", Object.assign(Object.assign({}, nativeProps), { ref: this._getMainRef, id: this.props.id, class: this.props.className, style: style, children: children })); return div; } _getMainRef(ref) { this._elRef = ref; } /** * The data passed to the row component. * ```typescript * // get the row data for the first selected row * let selectedRowData = this.grid.selectedRows[0].data; * ``` */ get data() { return this.i.h; } set data(v) { this.i.h = v; } /** * The index of the row. * ```typescript * // get the index of the second selected row * let selectedRowIndex = this.grid.selectedRows[1].index; * ``` */ get index() { return this.i.f; } set index(v) { this.i.f = +v; } /** * Sets whether this specific row has disabled functionality for editing and row selection. * Default value is `false`. * ```typescript * this.grid.selectedRows[0].pinned = true; * ``` */ get disabled() { return this.i.c; } set disabled(v) { this.i.c = ensureBool(v); } /** * Sets whether the row is pinned. * Default value is `false`. * ```typescript * this.grid.selectedRows[0].pinned = true; * ``` */ get pinned() { return this.i.e; } set pinned(v) { this.i.e = ensureBool(v); } /** * Gets the expanded state of the row. * ```typescript * let isExpanded = row.expanded; * ``` */ get expanded() { return this.i.d; } set expanded(v) { this.i.d = ensureBool(v); } findByName(name) { if (this.findEphemera) { if (name && name.indexOf("@@e:") == 0) { return this.findEphemera(name); } } return null; } get hasUserValues() { return this._hasUserValues; } __m(propertyName) { if (!this._inStyling) { this._hasUserValues.add(propertyName); } } _styling(container, component, parent) { if (this._inStyling) { return; } this._inStyling = true; this._stylingContainer = container; this._stylingParent = component; let genericPrefix = ""; let typeName = this.i.$type.name; if (typeName.indexOf("Xam") === 0) { typeName = typeName.substring(3); } genericPrefix = toSpinal("RowDirective"); let additionalPrefixes = []; let prefix = toSpinal(typeName); additionalPrefixes.push(prefix + "-"); let b = this.i.$type.baseType; while (b && b.name != "Object" && b.name != "Base" && b.name != "Control" && b.Name != "DependencyObject" && b.Name != "FrameworkElement") { typeName = b.name; if (typeName.indexOf("Xam") === 0) { typeName = typeName.substring(3); } let basePrefix = toSpinal(typeName); additionalPrefixes.push(basePrefix + "-"); b = b.baseType; } if (parent) { let parentTypeName = parent.i.$type.name; if (parentTypeName.indexOf("Xam") === 0) { parentTypeName = parentTypeName.substring(3); } let parentPrefix = toSpinal(parentTypeName); additionalPrefixes.push(parentPrefix + "-" + genericPrefix + "-"); additionalPrefixes.push(parentPrefix + "-" + prefix + "-"); } initializePropertiesFromCss(container, this, genericPrefix + "-", this.hasUserValues, false, additionalPrefixes); if (this._otherStyling) { this._otherStyling(container, component, parent); } this._inStyling = false; } setNativeElement(element) { this.i.setNativeElement(element); } /** * Updates the specified row object and the data source record with the passed value. * ```typescript * // update the second selected row's value * let newValue = "Apple"; * this.grid.selectedRows[1].update(newValue); * ``` */ update(value) { this.i.p(value); } /** * Removes the specified row from the grid's data source. * This method emits `rowDeleted` event. * ```typescript * // delete the third selected row from the grid * this.grid.selectedRows[2].delete(); * ``` */ del() { this.i.k(); } isCellActive(visibleColumnIndex) { this.i.l(visibleColumnIndex); } /** * Pins the specified row. * This method emits `rowPinning`\`rowPinned` event. * ```typescript * // pin the selected row from the grid * this.grid.selectedRows[0].pin(); * ``` */ pin() { this.i.m(); } /** * Unpins the specified row. * This method emits `rowPinning`\`rowPinned` event. * ```typescript * // unpin the selected row from the grid * this.grid.selectedRows[0].unpin(); * ``` */ unpin() { this.i.o(); } /** * Spawns the add row UI for the specific row. * @example * ```typescript * const row = this.grid1.getRowByIndex(1); * row.beginAddRow(); * ``` */ beginAddRow() { this.i.j(); } }