Skip to content

Commit b09b97d

Browse files
committed
fix: optimize table visibility
1 parent 8c1eacc commit b09b97d

File tree

3 files changed

+72
-28
lines changed

3 files changed

+72
-28
lines changed

packages/components-vue/src/components/select/Choice.vue

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
themeValues[!multiple && modelValue?.includes(option.value) ? 0 : 1]
3737
}`"
3838
:style="
39-
isURL(option.pattern)
40-
? { backgroundImage: `url('${option.pattern}')` }
41-
: { backgroundColor: option.pattern }
39+
isColor(option.pattern)
40+
? { backgroundColor: option.pattern }
41+
: { backgroundImage: `url('${option.pattern}')` }
4242
"
4343
></figure>
4444
</template>
@@ -49,7 +49,9 @@
4949

5050
<script setup lang="ts">
5151
import { computed } from "vue";
52-
import isURL from "validator/lib/isURL";
52+
import isHexColor from "validator/lib/isHexColor";
53+
import isRgbColor from "validator/lib/isRgbColor";
54+
import isHSL from "validator/lib/isHSL";
5355
5456
import { toOption, useI18n } from "@open-xamu-co/ui-common-helpers";
5557
@@ -121,4 +123,8 @@
121123
122124
return value.length <= 2 || !!option.pattern;
123125
}
126+
127+
function isColor(color: string): boolean {
128+
return isHexColor(color) || isRgbColor(color) || isHSL(color);
129+
}
124130
</script>

packages/components-vue/src/components/table/Body.vue

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
cloneNodeAndRefresh,
8888
deleteNodeAndRefresh,
8989
deleteNodesAndRefresh,
90-
show: showChildren(nodeIndex, node),
90+
show: visibility[nodeIndex].show,
9191
}"
9292
></slot>
9393
<ActionButton
@@ -99,7 +99,7 @@
9999
:size="size"
100100
round
101101
:disabled="selectedNodes.some(([n]) => n)"
102-
@click="updateNodeAndRefresh(node)"
102+
@click="() => updateNodeAndRefresh(node)"
103103
>
104104
<IconFa name="pencil" />
105105
</ActionButton>
@@ -119,7 +119,7 @@
119119
:size="size"
120120
:disabled="selectedNodes.some(([n]) => n)"
121121
toggle="dropdown"
122-
@click="setModel()"
122+
@click="() => setModel()"
123123
>
124124
<IconFa name="ellipsis-vertical" />
125125
</ActionLink>
@@ -131,7 +131,7 @@
131131
:theme="invertedTheme"
132132
:size="size"
133133
:aria-label="t('table_duplicate')"
134-
@click="cloneNodeAndRefresh(node, setModel)"
134+
@click="() => cloneNodeAndRefresh(node, setModel)"
135135
>
136136
<IconFa name="clone" />
137137
<span>
@@ -145,7 +145,12 @@
145145
:size="size"
146146
:aria-label="t('table_delete')"
147147
@click="
148-
deleteNodeAndRefresh(node, setModel, dropdownRef)
148+
() =>
149+
deleteNodeAndRefresh(
150+
node,
151+
setModel,
152+
dropdownRef
153+
)
149154
"
150155
>
151156
<IconFa name="trash-can" />
@@ -160,7 +165,7 @@
160165
cloneNodeAndRefresh,
161166
deleteNodeAndRefresh,
162167
deleteNodesAndRefresh,
163-
show: showChildren(nodeIndex, node),
168+
show: visibility[nodeIndex].show,
164169
}"
165170
></slot>
166171
</ul>
@@ -173,7 +178,7 @@
173178
<tr class="no--hover --width-100">
174179
<td :colspan="propertiesMeta.length + 2">
175180
<div
176-
v-show="showChildren(nodeIndex, node)"
181+
v-show="visibility[nodeIndex].show"
177182
class="box --button --bdr-solid --bgColor-none"
178183
>
179184
<slot
@@ -183,7 +188,7 @@
183188
cloneNodeAndRefresh,
184189
deleteNodeAndRefresh,
185190
deleteNodesAndRefresh,
186-
show: showChildren(nodeIndex, node),
191+
show: visibility[nodeIndex].show,
187192
}"
188193
></slot>
189194
</div>
@@ -195,10 +200,10 @@
195200
<ActionLink
196201
:theme="theme || themeValues"
197202
:size="size"
198-
:active="showChildren(nodeIndex, node)"
203+
:active="visibility[nodeIndex].show"
199204
:tooltip="
200205
t(
201-
showChildren(nodeIndex, node)
206+
visibility[nodeIndex].show
202207
? 'table_hide_name'
203208
: 'table_see_name',
204209
{
@@ -210,20 +215,23 @@
210215
)
211216
"
212217
tooltip-position="right"
213-
:disabled="!childrenCount(node) || showNodeChildren?.(node)"
218+
:disabled="
219+
!visibility[nodeIndex].childrenCount ||
220+
visibility[nodeIndex].showNodeChildren
221+
"
214222
class="--p-5"
215-
@click="toggleChildren(nodeIndex)"
223+
@click="() => toggleChildren(nodeIndex)"
216224
>
217-
<span v-if="childrenCount(node) >= 1">
218-
{{ childrenCount(node) }}
225+
<span v-if="visibility[nodeIndex].childrenCount >= 1">
226+
{{ visibility[nodeIndex].childrenCount }}
219227
</span>
220228
<IconFa name="chevron-down" indicator />
221229
</ActionLink>
222230
<ActionButtonLink
223231
v-if="createNodeChildren"
224232
:theme="theme || themeValues"
225233
:size="size"
226-
:disabled="!!disableCreateNodeChildren?.(node)"
234+
:disabled="visibility[nodeIndex].disableCreateNodeChildren"
227235
:tooltip="
228236
t('table_create_new_name', {
229237
name:
@@ -236,7 +244,7 @@
236244
class="--p-5:md-inv"
237245
link-button
238246
round
239-
@click="createNodeChildren(node)"
247+
@click="() => createNodeChildren?.(node)"
240248
>
241249
<IconFa name="plus" />
242250
</ActionButtonLink>
@@ -254,6 +262,8 @@
254262
</template>
255263

256264
<script setup lang="ts" generic="T extends Record<string, any>">
265+
import { computed } from "vue";
266+
257267
import { useI18n } from "@open-xamu-co/ui-common-helpers";
258268
259269
import IconFa from "../icon/Fa.vue";
@@ -270,6 +280,13 @@
270280
271281
export interface iTableBodyProps<Ti extends Record<string, any>> extends iTableChildProps<Ti> {}
272282
283+
interface INodeVisibility {
284+
disableCreateNodeChildren?: boolean;
285+
showNodeChildren?: boolean;
286+
childrenCount: number;
287+
show: boolean;
288+
}
289+
273290
/**
274291
* Table body
275292
*
@@ -283,10 +300,27 @@
283300
const { t } = useHelpers(useI18n);
284301
const { themeValues, dangerThemeValues } = useTheme(props);
285302
286-
/** Can the node be shown? */
287-
function showChildren(nodeIndex: number, node: T) {
288-
const shouldShow = props.selectedNodes[nodeIndex][1] && !!props.childrenCount(node);
303+
/**
304+
* Cached node visibility
305+
*/
306+
const visibility = computed(() => {
307+
return props.nodes.reduce(
308+
(acc, node, nodeIndex) => {
309+
const disableCreateNodeChildren = props.disableCreateNodeChildren?.(node);
310+
const showNodeChildren = props.showNodeChildren?.(node);
311+
const childrenCount = props.childrenCount(node);
312+
const shouldShow = props.selectedNodes[nodeIndex][1] && !!childrenCount;
289313
290-
return props.showNodeChildren?.(node) ?? shouldShow;
291-
}
314+
acc[nodeIndex] = {
315+
disableCreateNodeChildren,
316+
showNodeChildren,
317+
childrenCount,
318+
show: showNodeChildren ?? shouldShow,
319+
};
320+
321+
return acc;
322+
},
323+
{} as Record<number, INodeVisibility>
324+
);
325+
});
292326
</script>

packages/components-vue/src/components/table/Head.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626
:active="openNodesCount === selectedNodes.length"
2727
round=":sm-inv"
2828
@click="
29-
toggleAll(!(openNodesCount === selectedNodes.length), 1)
29+
() =>
30+
toggleAll(
31+
!(openNodesCount === selectedNodes.length),
32+
1
33+
)
3034
"
3135
>
3236
<span class="--hidden-full:sm-inv">
@@ -115,7 +119,7 @@
115119
tooltip-as-text
116120
tooltip-position="bottom"
117121
:size="size"
118-
@click="setOrdering('id')"
122+
@click="() => setOrdering('id')"
119123
>
120124
<span>#</span>
121125
<template v-if="!!ordering['id']">
@@ -148,7 +152,7 @@
148152
tooltip-as-text
149153
tooltip-position="bottom"
150154
:size="size"
151-
@click="setOrdering(meta.value)"
155+
@click="() => setOrdering(meta.value)"
152156
>
153157
<span>{{ meta.alias }}</span>
154158
<template v-if="!!ordering[meta.value]">

0 commit comments

Comments
 (0)