-
Notifications
You must be signed in to change notification settings - Fork 59
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
document an example of component composition #185
base: master
Are you sure you want to change the base?
Conversation
Thanks for your contribution. The chapter above (Embed ipywidgets) actually describes the recommended way of embedding/composing widgets. This works for all ipywidgets including ipyvuetify template widgets and is more flexible. The feature you describe is the older way of doing this (which we want to deprecate). Maybe using |
thanks for the explanation, and warning for deprecation (I just built a lot of components using that approach). Does the widget approach allow for creating custom props? I was able to work with custom props with the component approach but I couldn't figure out how to do it with the widget approach. |
Ah, yes, you can also use a class instead of an instance. In that case 'fruit-selector': FruitSelector instead of: python
'fruit-selector': FruitSelector() And I think it would then be good to show props being set in the example. It's also possible set full vue components in |
are you saying that those use cases (custom props and events) will remain possible only via On a related topic, any recommendations about how to manage state? props can flow from parent to child, but not the other way around. I've been using |
Could you give an example of that, and possibly open a new issue for that? |
Yeah, that would be great.
We depend on the way this is done in ipywidgets, so the way to "share" data between widgets is by linking them. However, traitles in ipyvuetify widgets are deep-watched on the vue side, so changes in a nested data structure are picked up. In a full vue component you could do something like this: import ipyvuetify as v
import traitlets
import ipyvue
ipyvue.register_component_from_string("my-child", """
<template>
<v-text-field :label="label" v-model="internalValue"></v-text-field>
</template>
<script>
module.exports = {
props: ['label', 'value'],
data() {
return {
internalValue: this.value,
}
},
watch: {
internalValue(v) {
this.$emit('update:value', v)
}
}
}
</script>
""")
class Parent(v.VuetifyTemplate):
@traitlets.default('template')
def _template(self):
return """
<template>
<div>
<div v-for="item in items">
<my-child :label="item.label" :value.sync="item.value"/>
</div>
</div>
</template>
"""
items = traitlets.List().tag(sync=True)
p = Parent(items=[{
'label': 'label 1', 'value': 'x'},
{'label': 'label 2', 'value': 'y'}
])
p.observe(lambda change: print(change['new']), names='items')
p |
368ca6d
to
e241012
Compare
0017486
to
f4937c0
Compare
Extending the template documentation to show how to compose components into each other, when all of them are created with ipyvuetify templates.