UNPKG

igniteui-react-grids

Version:

Ignite UI React grid components.

921 lines (916 loc) 27 kB
import * as React from 'react'; import { GridColumnDataType_$type } from "./GridColumnDataType"; import { IgrFilteringOperand } from "./igr-filtering-operand"; import { IgrFilteringExpressionsTree } from "./igr-filtering-expressions-tree"; import { Column } from "./Column"; import { TypeRegistrar } from "igniteui-react-core"; import { ReactRenderer, PortalManager } from "igniteui-react-core"; import { NamePatcher, getModifiedProps, isValidProp, ensureBool, ensureEnum, interfaceToInternal, toSpinal, initializePropertiesFromCss } from "igniteui-react-core"; import { SortingStrategy } from "./SortingStrategy"; import { IgrSummaryTemplateContext } from "./igr-summary-template-context"; import { ReactTemplateAdapter } from "igniteui-react-core"; import { html } from "lit-html"; import { IgrCellTemplateContext } from "./igr-cell-template-context"; import { IgrColumnTemplateContext } from "./igr-column-template-context"; import { ColumnPipeArgs } from "./ColumnPipeArgs"; import { ColumnEditorOptions } from "./ColumnEditorOptions"; export class IgrColumn extends React.Component { createImplementation() { var _a, _b; let impl = new Column(); let nat; if (typeof document !== 'undefined') { nat = document.createElement("igc-column"); } else { nat = { style: {} }; } if ((_a = this.props) === null || _a === void 0 ? void 0 : _a.className) { nat.className = this.props.className; } if ((_b = this.props) === null || _b === void 0 ? void 0 : _b.id) { nat.id = this.props.id; } impl.setNativeElement(nat); return impl; } 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._getMainRef = this._getMainRef.bind(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 = {}; style.display = 'contents'; if (this.props.style) { style = this.props.style; } let div = React.createElement("div", Object.assign(Object.assign({}, nativeProps), { ref: this._getMainRef, className: this.props.className, style: style, children: children })); return div; } _getMainRef(ref) { this._elRef = ref; } get field() { return this.i.bc; } set field(v) { this.i.bc = v; } get header() { return this.i.bd; } set header(v) { this.i.bd = v; } get title() { return this.i.bj; } set title(v) { this.i.bj = v; } get sortable() { return this.i.ag; } set sortable(v) { this.i.ag = ensureBool(v); } /** * Returns if the column is selectable. * ```typescript * let columnSelectable = this.column.selectable; * ``` * @memberof IgxColumnComponent */ get selectable() { return this.i.ae; } set selectable(v) { this.i.ae = ensureBool(v); } get groupable() { return this.i.x; } set groupable(v) { this.i.x = ensureBool(v); } /** * Gets whether the column is editable. * Default value is `false`. * ```typescript * let isEditable = this.column.editable; * ``` * @memberof IgxColumnComponent */ get editable() { return this.i.u; } set editable(v) { this.i.u = ensureBool(v); } get filterable() { return this.i.v; } set filterable(v) { this.i.v = ensureBool(v); } get resizable() { return this.i.ac; } set resizable(v) { this.i.ac = ensureBool(v); } get autosizeHeader() { return this.i.o; } set autosizeHeader(v) { this.i.o = ensureBool(v); } /** * Gets a value indicating whether the summary for the column is enabled. * ```typescript * let hasSummary = this.column.hasSummary; * ``` * @memberof IgxColumnComponent */ get hasSummary() { return this.i.y; } set hasSummary(v) { this.i.y = ensureBool(v); } /** * Gets whether the column is hidden. * ```typescript * let isHidden = this.column.hidden; * ``` * @memberof IgxColumnComponent */ get hidden() { return this.i.z; } set hidden(v) { this.i.z = ensureBool(v); } /** * Returns if the column is selected. * ```typescript * let isSelected = this.column.selected; * ``` * @memberof IgxColumnComponent */ get selected() { return this.i.af; } set selected(v) { this.i.af = ensureBool(v); } /** * Gets whether the hiding is disabled. * ```typescript * let isHidingDisabled = this.column.disableHiding; * ``` * @memberof IgxColumnComponent */ get disableHiding() { return this.i.s; } set disableHiding(v) { this.i.s = ensureBool(v); } /** * Gets whether the pinning is disabled. * ```typescript * let isPinningDisabled = this.column.disablePinning; * ``` * @memberof IgxColumnComponent */ get disablePinning() { return this.i.t; } set disablePinning(v) { this.i.t = ensureBool(v); } /** * Gets the `width` of the column. * ```typescript * let columnWidth = this.column.width; * ``` * @memberof IgxColumnComponent */ get width() { return this.i.bk; } set width(v) { this.i.bk = v; } get maxWidth() { return this.i.bg; } set maxWidth(v) { this.i.bg = v; } get headerClasses() { return this.i.be; } set headerClasses(v) { this.i.be = v; } get headerStyles() { return this.i.aw; } set headerStyles(v) { this.i.aw = v; } get headerGroupClasses() { return this.i.bf; } set headerGroupClasses(v) { this.i.bf = v; } get headerGroupStyles() { return this.i.av; } set headerGroupStyles(v) { this.i.av = v; } get cellClasses() { return this.i.at; } set cellClasses(v) { this.i.at = v; } get cellStyles() { return this.i.au; } set cellStyles(v) { this.i.au = v; } /** * Applies display format to cell values in the column. Does not modify the underlying data. * @remark * Note: As the formatter is used in places like the Excel style filtering dialog, in certain * scenarios (remote filtering for example), the row data argument can be `undefined`. * In this example, we check to see if the column name is Salary, and then provide a method as the column formatter * to format the value into a currency string. * @example * ```typescript * columnInit(column: IgxColumnComponent) { * if (column.field == "Salary") { * column.formatter = (salary => this.format(salary)); * } * } * format(value: number) : string { * return formatCurrency(value, "en-us", "$"); * } * ``` * @example * ```typescript * const column = this.grid.getColumnByName('Address'); * const addressFormatter = (address: string, rowData: any) => data.privacyEnabled ? 'unknown' : address; * column.formatter = addressFormatter; * ``` * @memberof IgxColumnComponent */ get formatter() { return this.i.d; } set formatter(v) { this.i.d = v; } get summaryFormatter() { return this.i.l; } set summaryFormatter(v) { this.i.l = v; } get filteringIgnoreCase() { return this.i.w; } set filteringIgnoreCase(v) { this.i.w = ensureBool(v); } get sortingIgnoreCase() { return this.i.ah; } set sortingIgnoreCase(v) { this.i.ah = ensureBool(v); } get searchable() { return this.i.ad; } set searchable(v) { this.i.ad = ensureBool(v); } get dataType() { return this.i.j; } set dataType(v) { this.i.j = ensureEnum(GridColumnDataType_$type, v); } get rowEnd() { return this.i.ao; } set rowEnd(v) { this.i.ao = +v; } get colEnd() { return this.i.ak; } set colEnd(v) { this.i.ak = +v; } get rowStart() { return this.i.ap; } set rowStart(v) { this.i.ap = +v; } get colStart() { return this.i.al; } set colStart(v) { this.i.al = +v; } get additionalTemplateContext() { return this.i.as; } set additionalTemplateContext(v) { this.i.as = v; } get minWidth() { return this.i.bh; } set minWidth(v) { this.i.bh = v; } /** * Gets the column index. * ```typescript * let columnIndex = this.column.index; * ``` * @memberof IgxColumnComponent */ get index() { return this.i.am; } /** * Gets whether the column is `pinned`. * ```typescript * let isPinned = this.column.pinned; * ``` * @memberof IgxColumnComponent */ get pinned() { return this.i.ab; } set pinned(v) { this.i.ab = ensureBool(v); } /** * Gets the column `summaries`. * ```typescript * let columnSummaries = this.column.summaries; * ``` * @memberof IgxColumnComponent */ get summaries() { return this.i.az; } set summaries(v) { this.i.az = v; } get disabledSummaries() { return this.i.c; } set disabledSummaries(v) { if (v && !Array.isArray(v) && typeof (v) == "string") { const re = /\s*(?:,|\s|$)\s*/gm; v = v.split(re); } this.i.c = v; } /** * Gets the column `filters`. * ```typescript * let columnFilters = this.column.filters' * ``` * @memberof IgxColumnComponent */ get filters() { const r = this.i.i; if (r == null) { return null; } if (!r.externalObject) { let e = new IgrFilteringOperand(); if (r.$type) { e._implementation = r; } else { if (e.i.setNativeElement) { e.i.setNativeElement(r); } } r.externalObject = e; } return r.externalObject; } set filters(v) { v == null ? this.i.i = null : this.i.i = v.i; } /** * Gets the column `sortStrategy`. * ```typescript * let sortStrategy = this.column.sortStrategy * ``` * @memberof IgxColumnComponent */ get sortStrategy() { return this.i.k.nativeElement; } set sortStrategy(v) { this.i.k = interfaceToInternal(v, () => new SortingStrategy()); } /** * Returns a reference to the `summaryTemplate`. * ```typescript * let summaryTemplate = this.column.summaryTemplate; * ``` * @memberof IgxColumnComponent */ get summaryTemplate() { return this._summaryTemplate; } set summaryTemplate(v) { this._summaryTemplate = v; if (!this._summaryTemplateAdapter) { this._summaryTemplateAdapter = new ReactTemplateAdapter(this._renderer, this._portalManager, "bb", { html: html }, () => new IgrSummaryTemplateContext()); } this._summaryTemplateAdapter.setValue(this.i, this._summaryTemplate); } /** * Returns a reference to the `bodyTemplate`. * ```typescript * let bodyTemplate = this.column.bodyTemplate; * ``` * @memberof IgxColumnComponent */ get bodyTemplate() { return this._bodyTemplate; } set bodyTemplate(v) { this._bodyTemplate = v; if (!this._bodyTemplateAdapter) { this._bodyTemplateAdapter = new ReactTemplateAdapter(this._renderer, this._portalManager, "a6", { html: html }, () => new IgrCellTemplateContext()); } this._bodyTemplateAdapter.setValue(this.i, this._bodyTemplate); } /** * Returns a reference to the header template. * ```typescript * let headerTemplate = this.column.headerTemplate; * ``` * @memberof IgxColumnComponent */ get headerTemplate() { return this._headerTemplate; } set headerTemplate(v) { this._headerTemplate = v; if (!this._headerTemplateAdapter) { this._headerTemplateAdapter = new ReactTemplateAdapter(this._renderer, this._portalManager, "a9", { html: html }, () => new IgrColumnTemplateContext()); } this._headerTemplateAdapter.setValue(this.i, this._headerTemplate); } /** * Returns a reference to the inline editor template. * ```typescript * let inlineEditorTemplate = this.column.inlineEditorTemplate; * ``` * @memberof IgxColumnComponent */ get inlineEditorTemplate() { return this._inlineEditorTemplate; } set inlineEditorTemplate(v) { this._inlineEditorTemplate = v; if (!this._inlineEditorTemplateAdapter) { this._inlineEditorTemplateAdapter = new ReactTemplateAdapter(this._renderer, this._portalManager, "ba", { html: html }, () => new IgrCellTemplateContext()); } this._inlineEditorTemplateAdapter.setValue(this.i, this._inlineEditorTemplate); } /** * Returns a reference to the validation error template. * ```typescript * let errorTemplate = this.column.errorTemplate; * ``` */ get errorTemplate() { return this._errorTemplate; } set errorTemplate(v) { this._errorTemplate = v; if (!this._errorTemplateAdapter) { this._errorTemplateAdapter = new ReactTemplateAdapter(this._renderer, this._portalManager, "a7", { html: html }, () => new IgrCellTemplateContext()); } this._errorTemplateAdapter.setValue(this.i, this._errorTemplate); } /** * Returns a reference to the `filterCellTemplate`. * ```typescript * let filterCellTemplate = this.column.filterCellTemplate; * ``` * @memberof IgxColumnComponent */ get filterCellTemplate() { return this._filterCellTemplate; } set filterCellTemplate(v) { this._filterCellTemplate = v; if (!this._filterCellTemplateAdapter) { this._filterCellTemplateAdapter = new ReactTemplateAdapter(this._renderer, this._portalManager, "a8", { html: html }, () => new IgrColumnTemplateContext()); } this._filterCellTemplateAdapter.setValue(this.i, this._filterCellTemplate); } /** * Gets the column visible index. * If the column is not visible, returns `-1`. * ```typescript * let visibleColumnIndex = this.column.visibleIndex; * ``` * @memberof IgxColumnComponent */ get visibleIndex() { return this.i.aq; } /** * Returns a boolean indicating if the column is a `ColumnGroup`. * ```typescript * let columnGroup = this.column.columnGroup; * ``` * @memberof IgxColumnComponent */ get columnGroup() { return this.i.p; } /** * Returns a boolean indicating if the column is a `ColumnLayout` for multi-row layout. * ```typescript * let columnGroup = this.column.columnGroup; * ``` * @memberof IgxColumnComponent */ get columnLayout() { return this.i.q; } /** * Returns a boolean indicating if the column is a child of a `ColumnLayout` for multi-row layout. * ```typescript * let columnLayoutChild = this.column.columnLayoutChild; * ``` * @memberof IgxColumnComponent */ get columnLayoutChild() { return this.i.r; } /** * A list containing all the child columns under this column (if any). * Empty without children or if this column is not Group or Layout. */ get childColumns() { if (!this.i.b) { return undefined; } let ret = []; for (let i = 0; i < this.i.b.length; i++) { let impl = this.i.b[i]; if (!impl.externalObject) { if (impl instanceof IgrColumn) { ret.push(impl); continue; } let e = IgrColumn._createFromInternal(impl); e._implementation = impl; impl.externalObject = e; } ret.push(impl.externalObject); } return ret; } /** * Returns the level of the column in a column group. * Returns `0` if the column doesn't have a `parent`. * ```typescript * let columnLevel = this.column.level; * ``` * @memberof IgxColumnComponent */ get level() { return this.i.an; } get visibleWhenCollapsed() { return this.i.aj; } set visibleWhenCollapsed(v) { this.i.aj = ensureBool(v); } get pipeArgs() { return this.i.g.nativeElement; } set pipeArgs(v) { this.i.g = interfaceToInternal(v, () => new ColumnPipeArgs()); } get editorOptions() { return this.i.f.nativeElement; } set editorOptions(v) { this.i.f = interfaceToInternal(v, () => new ColumnEditorOptions()); } /** * Returns the filteringExpressionsTree of the column. * ```typescript * let tree = this.column.filteringExpressionsTree; * ``` * @memberof IgxColumnComponent */ get filteringExpressionsTree() { const r = this.i.h; if (r == null) { return null; } if (!r.externalObject) { let e = new IgrFilteringExpressionsTree(); if (r.$type) { e._implementation = r; } else { if (e.i.setNativeElement) { e.i.setNativeElement(r); } } r.externalObject = e; } return r.externalObject; } /** * Sets/gets the parent column. * ```typescript * let parentColumn = this.column.parent; * ``` * ```typescript * this.column.parent = higherLevelColumn; * ``` * @memberof IgxColumnComponent */ get parent() { return this.i.ay; } set parent(v) { this.i.ay = v; } /** * Returns a reference to the top level parent column. * ```typescript * let topLevelParent = this.column.topLevelParent; * ``` */ get topLevelParent() { const r = this.i.e; if (r == null) { return null; } if (!r.externalObject) { let e = IgrColumn._createFromInternal(r); if (e) { e._implementation = r; } r.externalObject = e; } return r.externalObject; } get name() { return this.i.bi; } set name(v) { this.i.bi = v; } findByName(name) { if (this.findEphemera) { if (name && name.indexOf("@@e:") == 0) { return this.findEphemera(name); } } if (this.filters && this.filters.name && this.filters.name == name) { return this.filters; } if (this.sortStrategy && this.sortStrategy.name && this.sortStrategy.name == name) { return this.sortStrategy; } if (this.pipeArgs && this.pipeArgs.name && this.pipeArgs.name == name) { return this.pipeArgs; } if (this.editorOptions && this.editorOptions.name && this.editorOptions.name == name) { return this.editorOptions; } 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("Column"); 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.topLevelParent && this.topLevelParent._styling) { this.topLevelParent._styling(container, component, this); } if (this._otherStyling) { this._otherStyling(container, component, parent); } this._inStyling = false; } setNativeElement(element) { this.i.setNativeElement(element); } /** * Pins the column at the provided index in the pinned area. * Defaults to index `0` if not provided, or to the initial index in the pinned area. * Returns `true` if the column is successfully pinned. Returns `false` if the column cannot be pinned. * Column cannot be pinned if: * - Is already pinned * - index argument is out of range * - The pinned area exceeds 80% of the grid width * ```typescript * let success = this.column.pin(); * ``` * @memberof IgxColumnComponent */ pin(index) { let iv = this.i.aa(index); return (iv); } /** * Unpins the column and place it at the provided index in the unpinned area. * Defaults to index `0` if not provided, or to the initial index in the unpinned area. * Returns `true` if the column is successfully unpinned. Returns `false` if the column cannot be unpinned. * Column cannot be unpinned if: * - Is already unpinned * - index argument is out of range * ```typescript * let success = this.column.unpin(); * ``` * @memberof IgxColumnComponent */ unpin(index) { let iv = this.i.ai(index); return (iv); } /** * Moves a column to the specified visible index. * If passed index is invalid, or if column would receive a different visible index after moving, moving is not performed. * If passed index would move the column to a different column group. moving is not performed. * @example * ```typescript * column.move(index); * ``` * @memberof IgxColumnComponent */ move(index) { this.i.bm(index); } /** * Autosize the column to the longest currently visible cell value, including the header cell. * ```typescript * @ViewChild('grid') grid: IgxGridComponent; * let column = this.grid.columnList.filter(c => c.field === 'ID')[0]; * column.autosize(); * ``` * @memberof IgxColumnComponent * byHeaderOnly Set if column should be autosized based only on the header content. * @param byHeaderOnly * Set if column should be autosized based only on the header content. */ autosize(byHeaderOnly) { this.i.bl(byHeaderOnly); } }