// Copyright (c) Meta Platforms, Inc. and affiliates. 'use client'; import {useState} from 'react'; import * as stylex from '@stylexjs/stylex'; import {Avatar} from '@astryxdesign/core/Avatar'; import {CheckboxInput} from '@astryxdesign/core/CheckboxInput'; import {HStack} from '@astryxdesign/core/HStack'; import {useContainerReveal} from '@astryxdesign/core/hooks'; import { Table, proportional, useTableSelectionState, } from '@astryxdesign/core/Table'; import type {TableColumn} from '@astryxdesign/core/Table'; import {colorVars, spacingVars} from '@astryxdesign/core/theme/tokens.stylex'; import {mergeProps} from '@astryxdesign/core/utils'; interface Automation extends Record { id: string; name: string; owner: string; schedule: string; } const automations: Automation[] = [ {id: '1', name: 'Nightly backup', owner: 'Alice', schedule: 'Daily, 2:00'}, {id: '2', name: 'Weekly digest', owner: 'Bob', schedule: 'Mondays, 9:00'}, {id: '3', name: 'Cache warmup', owner: 'Diana', schedule: 'Hourly'}, {id: '4', name: 'Usage report', owner: 'Eve', schedule: 'Monthly, 1st'}, ]; const styles = stylex.create({ mark: { display: 'grid', placeItems: 'center', flexShrink: 0, }, // A flex layer is exactly as tall as the 24px avatar; a block layer // follows the taller line box and lets the avatar's edge show under the // checkbox. layer: { gridArea: '1 / 1', display: 'flex', }, // The checkbox covers the avatar while it shows, so the two never read as // one mark. checkbox: { borderRadius: '50%', backgroundColor: colorVars['--color-background-surface'], }, name: { paddingInlineStart: spacingVars['--spacing-1'], }, }); export default function TableLeadingMarkSelection() { const [selectedKeys, setSelectedKeys] = useState>(new Set()); // The mark reveals its checkbox on hover and keyboard focus, and always // shows it on touch. const {getContainerProps, getContentRevealProps} = useContainerReveal(); const {selectionConfig, selectionState} = useTableSelectionState({ data: automations, idKey: 'id', selectedKeys, setSelectedKeys, }); const columns: TableColumn[] = [ { key: 'name', header: ( selectionConfig.onSelectAll({isAllSelected: checked}) } /> Automation ), width: proportional(2), renderCell: item => { const isSelected = selectionConfig.getIsItemSelected(item); return ( {selectionState.hasSelection ? null : ( )} selectionConfig.onSelectItem({item, isSelected: checked}) } /> {item.name} ); }, }, {key: 'owner', header: 'Owner', width: proportional(1)}, {key: 'schedule', header: 'Schedule', width: proportional(1)}, ]; return ; }