Skip to content

Commit 5ed768c

Browse files
committed
split predefined and custom granularities into separate lists to be able to use different styles/icons
1 parent 2e3f97d commit 5ed768c

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

packages/cubejs-playground/src/QueryBuilderV2/components/TimeListMember.tsx

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
TimeIcon,
88
CalendarIcon,
99
TooltipProvider,
10+
CubeIcon,
1011
} from '@cube-dev/ui-kit';
1112
import { Cube, TCubeDimension, TimeDimensionGranularity } from '@cubejs-client/core';
1213

@@ -32,7 +33,7 @@ interface ListMemberProps {
3233
onToggleDataRange?: (name: string) => void;
3334
}
3435

35-
const GRANULARITIES: TimeDimensionGranularity[] = [
36+
const PREDEFINED_GRANULARITIES: TimeDimensionGranularity[] = [
3637
'second',
3738
'minute',
3839
'hour',
@@ -67,12 +68,16 @@ export function TimeListMember(props: ListMemberProps) {
6768
const description = member.description;
6869
const isTimestampSelected = isSelected();
6970

71+
const customGranularities = (member.type === 'time' && member.granularities) ?
72+
member.granularities.map(g => g.name) : [];
7073
const memberGranularities = (member.type === 'time' && member.granularities) ?
71-
member.granularities.map(g => g.name).concat(GRANULARITIES) :
72-
GRANULARITIES;
73-
const isGranularitySelectedList = memberGranularities.map((granularity) => isSelected(granularity));
74+
member.granularities.map(g => g.name).concat(PREDEFINED_GRANULARITIES) :
75+
PREDEFINED_GRANULARITIES;
76+
const isGranularitySelectedMap = {};
77+
memberGranularities.forEach((granularity) => {
78+
isGranularitySelectedMap[granularity] = isSelected(granularity)
79+
});
7480
const selectedGranularity = memberGranularities.find((granularity) => isSelected(granularity));
75-
// const isGranularitySelected = !!isGranularitySelectedList.find((gran) => gran);
7681

7782
open = isCompact ? false : open;
7883

@@ -134,6 +139,26 @@ export function TimeListMember(props: ListMemberProps) {
134139
</ListMemberButton>
135140
);
136141

142+
const granularityItems = (items, icon) => {
143+
if (!open || isCompact) {
144+
return null;
145+
}
146+
147+
return items.map((granularity, i) => (<ListMemberButton
148+
key={`${name}.${granularity}`}
149+
icon={icon}
150+
data-member="timeDimension"
151+
isSelected={isGranularitySelectedMap[granularity]}
152+
onPress={() => {
153+
onGranularityToggle(member.name, granularity);
154+
setOpen(false);
155+
}}
156+
>
157+
<Text ellipsis>{granularity}</Text>
158+
</ListMemberButton>
159+
));
160+
}
161+
137162
return (
138163
<>
139164
{hasOverflow ? (
@@ -166,22 +191,9 @@ export function TimeListMember(props: ListMemberProps) {
166191
<Text ellipsis>value</Text>
167192
</ListMemberButton>
168193
) : null}
169-
{memberGranularities.map((granularity, i) => {
170-
return open && !isCompact ? (
171-
<ListMemberButton
172-
key={`${name}.${granularity}`}
173-
icon={<CalendarIcon />}
174-
data-member="timeDimension"
175-
isSelected={isGranularitySelectedList[i]}
176-
onPress={() => {
177-
onGranularityToggle(member.name, granularity);
178-
setOpen(false);
179-
}}
180-
>
181-
<Text ellipsis>{granularity}</Text>
182-
</ListMemberButton>
183-
) : null;
184-
})}
194+
{/* TODO: Add special icon for custom granularities and use it here */}
195+
{granularityItems(customGranularities, <CubeIcon />)}
196+
{granularityItems(PREDEFINED_GRANULARITIES, <CalendarIcon />)}
185197
</Flex>
186198
) : null}
187199
</>

0 commit comments

Comments
 (0)