Skip to content

fix(styled): v-models not working(close #31) #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/core/src/styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
const componentName = generateComponentName(type)
const commonClassName = generateClassName()
const component = defineComponent(
(props, { slots }) => {
(props, { slots, attrs }) => {
const internalAttrs = computed<Record<string, any>>(() => {
if (typeof defaultAttrs === 'function') {
return defaultAttrs(props)
Expand Down Expand Up @@ -134,10 +134,20 @@ function baseStyled<T extends object>(target: string | InstanceType<any>, propsD
// Return the render function
return () => {
const node = isVueComponent(target) ? h(target, { as: props.as }) : props.as ?? target

// Extract event handlers from attrs
const eventHandlers: Record<string, (...args: any[]) => any> = {}
Object.entries(attrs).forEach(([key, value]) => {
if (key.startsWith('on')) {
eventHandlers[key] = value as (...args: any[]) => any
}
})

return h(
node,
{
...internalProps.value,
...eventHandlers, // Ensure event handlers are passed correctly
},
slots,
)
Expand Down
23 changes: 22 additions & 1 deletion packages/playground/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import styled, { ThemeProvider } from '@vue-styled-components/core'
import { reactive } from 'vue'
import { reactive, ref } from 'vue'

const Button = styled.button`
color: ${(props) => props.theme.fg};
Expand All @@ -27,6 +27,18 @@ const Container = styled.div`
color: #fff;
}
`

const Test = styled('div', { color: String })`
color: ${(props) => props.color};
`

const StyledInput = styled('input', { color: String })`
color: ${(props) => props.color};
`

const colors = ['red', 'blue', 'green']

const inputValue = ref(0)
</script>

<template>
Expand All @@ -45,6 +57,15 @@ const Container = styled.div`
<Container>
<Button>888</Button>
</Container>

<div v-for="c in colors" :key="c">
<Test :color="c">
{{ c }}
</Test>
</div>

<StyledInput v-model:value="inputValue" />
<button @click="inputValue++">click</button>
</ThemeProvider>
</template>

Expand Down