Skip to content
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

fix(runtime): 组件属性设置false在小程序环境不生效 #5990

Merged
merged 3 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/shared/src/components.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Shortcuts } from './shortcuts'
import { toDashed, hasOwn, toCamelCase } from './utils'
import { isBooleanStringLiteral } from './is'

const styles = {
style: `i.${Shortcuts.Style}`,
Expand Down Expand Up @@ -641,7 +642,7 @@ export function createMiniComponents (components: Components, buildType: string)
propValue = 'eh'
} else if (propValue === '') {
propValue = `i.${toCamelCase(prop)}`
} else if (propValue === 'true' || propValue === 'false') {
} else if (isBooleanStringLiteral(propValue)) {
propValue = `i.${toCamelCase(prop)} === undefined ? ${propValue} : i.${toCamelCase(prop)}`
} else {
propValue = `i.${toCamelCase(prop)} || ${propValue || singleQuote('')}`
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/src/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ export function isNumber (o: unknown): o is number {
return typeof o === 'number'
}

export function isBooleanStringLiteral (o: unknown): o is string {
return o === 'true' || o === 'false'
}

export const isArray = Array.isArray
10 changes: 9 additions & 1 deletion packages/taro-runtime/src/dom/element.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-dupe-class-members */
import { isArray, isUndefined, Shortcuts, EMPTY_OBJ, warn, isString, toCamelCase } from '@tarojs/shared'
import { isArray, isUndefined, Shortcuts, EMPTY_OBJ, warn, isString, toCamelCase, internalComponents, capitalize, hasOwn, isBooleanStringLiteral } from '@tarojs/shared'
import { TaroNode } from './node'
import { NodeType } from './node_types'
import { TaroEvent, eventSource } from './event'
Expand Down Expand Up @@ -107,6 +107,14 @@ export class TaroElement extends TaroNode {
public removeAttribute (qualifiedName: string) {
if (qualifiedName === 'style') {
this.style.cssText = ''
} else if (process.env.FRAMEWORK === 'vue') {
const compName = capitalize(toCamelCase(this.tagName.toLowerCase()))
if (compName in internalComponents && hasOwn(internalComponents[compName], qualifiedName) && isBooleanStringLiteral(internalComponents[compName][qualifiedName])) {
// avoid attribute being removed because set false value in vue
this.setAttribute(qualifiedName, false)
} else {
delete this.props[qualifiedName]
}
} else {
delete this.props[qualifiedName]
}
Expand Down