UNPKG

@ui5/webcomponents-react

Version:

React Wrapper for UI5 Web Components and additional components

52 lines (50 loc) 1.79 kB
'use client'; import { useEffect } from 'react'; import { AnalyticalTableSelectionMode } from '../../../enums/AnalyticalTableSelectionMode.js'; //todo: reuse `manualRowSelectedKey` react-table option (currently noop, add type again) and remove manualRowSelectedKey param here in v3. /** * A plugin hook for manual row selection. * * @param {string} [manualRowSelectedKey='isSelected'] - If this key is found on the original data row, and it is true, this row will be manually selected. * * __Note:__ Per default, this hook sets `reactTableOptions.autoResetSelectedRows = false` if not defined. */ export const useManualRowSelect = (manualRowSelectedKey = 'isSelected') => { const useInstanceAfterData = instance => { const { flatRows, toggleRowSelected, webComponentsReactProperties } = instance; const { selectionMode } = webComponentsReactProperties; if (!('autoResetSelectedRows' in instance)) { // `instance` is mutable // eslint-disable-next-line react-hooks/immutability instance.autoResetSelectedRows = false; } useEffect(() => { if (selectionMode === AnalyticalTableSelectionMode.None) { return; } flatRows.forEach(({ id, original, isSelected }) => { if (manualRowSelectedKey in original) { const shouldBeSelected = !!original[manualRowSelectedKey]; if (shouldBeSelected !== isSelected) { toggleRowSelected(id, shouldBeSelected); } } }); }, [flatRows, toggleRowSelected, selectionMode]); }; const manualRowSelect = hooks => { hooks.useInstanceAfterData.push(useInstanceAfterData); }; manualRowSelect.pluginName = 'useManualRowSelect'; return manualRowSelect; };