treat hyphen as underscore in keys of styles (#4810)
* treat hyphen as underscore in keys of styles * fix tests * more nuanced conversions
This commit is contained in:
parent
abab18e165
commit
7a6c7123bd
@ -101,7 +101,7 @@ class Tag:
|
|||||||
"""
|
"""
|
||||||
self.props.update(
|
self.props.update(
|
||||||
{
|
{
|
||||||
format.to_camel_case(name, allow_hyphens=True): (
|
format.to_camel_case(name, treat_hyphens_as_underscores=False): (
|
||||||
prop
|
prop
|
||||||
if types._isinstance(prop, (EventChain, Mapping))
|
if types._isinstance(prop, (EventChain, Mapping))
|
||||||
else LiteralVar.create(prop)
|
else LiteralVar.create(prop)
|
||||||
|
@ -190,11 +190,12 @@ def convert(
|
|||||||
for key, value in style_dict.items():
|
for key, value in style_dict.items():
|
||||||
keys = (
|
keys = (
|
||||||
format_style_key(key)
|
format_style_key(key)
|
||||||
if not isinstance(value, (dict, ObjectVar))
|
if not isinstance(value, (dict, ObjectVar, list))
|
||||||
or (
|
or (
|
||||||
isinstance(value, Breakpoints)
|
isinstance(value, Breakpoints)
|
||||||
and all(not isinstance(v, dict) for v in value.values())
|
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 (
|
or (
|
||||||
isinstance(value, ObjectVar)
|
isinstance(value, ObjectVar)
|
||||||
and not issubclass(get_origin(value._var_type) or value._var_type, dict)
|
and not issubclass(get_origin(value._var_type) or value._var_type, dict)
|
||||||
@ -236,7 +237,9 @@ def format_style_key(key: str) -> Tuple[str, ...]:
|
|||||||
Returns:
|
Returns:
|
||||||
Tuple of css style names corresponding to the key provided.
|
Tuple of css style names corresponding to the key provided.
|
||||||
"""
|
"""
|
||||||
key = format.to_camel_case(key, allow_hyphens=True)
|
if key.startswith("--"):
|
||||||
|
return (key,)
|
||||||
|
key = format.to_camel_case(key)
|
||||||
return STYLE_PROP_SHORTHAND_MAPPING.get(key, (key,))
|
return STYLE_PROP_SHORTHAND_MAPPING.get(key, (key,))
|
||||||
|
|
||||||
|
|
||||||
|
@ -168,7 +168,7 @@ def to_snake_case(text: str) -> str:
|
|||||||
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower().replace("-", "_")
|
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.
|
"""Convert a string to camel case.
|
||||||
|
|
||||||
The first word in the text is converted to lowercase and
|
The first word in the text is converted to lowercase and
|
||||||
@ -176,17 +176,16 @@ def to_camel_case(text: str, allow_hyphens: bool = False) -> str:
|
|||||||
|
|
||||||
Args:
|
Args:
|
||||||
text: The string to convert.
|
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:
|
Returns:
|
||||||
The camel case string.
|
The camel case string.
|
||||||
"""
|
"""
|
||||||
char = "_" if allow_hyphens else "-_"
|
char = "_" if not treat_hyphens_as_underscores else "-_"
|
||||||
words = re.split(f"[{char}]", text.lstrip(char))
|
words = re.split(f"[{char}]", text)
|
||||||
leading_underscores_or_hyphens = "".join(re.findall(rf"^[{char}]+", text))
|
|
||||||
# Capitalize the first letter of each word except the first one
|
# Capitalize the first letter of each word except the first one
|
||||||
converted_word = words[0] + "".join(x.capitalize() for x in words[1:])
|
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:
|
def to_title_case(text: str, sep: str = "") -> str:
|
||||||
|
@ -157,7 +157,7 @@ def test_create_map_fn_var_subclass(cls, fn_body, fn_args, explicit_return, expe
|
|||||||
value, **props
|
value, **props
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
r"""(({node, inline, className, children, ...props}) => { const match = (className || '').match(/language-(?<lang>.*)/); let _language = match ? match[1] : ''; ; return inline ? ( <RadixThemesCode {...props}>{children}</RadixThemesCode> ) : ( <RadixThemesBox css={({ ["pre"] : ({ ["margin"] : "0", ["padding"] : "24px", ["background"] : "transparent", ["overflow-x"] : "auto", ["border-radius"] : "6px" }) })} {...props}><ShikiCode code={((Array.isArray(children)) ? children.join("\n") : children)} decorations={[]} language={_language} theme={((resolvedColorMode === "light") ? "one-light" : "one-dark-pro")} transformers={[]}/></RadixThemesBox> ); })""",
|
r"""(({node, inline, className, children, ...props}) => { const match = (className || '').match(/language-(?<lang>.*)/); let _language = match ? match[1] : ''; ; return inline ? ( <RadixThemesCode {...props}>{children}</RadixThemesCode> ) : ( <RadixThemesBox css={({ ["pre"] : ({ ["margin"] : "0", ["padding"] : "24px", ["background"] : "transparent", ["overflowX"] : "auto", ["borderRadius"] : "6px" }) })} {...props}><ShikiCode code={((Array.isArray(children)) ? children.join("\n") : children)} decorations={[]} language={_language} theme={((resolvedColorMode === "light") ? "one-light" : "one-dark-pro")} transformers={[]}/></RadixThemesBox> ); })""",
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"h1",
|
"h1",
|
||||||
|
@ -651,7 +651,7 @@ def test_create_filters_none_props(test_component):
|
|||||||
|
|
||||||
# Assert that the style prop is present in the component's props
|
# Assert that the style prop is present in the component's props
|
||||||
assert str(component.style["color"]) == '"white"'
|
assert str(component.style["color"]) == '"white"'
|
||||||
assert str(component.style["text-align"]) == '"center"'
|
assert str(component.style["textAlign"]) == '"center"'
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
@ -189,11 +189,11 @@ def test_to_snake_case(input: str, output: str):
|
|||||||
("kebab-case", "kebabCase"),
|
("kebab-case", "kebabCase"),
|
||||||
("kebab-case-two", "kebabCaseTwo"),
|
("kebab-case-two", "kebabCaseTwo"),
|
||||||
("snake_kebab-case", "snakeKebabCase"),
|
("snake_kebab-case", "snakeKebabCase"),
|
||||||
("_hover", "_hover"),
|
("_hover", "Hover"),
|
||||||
("-starts-with-hyphen", "-startsWithHyphen"),
|
("-starts-with-hyphen", "StartsWithHyphen"),
|
||||||
("--starts-with-double-hyphen", "--startsWithDoubleHyphen"),
|
("--starts-with-double-hyphen", "StartsWithDoubleHyphen"),
|
||||||
("_starts_with_underscore", "_startsWithUnderscore"),
|
("_starts_with_underscore", "StartsWithUnderscore"),
|
||||||
("__starts_with_double_underscore", "__startsWithDoubleUnderscore"),
|
("__starts_with_double_underscore", "StartsWithDoubleUnderscore"),
|
||||||
(":start-with-colon", ":startWithColon"),
|
(":start-with-colon", ":startWithColon"),
|
||||||
(":-start-with-colon-dash", ":StartWithColonDash"),
|
(":-start-with-colon-dash", ":StartWithColonDash"),
|
||||||
],
|
],
|
||||||
|
Loading…
Reference in New Issue
Block a user