import { IColorList, IEmits } from '../types'; interface IUseRowBgColorParams { colorList: IColorList; emit: IEmits; } export function useRowBgColor({ colorList, emit }: IUseRowBgColorParams) { const isDefaultColor = (id: number) => { if (!id) { // 没有颜色id,则认为是默认色 return true; } return colorList.find(c => +c.id === +id)?.default; }; const getColorById = (id: number, type: 'bg' | 'sample' = 'bg') => { return colorList.find(c => +c.id === +id)?.[`${type}Color`] || ''; }; const setRowStyle = (scope) => { const row = scope.row; return { backgroundColor: row.__static_bg_color__ ?? (row.colorId ? getColorById(row.colorId) : '') }; }; const handleColorChange = async (colorId: number, scope) => { const { row, $index: rowIndex, store } = scope; const dataList = store.states.data; const curRow = { ...dataList[rowIndex], colorId: +colorId }; const newList = [...dataList]; newList.splice(rowIndex, 1, curRow); store.states.data = newList; emit('row-bg-change', { colorId, row, rowIndex }); }; return { isDefaultColor, getColorById, setRowStyle, handleColorChange, }; }