Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow for styled to parse additional props
- Loading branch information
1 parent
4054966
commit 1037027
Showing
6 changed files
with
199 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ module.exports = { | |
"error", | ||
{ | ||
allowList: { | ||
prop: true, | ||
props: true, | ||
Props: true, | ||
ref: true, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Styled - Custom Props can style custom props 1`] = ` | ||
Array [ | ||
<View | ||
style={ | ||
Array [ | ||
Object { | ||
"marginBottom": 4, | ||
"marginLeft": 4, | ||
"marginRight": 4, | ||
"marginTop": 4, | ||
}, | ||
] | ||
} | ||
/>, | ||
<View | ||
style={ | ||
Array [ | ||
Object { | ||
"paddingBottom": 8, | ||
"paddingLeft": 8, | ||
"paddingRight": 8, | ||
"paddingTop": 8, | ||
}, | ||
] | ||
} | ||
/>, | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { render } from "@testing-library/react-native"; | ||
import { View, ViewProps, ViewStyle } from "react-native"; | ||
import { styled } from "../../src"; | ||
import { TestProvider } from "../tailwindcss/runner"; | ||
|
||
const StyledTestComponent = styled( | ||
({ style, style2 }: ViewProps & { style2: ViewStyle }) => { | ||
return ( | ||
<> | ||
<View style={style} /> | ||
<View style={style2} /> | ||
</> | ||
); | ||
}, | ||
{ | ||
props: ["style2"], | ||
} | ||
) as any; | ||
|
||
describe("Styled - Custom Props", () => { | ||
test("can style custom props", () => { | ||
const tree = render( | ||
<TestProvider css="m-1 p-2"> | ||
<StyledTestComponent className="m-1" style2="p-2" /> | ||
</TestProvider> | ||
).toJSON(); | ||
|
||
expect(tree).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { ReactNode, Children, cloneElement } from "react"; | ||
import { isFragment } from "react-is"; | ||
import { matchChildAtRule } from "./match-at-rule"; | ||
import { AtRuleRecord } from "./types/common"; | ||
|
||
export interface UseStyledChildrenOptions { | ||
componentChildren: ReactNode; | ||
childStyles?: AtRuleRecord[]; | ||
} | ||
|
||
export function useStyledChildren({ | ||
componentChildren, | ||
childStyles, | ||
}: UseStyledChildrenOptions): ReactNode { | ||
let children = isFragment(componentChildren) | ||
? // This probably needs to be recursive | ||
componentChildren.props.children | ||
: componentChildren; | ||
|
||
if (childStyles) { | ||
children = Children.map(children, (child, index) => { | ||
const matchingStyles = []; | ||
for (const { atRules, ...styles } of childStyles) { | ||
const matches = atRules.every(([rule, params]) => { | ||
return matchChildAtRule({ | ||
nthChild: index + 1, | ||
rule, | ||
params, | ||
}); | ||
}); | ||
if (matches) { | ||
matchingStyles.push(styles); | ||
} | ||
} | ||
|
||
return cloneElement(child, { | ||
style: child.props.style | ||
? [child.props.style, matchingStyles] | ||
: matchingStyles.length > 0 | ||
? matchingStyles | ||
: undefined, | ||
}); | ||
}); | ||
} | ||
|
||
return children; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { StyleProp } from "react-native"; | ||
import { UseTailwindCallback } from "./use-tailwind"; | ||
import { ChildClassNameSymbol } from "./utils/child-styles"; | ||
|
||
export interface UseStyledPropsOptions { | ||
tw: UseTailwindCallback<any>; | ||
classes: string | undefined; | ||
componentStyles: StyleProp<any>; | ||
propsToTransform?: boolean | string[]; | ||
componentProps: Record<string, unknown>; | ||
} | ||
|
||
export function useStyledProps({ | ||
tw, | ||
classes, | ||
componentStyles, | ||
propsToTransform, | ||
componentProps, | ||
}: UseStyledPropsOptions) { | ||
const mainStyles = tw(classes); | ||
|
||
const style = componentStyles ? [mainStyles, componentStyles] : mainStyles; | ||
|
||
const styledProps: Record<string, unknown> = {}; | ||
|
||
if (propsToTransform) { | ||
if (propsToTransform === true) { | ||
propsToTransform = Object.keys(componentProps); | ||
} | ||
|
||
for (const prop of propsToTransform) { | ||
const value = componentProps[prop]; | ||
|
||
if (typeof value === "string") { | ||
styledProps[prop] = tw(value); | ||
} | ||
} | ||
} | ||
|
||
console.log(styledProps); | ||
|
||
return { | ||
childStyles: mainStyles[ChildClassNameSymbol], | ||
style, | ||
...styledProps, | ||
}; | ||
} |