Skip to content

Commit 6cdf2f9

Browse files
committed
Use prop conversion logic from reflex
1 parent 65eafcb commit 6cdf2f9

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ packages = [{include = "reflex_chakra"}]
1212
python = "^3.8"
1313
reflex = ">=0.6.0a"
1414

15+
[tool.poetry.group.dev.dependencies]
16+
pytest = ">=7.1.2,<8.0"
17+
1518

1619
[build-system]
1720
requires = ["poetry-core"]

reflex_chakra/components/base.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ def create(cls, *children, **props) -> Component:
7171
constants.ASSETS_DIR / constants.COLOR_MODE_PROVIDER_FILENAME,
7272
client_color_mode_provider.parent,
7373
)
74+
75+
new_prop_names = [
76+
prop for prop in cls.get_props() if prop in ["type", "min", "max"]
77+
]
78+
for prop in new_prop_names:
79+
under_prop = f"{prop}_"
80+
if under_prop in props:
81+
# TODO: decide whether to deprecate this prop namespace conversion
82+
props[prop] = props.pop(under_prop)
83+
7484
return super().create(*children, **props)
7585

7686

tests/__init__.py

Whitespace-only changes.

tests/components/__init__.py

Whitespace-only changes.

tests/components/base.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import pytest
2+
import reflex as rx
3+
4+
5+
def test_deprecated_props(capsys):
6+
"""Assert that deprecated underscore suffix props are translated.
7+
8+
Args:
9+
capsys: Pytest fixture for capturing stdout and stderr.
10+
"""
11+
12+
class C1(rx.Component):
13+
tag = "C1"
14+
15+
type: rx.Var[str]
16+
min: rx.Var[str]
17+
max: rx.Var[str]
18+
19+
# No warnings are emitted when using the new prop names.
20+
c1_1 = C1.create(type="type1", min="min1", max="max1")
21+
out_err = capsys.readouterr()
22+
assert not out_err.err
23+
assert not out_err.out
24+
25+
c1_1_render = c1_1.render()
26+
assert 'type={"type1"}' in c1_1_render["props"]
27+
assert 'min={"min1"}' in c1_1_render["props"]
28+
assert 'max={"max1"}' in c1_1_render["props"]
29+
30+
# Deprecation warning is emitted with underscore suffix,
31+
# but the component still works.
32+
c1_2 = C1.create(type_="type2", min_="min2", max_="max2")
33+
out_err = capsys.readouterr()
34+
assert out_err.out.count("DeprecationWarning:") == 3
35+
assert not out_err.err
36+
37+
c1_2_render = c1_2.render()
38+
assert 'type={"type2"}' in c1_2_render["props"]
39+
assert 'min={"min2"}' in c1_2_render["props"]
40+
assert 'max={"max2"}' in c1_2_render["props"]
41+
42+
class C2(rx.Component):
43+
tag = "C2"
44+
45+
type_: rx.Var[str]
46+
min_: rx.Var[str]
47+
max_: rx.Var[str]
48+
49+
# No warnings are emitted if the actual prop has an underscore suffix
50+
c2_1 = C2.create(type_="type1", min_="min1", max_="max1")
51+
out_err = capsys.readouterr()
52+
assert not out_err.err
53+
assert not out_err.out
54+
55+
c2_1_render = c2_1.render()
56+
assert 'type={"type1"}' in c2_1_render["props"]
57+
assert 'min={"min1"}' in c2_1_render["props"]
58+
assert 'max={"max1"}' in c2_1_render["props"]

0 commit comments

Comments
 (0)