Skip to content

Commit

Permalink
fix overriding the event in the props
Browse files Browse the repository at this point in the history
  • Loading branch information
ECorreia45 committed Jan 10, 2025
1 parent b45f3ca commit 8ffbdc1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@beforesemicolon/markup",
"version": "1.13.3",
"version": "1.13.4",
"description": "Reactive HTML Templating System",
"engines": {
"node": ">=18.16.0"
Expand Down
23 changes: 23 additions & 0 deletions src/html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import '../test.common.ts';
import { html, HtmlTemplate } from './html.ts'
import { effect, state } from './state.ts'
import {when, repeat, oneOf, is, element, suspense} from './helpers/index.ts'
import { text } from 'node:stream/consumers'
import exp = require('node:constants')

describe('html', () => {

Expand Down Expand Up @@ -842,6 +844,27 @@ describe('html', () => {
expect(countUpSpy).toHaveBeenCalledWith(expect.any(Event));
})

it('should override attribute in the object', () => {
const clickMock1 = jest.fn();
const clickMock2 = jest.fn();

const props = {
onclick: clickMock1,
type: 'submit'
}

const temp = html`<button ${props} type="button" onclick="${clickMock2}" ref="btn">click me</button>`.render(document.body)

const [btn] = temp.refs['btn'] as HTMLButtonElement[];

expect(document.body.innerHTML).toBe('<button type="button">click me</button>');

btn.click();

expect(clickMock1).not.toHaveBeenCalled()
expect(clickMock2).toHaveBeenCalled()
})

it('should handle input value', () => {
const [value, setValue] = state("");

Expand Down
12 changes: 7 additions & 5 deletions src/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,22 +187,24 @@ function createTemplate(

const isRef = name === 'ref'

!isRef && slots.remove(attrSlots[name])

if (isRef || /\$val([0-9]+)/.test(value)) {
__self__.setAttribute('data-slot-id', id)
const v = value.trim()
return slots.push({

attrSlots[name] = {
type: 'attribute',
name,
value: v,
nodeSelector: `[data-slot-id="${id}"]`,
valueParts: isRef ? [v] : parseDynamicRawValue(v),
})
}

return slots.push(attrSlots[name])
}

setElementAttribute(__self__, name, value)

// ensure prop attributes do not override inline attributes
slots.remove(attrSlots[name])
},
} as unknown as ElementLike
},
Expand Down

0 comments on commit 8ffbdc1

Please sign in to comment.