Skip to content

An open source alternative to Tableau. Embeddable visual analytic

feat: select all when have keyword (#461)

islxyqwe3ea7c98eeda5
diff --git a/packages/graphic-walker/src/fields/filterField/tabs.tsx b/packages/graphic-walker/src/fields/filterField/tabs.tsx
index f8dee7a1..a0683b76 100644
--- a/packages/graphic-walker/src/fields/filterField/tabs.tsx
+++ b/packages/graphic-walker/src/fields/filterField/tabs.tsx
@@ -21,6 +21,7 @@ import { Input } from '@/components/ui/input';
import { Skeleton } from '@/components/ui/skeleton';
import { Checkbox } from '@/components/ui/checkbox';
import Tooltip from '@/components/tooltip';
+import Spinner from '@/components/spinner';
import { SparseArray } from './array';
export type RuleFormProps = {
@@ -111,7 +112,7 @@ const TabPanel = styled.div``;
const TabItem = styled.div``;
-const StatusCheckbox: React.FC<{ currentNum: number; totalNum: number; onChange: () => void; disabled?: boolean }> = (props) => {
+const StatusCheckbox: React.FC<{ currentNum: number; totalNum: number; onChange: () => void; disabled?: boolean; loading?: boolean }> = (props) => {
const { currentNum, totalNum, onChange } = props;
let checked: boolean | 'indeterminate';
@@ -123,6 +124,9 @@ const StatusCheckbox: React.FC<{ currentNum: number; totalNum: number; onChange:
checked = false;
}
+ if (props.loading) {
+ return <Spinner className="h-4 w-4 text-muted-foreground" />;
+ }
return <Checkbox checked={checked} disabled={props.disabled} onCheckedChange={() => onChange()} />;
};
@@ -297,7 +301,68 @@ export const useVisualCount = (
type: currentCount === metaData.valuesMeta.distinctTotal ? 'one of' : 'not in',
value: [],
});
- }, [field.rule, onChange, metaData]);
+ }, [field.rule, onChange, metaData, currentCount]);
+
+ // Add new state for loading select all button
+ const [loadingSelectAll, setLoadingSelectAll] = useState(false);
+
+ // New function to handle selecting all filtered results
+ const handleToggleCurrentFullSet = useCallback(async () => {
+ if (!field.rule || (field.rule.type !== 'one of' && field.rule.type !== 'not in') || !currentMeta) return;
+ if (!options.keyword) {
+ // If no keyword is set, just toggle the full set
+ handleToggleFullOrEmptySet();
+ return;
+ }
+
+ try {
+ setLoadingSelectAll(true);
+
+ // Fetch all values that match the current search keyword
+ const result = await fieldStat(
+ computation,
+ field,
+ {
+ range: false,
+ values: true,
+ valuesMeta: false,
+ sortBy,
+ keyword: options.keyword,
+ timezoneDisplayOffset: options.displayOffset,
+ },
+ allFields
+ );
+
+ const { values } = result;
+ if (!values || values.length === 0) return;
+ const existingValues = new Set(field.rule.value.map((v) => _unstable_encodeRuleValue(v)));
+ let totalCount = 0;
+ values.forEach((item) => {
+ if (existingValues.has(_unstable_encodeRuleValue(item.value))) return; // Skip if already selected
+ existingValues.add(_unstable_encodeRuleValue(item.value));
+ totalCount += item.count;
+ });
+ if (totalCount === 0) {
+ // all values are already selected
+ // deselect all values
+ values.forEach((item) => {
+ existingValues.delete(_unstable_encodeRuleValue(item.value));
+ totalCount += item.count;
+ });
+ setSelectedValueSum((x) => x - totalCount);
+ } else {
+ setSelectedValueSum((x) => x + totalCount);
+ }
+
+ onChange({
+ type: field.rule.type,
+ value: Array.from(existingValues),
+ });
+ } finally {
+ setLoadingSelectAll(false);
+ }
+ }, [field.rule, onChange, currentMeta, currentCount, computation, sortBy, options.keyword, options.displayOffset, allFields, loadedPageData]);
+
const handleToggleReverseSet = useCallback(() => {
if (!field.rule || (field.rule.type !== 'one of' && field.rule.type !== 'not in') || !metaData) return;
onChange({
@@ -332,11 +397,13 @@ export const useVisualCount = (
currentSum,
handleToggleFullOrEmptySet,
handleToggleReverseSet,
+ handleToggleCurrentFullSet,
handleSelect,
data,
loadData,
loading: !metaData,
loadingPageData,
+ loadingSelectAll,
};
};
@@ -450,10 +517,12 @@ export const FilterOneOfRule: React.FC<RuleFormProps & { active: boolean }> = ({
currentRows,
handleSelect,
handleToggleFullOrEmptySet,
+ handleToggleCurrentFullSet,
handleToggleReverseSet,
loadData,
loading,
loadingPageData,
+ loadingSelectAll,
} = useVisualCount(field, `${sortConfig.key}${sortConfig.ascending ? '' : '_dsc'}`, computation, onChange, allFields, {
displayOffset,
keyword: searchKeyword,
@@ -536,10 +605,10 @@ export const FilterOneOfRule: React.FC<RuleFormProps & { active: boolean }> = ({
<TableRow>
<div className="flex justify-center items-center">
<StatusCheckbox
- disabled={!!searchKeyword}
currentNum={currentCount}
totalNum={distinctTotal ?? 0}
- onChange={handleToggleFullOrEmptySet}
+ onChange={handleToggleCurrentFullSet}
+ loading={loadingSelectAll}
/>
</div>
<div className="header text-muted-foreground flex items-center">