Skip to content

Commit

Permalink
check if checkbox or radio
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Jun 27, 2021
1 parent 92dc06c commit 3ee017b
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 12 deletions.
12 changes: 5 additions & 7 deletions src/compiler/compile/render_ssr/handlers/Element.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { is_void } from '../../../utils/names';
import { get_attribute_value, get_class_attribute_value } from './shared/get_attribute_value';
import { get_attribute_expression, get_attribute_value, get_class_attribute_value } from './shared/get_attribute_value';
import { boolean_attributes } from './shared/boolean_attributes';
import Renderer, { RenderOptions } from '../Renderer';
import Element from '../../nodes/Element';
Expand Down Expand Up @@ -112,13 +112,11 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
if (name === 'group') {
const value_attribute = node.attributes.find(({ name }) => name === 'value');
if (value_attribute) {
const value = get_attribute_value(value_attribute);
const value = get_attribute_expression(value_attribute);
const type = node.get_static_attribute_value('type');
const bound = expression.node;
renderer.add_expression(x`
${value} === ${bound} || Array.isArray(${bound}) && ${bound}.includes(${value})
? @add_attribute("checked", true, 1)
: ""
`);
const condition = type === 'checkbox' ? x`~${bound}.indexOf(${value})`: x`${value} === ${bound}`;
renderer.add_expression(x`${condition} ? @add_attribute("checked", true, 1) : ""`);
}
} else if (contenteditable && (name === 'textContent' || name === 'innerHTML')) {
node_contents = expression.node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ export function get_attribute_value(attribute: Attribute): ESTreeExpression {
})
.reduce((lhs, rhs) => x`${lhs} + ${rhs}`);
}

