From e3106c5020070546bfcbfd17f17742c278c27c77 Mon Sep 17 00:00:00 2001 From: Daniel Lu Date: Wed, 25 Jan 2023 16:52:28 -0800 Subject: [PATCH] Provide defaults for TableColumnLayout if options arent provided --- .../@react-stately/table/src/TableColumnLayout.ts | 4 ++-- .../@react-stately/table/test/TableUtils.test.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/@react-stately/table/src/TableColumnLayout.ts b/packages/@react-stately/table/src/TableColumnLayout.ts index f7d03d1574d..ad38dddf415 100644 --- a/packages/@react-stately/table/src/TableColumnLayout.ts +++ b/packages/@react-stately/table/src/TableColumnLayout.ts @@ -34,8 +34,8 @@ export class TableColumnLayout { columnMaxWidths: Map = new Map(); constructor(options: TableColumnLayoutOptions) { - this.getDefaultWidth = options.getDefaultWidth; - this.getDefaultMinWidth = options.getDefaultMinWidth; + this.getDefaultWidth = options?.getDefaultWidth ?? (() => '1fr'); + this.getDefaultMinWidth = options?.getDefaultMinWidth ?? (() => 0); } /** Takes an array of columns and splits it into 2 maps of columns with controlled and columns with uncontrolled widths. */ diff --git a/packages/@react-stately/table/test/TableUtils.test.js b/packages/@react-stately/table/test/TableUtils.test.js index 7a18c47f381..5942dd4a30a 100644 --- a/packages/@react-stately/table/test/TableUtils.test.js +++ b/packages/@react-stately/table/test/TableUtils.test.js @@ -42,6 +42,20 @@ describe('TableUtils', () => { }); }); + describe('table column layout', () => { + it('should generate column widths with defaults if none are provided', () => { + let layout = new TableColumnLayout(); + let collection = {columns: [{key: 'name', column: {props: {}}}, {key: 'type', column: {props: {}}}, {key: 'height', column: {props: {}}}, {key: 'weight', column: {props: {}}}, {key: 'level', column: {props: {}}}]}; + let columns = layout.buildColumnWidths( + 1000, + collection, + new Map([['name', undefined], ['type', undefined], ['height', undefined], ['weight', undefined], ['level', undefined]]) + ); + + expect(columns).toStrictEqual(new Map([['name', 200], ['type', 200], ['height', 200], ['weight', 200], ['level', 200]])); + }); + }); + describe('resizing', () => { it('can resize both controlled and uncontrolled columns', () => { let layout = new TableColumnLayout({