Skip to content

Commit

Permalink
Merge pull request #1272 from interval/select-table-initially-selected
Browse files Browse the repository at this point in the history
Add `initiallySelected` prop to `io.select.table`
  • Loading branch information
jacobmischka authored Jun 5, 2023
2 parents 28b2efe + 4eafa31 commit 9e9eb54
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/components/selectTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,45 @@ type PublicProps<Row> = Omit<T_IO_PROPS<'SELECT_TABLE'>, 'data' | 'columns'> & {
data: Row[]
columns?: (TableColumn<Row> | (string & keyof Row))[]
rowMenuItems?: (row: Row) => MenuItem[]
initiallySelected?: (row: Row) => boolean
}

export default function selectTable(logger: Logger) {
return function <Row extends z.input<typeof tableRow> = any>(
props: PublicProps<Row>
) {
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: {
...props,
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))
Expand Down
3 changes: 2 additions & 1 deletion src/examples/basic/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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', {
Expand Down

0 comments on commit 9e9eb54

Please sign in to comment.