Skip to content

Commit

Permalink
fix: don't reset controlled values
Browse files Browse the repository at this point in the history
  • Loading branch information
schummar committed Sep 2, 2022
1 parent 41de6b4 commit 7e02bad
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
17 changes: 12 additions & 5 deletions docs/stories/ExternalFilters.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export default {
} as ComponentMeta<typeof Table>;

export const Primary = () => {
const [sort, setSort] = useState<Sort[]>();
const [firstName, setFirstName] = useState<string>();
const [lastName, setLastName] = useState<string>();
const [jobTitle, setJobTitle] = useState<Set<string>>();
const [birthday, setBirthday] = useState<Date | DateRange | null>();
const [sort, setSort] = useState(new Array<Sort>());
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
const [jobTitle, setJobTitle] = useState(new Set<string>());
const [birthday, setBirthday] = useState<Date | DateRange | null>(null);

return (
<Box>
Expand All @@ -39,6 +39,13 @@ export const Primary = () => {
sort={sort}
onSortChange={setSort}
externalSort
onReset={() => {
setSort([]);
setFirstName('');
setLastName('');
setJobTitle(new Set());
setBirthday(null);
}}
columns={(col) => [
//
col((x) => x.avatar, {
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/useFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export function useFilter<T, V, F, S extends SerializableValue>(impl: FilterImpl

// On mount and reset: Fire onChange
useEffect(() => {
impl.onChange?.(impl.defaultValue);
if (impl.value === undefined) {
impl.onChange?.(impl.defaultValue);
}
}, [table]);

// Update implementation
Expand Down
22 changes: 18 additions & 4 deletions src/internalState/useTableState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,24 @@ export function useTableState<T>(_props: TableProps<T>): [Store<InternalTableSta

function reset() {
setKey({});
props.onSortChange?.(props.defaultSort ?? []);
props.onExpandedChange?.(props.defaultExpanded ?? new Set());
props.onSelectionChange?.(props.defaultSelection ?? new Set());
props.onHiddenColumnsChange?.(props.defaultHiddenColumns ?? new Set());

if (props.sort === undefined) {
props.onSortChange?.(props.defaultSort ?? []);
}

if (props.expanded === undefined) {
props.onExpandedChange?.(props.defaultExpanded ?? new Set());
}

if (props.selection === undefined) {
props.onSelectionChange?.(props.defaultSelection ?? new Set());
}

if (props.hiddenColumns === undefined) {
props.onHiddenColumnsChange?.(props.defaultHiddenColumns ?? new Set());
}

props.onReset?.();
}

const state = useMemo(
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ export interface TableProps<T> extends PartialTableTheme<T> {
};
debug?: (...output: any) => void;
debugRender?: (...output: any) => void;
onReset?: () => void;
}

export type InternalTableProps<T> = MemoizedFunctions<
Expand Down

0 comments on commit 7e02bad

Please sign in to comment.