Skip to content

Commit

Permalink
[useTableState]: fix error when params not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekseyManetov committed Nov 14, 2024
1 parent aa9eb27 commit 8597e59
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions uui-core/src/hooks/useTableState/useTableState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,26 +196,26 @@ export interface UseTableStateHookParams<TFilter = Record<string, any>, TViewSta
extends UseTableStateHookBaseParams<TFilter, TViewState>, Partial<IEditable<DataTableState<TFilter, TViewState>>> {}

export const useTableState = <TFilter = Record<string, any>, TViewState = any>
(params?: UseTableStateHookParams<TFilter, TViewState>): ITableState<TFilter, TViewState> => {
(params: UseTableStateHookParams<TFilter, TViewState> = {}): ITableState<TFilter, TViewState> => {
const context = useUuiContext();

const externalValue = useRef(params.value);
const externalValue = useRef(params?.value);

const [tableStateValue, setTableStateValue] = useState<DataTableState<TFilter, TViewState>>(() => {
const value = getValueFromUrl(context.uuiRouter.getCurrentLink().query);
const activePreset = params.initialPresets?.find((p: ITablePreset<TFilter, TViewState>) => p.id === value.presetId);
const activePreset = params?.initialPresets?.find((p: ITablePreset<TFilter, TViewState>) => p.id === value.presetId);
const filtersConfig = normalizeFilterConfig(activePreset?.filtersConfig, value.filter, params?.filters);
return {
...value,
filtersConfig,
topIndex: 0,
visibleCount: params.initialVisibleCount || 40,
visibleCount: params?.initialVisibleCount || 40,
};
});

const getTableStateValue = () => {
if (params?.onValueChange) {
return params.value;
return params?.value;
}

const valueFromUrl = getValueFromUrl(context.uuiRouter.getCurrentLink().query);
Expand All @@ -241,11 +241,11 @@ export const useTableState = <TFilter = Record<string, any>, TViewState = any>
}
};
const onValueChange = useCallback((update: (val: DataTableState<TFilter, TViewState>) => DataTableState<TFilter, TViewState>) => {
if (params.onValueChange) {
if (params?.onValueChange) {
const newValue = update(externalValue.current);
const resultValue = normalizeTableStateValue(newValue, externalValue.current, params.filters);
externalValue.current = resultValue;
params.onValueChange(resultValue);
params?.onValueChange(resultValue);
} else {
setTableStateValue((currentValue) => {
const newValue = update(currentValue);
Expand Down

0 comments on commit 8597e59

Please sign in to comment.