Skip to content

Commit

Permalink
Merge pull request #10 from reflex-dev/elijah/prop-conversion
Browse files Browse the repository at this point in the history
Use prop conversion logic from reflex
  • Loading branch information
ElijahAhianyo authored Sep 9, 2024
2 parents 65eafcb + 4492db7 commit ef6d672
Show file tree
Hide file tree
Showing 6 changed files with 492 additions and 350 deletions.
771 changes: 421 additions & 350 deletions poetry.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ packages = [{include = "reflex_chakra"}]
python = "^3.8"
reflex = ">=0.6.0a"

[tool.poetry.group.dev.dependencies]
pytest = ">=7.1.2,<8.0"


[build-system]
requires = ["poetry-core"]
Expand Down
10 changes: 10 additions & 0 deletions reflex_chakra/components/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ def create(cls, *children, **props) -> Component:
constants.ASSETS_DIR / constants.COLOR_MODE_PROVIDER_FILENAME,
client_color_mode_provider.parent,
)

new_prop_names = [
prop for prop in cls.get_props() if prop in ["type", "min", "max"]
]
for prop in new_prop_names:
under_prop = f"{prop}_"
if under_prop in props:
# TODO: decide whether to deprecate this prop namespace conversion
props[prop] = props.pop(under_prop)

return super().create(*children, **props)


Expand Down
Empty file added tests/__init__.py
Empty file.
Empty file added tests/components/__init__.py
Empty file.
58 changes: 58 additions & 0 deletions tests/components/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pytest
import reflex as rx


def test_deprecated_props(capsys):
"""Assert that deprecated underscore suffix props are translated.
Args:
capsys: Pytest fixture for capturing stdout and stderr.
"""

class C1(rx.Component):
tag = "C1"

type: rx.Var[str]
min: rx.Var[str]
max: rx.Var[str]

# No warnings are emitted when using the new prop names.
c1_1 = C1.create(type="type1", min="min1", max="max1")
out_err = capsys.readouterr()
assert not out_err.err
assert not out_err.out

c1_1_render = c1_1.render()
assert 'type={"type1"}' in c1_1_render["props"]
assert 'min={"min1"}' in c1_1_render["props"]
assert 'max={"max1"}' in c1_1_render["props"]

# Deprecation warning is emitted with underscore suffix,
# but the component still works.
c1_2 = C1.create(type_="type2", min_="min2", max_="max2")
out_err = capsys.readouterr()
assert out_err.out.count("DeprecationWarning:") == 3
assert not out_err.err

c1_2_render = c1_2.render()
assert 'type={"type2"}' in c1_2_render["props"]
assert 'min={"min2"}' in c1_2_render["props"]
assert 'max={"max2"}' in c1_2_render["props"]

class C2(rx.Component):
tag = "C2"

type_: rx.Var[str]
min_: rx.Var[str]
max_: rx.Var[str]

# No warnings are emitted if the actual prop has an underscore suffix
c2_1 = C2.create(type_="type1", min_="min1", max_="max1")
out_err = capsys.readouterr()
assert not out_err.err
assert not out_err.out

c2_1_render = c2_1.render()
assert 'type={"type1"}' in c2_1_render["props"]
assert 'min={"min1"}' in c2_1_render["props"]
assert 'max={"max1"}' in c2_1_render["props"]

0 comments on commit ef6d672

Please sign in to comment.