Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`compiler: element transform > checkbox with static indeterminate 1`] = `
"import { setProp as _setProp, template as _template } from 'vue';
const t0 = _template("<input type=\\"checkbox\\">", true)

export function render(_ctx) {
const n0 = t0()
_setProp(n0, "indeterminate", "")
return n0
}"
`;

exports[`compiler: element transform > component > cache v-on expression with unique handler name 1`] = `
"import { resolveComponent as _resolveComponent, createComponentWithFallback as _createComponentWithFallback } from 'vue';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,15 @@ describe('compiler: element transform', () => {
expect(ir.block.effect).lengthOf(0)
})

test('checkbox with static indeterminate', () => {
const { code } = compileWithElementTransform(
`<input type="checkbox" indeterminate/>`,
)

expect(code).toContain('_setProp(n0, "indeterminate", "")')
expect(code).toMatchSnapshot()
})

test('props + children', () => {
const { code, ir } = compileWithElementTransform(
`<div id="foo"><span/></div>`,
Expand Down
10 changes: 9 additions & 1 deletion packages/compiler-vapor/src/transforms/transformElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ function resolveSetupReference(name: string, context: TransformContext) {
: undefined
}

// keys cannot be a part of the template and need to be set dynamically
const dynamicKeys = ['indeterminate']

function transformNativeElement(
node: PlainElementNode,
propsResult: PropsResult,
Expand Down Expand Up @@ -223,7 +226,12 @@ function transformNativeElement(
} else {
for (const prop of propsResult[1]) {
const { key, values } = prop
if (key.isStatic && values.length === 1 && values[0].isStatic) {
if (
key.isStatic &&
values.length === 1 &&
values[0].isStatic &&
!dynamicKeys.includes(key.content)
) {
template += ` ${key.content}`
if (values[0].content) template += `="${values[0].content}"`
} else {
Expand Down
11 changes: 11 additions & 0 deletions packages/runtime-vapor/__tests__/dom/prop.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,17 @@ describe('patchProp', () => {
`Failed setting prop "someProp" on <div>: value foo is invalid.`,
).toHaveBeenWarnedLast()
})

test('checkbox with indeterminate', () => {
const el = document.createElement('input')
el.type = 'checkbox'
setProp(el, 'indeterminate', true)
expect(el.indeterminate).toBe(true)
setProp(el, 'indeterminate', false)
expect(el.indeterminate).toBe(false)
setProp(el, 'indeterminate', '')
expect(el.indeterminate).toBe(true)
})
})

describe('setDynamicProp', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/runtime-vapor/src/dom/prop.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
type NormalizedStyle,
canSetValueDirectly,
includeBooleanAttr,
isOn,
isString,
normalizeClass,
Expand Down Expand Up @@ -81,7 +82,9 @@ export function setDOMProp(el: any, key: string, value: any): void {
let needRemove = false
if (value === '' || value == null) {
const type = typeof prev
if (value == null && type === 'string') {
if (type === 'boolean') {
value = includeBooleanAttr(value)
} else if (value == null && type === 'string') {
// e.g. <div :id="null">
value = ''
needRemove = true
Expand Down
Loading