Skip to content

Commit

Permalink
feat(shared): 支持为第三方自定义组件的属性设置默认值,fix #11575
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen-jj committed May 21, 2022
1 parent 226d44a commit bd849cb
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions packages/shared/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import { Shortcuts } from './shortcuts'
import { isBooleanStringLiteral, isNumber, isFunction } from './is'
import { toCamelCase, toKebabCase, toDashed, hasOwn, indent, capitalize } from './utils'
import { isString } from 'lodash'

interface Component {
nodeName: string;
Expand Down Expand Up @@ -79,6 +80,7 @@ export class BaseTemplate {
protected isSupportRecursive: boolean
protected supportXS = false
protected miniComponents: Components
protected thirdPartyPatcher: Record<string, Record<string, string>> = {}
protected modifyCompProps?: (compName: string, target: Record<string, string>) => Record<string, string>
protected modifyLoopBody?: (child: string, nodeName: string) => string
protected modifyLoopContainer?: (children: string, nodeName: string) => string
Expand Down Expand Up @@ -206,7 +208,7 @@ export class BaseTemplate {
`
}

protected buildThirdPartyAttr (attrs: Set<string>) {
protected buildThirdPartyAttr (attrs: Set<string>, patcher: Record<string, string> = {}) {
return Array.from(attrs).reduce((str, attr) => {
if (attr.startsWith('@')) {
// vue2
Expand All @@ -231,6 +233,13 @@ export class BaseTemplate {
return str + `style="{{i.${Shortcuts.Style}}}" `
}

const patchValue = patcher[attr]
if (isBooleanStringLiteral(patchValue) || isNumber(patchValue) || isString(patchValue)) {
const propValue = this.supportXS
? `xs.b(i.${toCamelCase(attr)},${patchValue})`
: `i.${toCamelCase(attr)}===undefined?${patchValue}:i.${toCamelCase(attr)}`
return str + `${attr}="{{${propValue}}}" `
}
return str + `${attr}="{{i.${toCamelCase(attr)}}}" `
}, '')
}
Expand Down Expand Up @@ -375,7 +384,7 @@ export class BaseTemplate {

template += `
<template name="tmpl_${level}_${compName}">
<${compName} ${this.buildThirdPartyAttr(attrs)} id="{{i.uid||i.sid}}" data-sid="{{i.sid}}">
<${compName} ${this.buildThirdPartyAttr(attrs, this.thirdPartyPatcher[compName] || {})} id="{{i.uid||i.sid}}" data-sid="{{i.sid}}">
<block ${Adapter.for}="{{i.${Shortcuts.Childnodes}}}" ${Adapter.key}="sid">
${child}
</block>
Expand Down Expand Up @@ -481,6 +490,10 @@ export class BaseTemplate {
ctx.helper.recursiveMerge(this.internalComponents, patch)
}

public mergeThirdPartyComponents (patch: Record<string, Record<string, string>>) {
this.thirdPartyPatcher = patch
}

protected buildXSTmplName () {
return `function (l, n) {
return 'tmpl_' + l + '_' + n
Expand Down

0 comments on commit bd849cb

Please sign in to comment.