From 4eafa31d45480b4de758e5eeaeed1b551632efa9 Mon Sep 17 00:00:00 2001 From: Jacob Mischka Date: Thu, 1 Jun 2023 13:12:30 -0500 Subject: [PATCH] Add initiallySelected prop to io.select.table --- src/components/selectTable.ts | 18 ++++++++++++++---- src/examples/basic/table.ts | 3 ++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/components/selectTable.ts b/src/components/selectTable.ts index c24b657..4220385 100644 --- a/src/components/selectTable.ts +++ b/src/components/selectTable.ts @@ -15,28 +15,37 @@ type PublicProps = Omit, 'data' | 'columns'> & { data: Row[] columns?: (TableColumn | (string & keyof Row))[] rowMenuItems?: (row: Row) => MenuItem[] + initiallySelected?: (row: Row) => boolean } export default function selectTable(logger: Logger) { return function = any>( props: PublicProps ) { - type DataList = typeof props['data'] + type DataList = (typeof props)['data'] const columns = columnsBuilder(props, column => logger.warn(missingColumnMessage('io.select.table')(column)) ) + let selectedKeys: string[] = [] + // Rendering all rows on initialization is necessary for filtering and sorting - const data = props.data.map((row, index) => - tableRowSerializer({ + const data = props.data.map((row, index) => { + const rowData = tableRowSerializer({ key: index.toString(), row, columns, menuBuilder: props.rowMenuItems, logger, }) - ) + + if (props.initiallySelected?.(row)) { + selectedKeys.push(rowData.key) + } + + return rowData + }) return { props: { @@ -44,6 +53,7 @@ export default function selectTable(logger: Logger) { data: data.slice(0, TABLE_DATA_BUFFER_SIZE), totalRecords: data.length, columns, + selectedKeys, }, getValue(response: T_IO_RETURNS<'SELECT_TABLE'>) { const indices = response.map(({ key }) => Number(key)) diff --git a/src/examples/basic/table.ts b/src/examples/basic/table.ts index ac3f4f4..61adb40 100644 --- a/src/examples/basic/table.ts +++ b/src/examples/basic/table.ts @@ -302,7 +302,7 @@ export const async_table: IntervalActionHandler = async () => { export const select_table: IntervalActionHandler = async io => { faker.seed(0) - const data = generateRows(50_000) + const data = generateRows(500) const selected = await io.select.table('Display users', { data, @@ -334,6 +334,7 @@ export const select_table: IntervalActionHandler = async io => { params: { email: row.email }, }, ], + initiallySelected: row => row.id % 2 === 0, }) await io.display.table('Display users', {