Skip to content

Commit

Permalink
[core] Exclude undefined className from render fn props (#1041)
Browse files Browse the repository at this point in the history
  • Loading branch information
mj12albert authored Dec 12, 2024
1 parent e70f892 commit a89b832
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
33 changes: 33 additions & 0 deletions packages/react/src/utils/useComponentRenderer.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as React from 'react';
import { expect } from 'chai';
import { createRenderer } from '@mui/internal-test-utils';
import { useComponentRenderer, type ComponentRendererSettings } from './useComponentRenderer';

describe('useComponentRenderer', () => {
const { render } = createRenderer();

it('render props does not overwrite className in a render function when unspecified', async () => {
function TestComponent(props: {
render: ComponentRendererSettings<any, Element>['render'];
className?: ComponentRendererSettings<any, Element>['className'];
}) {
const { render: renderProp, className } = props;
const { renderElement } = useComponentRenderer({
render: renderProp,
state: {},
className,
});
return renderElement();
}

const { container } = await render(
<TestComponent
render={(props: any, state: any) => <span className="my-span" {...props} {...state} />}
/>,
);

const element = container.firstElementChild;

expect(element).to.have.attribute('class', 'my-span');
});
});
6 changes: 4 additions & 2 deletions packages/react/src/utils/useComponentRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ export function useComponentRenderer<
}

const renderedElementProps = propGetter(ownProps);
const propsWithRef = {
const propsWithRef: React.HTMLAttributes<any> & React.RefAttributes<any> = {
...renderedElementProps,
ref: useRenderPropForkRef(resolvedRenderProp, ref as React.Ref<any>, renderedElementProps.ref),
className,
};
if (className !== undefined) {
propsWithRef.className = className;
}

const renderElement = () => evaluateRenderProp(resolvedRenderProp, propsWithRef, state);

Expand Down

0 comments on commit a89b832

Please sign in to comment.