Skip to content

Commit

Permalink
feat: pass elementToBeCreated to shouldForwardProp
Browse files Browse the repository at this point in the history
The internal decision whether to forward a prop is based on the
combination of the key and elementToBeCreated, but only the former was
passed to shouldForwardProp.
  • Loading branch information
agriffis committed Mar 19, 2021
1 parent 9bef00c commit 67045e4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
14 changes: 10 additions & 4 deletions packages/styled-components/src/models/StyledComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ function useStyledComponentImpl(
else if (key === 'forwardedAs') {
propsForElement.as = computedProps[key];
} else if (
shouldForwardProp ? shouldForwardProp(key, validAttr) : isTargetTag ? validAttr(key) : true
shouldForwardProp
? shouldForwardProp(key, validAttr, elementToBeCreated)
: isTargetTag
? validAttr(key)
: true
) {
// Don't pass through non HTML tags through to HTML elements
propsForElement[key] = computedProps[key];
Expand Down Expand Up @@ -210,11 +214,13 @@ export default function createStyledComponent(
if (isTargetStyledComp && target.shouldForwardProp) {
if (options.shouldForwardProp) {
// compose nested shouldForwardProp calls
shouldForwardProp = (prop, filterFn) =>
shouldForwardProp = (prop, filterFn, elementToBeCreated) =>
((((target: any): IStyledComponent).shouldForwardProp: any): ShouldForwardProp)(
prop,
filterFn
) && ((options.shouldForwardProp: any): ShouldForwardProp)(prop, filterFn);
filterFn,
elementToBeCreated
) &&
((options.shouldForwardProp: any): ShouldForwardProp)(prop, filterFn, elementToBeCreated);
} else {
// eslint-disable-next-line prefer-destructuring
shouldForwardProp = ((target: any): IStyledComponent).shouldForwardProp;
Expand Down
10 changes: 6 additions & 4 deletions packages/styled-components/src/models/StyledNativeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function useStyledComponentImpl(
if (key[0] === '$' || key === 'as') continue;
else if (key === 'forwardedAs') {
propsForElement.as = computedProps[key];
} else if (!shouldForwardProp || shouldForwardProp(key, validAttr)) {
} else if (!shouldForwardProp || shouldForwardProp(key, validAttr, elementToBeCreated)) {
propsForElement[key] = computedProps[key];
}
}
Expand Down Expand Up @@ -143,11 +143,13 @@ export default (InlineStyle: Class<IInlineStyle>) => {
if (isTargetStyledComp && target.shouldForwardProp) {
if (options.shouldForwardProp) {
// compose nested shouldForwardProp calls
shouldForwardProp = (prop, filterFn) =>
shouldForwardProp = (prop, filterFn, elementToBeCreated) =>
((((target: any): IStyledNativeComponent).shouldForwardProp: any): ShouldForwardProp)(
prop,
filterFn
) && ((options.shouldForwardProp: any): ShouldForwardProp)(prop, filterFn);
filterFn,
elementToBeCreated
) &&
((options.shouldForwardProp: any): ShouldForwardProp)(prop, filterFn, elementToBeCreated);
} else {
// eslint-disable-next-line prefer-destructuring
shouldForwardProp = ((target: any): IStyledNativeComponent).shouldForwardProp;
Expand Down
4 changes: 2 additions & 2 deletions packages/styled-components/src/test/props.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ describe('props', () => {
expect(props.filterThis).toBeUndefined();
});

it('passes the default prop filtering function for use if desired', () => {
it('passes the default prop filtering function and target element for use if desired', () => {
const stub = jest.fn();

const Comp = styled('div').withConfig({
Expand All @@ -256,7 +256,7 @@ describe('props', () => {

TestRenderer.create(<Comp as="a" filterThis="abc" passThru="def" />);

expect(stub.mock.calls[0]).toEqual(['filterThis', expect.any(Function)]);
expect(stub.mock.calls[0]).toEqual(['filterThis', expect.any(Function), 'a']);
expect(stub.mock.calls[0][1]('filterThis')).toBe(false);
expect(stub.mock.calls[0][1]('id')).toBe(true);
});
Expand Down
6 changes: 5 additions & 1 deletion packages/styled-components/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export type Stringifier = {
hash: string,
};

export type ShouldForwardProp = (prop: string, isValidAttr: (prop: string) => boolean) => boolean;
export type ShouldForwardProp = (
prop: string,
isValidAttr: (prop: string) => boolean,
elementToBeCreated: Target
) => boolean;

export interface IStyledStatics {
attrs: Attrs;
Expand Down

0 comments on commit 67045e4

Please sign in to comment.