Skip to content

Commit 14958c9

Browse files
committed
feat(design-config): design-config添加全局配置组件任意属性特性
1 parent 580b065 commit 14958c9

File tree

8 files changed

+76
-20
lines changed

8 files changed

+76
-20
lines changed

examples/sites/demos/pc/app/config-provider/base-composition-api.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ const design = {
5353
icons: {
5454
warning: iconWarningTriangle()
5555
},
56+
props: {
57+
center: true
58+
},
5659
/**
5760
*
5861
* @param {*} props 组件属性集合

examples/sites/demos/pc/app/config-provider/base.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ export default {
6060
}
6161
},
6262
Alert: {
63+
props: {
64+
center: true
65+
},
6366
icons: {
6467
warning: iconWarningTriangle()
6568
},

examples/sites/demos/pc/app/config-provider/basic.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ test('测试自定义事件', async ({ page }) => {
1010
await page.waitForTimeout(500)
1111
await expect(page.locator('.tiny-modal > .tiny-modal__box').nth(1)).toHaveText('触发自定方法')
1212

13+
// 验证文字居中
14+
await expect(demo.locator('.tiny-alert')).toHaveCSS('text-align', 'center')
15+
1316
// 验证必填星号
1417
await expect(demo.locator('.tiny-form')).toBeVisible()
1518
const beforeElement = await page.evaluate(() => {

packages/vue-common/src/adapter/vue2.7/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ export const renderComponent = ({
3838

3939
export const rootConfig = () => hooks.getCurrentInstance()?.proxy.$root
4040

41+
export const getCustomProps = () => {
42+
const instance = hooks.getCurrentInstance()?.proxy
43+
return instance?.$options?.propsData || {}
44+
}
45+
4146
export const getComponentName = () => {
4247
// 此处组件最多为两层组件,所以对多获取到父级组件即可
4348
const instance = hooks.getCurrentInstance()

packages/vue-common/src/adapter/vue2/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ export const renderComponent = ({
4949

5050
export const rootConfig = () => hooks.getCurrentInstance()?.proxy.$root
5151

52+
export const getCustomProps = () => {
53+
const instance = hooks.getCurrentInstance()?.proxy
54+
return instance?.$options?.propsData || {}
55+
}
56+
5257
export const getComponentName = () => {
5358
// 此处组件最多为两层组件,所以对多获取到父级组件即可
5459
const instance = hooks.getCurrentInstance()

packages/vue-common/src/adapter/vue3/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,16 @@ export const renderComponent = ({
2626
view = undefined as any,
2727
component = undefined as any,
2828
props,
29+
customDesignProps,
2930
context: { attrs, slots },
3031
extend = {}
3132
}) => {
32-
return () => hooks.h((view && view.value) || component, { ref: 'modeTemplate', ...props, ...attrs, ...extend }, slots)
33+
return () =>
34+
hooks.h(
35+
(view && view.value) || component,
36+
{ ref: 'modeTemplate', ...props, ...attrs, ...customDesignProps, ...extend },
37+
slots
38+
)
3339
}
3440

3541
export const rootConfig = (context) => {
@@ -38,6 +44,11 @@ export const rootConfig = (context) => {
3844
return instance?.appContext.config.globalProperties
3945
}
4046

47+
export const getCustomProps = () => {
48+
const instance = hooks.getCurrentInstance()
49+
return instance?.vnode?.props || {}
50+
}
51+
4152
export const getComponentName = () => {
4253
// 此处组件最多为两层组件,所以对多获取到父级组件即可
4354
const instance = hooks.getCurrentInstance()

packages/vue-common/src/index.ts

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
tools,
1717
useRouter,
1818
getComponentName,
19+
getCustomProps,
1920
isVnode
2021
} from './adapter'
2122
import { t } from '@opentiny/vue-locale'
@@ -122,19 +123,6 @@ const resolveChartTheme = (props, context) => {
122123
return tinyChartTheme
123124
}
124125

125-
export const $setup = ({ props, context, template, extend = {} }) => {
126-
const mode = resolveMode(props, context)
127-
const view = hooks.computed(() => {
128-
if (typeof props.tiny_template !== 'undefined') return props.tiny_template
129-
130-
const component = template(mode, props)
131-
132-
return typeof component === 'function' ? defineAsyncComponent(component) : component
133-
})
134-
135-
return renderComponent({ view, props, context, extend })
136-
}
137-
138126
// 提供给没有renderless层的组件使用(比如TinyVuePlus组件)
139127
export const design = {
140128
configKey: Symbol('designConfigKey'),
@@ -168,17 +156,55 @@ export const customDesignConfig: CustomDesignConfig = {
168156
twMerge: () => ''
169157
}
170158

171-
export const mergeClass = (...cssClasses) => customDesignConfig.twMerge(stringifyCssClass(cssClasses))
172-
173-
export const setup = ({ props, context, renderless, api, extendOptions = {}, mono = false, classes = {} }) => {
174-
const render = typeof props.tiny_renderless === 'function' ? props.tiny_renderless : renderless
175-
159+
const getDesignConfig = () => {
176160
// 获取组件级配置和全局配置(inject需要带有默认值,否则控制台会报警告)
177161
let globalDesignConfig: DesignConfig = customDesignConfig.designConfig || hooks.inject(design.configKey, {})
162+
178163
// globalDesignConfig 可能是响应式对象,比如 computed
179164
globalDesignConfig = globalDesignConfig?.value || globalDesignConfig || {}
180165
const designConfig = globalDesignConfig?.components?.[getComponentName().replace($prefix, '')]
166+
return {
167+
designConfig,
168+
globalDesignConfig
169+
}
170+
}
171+
172+
export const $setup = ({ props: propData, context, template, extend = {} }) => {
173+
const mode = resolveMode(propData, context)
174+
const view = hooks.computed(() => {
175+
if (typeof propData.tiny_template !== 'undefined') return propData.tiny_template
176+
177+
const component = template(mode, propData)
178+
179+
return typeof component === 'function' ? defineAsyncComponent(component) : component
180+
})
181+
182+
const { designConfig } = getDesignConfig()
183+
const customDesignProps = {}
184+
185+
const designProps = designConfig?.props
186+
187+
if (designProps) {
188+
// 获取用户传递的props
189+
const customProps = getCustomProps()
190+
191+
Object.keys(designProps).forEach((key) => {
192+
// 用户没有配置的属性才进行覆盖
193+
if (!Object.prototype.hasOwnProperty.call(customProps, key)) {
194+
customDesignProps[key] = designProps[key]
195+
}
196+
})
197+
}
198+
199+
return renderComponent({ view, props: propData, customDesignProps, context, extend })
200+
}
201+
202+
export const mergeClass = (...cssClasses) => customDesignConfig.twMerge(stringifyCssClass(cssClasses))
203+
204+
export const setup = ({ props, context, renderless, api, extendOptions = {}, mono = false, classes = {} }) => {
205+
const render = typeof props.tiny_renderless === 'function' ? props.tiny_renderless : renderless
181206

207+
const { designConfig, globalDesignConfig } = getDesignConfig()
182208
const utils = {
183209
$prefix,
184210
t,

packages/vue/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,4 @@
237237
"build": "pnpm -w build:ui",
238238
"postversion": "pnpm build"
239239
}
240-
}
240+
}

0 commit comments

Comments
 (0)