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: add APIs that return added class names #1561

Merged
merged 4 commits into from
Jan 21, 2022
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
23 changes: 23 additions & 0 deletions packages/toast-ui.grid/cypress/integration/attributes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,29 @@ describe('className', () => {
cy.wrap($el).should('not.have.class', 'tui-grid-row-test');
});
});

it('getRowClassName() / getColumnClassName() / getCellClassName()', () => {
cy.gridInstance().invoke('addRowClassName', 0, 'tui-grid-row-test');
cy.gridInstance().invoke('addColumnClassName', 'age', 'tui-grid-column-test');
cy.gridInstance().invoke('addCellClassName', 0, 'age', 'tui-grid-cell-test');

cy.gridInstance()
.invoke('getRowClassName', 0)
.should('deep.equal', ['row-test-a', 'tui-grid-row-test']);

cy.gridInstance()
.invoke('getColumnClassName', 'age')
.should('deep.equal', ['tui-grid-column-test']);

cy.gridInstance()
.invoke('getCellClassName', 0, 'age')
.should('deep.equal', [
'row-test-a',
'tui-grid-row-test',
'tui-grid-cell-test',
'tui-grid-column-test',
]);
});
});

describe('row disabled', () => {
Expand Down
59 changes: 59 additions & 0 deletions packages/toast-ui.grid/src/grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ import {
pick,
deepCopy,
find,
includes,
isEmpty,
isNil,
} from './helper/common';
import { Observable, getOriginObject } from './helper/observable';
import { createEventBus, EventBus } from './event/eventBus';
Expand Down Expand Up @@ -1325,6 +1328,36 @@ export default class Grid implements TuiGrid {
this.dispatch('removeRowClassName', rowKey, className);
}

/**
* Return a list of class names of specific cell.
* @param {number|string} rowKey - The unique key of the row
* @param {string} columnName - The name of the column
* @returns {Array} - A list of class names
*/
public getCellClassName(rowKey: RowKey, columnName: string) {
const targetRow = this.getRow(rowKey);
const isExistColumnName = this.store.column.allColumns.some(({ name }) => name === columnName);

if (!isNil(targetRow) && isExistColumnName) {
const { row, column } = targetRow._attributes.className;

return [...row, ...(column[columnName] ?? [])];
}

return [];
}

/**
* Return a list of class names of specific row.
* @param {number|string} rowKey - The unique key of the row
* @returns {Array} - A list of class names
*/
public getRowClassName(rowKey: RowKey) {
const targetRow = this.getRow(rowKey);

return isNil(targetRow) ? [] : targetRow._attributes.className.row;
}

/**
* Add custom event to grid.
* @param {string} eventName - custom event name
Expand Down Expand Up @@ -1687,6 +1720,32 @@ export default class Grid implements TuiGrid {
this.dispatch('removeColumnClassName', columnName, className);
}

/**
* Return a list of class names of specific column.
* @param {string} columnName - The name of the column
* @returns {Array} - A list of class names
*/
public getColumnClassName(columnName: string) {
const { rawData } = this.store.data;
const classNamesOfFirstRow: string[] = rawData[0]._attributes.className.column[columnName];

if (isEmpty(classNamesOfFirstRow)) {
return [];
}

return rawData.slice(1).reduce((acc, row, _, arr) => {
const classNames = row._attributes.className.column[columnName];

if (isEmpty(classNames) || isEmpty(acc)) {

Choose a reason for hiding this comment

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

isEmpty(acc) ๋ถ€๋ถ„์€ ์œ„์—์„œ ์ด๋ฏธ ๊ฑธ๋Ÿฌ์ง€๋Š”๊ฑฐ ์•„๋‹Œ๊ฐ€์š”?
๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜์—์„œ arr.splice(0) ๋ฅผ ํ•˜๊ฒŒ๋˜๋Š” ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ด์ง€๋„ค์š”.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

์•„๋ž˜์ชฝ์˜ return acc.filter((className) => includes(classNames, className));์„ ๋ณด์‹œ๋ฉด ์•„์‹œ๊ฒ ์ง€๋งŒ, ๊ธฐ๋ณธ ๋™์ž‘์ด ์ฒซ ํ–‰์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ClassNames์—์„œ ๋‹ค๋ฅธ ํ–‰๋“ค์€ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š๋Š” ClassNames๋ฅผ ์ œ์™ธํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

์ด ๊ณผ์ •์—์„œ, ๋งŒ์•ฝ ๋ชจ๋“  ClassName์ด ์ œ๊ฑฐ๋œ๋‹ค๋ฉด(์ปฌ๋Ÿผ์— ํ• ๋‹นํ•œ ClassName์ด ์—†๋‹ค๋ฉด) ๋” ์ด์ƒ loop๋ฅผ ์ˆ˜ํ–‰ํ•  ์ด์œ ๊ฐ€ ์—†์–ด์ ธ์„œ ์ด๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด isEmpty(acc)๋ฅผ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  Array.reduce๋Š” ๋„ค ๋ฒˆ์žฌ ์ธ์ž๋กœ ์ „๋‹ฌ๋˜๋Š” ๋ฐฐ์—ด์— ๋Œ€ํ•ด ์ˆ˜ํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋” ์ด์ƒ loop๋ฅผ ์ˆ˜ํ–‰ํ•  ํ•„์š”๊ฐ€ ์—†์–ด์ง„ ๊ฒฝ์šฐ ์ด ๋ฐฐ์—ด์„ ๋ณ€ํ˜•ํ•ด ์กฐ๊ธฐ ์ข…๋ฃŒ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด arr.splice(0)๋ฅผ ์ˆ˜ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.

arr.splice(0);

return [];
}

return acc.filter((className) => includes(classNames, className));
}, classNamesOfFirstRow);
}

/**
* Set new data to the row identified by the specified rowKey.
* @param {number|string} rowKey - The unique key of the row
Expand Down
6 changes: 6 additions & 0 deletions packages/toast-ui.grid/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ declare namespace tui {

public removeRowClassName(rowKey: RowKey, className: string): void;

public getCellClassName(rowKey: RowKey, columnName: string): string[];

public getRowClassName(rowKey: RowKey): string[];

public on(eventName: GridEventName, fn: GridEventListener): void;

public off(eventName: GridEventName, fn?: GridEventListener): void;
Expand Down Expand Up @@ -275,6 +279,8 @@ declare namespace tui {

public removeColumnClassName(columnName: string, className: string): void;

public getColumnClassName(columnName: string): string[];

public setRow(rowKey: RowKey, row: OptRow): void;

public moveRow(rowKey: RowKey, targetIndex: number, options: OptMoveRow): void;
Expand Down