Skip to content

Commit

Permalink
Add attrs support (#16)
Browse files Browse the repository at this point in the history
* Add attrs support (#15)

Co-authored-by: yuanjunkai <yuanjunkai@linklogis.com>

* + Add unit testing for attrs.

For merge request #15.
For Vue attrs, please read: https://github.com/vuejs/jsx-vue2#attributesprops

---------

Co-authored-by: DLillard0 <53611763+DLillard0@users.noreply.github.com>
Co-authored-by: yuanjunkai <yuanjunkai@linklogis.com>
  • Loading branch information
3 people authored Nov 18, 2023
1 parent 32943df commit 3a3bcf1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
removeNativeOn,
removeOn,
checkKeyIsRef,
checkKeyIsRefInFor
checkKeyIsRefInFor,
checkKeyIsAttrs
} from './utils'
import { dealWithDirective } from './directives'
import { ConfigType, TagType } from './type'
Expand Down Expand Up @@ -56,6 +57,15 @@ const jsx = function (

const attrKeys = Object.keys(config)
for (let key of attrKeys) {
// attrs
if (checkKeyIsAttrs(key)) {
Object.keys(config[key]).forEach(k => {
config[k] = config[key][k]
attrKeys.push(k)
})
continue
}

// Children shouldn't be set in vNodeData.
if (checkKeyIsChildren(key)) {
continue
Expand Down
2 changes: 2 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const checkKeyIsNativeOn = (key: string) => NATIVE_ON_REGEXP.test(key)
const checkKeyIsVueDirective = (key: string) => VUE_DIRECTIVE_REGEXP.test(key)
const checkKeyIsRef = (key: string) => key === 'ref'
const checkKeyIsRefInFor = (key: string) => key === 'refInFor'
const checkKeyIsAttrs = (key: string) => key === 'attrs'

// The reason why I don't use "isRef" which is provided by @vue/composition-api is that
// this function will be broken under SWC.
Expand Down Expand Up @@ -117,6 +118,7 @@ export {
checkKeyIsRef,
checkIsRefObj,
checkKeyIsRefInFor,
checkKeyIsAttrs,

checkKeyIsVueDirective,

Expand Down
16 changes: 16 additions & 0 deletions test/attrs.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ describe('HTML testing.', () => {

const wrapper = shallowMount(Comp)
})

it('Should support attr spread operator.', () => {
const wrapper = shallowMount({
setup () {
const attrs = {
type: 'email',
placeholder: 'Enter your email'
}

return () => (
<input { ...{ attrs } } />
)
}
})
expect(wrapper.html()).toBe('<input type="email" placeholder="Enter your email">')
})
})

describe('Vue component testing.', () => {
Expand Down

0 comments on commit 3a3bcf1

Please sign in to comment.