import { callable, isNullish, mergeObject } from 'mixlea-utils-js'; import { mergeElementProps } from '@/components/ml-base-form-list'; import type { CellClickExtensionConfig } from './types'; import type { Pipeline } from '../pipeline'; import type { MlBaseFormListColumn } from '@/components/ml-base-form-list'; export type { CellClickExtensionConfig }; export const cellClickExtensionKey = 'cellClick'; export function cellClickExtension(config?: CellClickExtensionConfig) { if (isNullish(config)) { return (pipeline: Pipeline) => pipeline; } return function (pipeline: Pipeline) { const mapping = config.mapping; const columns = pipeline.getColumns(); pipeline.inputColumns(processColumns(columns)); return pipeline; function processColumns(columns: MlBaseFormListColumn[]) { return columns.map((col) => { const { getContentProps, field } = col; const callback = mapping[field]; if (callback) { const partialColumns: Partial = { getContentProps: (cellValue, row, opts) => { const prevContentProps = callable(getContentProps, cellValue, row, opts) ?? {}; return mergeElementProps(prevContentProps, { className: 'underline underline-offset-4 is-clickable', onClick: () => { callback({ cellValue, row, rowIndex: opts.rowIndex, colIndex: opts.colIndex }); }, }); }, }; return mergeObject(col, partialColumns); } return col; }); } }; }