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.

This is a port of styled-components#3436 to legacy-v5
  • Loading branch information
agriffis committed Apr 2, 2021
1 parent 4b23158 commit 6989a4d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 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 @@ -143,7 +143,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 @@ -207,11 +211,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
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,10 @@ export default (InlineStyle: Function) => {
if (isTargetStyledComp && target.shouldForwardProp) {
if (shouldForwardProp) {
// compose nested shouldForwardProp calls
shouldForwardProp = (prop, filterFn) =>
shouldForwardProp = (prop, filterFn, elementToBeCreated) =>
// $FlowFixMe
target.shouldForwardProp(prop, filterFn) && options.shouldForwardProp(prop, filterFn);
target.shouldForwardProp(prop, filterFn, elementToBeCreated) &&
options.shouldForwardProp(prop, filterFn, elementToBeCreated);
} else {
// eslint-disable-next-line prefer-destructuring
shouldForwardProp = target.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 6989a4d

Please sign in to comment.