Skip to content

Commit f54719e

Browse files
committed
fix(compiler-vapor): set static indeterminate as prop
1 parent 3b5e13c commit f54719e

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

packages/compiler-vapor/__tests__/transforms/__snapshots__/transformElement.spec.ts.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`compiler: element transform > checkbox with static indeterminate 1`] = `
4+
"import { setProp as _setProp, template as _template } from 'vue';
5+
const t0 = _template("<input type=\\"checkbox\\">", true)
6+
7+
export function render(_ctx) {
8+
const n0 = t0()
9+
_setProp(n0, "indeterminate", "")
10+
return n0
11+
}"
12+
`;
13+
314
exports[`compiler: element transform > component > cache v-on expression with unique handler name 1`] = `
415
"import { resolveComponent as _resolveComponent, createComponentWithFallback as _createComponentWithFallback } from 'vue';
516

packages/compiler-vapor/__tests__/transforms/transformElement.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,15 @@ describe('compiler: element transform', () => {
583583
expect(ir.block.effect).lengthOf(0)
584584
})
585585

586+
test('checkbox with static indeterminate', () => {
587+
const { code } = compileWithElementTransform(
588+
`<input type="checkbox" indeterminate/>`,
589+
)
590+
591+
expect(code).toContain('_setProp(n0, "indeterminate", "")')
592+
expect(code).toMatchSnapshot()
593+
})
594+
586595
test('props + children', () => {
587596
const { code, ir } = compileWithElementTransform(
588597
`<div id="foo"><span/></div>`,

packages/compiler-vapor/src/transforms/transformElement.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ function resolveSetupReference(name: string, context: TransformContext) {
192192
: undefined
193193
}
194194

195+
// keys cannot be a part of the template and needs to be set dynamically
196+
const dynamicKeys = ['indeterminate']
197+
195198
function transformNativeElement(
196199
node: PlainElementNode,
197200
propsResult: PropsResult,
@@ -223,7 +226,12 @@ function transformNativeElement(
223226
} else {
224227
for (const prop of propsResult[1]) {
225228
const { key, values } = prop
226-
if (key.isStatic && values.length === 1 && values[0].isStatic) {
229+
if (
230+
key.isStatic &&
231+
values.length === 1 &&
232+
values[0].isStatic &&
233+
!dynamicKeys.includes(key.content)
234+
) {
227235
template += ` ${key.content}`
228236
if (values[0].content) template += `="${values[0].content}"`
229237
} else {

packages/runtime-vapor/__tests__/dom/prop.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,17 @@ describe('patchProp', () => {
299299
`Failed setting prop "someProp" on <div>: value foo is invalid.`,
300300
).toHaveBeenWarnedLast()
301301
})
302+
303+
test('checkbox with indeterminate', () => {
304+
const el = document.createElement('input')
305+
el.type = 'checkbox'
306+
setProp(el, 'indeterminate', true)
307+
expect(el.indeterminate).toBe(true)
308+
setProp(el, 'indeterminate', false)
309+
expect(el.indeterminate).toBe(false)
310+
setProp(el, 'indeterminate', '')
311+
expect(el.indeterminate).toBe(true)
312+
})
302313
})
303314

304315
describe('setDynamicProp', () => {

packages/runtime-vapor/src/dom/prop.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
type NormalizedStyle,
33
canSetValueDirectly,
4+
includeBooleanAttr,
45
isOn,
56
isString,
67
normalizeClass,
@@ -81,7 +82,9 @@ export function setDOMProp(el: any, key: string, value: any): void {
8182
let needRemove = false
8283
if (value === '' || value == null) {
8384
const type = typeof prev
84-
if (value == null && type === 'string') {
85+
if (type === 'boolean') {
86+
value = includeBooleanAttr(value)
87+
} else if (value == null && type === 'string') {
8588
// e.g. <div :id="null">
8689
value = ''
8790
needRemove = true

0 commit comments

Comments
 (0)