Skip to content

Commit 81b01c5

Browse files
authored
feat: Add label to the state column of DataGrid (#139)
1 parent 3b45b86 commit 81b01c5

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/DesignKit.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -4687,13 +4687,14 @@ const DataGridSection: React.FC = () => {
46874687
lastLogin: Date;
46884688
state: string;
46894689
array?: unknown[];
4690+
label?: string;
46904691
}
46914692
const [columnWidths, setColumnWidths] = useState<Record<string, number>>({
46924693
array: 100,
46934694
lastLogin: 200,
46944695
name: 150,
46954696
score: 100,
4696-
state: 75,
4697+
state: 120,
46974698
});
46984699
const [columnsOrder, setColumnsOrder] = useState<string[]>([
46994700
'name',
@@ -4736,6 +4737,7 @@ const DataGridSection: React.FC = () => {
47364737
data: {
47374738
appTheme: theme,
47384739
kind: STATE_CELL,
4740+
label: record.label,
47394741
state: record.state,
47404742
},
47414743
kind: GridCellKind.Custom,
@@ -4764,6 +4766,7 @@ const DataGridSection: React.FC = () => {
47644766
const data: Loadable<Person>[] = [
47654767
Loaded({
47664768
array: undefined,
4769+
label: 'OOM',
47674770
lastLogin: new Date('01/01/2011'),
47684771
name: 'Alice',
47694772
score: 99,

src/kit/DataGrid/custom-renderers/cells/stateCell.tsx

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { CustomCell, CustomRenderer, GridCellKind } from '@glideapps/glide-data-grid';
1+
import {
2+
CustomCell,
3+
CustomRenderer,
4+
getMiddleCenterBias,
5+
GridCellKind,
6+
measureTextCached,
7+
} from '@glideapps/glide-data-grid';
28

39
import { roundedRect } from 'kit/DataGrid/custom-renderers/utils';
410
import { Theme } from 'kit/Theme';
@@ -23,17 +29,21 @@ interface StateCellProps {
2329
readonly appTheme: Theme;
2430
readonly kind: typeof STATE_CELL;
2531
readonly state: State;
32+
readonly label?: string;
2633
}
2734

2835
const PI = Math.PI;
2936
const QUADRANT = PI / 2;
37+
const TAG_HEIGHT = 18;
38+
const maxLabelLength = 12;
39+
const labelColor = '#CC0000';
3040

3141
export type StateCell = CustomCell<StateCellProps>;
3242

3343
const renderer: CustomRenderer<StateCell> = {
3444
draw: (args, cell) => {
3545
const { ctx, rect, theme, requestAnimationFrame } = args;
36-
const { state, appTheme } = cell.data;
46+
const { state, appTheme, label } = cell.data;
3747

3848
const xPad = theme.cellHorizontalPadding;
3949

@@ -201,6 +211,32 @@ const renderer: CustomRenderer<StateCell> = {
201211
}
202212
}
203213

214+
if (label) {
215+
const labelText =
216+
label.length < maxLabelLength ? label : `${label.slice(0, maxLabelLength)}...`;
217+
218+
const tagFont = `600 11px ${theme.fontFamily}`;
219+
220+
ctx.fillStyle = labelColor;
221+
ctx.strokeStyle = labelColor;
222+
ctx.lineWidth = 2;
223+
224+
ctx.beginPath();
225+
ctx.font = tagFont;
226+
roundedRect(
227+
ctx,
228+
x + 12,
229+
y - 8,
230+
measureTextCached(labelText, ctx, tagFont).width + 10,
231+
TAG_HEIGHT,
232+
4,
233+
);
234+
ctx.stroke();
235+
ctx.fill();
236+
ctx.fillStyle = '#fff';
237+
ctx.fillText(labelText, x + 16, y - 8 + TAG_HEIGHT / 2 + getMiddleCenterBias(ctx, tagFont));
238+
}
239+
204240
ctx.restore();
205241

206242
return true;

0 commit comments

Comments
 (0)