Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Exclude undefined className from render fn props #1041

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading