Skip to content

Commit

Permalink
feat(kit): editable component props (#674)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzhang1030 authored Nov 5, 2024
1 parent bf8ad74 commit 5807c07
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 6 deletions.
20 changes: 14 additions & 6 deletions packages/devtools-kit/src/core/component/state/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,22 @@ export async function editComponentState(payload: InspectorStateEditorPayload, s

let target: Record<string, unknown> | undefined

// TODO: props

// 1. check if is setup
if (instance.devtoolsRawSetupState && Object.keys(instance.devtoolsRawSetupState).includes(path[0]))
// 1. check if is props
if (Object.keys(instance.props).includes(path[0])) {
target = instance.props
}
// 2. check if is setup
else if (instance.devtoolsRawSetupState && Object.keys(instance.devtoolsRawSetupState).includes(path[0])) {
target = instance.devtoolsRawSetupState
// 2. check if is options
if (instance.data && Object.keys(instance.data).includes(path[0]))
}
// 3. check if is options
else if (instance.data && Object.keys(instance.data).includes(path[0])) {
target = instance.data
}
else {
// 4. fallback
target = instance.proxy
}

if (target && targetPath) {
if (state.type === 'object' && type === 'reactive') {
Expand Down
1 change: 1 addition & 0 deletions packages/devtools-kit/src/core/component/state/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function processProps(instance: VueAppInstance) {
type: 'props',
key: camelizeKey,
value: returnError(() => instance.props[key]),
editable: true,
meta: propDefinition
? {
type: propDefinition.type ? getPropType(propDefinition.type) : 'any',
Expand Down
6 changes: 6 additions & 0 deletions packages/playground/basic/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
/hey/123123
</span>
</RouterLink>
|
<RouterLink to="/prop-mutation">
<span>
Prop mutation
</span>
</RouterLink>
</div>
</div>
</template>
17 changes: 17 additions & 0 deletions packages/playground/basic/src/components/PropChild.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script setup lang="ts">
const props = defineProps<{
bool: boolean
num: number
str: string
obj: Record<string, any>
}>()
</script>

<template>
<div>
<div>bool: {{ props.bool }}</div>
<div>num: {{ props.num }}</div>
<div>str: {{ props.str }}</div>
<div>obj: {{ props.obj }}</div>
</div>
</template>
5 changes: 5 additions & 0 deletions packages/playground/basic/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ const routes: RouteRecordRaw[] = [
component: () => import('./pages/IntervalUpdate.vue'),
name: 'interval-update',
},
{
path: '/prop-mutation',
component: () => import('./pages/PropMutation.vue'),
name: 'prop-mutation',
},
]

const router = createRouter({
Expand Down
26 changes: 26 additions & 0 deletions packages/playground/basic/src/pages/PropMutation.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<script setup lang="ts">
import PropChild from '../components/PropChild.vue'
const props = {
bool: ref(false),
num: ref(0),
str: ref('hey'),
obj: ref({
foo: 'bar',
}),
}
</script>

<template>
<div>
<div>Prop Page</div>
<PropChild
v-bind="{
bool: props.bool.value,
num: props.num.value,
str: props.str.value,
obj: props.obj.value,
}"
/>
</div>
</template>

0 comments on commit 5807c07

Please sign in to comment.