-
Notifications
You must be signed in to change notification settings - Fork 303
feat(design-config): design-config adds arbitrary attribute features of global configuration components #3419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
14958c9
92c2625
22aeaf0
6af4491
3e400b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,9 @@ export default { | |
} | ||
}, | ||
Alert: { | ||
props: { | ||
center: true | ||
}, | ||
icons: { | ||
warning: iconWarningTriangle() | ||
}, | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -26,10 +26,16 @@ export const renderComponent = ({ | |||||||||||||||||
view = undefined as any, | ||||||||||||||||||
component = undefined as any, | ||||||||||||||||||
props, | ||||||||||||||||||
customDesignProps, | ||||||||||||||||||
context: { attrs, slots }, | ||||||||||||||||||
extend = {} | ||||||||||||||||||
}) => { | ||||||||||||||||||
return () => hooks.h((view && view.value) || component, { ref: 'modeTemplate', ...props, ...attrs, ...extend }, slots) | ||||||||||||||||||
return () => | ||||||||||||||||||
hooks.h( | ||||||||||||||||||
(view && view.value) || component, | ||||||||||||||||||
{ ref: 'modeTemplate', ...props, ...attrs, ...customDesignProps, ...extend }, | ||||||||||||||||||
slots | ||||||||||||||||||
) | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
export const rootConfig = (context) => { | ||||||||||||||||||
|
@@ -38,6 +44,11 @@ export const rootConfig = (context) => { | |||||||||||||||||
return instance?.appContext.config.globalProperties | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
export const getCustomProps = () => { | ||||||||||||||||||
const instance = hooks.getCurrentInstance() | ||||||||||||||||||
return instance?.vnode?.props || {} | ||||||||||||||||||
} | ||||||||||||||||||
Comment on lines
+47
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Prefer
-export const getCustomProps = () => {
- const instance = hooks.getCurrentInstance()
- return instance?.vnode?.props || {}
-}
+export const getCustomProps = () => {
+ const instance = hooks.getCurrentInstance()
+ return instance?.props || {}
+} This avoids false negatives when a user passes 📝 Committable suggestion
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
export const getComponentName = () => { | ||||||||||||||||||
// 此处组件最多为两层组件,所以对多获取到父级组件即可 | ||||||||||||||||||
const instance = hooks.getCurrentInstance() | ||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ import { | |
tools, | ||
useRouter, | ||
getComponentName, | ||
getCustomProps, | ||
isVnode | ||
} from './adapter' | ||
import { t } from '@opentiny/vue-locale' | ||
|
@@ -122,19 +123,6 @@ const resolveChartTheme = (props, context) => { | |
return tinyChartTheme | ||
} | ||
|
||
export const $setup = ({ props, context, template, extend = {} }) => { | ||
const mode = resolveMode(props, context) | ||
const view = hooks.computed(() => { | ||
if (typeof props.tiny_template !== 'undefined') return props.tiny_template | ||
|
||
const component = template(mode, props) | ||
|
||
return typeof component === 'function' ? defineAsyncComponent(component) : component | ||
}) | ||
|
||
return renderComponent({ view, props, context, extend }) | ||
} | ||
|
||
// 提供给没有renderless层的组件使用(比如TinyVuePlus组件) | ||
export const design = { | ||
configKey: Symbol('designConfigKey'), | ||
|
@@ -168,17 +156,55 @@ export const customDesignConfig: CustomDesignConfig = { | |
twMerge: () => '' | ||
} | ||
|
||
export const mergeClass = (...cssClasses) => customDesignConfig.twMerge(stringifyCssClass(cssClasses)) | ||
|
||
export const setup = ({ props, context, renderless, api, extendOptions = {}, mono = false, classes = {} }) => { | ||
const render = typeof props.tiny_renderless === 'function' ? props.tiny_renderless : renderless | ||
|
||
const getDesignConfig = () => { | ||
// 获取组件级配置和全局配置(inject需要带有默认值,否则控制台会报警告) | ||
let globalDesignConfig: DesignConfig = customDesignConfig.designConfig || hooks.inject(design.configKey, {}) | ||
|
||
// globalDesignConfig 可能是响应式对象,比如 computed | ||
globalDesignConfig = globalDesignConfig?.value || globalDesignConfig || {} | ||
const designConfig = globalDesignConfig?.components?.[getComponentName().replace($prefix, '')] | ||
return { | ||
designConfig, | ||
globalDesignConfig | ||
} | ||
} | ||
|
||
export const $setup = ({ props: propData, context, template, extend = {} }) => { | ||
const mode = resolveMode(propData, context) | ||
const view = hooks.computed(() => { | ||
if (typeof propData.tiny_template !== 'undefined') return propData.tiny_template | ||
|
||
const component = template(mode, propData) | ||
|
||
return typeof component === 'function' ? defineAsyncComponent(component) : component | ||
}) | ||
|
||
const { designConfig } = getDesignConfig() | ||
const customDesignProps = {} | ||
|
||
const designProps = designConfig?.props | ||
|
||
if (designProps) { | ||
// 获取用户传递的props | ||
const customProps = getCustomProps() | ||
|
||
Object.keys(designProps).forEach((key) => { | ||
// 用户没有配置的属性才进行覆盖 | ||
if (!Object.prototype.hasOwnProperty.call(customProps, key)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that |
||
customDesignProps[key] = designProps[key] | ||
} | ||
}) | ||
} | ||
|
||
return renderComponent({ view, props: propData, customDesignProps, context, extend }) | ||
} | ||
|
||
export const mergeClass = (...cssClasses) => customDesignConfig.twMerge(stringifyCssClass(cssClasses)) | ||
|
||
export const setup = ({ props, context, renderless, api, extendOptions = {}, mono = false, classes = {} }) => { | ||
const render = typeof props.tiny_renderless === 'function' ? props.tiny_renderless : renderless | ||
|
||
const { designConfig, globalDesignConfig } = getDesignConfig() | ||
const utils = { | ||
$prefix, | ||
t, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -237,4 +237,4 @@ | |
"build": "pnpm -w build:ui", | ||
"postversion": "pnpm build" | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getCustomProps
added, but the surrounding adapter is still oblivious to the newcustomDesignProps
flowgetCustomProps
itself looks fine, yet the Vue 2.7 adapter never forwards the mergedcustomDesignProps
object that$setup
now passes in.As a result, any default props coming from the global design-config are silently ignored in a Vue 2.7 runtime, breaking the very feature this PR introduces (e.g. the new
Alert.center
default will not work).Minimal patch (works for both Vue 2 and Vue 2.7 adapters):
Key points
{}
to stay backwards-compatible with callers that do not yet supply the parameter.Please mirror the same change in
packages/vue-common/src/adapter/vue2/index.ts
to keep all Vue-2 builds consistent.📝 Committable suggestion