Skip to content

Commit

Permalink
more nuanced conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
adhami3310 committed Feb 12, 2025
1 parent a826281 commit 70bedab
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion reflex/components/tags/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def add_props(self, **kwargs: Optional[Any]) -> Tag:
"""
self.props.update(
{
format.to_camel_case(name, allow_hyphens=True): (
format.to_camel_case(name, treat_hyphens_as_underscores=False): (
prop
if types._isinstance(prop, (EventChain, Mapping))
else LiteralVar.create(prop)
Expand Down
5 changes: 4 additions & 1 deletion reflex/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,12 @@ def update_out_dict(
for key, value in style_dict.items():
keys = (
format_style_key(key)
if not isinstance(value, (dict, ObjectVar))
if not isinstance(value, (dict, ObjectVar, list))
or (
isinstance(value, Breakpoints)
and all(not isinstance(v, dict) for v in value.values())
)
or (isinstance(value, list) and all(not isinstance(v, dict) for v in value))
or (
isinstance(value, ObjectVar)
and not issubclass(get_origin(value._var_type) or value._var_type, dict)
Expand Down Expand Up @@ -237,6 +238,8 @@ def format_style_key(key: str) -> Tuple[str, ...]:
Returns:
Tuple of css style names corresponding to the key provided.
"""
if key.startswith("--"):
return (key,)
key = format.to_camel_case(key)
return STYLE_PROP_SHORTHAND_MAPPING.get(key, (key,))

Expand Down
11 changes: 5 additions & 6 deletions reflex/utils/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,25 +168,24 @@ def to_snake_case(text: str) -> str:
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower().replace("-", "_")


def to_camel_case(text: str, allow_hyphens: bool = False) -> str:
def to_camel_case(text: str, treat_hyphens_as_underscores: bool = True) -> str:
"""Convert a string to camel case.
The first word in the text is converted to lowercase and
the rest of the words are converted to title case, removing underscores.
Args:
text: The string to convert.
allow_hyphens: Whether to allow hyphens in the string.
treat_hyphens_as_underscores: Whether to allow hyphens in the string.
Returns:
The camel case string.
"""
char = "_" if allow_hyphens else "-_"
words = re.split(f"[{char}]", text.lstrip(char))
leading_underscores_or_hyphens = "".join(re.findall(rf"^[{char}]+", text))
char = "_" if not treat_hyphens_as_underscores else "-_"
words = re.split(f"[{char}]", text)
# Capitalize the first letter of each word except the first one
converted_word = words[0] + "".join(x.capitalize() for x in words[1:])
return leading_underscores_or_hyphens + converted_word
return converted_word


def to_title_case(text: str, sep: str = "") -> str:
Expand Down
10 changes: 5 additions & 5 deletions tests/units/utils/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ def test_to_snake_case(input: str, output: str):
("kebab-case", "kebabCase"),
("kebab-case-two", "kebabCaseTwo"),
("snake_kebab-case", "snakeKebabCase"),
("_hover", "_hover"),
("-starts-with-hyphen", "-startsWithHyphen"),
("--starts-with-double-hyphen", "--startsWithDoubleHyphen"),
("_starts_with_underscore", "_startsWithUnderscore"),
("__starts_with_double_underscore", "__startsWithDoubleUnderscore"),
("_hover", "Hover"),
("-starts-with-hyphen", "StartsWithHyphen"),
("--starts-with-double-hyphen", "StartsWithDoubleHyphen"),
("_starts_with_underscore", "StartsWithUnderscore"),
("__starts_with_double_underscore", "StartsWithDoubleUnderscore"),
(":start-with-colon", ":startWithColon"),
(":-start-with-colon-dash", ":StartWithColonDash"),
],
Expand Down

0 comments on commit 70bedab

Please sign in to comment.