export function get_attribute_expression(attribute: Attribute): ESTreeExpression {
if (attribute.chunks.length === 1 && attribute.chunks[0].type === 'Expression') {
return (attribute.chunks[0] as Expression).node as ESTreeExpression;
}
return get_attribute_value(attribute);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export default {
skip_if_ssr: true,

props: {
foo: false
Expand Down
10 changes: 9 additions & 1 deletion test/runtime/samples/binding-indirect-spread/_config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
export default {
skip_if_ssr: true,
ssrHtml: `
<input type="radio" value="radio1">
<input type="radio" value="radio2" checked>
<input type="radio" value="radio3">
<input type="checkbox" value="check1">
<input type="checkbox" value="check2" checked>
<input type="checkbox" value="check3">
`,
async test({ assert, component, target, window }) {
const event = new window.MouseEvent('click');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ export default {
<p>Beta</p>`,

ssrHtml: `
<label>
<input type="checkbox" value="[object Object]"> Alpha
</label>
<label>
<input type="checkbox" value="[object Object]" checked> Beta
</label>
<label>
<input type="checkbox" value="[object Object]"> Gamma
</label>
<p>Beta</p>`,

async test({ assert, component, target, window }) {
const inputs = target.querySelectorAll('input');
assert.equal(inputs[0].checked, false);
Expand Down
15 changes: 15 additions & 0 deletions test/runtime/samples/binding-input-checkbox-group/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ export default {
<p>Beta</p>`,

ssrHtml: `
<label>
<input type="checkbox" value="[object Object]"> Alpha
</label>
<label>
<input type="checkbox" value="[object Object]" checked> Beta
</label>
<label>
<input type="checkbox" value="[object Object]"> Gamma
</label>
<p>Beta</p>`,

async test({ assert, component, target, window }) {
const inputs = target.querySelectorAll('input');
assert.equal(inputs[0].checked, false);
Expand Down
48 changes: 48 additions & 0 deletions test/runtime/samples/binding-input-group-each-1/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,54 @@ export default {
</div>
`,

ssrHtml: `
<div>
<label>
<input type="checkbox" value="[object Object]"> Alpha
</label>
<label>
<input type="checkbox" value="[object Object]" checked> Beta
</label>
<label>
<input type="checkbox" value="[object Object]"> Gamma
</label>
<p>Beta</p>
</div>
<div>
<label>
<input type="checkbox" value="[object Object]"> Alpha
</label>
<label>
<input type="checkbox" value="[object Object]"> Beta
</label>
<label>
<input type="checkbox" value="[object Object]"> Gamma
</label>
<p></p>
</div>
<div>
<label>
<input type="checkbox" value="[object Object]"> Alpha
</label>
<label>
<input type="checkbox" value="[object Object]"> Beta
</label>
<label>
<input type="checkbox" value="[object Object]" checked> Gamma
</label>
<p>Gamma</p>
</div>
`,

async test({ assert, component, target, window }) {
const inputs = target.querySelectorAll('input');
assert.equal(inputs[0].checked, false);
Expand Down
13 changes: 13 additions & 0 deletions test/runtime/samples/binding-input-group-each-2/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ export default {
<p>1, 2, 3</p>`,

ssrHtml: `
<label>
<input type="checkbox" value="1" checked> 1
</label>
<label>
<input type="checkbox" value="2" checked> 2
</label>
<label>
<input type="checkbox" value="3" checked> 3
</label>
<p>1, 2, 3</p>`,

async test({ assert, component, target, window }) {
const inputs = target.querySelectorAll('input');
assert.equal(inputs[0].checked, true);
Expand Down
46 changes: 46 additions & 0 deletions test/runtime/samples/binding-input-group-each-3/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,53 @@ export default {
<p>Gamma</p>
</div>
`,
ssrHtml: `
<div>
<label>
<input type="checkbox" value="[object Object]"> Alpha
</label>
<label>
<input type="checkbox" value="[object Object]" checked> Beta
</label>
<label>
<input type="checkbox" value="[object Object]"> Gamma
</label>
<p>Beta</p>
</div>
<div>
<label>
<input type="checkbox" value="[object Object]"> Alpha
</label>
<label>
<input type="checkbox" value="[object Object]"> Beta
</label>
<label>
<input type="checkbox" value="[object Object]"> Gamma
</label>
<p></p>
</div>
<div>
<label>
<input type="checkbox" value="[object Object]"> Alpha
</label>
<label>
<input type="checkbox" value="[object Object]"> Beta
</label>
<label>
<input type="checkbox" value="[object Object]" checked> Gamma
</label>
<p>Gamma</p>
</div>
`,
async test({ assert, component, target, window }) {
const inputs = target.querySelectorAll('input');
assert.equal(inputs[0].checked, false);
Expand Down
19 changes: 18 additions & 1 deletion test/runtime/samples/binding-input-group-each-4/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,24 @@ export default {
<label><input type="checkbox" value="3"> 3</label>
<p>3</p>
`,

ssrHtml: `
<label><input type="checkbox" value="1" checked> 1</label>
<label><input type="checkbox" value="2"> 2</label>
<label><input type="checkbox" value="3"> 3</label>
<p>1</p>
<label><input type="checkbox" value="1"> 1</label>
<label><input type="checkbox" value="2" checked> 2</label>
<label><input type="checkbox" value="3"> 3</label>
<p>2</p>
<label><input type="checkbox" value="1"> 1</label>
<label><input type="checkbox" value="2"> 2</label>
<label><input type="checkbox" value="3"> 3</label>
<p></p>
<label><input type="checkbox" value="1"> 1</label>
<label><input type="checkbox" value="2"> 2</label>
<label><input type="checkbox" value="3" checked> 3</label>
<p>3</p>
`,
async test({ assert, component, target, window }) {
const inputs = target.querySelectorAll('input');
assert.equal(inputs[0].checked, true);
Expand Down
19 changes: 18 additions & 1 deletion test/runtime/samples/binding-input-group-each-5/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,24 @@ export default {
<label><input type="checkbox" value="3"> 3</label>
<p>1</p>
`,

ssrHtml: `
<label><input type="checkbox" value="1" checked> 1</label>
<label><input type="checkbox" value="2"> 2</label>
<label><input type="checkbox" value="3"> 3</label>
<p>1</p>
<label><input type="checkbox" value="1" checked> 1</label>
<label><input type="checkbox" value="2" checked> 2</label>
<label><input type="checkbox" value="3" checked> 3</label>
<p>1, 2, 3</p>
<label><input type="checkbox" value="1"> 1</label>
<label><input type="checkbox" value="2" checked> 2</label>
<label><input type="checkbox" value="3"> 3</label>
<p>2</p>
<label><input type="checkbox" value="1" checked> 1</label>
<label><input type="checkbox" value="2"> 2</label>
<label><input type="checkbox" value="3"> 3</label>
<p>1</p>
`,
async test({ assert, component, target, window }) {
const inputs = target.querySelectorAll('input');
assert.equal(inputs[0].checked, true);
Expand Down
1 change: 0 additions & 1 deletion test/runtime/samples/binding-input-group-each-6/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export default {
<label><input type="checkbox" value="z"> z</label>
<p></p>
`,

async test({ assert, component, target, window }) {
const inputs = target.querySelectorAll('input');
assert.equal(inputs[0].checked, false);
Expand Down
15 changes: 15 additions & 0 deletions test/runtime/samples/binding-input-radio-group/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ export default {
<p>Beta</p>`,

ssrHtml: `
<label>
<input type="radio" value="[object Object]"> Alpha
</label>
<label>
<input type="radio" value="[object Object]" checked> Beta
</label>
<label>
<input type="radio" value="[object Object]"> Gamma
</label>
<p>Beta</p>`,

async test({ assert, component, target, window }) {
const inputs = target.querySelectorAll('input');
assert.equal(inputs[0].checked, false);
Expand Down

0 comments on commit 3ee017b

Please sign in to comment.