Skip to content

Commit

Permalink
fix(FilterInput): synchronize filters state with props and optimize e…
Browse files Browse the repository at this point in the history
…ffect dependencies (#331)

* fix(FilterInput): synchronize filters state with props and optimize effect dependencies

Signed-off-by: ya zhou <[email protected]>

* feat(Table): enhance DataTableSimple with loading state and dynamic data updates

Signed-off-by: ya zhou <[email protected]>

* ci: update version

Signed-off-by: ya zhou <[email protected]>

---------

Signed-off-by: ya zhou <[email protected]>
  • Loading branch information
yazhouio authored Jan 10, 2025
1 parent c8f2853 commit 425225e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
13 changes: 13 additions & 0 deletions .changeset/tame-owls-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@kubed/code-editor': patch
'@kubed/diff-viewer': patch
'@kubed/components': patch
'@kubed/log-viewer': patch
'@kubed/charts': patch
'@kubed/hooks': patch
'@kubed/icons': patch
'@kubed/tests': patch
'kubed-documents': patch
---

fix(FilterInput): synchronize filters state with props and optimize effect dependencies
14 changes: 11 additions & 3 deletions packages/components/src/FilterInput/FilterInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState, useEffect, useRef, ReactNode } from 'react';
import cx from 'classnames';
import { find, isEmpty, trim, omit } from 'lodash';
import { find, isEmpty, trim, omit, isEqual } from 'lodash';
import { Magnifier, Close } from '@kubed/icons';
import { useClickOutside } from '@kubed/hooks';
import forwardRef from '../utils/forwardRef';
Expand Down Expand Up @@ -72,15 +72,23 @@ export const FilterInput = forwardRef<FilterInputProps, null>((props, ref) => {
});
const inputRef = useRef<HTMLInputElement>();

const [filters, setFilters] = React.useState(props.filters);
React.useEffect(() => {
if (isEqual(filters, props.filters)) {
return;
}
setFilters(props.filters);
}, [props.filters]);

useEffect(() => {
if (!props.simpleMode) {
const newTags = getTags(props.suggestions, props.filters);
const newTags = getTags(props.suggestions, filters);
setTags(newTags);
setActiveSuggestion(null);
setOptionVisible(false);
setValue('');
}
}, [props.filters]);
}, [filters]);

useEffect(() => {
if (props.simpleMode) {
Expand Down
21 changes: 15 additions & 6 deletions packages/components/src/Table/Table.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ export const DataTableWithDefault = () => {
};

export const DataTableSimple = () => {
const [loading] = React.useState(false);
const [loading, setLoading] = React.useState(false);
const [columns] = React.useState<typeof defaultColumns>(() => [
{
id: '_selector',
Expand Down Expand Up @@ -1016,19 +1016,27 @@ export const DataTableSimple = () => {
},
...defaultColumns,
]);
const [key, setKey] = React.useState(0);
React.useEffect(() => {
const timer = setInterval(() => {
setKey((_key) => _key + 1);
setLoading((_loading) => !_loading);
}, 2000);
return () => clearInterval(timer);
}, []);
const data = React.useMemo(() => {
if (loading) {
return [];
}
return [...Array(100).fill(1)].map((_, i) => ({
firstName: `firstName-${i}`,
lastName: `lastName-${i}`,
firstName: `firstName-${i}-${key}`,
lastName: `lastName-${i}-${key}`,
age: i,
visits: i,
status: `status-${i}`,
status: `status-${i}-${key}`,
progress: i,
}));
}, [loading]);
}, [loading, key]);

const forceUpdate = React.useReducer(() => ({}), {})[1];

Expand All @@ -1046,6 +1054,7 @@ export const DataTableSimple = () => {
...defaultOption,
data,
columns,
loading,
getRowId: (row) => row.firstName,
meta: {
refetch: () => forceUpdate(),
Expand All @@ -1066,7 +1075,7 @@ export const DataTableSimple = () => {
},
filters: () => {
return {
disabled: true,
disabled: false,
simpleMode: false,
suggestions: [
{
Expand Down

0 comments on commit 425225e

Please sign in to comment.