Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom header by passing html element #1955

Merged
merged 3 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions packages/toast-ui.grid/cypress/integration/column.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,53 @@ describe('move column', () => {
});
});
});

describe('customHeader', () => {
let customHeader: HTMLElement;

const CUSTOM_HEADER_CLASS_NAME = 'custom-header';
const data = [
{ name: 'Kim', age: 10, price: 100 },
{ name: 'Lee', age: 20, price: 200 },
{ name: 'Ryu', age: 30, price: 300 },
{ name: 'Han', age: 40, price: 400 },
];

beforeEach(() => {
customHeader = document.createElement('div');
customHeader.className = CUSTOM_HEADER_CLASS_NAME;
customHeader.textContent = 'Custom Header';
});

it('should render the given HTML element to the header.', () => {
const columns = [
{ name: 'name', header: 'Name', customHeader },
{ name: 'age', header: 'Age' },
{ name: 'price', header: 'Price' },
];

cy.createGrid({
data,
columns,
});

cy.getHeaderCell('name').click().should('contain.html', customHeader.outerHTML);
});

it('should sets the textContent of the customHeader to the value of the header property if no header property is passed.', () => {
const columns = [
{ name: 'name', customHeader },
{ name: 'age', header: 'Age' },
{ name: 'price', header: 'Price' },
];

cy.createGrid({
data,
columns,
});

cy.gridInstance()
.invoke('getColumn', 'name')
.should('contain', { header: customHeader.textContent });
});
});
2 changes: 2 additions & 0 deletions packages/toast-ui.grid/src/grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ if ((module as any).hot) {
* the text line is broken by fitting to the column's width and new line characters.(This option will be deprecated)
* @param {boolean} [options.columns.rowSpan=false] - If set to true, apply dynamic rowspan to column data.
* If it is not a top-level relational column of a column relationship or the grid has tree data, dynamic rowspan is not applied.
* @param {HTMLElement} [options.columns.customHeader] - If set HTML element, the element will render in header cell.
* If set without header property, the text content of element will be set to header property.
* @param {Object} [options.summary] - The object for configuring summary area.
* @param {number} [options.summary.height] - The height of the summary area.
* @param {string} [options.summary.position='bottom'] - The position of the summary area. ('bottom', 'top')
Expand Down
36 changes: 19 additions & 17 deletions packages/toast-ui.grid/src/store/column.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
import {
OptColumn,
OptRowHeader,
OptTree,
Dictionary,
OptCellEditor,
OptCellRenderer,
OptColumn,
OptColumnHeaderInfo,
OptComplexColumnInfo,
Dictionary,
OptFilter,
OptRowHeader,
OptRowHeaderColumn,
OptTree,
} from '@t/options';
Comment on lines +2 to 12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P5: grid의 eslint에는 sort-imports 규칙이 없는데 import가 파일마다 변경되어 있네요. 개인 설정에 이 규칙이 적용되어 있는건지 확인 부탁드려요.
원래 sort-imports 규칙이 없다보니 다른 파일도 전부 import가 정렬되어 있지 않아서 확인하는 파일마다 전부 변경사항이 생길텐데, 사실 이건 PR과 무관한 변경사항이라서요. 만약 sort-imports를 적용하는게 좋을 것 같다면 PR 새로 만들어서 한번에 전부 변경하도록 하면 좋을 것 같습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

intellij에 설정되어 있어서 변경되었나 보네요... 이건 추후에 한번에 변경하도록 하겠습니다 ㅎㅎ

import {
ColumnOptions,
AlignType,
VAlignType,
Relations,
ColumnInfo,
CellRendererOptions,
CellEditorOptions,
CellRendererOptions,
ClipboardCopyOptions,
ColumnFilterOption,
Column,
ColumnFilterOption,
ColumnHeaderInfo,
ColumnInfo,
ColumnOptions,
Relations,
VAlignType,
} from '@t/store/column';
import { FilterOptionType } from '@t/store/filterLayerState';
import { observable } from '../helper/observable';
import { isRowNumColumn } from '../helper/column';
import {
createMapFromArray,
findIndex,
findProp,
includes,
omit,
isString,
isEmpty,
isFunction,
isNumber,
isObject,
isString,
isUndefined,
isNumber,
findProp,
omit,
uniq,
isEmpty,
findIndex,
} from '../helper/common';
import { DefaultRenderer } from '../renderer/default';
import { editorMap } from '../editor/manager';
Expand Down Expand Up @@ -244,6 +244,7 @@ export function createColumn(
filter,
className,
comparator,
customHeader,
} = column;

const editorOptions = createEditorOptions(editor);
Expand All @@ -262,7 +263,7 @@ export function createColumn(
return observable({
name,
escapeHTML,
header: header || name,
header: header || customHeader?.textContent || name,
hidden: Boolean(hidden),
resizable: isUndefined(resizable) ? Boolean(columnOptions.resizable) : Boolean(resizable),
align: align || 'left',
Expand Down Expand Up @@ -295,6 +296,7 @@ export function createColumn(
comparator,
autoResizing: width === 'auto',
rowSpan,
customHeader,
});
}

Expand Down
32 changes: 26 additions & 6 deletions packages/toast-ui.grid/src/view/columnHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { h, Component } from 'preact';
import { Component, h } from 'preact';
import { cls } from '../helper/dom';
import { HeaderCheckbox } from './headerCheckbox';
import { SortingButton } from './sortingButton';
import { SortingOrder } from './sortingOrder';
import { FilterButton } from './filterButton';
import { isRowHeader, isCheckboxColumn } from '../helper/column';
import { HeaderRenderer, ColumnHeaderInfo } from '@t/renderer';
import { isCheckboxColumn, isRowHeader } from '../helper/column';
import { ColumnHeaderInfo, HeaderRenderer } from '@t/renderer';
import Grid from '../grid';
import { isFunction } from '../helper/common';
import { isDraggableColumn } from '../query/column';
Expand All @@ -28,15 +28,35 @@ export class ColumnHeader extends Component<Props> {

private getElement(type: string) {
const { columnInfo } = this.props;
const { name, sortable, sortingType, filter, headerRenderer, header } = columnInfo;
const {
name,
sortable,
sortingType,
filter,
headerRenderer,
header,
customHeader,
} = columnInfo;

if (headerRenderer) {
return null;
}

switch (type) {
case 'checkbox':
return isCheckboxColumn(name) ? <HeaderCheckbox /> : header;
case 'checkbox': {
if (isCheckboxColumn(name)) {
return <HeaderCheckbox />;
}

if (this.el && customHeader) {
this.el.appendChild(customHeader);

return null;
}

return header;
}

case 'sortingBtn':
return sortable && <SortingButton columnName={name} sortingType={sortingType} />;
case 'sortingOrder':
Expand Down
9 changes: 7 additions & 2 deletions packages/toast-ui.grid/src/view/headerCheckbox.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { h, Component } from 'preact';
import { Component, h } from 'preact';
import { connect } from './hoc';
import { DispatchProps } from '../dispatch/create';
import { ColumnInfo } from '@t/store/column';

interface StoreProps {
header: string;
customHeader: ColumnInfo['customHeader'];
checkedAllRows: boolean;
disabled: boolean;
}
Expand Down Expand Up @@ -63,8 +65,11 @@ export const HeaderCheckbox = connect<StoreProps>((store) => {
column: { allColumnMap },
} = store;

const { header, customHeader } = allColumnMap._checked;

return {
header: allColumnMap._checked.header,
header,
customHeader,
checkedAllRows,
disabled: disabledAllCheckbox,
};
Expand Down
7 changes: 4 additions & 3 deletions packages/toast-ui.grid/types/renderer/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import TuiGrid from '../index';
import { CellRenderData, RowKey } from '../store/data';
import {
ColumnInfo,
AlignType,
VAlignType,
SortingType,
ColumnFilterOption,
ColumnInfo,
SortingType,
VAlignType,
} from '../store/column';

export type CellRendererProps = CellRenderData & {
Expand All @@ -29,6 +29,7 @@ export interface CellRendererClass {
export interface ColumnHeaderInfo {
name: string;
header: string;
customHeader?: ColumnInfo['customHeader'];
headerAlign?: AlignType;
headerVAlign?: VAlignType;
sortable?: boolean;
Expand Down
5 changes: 3 additions & 2 deletions packages/toast-ui.grid/types/store/column.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Side } from './focus';
import { CellValue, Row } from './data';
import { OptTree, Dictionary, OptColumnHeaderInfo, GridEventListener } from '../options';
import { HeaderRendererClass, CellRendererClass, CellRendererProps } from '../renderer';
import { Dictionary, GridEventListener, OptColumnHeaderInfo, OptTree } from '../options';
import { CellRendererClass, CellRendererProps, HeaderRendererClass } from '../renderer';
import { CellEditorClass } from '../editor';
import { FilterOptionType, OperatorType } from './filterLayerState';

Expand Down Expand Up @@ -125,6 +125,7 @@ export interface InvalidColumn {

export interface CommonColumnInfo {
header: string;
customHeader?: HTMLElement;
hidden: boolean;
align: AlignType;
valign: VAlignType;
Expand Down
Loading