Skip to content

Commit

Permalink
feat(client): support slot.disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 13, 2023
1 parent 392eb6b commit 9693dac
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
3 changes: 3 additions & 0 deletions packages/client/client/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export namespace Activity {
authority?: number
position?: 'top' | 'bottom'
fields?: Field[]
/** @deprecated */
when?: () => boolean
disabled?: () => boolean
}
}

Expand Down Expand Up @@ -71,6 +73,7 @@ export class Activity {
if (root.bail('activity', this)) return
if (!this.fields.every(key => store[key])) return
if (this.when && !this.when()) return
if (this.disabled?.()) return
return this.options.position ?? 'top'
}

Expand Down
4 changes: 3 additions & 1 deletion packages/client/client/components/slot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export interface SlotItem {

export interface SlotOptions extends SlotItem {
type: string
/** @deprecated */
when?: () => boolean
disabled?: () => boolean
}

export const KSlot = defineComponent({
Expand All @@ -24,7 +26,7 @@ export const KSlot = defineComponent({
.filter(node => node.type === KSlotItem)
.map(node => ({ node, order: node.props?.order || 0 }))
const external = [...ctx.internal.views[props.name] || []]
.filter(item => !item.when || item.when())
.filter(item => !item.disabled?.())
.map(item => ({
node: h(item.component, { data: props.data, ...props.data }, slots),
order: item.order,
Expand Down
6 changes: 3 additions & 3 deletions packages/client/client/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ export interface MenuItem {
order?: number
}

interface SettingOptions {
interface SettingOptions extends Ordered {
id: string
title?: string
order?: number
disabled?: () => boolean
schema?: Schema
component?: Component
Expand Down Expand Up @@ -172,6 +171,7 @@ export class Context extends cordis.Context {
slot(options: SlotOptions) {
options.order ??= 0
options.component = this.wrapComponent(options.component)
if (options.when) options.disabled = () => !options.when()
const list = this.internal.views[options.type] ||= []
insert(list, options)
return this.scope.collect('view', () => remove(list, options))
Expand Down Expand Up @@ -231,7 +231,7 @@ export class Context extends cordis.Context {
for (const [type, component] of Object.entries(options.components || {})) {
this.slot({
type,
when: () => config.value.theme[mode.value] === options.id,
disabled: () => config.value.theme[mode.value] !== options.id,
component,
})
}
Expand Down
6 changes: 5 additions & 1 deletion packages/client/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { config } from './config'
import { initTask } from './loader'
import { Context } from './context'
import { createI18n } from 'vue-i18n'
import { watchEffect } from 'vue'

export * from './activity'
export * from './components'
Expand Down Expand Up @@ -42,10 +43,13 @@ export const router = createRouter({

export const i18n = createI18n({
legacy: false,
locale: config.value.locale,
fallbackLocale: 'zh-CN',
})

watchEffect(() => {
i18n.global.locale.value = config.value.locale
})

root.app.use(install)
root.app.use(i18n)
root.app.use(router)
Expand Down

0 comments on commit 9693dac

Please sign in to comment.