/**
* @file 行选择相关逻辑
*/
import React, { ReactNode, useContext } from 'react'
import { CheckboxGroup, CheckAll, Checkbox } from '../Checkbox'
import { RadioGroup, Radio } from '../Radio'
import { InternalSelectionOptions, DataTableSelectionType } from './defs'
import { dataTableCtx } from './context'
export function SelectionHeadCell({ type }: { type: DataTableSelectionType }) {
if (type === 'single') return null
// eslint-disable-next-line react/no-children-prop
return // `children={null}` 是为了干掉 `CheckAll` 默认的 `checkAllText`
}
export function SelectionBodyCell({ index, type, disabled, hidden }: {
index: number
type: DataTableSelectionType
disabled: boolean
hidden: boolean
}) {
const { records } = useContext(dataTableCtx)
if (hidden) return <>>
const id = records[index].id
if (type === 'single') return
return
}
export function SelectionProvider({ selection, children }: {
selection: InternalSelectionOptions | undefined
children: ReactNode
}) {
if (selection == null) return <>{children}>
const type = selection.type ?? 'multiple'
const { selectedIds, onChange } = selection
if (type === 'single') {
const singleValue = selectedIds.length > 0 ? selectedIds[0] : null
const onSingleChange = (v: string | null) => onChange?.(v == null ? [] : [v])
return {children}
}
if (type === 'multiple') return {children}
throw new Error(`Invalid selection type: ${type}`)
}