Skip to content

Commit

Permalink
fix: Don't use child comp slot if isn't used
Browse files Browse the repository at this point in the history
related: #180 #181
  • Loading branch information
jd-solanki committed Jun 7, 2023
1 parent 9f88f18 commit 7be591f
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 20 deletions.
3 changes: 2 additions & 1 deletion packages/anu-vue/src/components/card/ACard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { isTypographyUsed } from '@/components/typography/utils'
import { type ConfigurableValue, useConfigurable } from '@/composables/useConfigurable'
import { useDefaults } from '@/composables/useDefaults'
import { useLayer } from '@/composables/useLayer'
import { filterUsedSlots } from '@/utils/vue'
// SECTION Meta
const _props = defineProps(aCardProps)
Expand Down Expand Up @@ -77,7 +78,7 @@ else
>
<!-- ℹ️ Recursively pass down slots to child -->
<template
v-for="(_, name) in aCardTypographySlots"
v-for="name in filterUsedSlots(aCardTypographySlots)"
#[name]="slotProps"
>
<slot
Expand Down
4 changes: 3 additions & 1 deletion packages/anu-vue/src/components/data-table/ADataTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useSearch } from '@/composables/useSearch'
import type { typeSortBy } from '@/composables/useSort'
import { useSort } from '@/composables/useSort'
import { objectKeys } from '@/utils/typescripts'
import { filterUsedSlots } from '@/utils/vue'
// TODO: Check usage with useDebounceFn. Can we limit the # of req to server?
Expand Down Expand Up @@ -372,8 +373,9 @@ const paginationMeta = computed(() => {

<!-- TODO: If you are using child component props in component => Filter them out -->
<!-- ℹ️ Recursively pass down slots to child -->
<!-- TODO: (types) Don't use type assertion -->
<template
v-for="(_, name) in _aDataTableTableSlots"
v-for="name in filterUsedSlots(_aDataTableTableSlots as any)"
#[name]="slotProps"
>
<slot
Expand Down
3 changes: 2 additions & 1 deletion packages/anu-vue/src/components/dialog/ADialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ACard } from '@/components/card'
import { useDOMScrollLock } from '@/composables/useDOMScrollLock'
import { useDefaults } from '@/composables/useDefaults'
import { useTeleport } from '@/composables/useTeleport'
import { filterUsedSlots } from '@/utils/vue'
// SECTION Meta
const _props = defineProps(aDialogProps)
Expand Down Expand Up @@ -56,7 +57,7 @@ useDOMScrollLock(toRef(props, 'modelValue') as Ref<boolean>)
>
<!-- ℹ️ Recursively pass down slots to child -->
<template
v-for="(_, name) in aDialogSlots"
v-for="name in filterUsedSlots(aDialogSlots)"
#[name]="slotProps"
>
<slot
Expand Down
3 changes: 2 additions & 1 deletion packages/anu-vue/src/components/drawer/ADrawer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ACard } from '@/components/card'
import { useDOMScrollLock } from '@/composables/useDOMScrollLock'
import { useDefaults } from '@/composables/useDefaults'
import { useTeleport } from '@/composables/useTeleport'
import { filterUsedSlots } from '@/utils/vue'
// SECTION Meta
const _props = defineProps(aDrawerProps)
Expand Down Expand Up @@ -92,7 +93,7 @@ useDOMScrollLock(toRef(props, 'modelValue') as Ref<boolean>)
>
<!-- ℹ️ Recursively pass down slots to child -->
<template
v-for="(_, name) in aDrawerSlots"
v-for="name in filterUsedSlots(aDrawerSlots)"
#[name]="slotProps"
>
<slot
Expand Down
3 changes: 2 additions & 1 deletion packages/anu-vue/src/components/input/AInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { AInputEvents } from './meta'
import { aInputProps, aTextareaBaseInputSlots } from './meta'
import { ABaseInput, aBaseInputProps } from '@/components/base-input'
import { useDefaults } from '@/composables/useDefaults'
import { filterUsedSlots } from '@/utils/vue'
// SECTION Meta
const _props = defineProps(aInputProps)
Expand Down Expand Up @@ -39,7 +40,7 @@ function handleInputWrapperClick() {
>
<!-- ℹ️ Recursively pass down slots to child -->
<template
v-for="(_, name) in aTextareaBaseInputSlots"
v-for="name in filterUsedSlots(aTextareaBaseInputSlots)"
#[name]="slotProps"
>
<slot
Expand Down
3 changes: 2 additions & 1 deletion packages/anu-vue/src/components/list/AList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { aListListItemSlotsWithPrefixMeta, aListProps } from './meta'
import { AListItem } from '@/components/list-item'
import { useDefaults } from '@/composables/useDefaults'
import { calculateSelectionItems, extractItemValueFromItemOption, useSelection } from '@/composables/useSelection'
import { filterUsedRenamedSlots } from '@/utils/vue'
// SECTION Meta
const _props = defineProps(aListProps)
Expand Down Expand Up @@ -73,7 +74,7 @@ function handleListItemClick(item: AListPropItems[number]) {
>
<!-- ℹ️ Recursively pass down slots to child -->
<template
v-for="{ originalKey: originalSlotName, prefixedKey: updatedSlotName } in aListListItemSlotsWithPrefixMeta"
v-for="{ originalKey: originalSlotName, prefixedKey: updatedSlotName } in filterUsedRenamedSlots(aListListItemSlotsWithPrefixMeta)"
#[originalSlotName]="slotProps"
>
<slot
Expand Down
3 changes: 2 additions & 1 deletion packages/anu-vue/src/components/loader/ALoader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { isTypographyUsed } from '@/components/typography/utils'
import { type ConfigurableValue, useConfigurable } from '@/composables/useConfigurable'
import { useDOMScrollLock } from '@/composables/useDOMScrollLock'
import { useDefaults } from '@/composables/useDefaults'
import { filterUsedSlots } from '@/utils/vue'
// SECTION Meta
const _props = defineProps(aLoaderProps)
Expand Down Expand Up @@ -74,7 +75,7 @@ if (props.fullPage) {
>
<!-- ℹ️ Recursively pass down slots to child -->
<template
v-for="(_, name) in aLoaderTypographySlots"
v-for="name in filterUsedSlots(aLoaderTypographySlots)"
#[name]="slotProps"
>
<slot
Expand Down
6 changes: 3 additions & 3 deletions packages/anu-vue/src/components/select/ASelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { AFloating, sameWidthFloatingUIMiddleware } from '@/components/floating'
import type { AListPropItems } from '@/components/list'
import { useDefaults } from '@/composables/useDefaults'
import { extractItemValueFromItemOption } from '@/composables/useSelection'
import { filterUsedSlots } from '@/utils/reactivity'
import { filterUsedRenamedSlots, filterUsedSlots } from '@/utils/vue'
// SECTION Meta
export interface ObjectOption { label: string; value: string | number }
Expand Down Expand Up @@ -102,7 +102,7 @@ function middleware() {
>
<!-- ℹ️ Recursively pass down slots to child -->
<template
v-for="(_, name) in aSelectBaseInputSlots"
v-for="name in filterUsedSlots(aSelectBaseInputSlots)"
#[name]="slotProps"
>
<slot
Expand Down Expand Up @@ -154,7 +154,7 @@ function middleware() {
>
<!-- ℹ️ Recursively pass down slots to child -->
<template
v-for="{ originalKey: originalSlotName, prefixedKey: updatedSlotName } in aSelectListSlotsWithPrefixMeta"
v-for="{ originalKey: originalSlotName, prefixedKey: updatedSlotName } in filterUsedRenamedSlots(aSelectListSlotsWithPrefixMeta)"
#[originalSlotName]="slotProps"
>
<slot
Expand Down
3 changes: 2 additions & 1 deletion packages/anu-vue/src/components/table/ATable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { ACardProps } from '@/components/card'
import { aCardProps } from '@/components/card'
import { useDefaults } from '@/composables/useDefaults'
import { objectKeys } from '@/utils/typescripts'
import { filterUsedSlots } from '@/utils/vue'
// SECTION Meta
const _props = defineProps(aTableProps<Row>())
Expand Down Expand Up @@ -121,7 +122,7 @@ const _cols = computed<ATablePropColumn<Row>[]>(() => {

<!-- ℹ️ Recursively pass down slots to child -->
<template
v-for="(_, name) in aTableCardSlots"
v-for="name in filterUsedSlots(aTableCardSlots)"
#[name]="slotProps"
>
<slot
Expand Down
3 changes: 2 additions & 1 deletion packages/anu-vue/src/components/textarea/ATextarea.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ATextareaEvents, aTextareaSlots } from './meta'
import { aTextareaBaseInputSlots, aTextareaProps } from './meta'
import { ABaseInput, aBaseInputProps } from '@/components/base-input'
import { useDefaults } from '@/composables/useDefaults'
import { filterUsedSlots } from '@/utils/vue'
// SECTION Meta
const _props = defineProps(aTextareaProps)
Expand Down Expand Up @@ -55,7 +56,7 @@ if (props.autoSize) {
>
<!-- ℹ️ Recursively pass down slots to child -->
<template
v-for="(_, name) in aTextareaBaseInputSlots"
v-for="name in filterUsedSlots(aTextareaBaseInputSlots)"
#[name]="slotProps"
>
<slot
Expand Down
8 changes: 0 additions & 8 deletions packages/anu-vue/src/utils/reactivity.ts

This file was deleted.

15 changes: 15 additions & 0 deletions packages/anu-vue/src/utils/vue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { objectKeys, objectMap } from '@antfu/utils'
import type { prefixObjectKeysWithMeta } from '@/utils/helpers'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const filterUsedSlots = computed(() => <const T extends Record<string, any>>(slotsToUse: T): (keyof T)[] => {
const slots = useSlots()

return objectKeys(slotsToUse).filter(key => slots[key as keyof typeof slots]) as (keyof T)[]
})

export const filterUsedRenamedSlots = computed(() => <const T extends ReturnType<typeof prefixObjectKeysWithMeta>>(slotsToUse: T): T => {
const slots = useSlots()

return objectMap(slotsToUse, (originalSlotName, obj) => slots[obj.prefixedKey] ? [originalSlotName, obj] : undefined) as T
})

0 comments on commit 7be591f

Please sign in to comment.