Skip to content

Commit

Permalink
Add style.visibility property (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi authored Nov 22, 2023
1 parent 38db1ba commit bc29a4a
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 16 deletions.
24 changes: 24 additions & 0 deletions packages/cheetah-grid/src/js/columns/style/BaseStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
ColumnStyle,
IndicatorDefine,
IndicatorObject,
Visibility,
} from "../../ts-types";
import { EventTarget } from "../../core/EventTarget";

Expand All @@ -14,6 +15,7 @@ const STYLE_EVENT_TYPE = {
let defaultStyle: BaseStyle;
export class BaseStyle extends EventTarget implements ColumnStyle {
private _bgColor?: ColorDef;
private _visibility?: Visibility;
private _indicatorTopLeft?: IndicatorObject;
private _indicatorTopRight?: IndicatorObject;
private _indicatorBottomRight?: IndicatorObject;
Expand All @@ -26,13 +28,15 @@ export class BaseStyle extends EventTarget implements ColumnStyle {
}
constructor({
bgColor,
visibility,
indicatorTopLeft,
indicatorTopRight,
indicatorBottomRight,
indicatorBottomLeft,
}: BaseStyleOption = {}) {
super();
this._bgColor = bgColor;
this._visibility = normalizeVisibility(visibility, undefined);
this._indicatorTopLeft = normalizeIndicator(indicatorTopLeft);
this._indicatorTopRight = normalizeIndicator(indicatorTopRight);
this._indicatorBottomRight = normalizeIndicator(indicatorBottomRight);
Expand All @@ -45,6 +49,17 @@ export class BaseStyle extends EventTarget implements ColumnStyle {
this._bgColor = bgColor;
this.doChangeStyle();
}
get visibility(): Visibility | undefined {
return this._visibility;
}
set visibility(visibility: Visibility | undefined) {
const normalized = normalizeVisibility(visibility, this._visibility);
if (this._visibility === normalized) {
return;
}
this._visibility = normalized;
this.doChangeStyle();
}
get indicatorTopLeft(): IndicatorObject | undefined {
return this._indicatorTopLeft;
}
Expand Down Expand Up @@ -88,3 +103,12 @@ function normalizeIndicator(
}
return indicator;
}
function normalizeVisibility(
visibility: Visibility | undefined,
defaultValue: Visibility | undefined
): Visibility | undefined {
if (visibility && visibility !== "visible" && visibility !== "hidden") {
return defaultValue;
}
return visibility;
}
Original file line number Diff line number Diff line change
Expand Up @@ -634,12 +634,16 @@ export class BranchGraphColumn<T> extends BaseColumn<T> {
mergeStyle,
margin,
bgColor,
visibility,
} = style;
if (bgColor) {
drawCellBase({
bgColor,
});
}
if (visibility === "hidden") {
return;
}

const rect = context.getRect();
const radius = circleSize / 2;
Expand Down
4 changes: 4 additions & 0 deletions packages/cheetah-grid/src/js/columns/type/ButtonColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,16 @@ export class ButtonColumn<T> extends Column<T> {
font,
padding,
textOverflow,
visibility,
} = style;
if (bgColor) {
drawCellBase({
bgColor,
});
}
if (visibility === "hidden") {
return;
}
const textValue = value != null ? String(value) : "";
helper.testFontLoad(font, textValue, context);
const { col, row } = context;
Expand Down
4 changes: 4 additions & 0 deletions packages/cheetah-grid/src/js/columns/type/CheckColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ export class CheckColumn<T> extends BaseColumn<T> {
checkBgColor,
uncheckBgColor,
bgColor,
visibility,
} = style;
if (bgColor) {
drawCellBase({
bgColor,
});
}
if (visibility === "hidden") {
return;
}

const { col, row } = context;
const range = grid.getCellRange(col, row);
Expand Down
4 changes: 4 additions & 0 deletions packages/cheetah-grid/src/js/columns/type/Column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ export class Column<T> extends BaseColumn<T> {
bgColor,
padding,
textOverflow,
visibility,
} = style;
if (bgColor) {
drawCellBase({
bgColor,
});
}
if (visibility === "hidden") {
return;
}
const textValue = value != null ? String(value) : "";
helper.testFontLoad(font, textValue, context);
utils.loadIcons(getIcon(), context, helper, (icons, context) => {
Expand Down
15 changes: 9 additions & 6 deletions packages/cheetah-grid/src/js/columns/type/ImageColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,16 @@ export class ImageColumn<T> extends BaseColumn<T> {
_grid: ListGridAPI<T>,
{ drawCellBase }: DrawCellInfo<T>
): void {
const { textAlign, textBaseline, margin, bgColor, visibility } = style;
if (bgColor) {
drawCellBase({
bgColor,
});
}
if (visibility === "hidden") {
return;
}
if (value) {
const { textAlign, textBaseline, margin, bgColor } = style;
if (bgColor) {
drawCellBase({
bgColor,
});
}
helper.drawWithClip(context, (ctx) => {
ctx.textAlign = textAlign;
ctx.textBaseline = textBaseline;
Expand Down
4 changes: 4 additions & 0 deletions packages/cheetah-grid/src/js/columns/type/MenuColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ export class MenuColumn<T> extends BaseColumn<T> {
padding,
textOverflow,
appearance,
visibility,
} = style;
let { color } = style;
if (bgColor) {
drawCellBase({
bgColor,
});
}
if (visibility === "hidden") {
return;
}
const convertedValue = this._convertInternal(value);
const text = convertedValue != null ? String(convertedValue) : "";
helper.testFontLoad(font, text, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ export class MultilineTextColumn<T> extends BaseColumn<T> {
autoWrapText,
lineClamp,
textOverflow,
visibility,
} = style;
if (bgColor) {
drawCellBase({
bgColor,
});
}
if (visibility === "hidden") {
return;
}
const textValue = value != null ? String(value) : "";
const multilines = textValue
.replace(/\r?\n/g, "\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ export class PercentCompleteBarColumn<T> extends Column<T> {
grid,
info
);
const { barColor, barBgColor, barHeight } = style;
const { barColor, barBgColor, barHeight, visibility } = style;
if (visibility === "hidden") {
return;
}

let textValue = value != null ? String(value) : "";
if (str.endsWith(textValue, "%")) {
Expand Down
4 changes: 4 additions & 0 deletions packages/cheetah-grid/src/js/columns/type/RadioColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ export class RadioColumn<T> extends BaseColumn<T> {
uncheckBgColor,
checkBgColor,
bgColor,
visibility,
} = style;
if (bgColor) {
drawCellBase({
bgColor,
});
}
if (visibility === "hidden") {
return;
}

const { col, row } = context;
const range = grid.getCellRange(col, row);
Expand Down
3 changes: 3 additions & 0 deletions packages/cheetah-grid/src/js/ts-types/column/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import type {
IndicatorObject,
LineClamp,
TextOverflow,
Visibility,
} from "../define";
import type { ColorDef } from "../base";

export interface ColumnStyle {
bgColor?: ColorDef;
visibility?: Visibility;
indicatorTopLeft?: IndicatorObject;
indicatorTopRight?: IndicatorObject;
indicatorBottomRight?: IndicatorObject;
Expand All @@ -18,6 +20,7 @@ export interface ColumnStyle {

export interface BaseStyleOption {
bgColor?: ColorDef;
visibility?: Visibility;
indicatorTopLeft?: IndicatorDefine;
indicatorTopRight?: IndicatorDefine;
indicatorBottomRight?: IndicatorDefine;
Expand Down
3 changes: 3 additions & 0 deletions packages/cheetah-grid/src/js/ts-types/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ export interface ColumnMenuItemObjectOptions {
[value: string]: string;
}

// ****** Style Options *******
export type Visibility = "visible" | "hidden";

// ****** TextStyle Options *******
export type TextOverflow = "clip" | "ellipsis" | string /* a char */;
export type LineClamp = number | "auto";
Expand Down
6 changes: 6 additions & 0 deletions packages/cheetah-grid/src/test/ListGrid_sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,13 @@ Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deseru
action: function(rec) {
alert('ID:' + rec.personid + ' ' + JSON.stringify(rec));//eslint-disable-line
},
disabled: function(rec) {
return rec.personid === 1;
},
}),
style: function(rec) {
return rec.personid === 1 ? {visibility: 'hidden'} : null;
},
},
],
},
Expand Down
19 changes: 10 additions & 9 deletions packages/docs/api/js/column_styles/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ Define column style by using `style` property.

Properties below are prepared in standard.

| Property | Description |
| ------------ | ------------------------------------------------------------------------------------------------ |
| color | Define the color of cell. |
| textAlign | Define the horizontal position of text in cell. |
| textBaseline | Define the vertical position of text in cell. |
| bgColor | Define the background color of cell. |
| font | Define the font of cell. |
| padding | Define the padding of cell. If you set 4 values separately, please set the `Array`. |
| textOverflow | Define how to display when text overflows the area of a cell. `clip` or `ellipsis` is available. |
| Property | Description |
| ------------ | --------------------------------------------------------------------------------------------------------------- |
| color | Define the color of cell. |
| textAlign | Define the horizontal position of text in cell. |
| textBaseline | Define the vertical position of text in cell. |
| bgColor | Define the background color of cell. |
| font | Define the font of cell. |
| padding | Define the padding of cell. If you set 4 values separately, please set the `Array`. |
| textOverflow | Define how to display when text overflows the area of a cell. `clip` or `ellipsis` is available. |
| visibility | Specifying `hidden` to it will stop drawing the cell's content. Note that this does not stop the cell's action. |

Note that the column type may add style properties that you can use. The properties added are described in the documentation for each column type.

Expand Down

0 comments on commit bc29a4a

Please sign in to comment.