Skip to content

Commit 3a3bcf1

Browse files
LancerCometDLillard0yuanjunkai
authored
Add attrs support (#16)
* Add attrs support (#15) Co-authored-by: yuanjunkai <[email protected]> * + 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 <[email protected]> Co-authored-by: yuanjunkai <[email protected]>
1 parent 32943df commit 3a3bcf1

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

lib/jsx.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import {
1818
removeNativeOn,
1919
removeOn,
2020
checkKeyIsRef,
21-
checkKeyIsRefInFor
21+
checkKeyIsRefInFor,
22+
checkKeyIsAttrs
2223
} from './utils'
2324
import { dealWithDirective } from './directives'
2425
import { ConfigType, TagType } from './type'
@@ -56,6 +57,15 @@ const jsx = function (
5657

5758
const attrKeys = Object.keys(config)
5859
for (let key of attrKeys) {
60+
// attrs
61+
if (checkKeyIsAttrs(key)) {
62+
Object.keys(config[key]).forEach(k => {
63+
config[k] = config[key][k]
64+
attrKeys.push(k)
65+
})
66+
continue
67+
}
68+
5969
// Children shouldn't be set in vNodeData.
6070
if (checkKeyIsChildren(key)) {
6171
continue

lib/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const checkKeyIsNativeOn = (key: string) => NATIVE_ON_REGEXP.test(key)
4545
const checkKeyIsVueDirective = (key: string) => VUE_DIRECTIVE_REGEXP.test(key)
4646
const checkKeyIsRef = (key: string) => key === 'ref'
4747
const checkKeyIsRefInFor = (key: string) => key === 'refInFor'
48+
const checkKeyIsAttrs = (key: string) => key === 'attrs'
4849

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

121123
checkKeyIsVueDirective,
122124

test/attrs.spec.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ describe('HTML testing.', () => {
6262

6363
const wrapper = shallowMount(Comp)
6464
})
65+
66+
it('Should support attr spread operator.', () => {
67+
const wrapper = shallowMount({
68+
setup () {
69+
const attrs = {
70+
type: 'email',
71+
placeholder: 'Enter your email'
72+
}
73+
74+
return () => (
75+
<input { ...{ attrs } } />
76+
)
77+
}
78+
})
79+
expect(wrapper.html()).toBe('<input type="email" placeholder="Enter your email">')
80+
})
6581
})
6682

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

0 commit comments

Comments
 (0)