@ui5/webcomponents-react
Version:
React Wrapper for UI5 Web Components and additional components
147 lines • 4.11 kB
JavaScript
export const stateReducer = (state, action, _prevState, instance) => {
const {
payload
} = action;
switch (action.type) {
case 'setFilter':
{
const {
onFilter
} = instance.webComponentsReactProperties;
if (typeof onFilter === 'function') {
instance.webComponentsReactProperties.onFilter({
filters: state.filters,
value: action.filterValue,
columnId: action.columnId
});
}
return state;
}
case 'toggleRowExpanded':
// this flag disables scrolling to the top of the table if a table is collapsed
if (!state.expanded[action.id]) {
instance.dispatch({
type: 'ROW_COLLAPSED_FLAG',
payload: true
});
}
return state;
case 'TABLE_RESIZE':
{
const nextWidth = Math.floor(payload.tableClientWidth);
if (nextWidth === state.tableClientWidth) {
return state;
}
// `tableClientWidth` is misleading, as only when scaled the `clientWidth` is used. In all other cases `getBoundingClientRect` is measuring the width.
// Without `retainColumnWidth` (!state.tableColResized), clear user-resized widths on container resize so `adjustColumnWidths` recalculates.
if (!state.tableColResized && Object.keys(state.columnResizing?.columnWidths ?? {}).length > 0) {
// dead-zone for reset trigger, to prevent resizes when a scrollbar is briefly displayed
const widthDelta = Math.abs(nextWidth - state.tableClientWidth);
if (widthDelta > 20) {
return {
...state,
tableClientWidth: nextWidth,
columnResizing: {
...state.columnResizing,
columnWidths: {}
}
};
}
return {
...state,
tableClientWidth: nextWidth
};
}
return {
...state,
tableClientWidth: nextWidth
};
}
case 'VISIBLE_ROWS':
if (payload.visibleRows === state.visibleRows) {
return state;
}
return {
...state,
visibleRows: payload.visibleRows
};
case 'TABLE_SCROLLING_ENABLED':
if (payload.isScrollable === state.isScrollable) {
return state;
}
return {
...state,
isScrollable: payload.isScrollable
};
case 'SET_SELECTED_ROW_IDS':
return {
...state,
selectedRowIds: payload.selectedRowIds
};
case 'SET_POPIN_COLUMNS':
return {
...state,
popInColumns: payload
};
case 'INTERACTIVE_ROWS_HAVE_POPIN':
return {
...state,
interactiveRowsHavePopIn: payload
};
case 'IS_RTL':
return {
...state,
isRtl: payload.isRtl
};
case 'SUB_COMPONENTS_HEIGHT':
return {
...state,
subComponentsHeight: payload
};
case 'TABLE_COL_RESIZED':
return {
...state,
tableColResized: payload
};
case 'resetResize':
// reset `tableColResized` state, when react-table resize state is reset
return {
...state,
tableColResized: undefined
};
case 'ROW_COLLAPSED_FLAG':
return {
...state,
rowCollapsed: payload
};
case 'COLUMN_DND_START':
return {
...state,
dndColumn: payload
};
case 'COLUMN_DND_END':
return {
...state,
dndColumn: ''
};
// fallback if the component wasn't ready yet for scrolling (elements are not initialized), e.g. when calling `.scrollToItem` on mount
case 'TRIGGER_PROG_SCROLL':
return {
...state,
triggerScroll: payload
};
case 'AUTO_RESIZE':
return {
...state,
columnResizing: {
...state.columnResizing,
columnWidths: {
...state.columnResizing.columnWidths,
...payload
}
}
};
default:
return state;
}
};