Handle multi-select (#1082)
This commit is contained in:
parent
73bf8543bd
commit
9485127c1c
@ -104,6 +104,11 @@ class Component(Base, ABC):
|
|||||||
initial_kwargs = {
|
initial_kwargs = {
|
||||||
"id": kwargs.get("id"),
|
"id": kwargs.get("id"),
|
||||||
"children": kwargs.get("children", []),
|
"children": kwargs.get("children", []),
|
||||||
|
**{
|
||||||
|
prop: Var.create(kwargs[prop])
|
||||||
|
for prop in self.get_initial_props()
|
||||||
|
if prop in kwargs
|
||||||
|
},
|
||||||
}
|
}
|
||||||
super().__init__(**initial_kwargs)
|
super().__init__(**initial_kwargs)
|
||||||
|
|
||||||
@ -343,6 +348,15 @@ class Component(Base, ABC):
|
|||||||
"""
|
"""
|
||||||
return set(cls.get_fields()) - set(Component.get_fields())
|
return set(cls.get_fields()) - set(Component.get_fields())
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_initial_props(cls) -> Set[str]:
|
||||||
|
"""Get the initial props to set for the component.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The initial props to set.
|
||||||
|
"""
|
||||||
|
return set()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create(cls, *children, **props) -> Component:
|
def create(cls, *children, **props) -> Component:
|
||||||
"""Create the component.
|
"""Create the component.
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
"""Provides a feature-rich Select and some (not all) related components."""
|
"""Provides a feature-rich Select and some (not all) related components."""
|
||||||
|
|
||||||
from typing import Any, Dict, List, Optional, Union
|
from typing import Any, Dict, List, Optional, Set, Union
|
||||||
|
|
||||||
from pynecone.base import Base
|
from pynecone.base import Base
|
||||||
from pynecone.components.component import Component
|
from pynecone.components.component import Component
|
||||||
@ -304,9 +304,22 @@ class Select(Component):
|
|||||||
Returns:
|
Returns:
|
||||||
A dict mapping the event trigger to the var that is passed to the handler.
|
A dict mapping the event trigger to the var that is passed to the handler.
|
||||||
"""
|
"""
|
||||||
return {
|
# A normal select returns the value.
|
||||||
"on_change": EVENT_ARG.value,
|
value = EVENT_ARG.value
|
||||||
}
|
|
||||||
|
# Multi-select returns a list of values.
|
||||||
|
if self.is_multi:
|
||||||
|
value = Var.create_safe(f"{EVENT_ARG}.map(e => e.value)", is_local=True)
|
||||||
|
return {"on_change": value}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_initial_props(cls) -> Set[str]:
|
||||||
|
"""Get the initial props to set for the component.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The initial props to set.
|
||||||
|
"""
|
||||||
|
return super().get_initial_props() | {"is_multi"}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create(
|
def create(
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[tool.poetry]
|
[tool.poetry]
|
||||||
name = "pynecone"
|
name = "pynecone"
|
||||||
version = "0.1.31"
|
version = "0.1.32"
|
||||||
description = "Web apps in pure Python."
|
description = "Web apps in pure Python."
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
authors = [
|
authors = [
|
||||||
|
@ -47,18 +47,18 @@ def test_upload_component_render(upload_component):
|
|||||||
Args:
|
Args:
|
||||||
upload_component: component fixture
|
upload_component: component fixture
|
||||||
"""
|
"""
|
||||||
uplaod = upload_component.render()
|
upload = upload_component.render()
|
||||||
|
|
||||||
# upload
|
# upload
|
||||||
assert uplaod["name"] == "ReactDropzone"
|
assert upload["name"] == "ReactDropzone"
|
||||||
assert uplaod["props"] == [
|
assert upload["props"] == [
|
||||||
"multiple={true}",
|
"multiple={true}",
|
||||||
"onDrop={e => File(e)}",
|
"onDrop={e => File(e)}",
|
||||||
]
|
]
|
||||||
assert uplaod["args"] == ("getRootProps", "getInputProps")
|
assert upload["args"] == ("getRootProps", "getInputProps")
|
||||||
|
|
||||||
# box inside of upload
|
# box inside of upload
|
||||||
[box] = uplaod["children"]
|
[box] = upload["children"]
|
||||||
assert box["name"] == "Box"
|
assert box["name"] == "Box"
|
||||||
assert box["props"] == [
|
assert box["props"] == [
|
||||||
'sx={{"border": "1px dotted black"}}',
|
'sx={{"border": "1px dotted black"}}',
|
||||||
@ -86,9 +86,9 @@ def test_upload_component_with_props_render(upload_component_with_props):
|
|||||||
Args:
|
Args:
|
||||||
upload_component_with_props: component fixture
|
upload_component_with_props: component fixture
|
||||||
"""
|
"""
|
||||||
uplaod = upload_component_with_props.render()
|
upload = upload_component_with_props.render()
|
||||||
|
|
||||||
assert uplaod["props"] == [
|
assert upload["props"] == [
|
||||||
"maxFiles={2}",
|
"maxFiles={2}",
|
||||||
"multiple={true}",
|
"multiple={true}",
|
||||||
"noDrag={true}",
|
"noDrag={true}",
|
||||||
|
Loading…
Reference in New Issue
Block a user