Skip to content
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
2 changes: 1 addition & 1 deletion src/display/data-schema/data.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export interface Grid {
id?: string; // Default: uid
label?: string;
show?: boolean; // Default: true
cells: (0 | 1)[][];
cells: (0 | 1 | string)[][];
gap?: Gap;
item: {
components?: Component[];
Expand Down
2 changes: 1 addition & 1 deletion src/display/data-schema/element-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const groupSchema = Base.extend({
*/
export const gridSchema = Base.extend({
type: z.literal('grid'),
cells: z.array(z.array(z.union([z.literal(0), z.literal(1)]))),
cells: z.array(z.array(z.union([z.literal(0), z.literal(1), z.string()]))),
gap: Gap,
item: z.object({
components: componentArraySchema.default([]),
Expand Down
8 changes: 6 additions & 2 deletions src/display/mixins/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ export const Base = (superClass) => {
? Object.keys(nextProps)
: Object.keys(actualChanges);

const { id, label, attrs } = validatedChanges;
if (id || label || attrs) {
if (
['id', 'label', 'attrs'].some((key) =>
Object.hasOwn(validatedChanges, key),
)
) {
const { id, label, attrs } = validatedChanges;
this._applyRaw({ id, label, ...attrs }, mergeStrategy);
}

Expand Down
36 changes: 20 additions & 16 deletions src/display/mixins/Cellsable.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,31 @@ export const Cellsable = (superClass) => {
const { cells } = relevantChanges;

const { gap, item: itemProps } = this.props;
const currentItemIds = new Set(this.children.map((child) => child.id));

const requiredItemIds = new Set();
const childrenMap = new Map(
this.children.map((child) => [child.id, child]),
);

cells.forEach((row, rowIndex) => {
row.forEach((col, colIndex) => {
if (!col) return;
const id = `${this.id}.${rowIndex}.${colIndex}`;
if (col === 1) {
requiredItemIds.add(id);
if (!currentItemIds.has(id)) {
const item = newElement('item', this.context);
item.apply({
type: 'item',
id,
...itemProps,
attrs: {
x: colIndex * (itemProps.size.width + gap.x),
y: rowIndex * (itemProps.size.height + gap.y),
},
});
this.addChild(item);
}
requiredItemIds.add(id);

const label = typeof col === 'string' ? col : '';
const existingItem = childrenMap.get(id);
if (!existingItem) {
const attrs = {
gridIndex: { row: rowIndex, col: colIndex },
x: colIndex * (itemProps.size.width + gap.x),
y: rowIndex * (itemProps.size.height + gap.y),
};
const item = newElement('item', this.context);
item.apply({ type: 'item', id, ...itemProps, label, attrs });
this.addChild(item);
} else {
existingItem.apply({ label });
}
});
});
Expand Down