Skip to content

Commit 846ef3a

Browse files
fix: allow columns with empty names in DataGrid
1 parent ca671cd commit 846ef3a

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

js/data-frame/index.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ const ShinyDataGrid: FC<ShinyDataGridProps<unknown>> = ({
244244
}
245245
},
246246
enableSorting,
247+
...(colname === "" && { id: findUnusedColumnName(columns, " ") }),
247248
};
248249
}),
249250
[columns, typeHints]
@@ -1018,6 +1019,21 @@ const ShinyDataGrid: FC<ShinyDataGridProps<unknown>> = ({
10181019
);
10191020
};
10201021

1022+
/**
1023+
* Solves the problem arising when there's more than one column with the same name (or when
1024+
* the column name is illegal, like in the case of an empty name) by returning a name with
1025+
* enough spaces appended so that it becomes unique.
1026+
* @param columns Names of existing columns.
1027+
* @param initName String to start the search with.
1028+
* @returns A string that does not exist in columns and thus can be used as column ID.
1029+
*/
1030+
function findUnusedColumnName(columns: ReadonlyArray<string>, initName = " "): string {
1031+
while (columns.includes(initName)) {
1032+
initName += " ";
1033+
}
1034+
return initName;
1035+
}
1036+
10211037
function findKeysBetween<TData>(
10221038
rowModel: RowModel<TData>,
10231039
fromKey: string,

shiny/www/py-shiny/data-frame/data-frame.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

shiny/www/py-shiny/data-frame/data-frame.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)