Skip to content

Commit 364ac94

Browse files
committed
fix: 支持 data-*
1 parent 371c429 commit 364ac94

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

example/module/basic/simple-component/index.view.tsx

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,20 @@ export const SimpleStateComponent = defineComponent(
3131
export const SimpleStateWithDefaultValueComponent = defineComponent(
3232
function SimpleStateWithDefaultValueComponent(
3333
props: SimpleStateComponentProps,
34+
{ attrs },
3435
) {
3536
const classAndStyle = useClassAndStyle()
3637
const count = ref(props.initialValue || 0)
37-
return () => (
38-
<div {...classAndStyle}>
39-
<h3>带默认属性参数的组件</h3>
40-
<input type={'number'} v-model={count.value} />
41-
</div>
42-
)
38+
return () => {
39+
console.log(2222, props.initialValue, attrs)
40+
console.log(3333, { ...props })
41+
return (
42+
<div {...classAndStyle} {...props}>
43+
<h3>带默认属性参数的组件</h3>
44+
<input type={'number'} v-model={count.value} />
45+
</div>
46+
)
47+
}
4348
},
4449
{
4550
props: {
@@ -55,13 +60,23 @@ export const SimpleStateWithDefaultValueComponent = defineComponent(
5560

5661
// 简单状态组件定义
5762
const SimpleComponent = defineComponent(() => {
63+
const init = ref<undefined | number>(10)
64+
5865
return () => (
5966
<div>
6067
<h2>简单组件定义</h2>
6168
<h3>函数组件</h3>
6269
<SimpleFuncComponent count={20}></SimpleFuncComponent>
6370
<SimpleStateComponent initialValue={10}></SimpleStateComponent>
64-
<SimpleStateWithDefaultValueComponent></SimpleStateWithDefaultValueComponent>
71+
<button onClick={() => (init.value = init.value ? undefined : 10)}>
72+
切换默认值
73+
</button>
74+
<SimpleStateWithDefaultValueComponent
75+
class={'aaaa'}
76+
initialValue={init.value}
77+
// @ts-ignore
78+
data-a={1111}
79+
></SimpleStateWithDefaultValueComponent>
6580
</div>
6681
)
6782
})

example/module/basic/user-input/user-input.view.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default class UserInputView extends VueComponent {
1919
return (
2020
<Card title={'增删改查'}>
2121
<div>
22-
<Button type={'primary'} onClick={this.add}>
22+
<Button type={'primary'} onClick={this.add} data-a={'111'}>
2323
增加
2424
</Button>
2525
</div>

src/simple-props/composables.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import {
77
} from 'vue'
88

99
export function camelizePropKey(p: string | symbol): string | symbol {
10-
return typeof p === 'string' ? camelize(p) : p
10+
if (typeof p === 'string') {
11+
if (p.startsWith('data-')) return p
12+
return camelize(p)
13+
}
14+
return p
1115
}
1216

1317
export function useProps<T>(): T {
@@ -20,7 +24,7 @@ export function useProps<T>(): T {
2024
const getProps = () => {
2125
return Object.fromEntries(
2226
Object.entries(instance.vnode.props || {}).map(([k, v]) => [
23-
camelize(k),
27+
camelizePropKey(k),
2428
v,
2529
]),
2630
)
@@ -47,6 +51,7 @@ export function useProps<T>(): T {
4751
ownKeys() {
4852
return [
4953
...new Set([
54+
...Reflect.ownKeys(instance.props),
5055
...Reflect.ownKeys(getProps()),
5156
...Reflect.ownKeys(slots).map((k) =>
5257
typeof k === 'string' ? camelize(`render-${k}`) : k,

0 commit comments

Comments
 (0)