1- import { ComponentType , FC , createElement , forwardRef } from 'react'
1+ import { ComponentType , FC , JSX } from 'react'
2+ import { jsx } from 'react/jsx-runtime'
23import { cn , NoStrictEntityMods , ClassNameFormatter } from '@bem-react/classname'
34import { classnames } from '@bem-react/classnames'
45
@@ -71,7 +72,7 @@ export function withBemMod<T, U extends IClassNameProps = {}>(
7172 entity = entity || cn ( blockName )
7273 entityClassName = entityClassName || entity ( )
7374
74- const BemMod = forwardRef ( ( props : T & K , ref ) => {
75+ const BemMod : FC < T & K > = ( props ) => {
7576 modNames = modNames || Object . keys ( mod )
7677
7778 // TODO: For performance can rewrite `every` to `for (;;)`.
@@ -82,8 +83,6 @@ export function withBemMod<T, U extends IClassNameProps = {}>(
8283 return modValue === propValue || ( modValue === '*' && Boolean ( propValue ) )
8384 } )
8485
85- const nextProps = Object . assign ( { } , props , { ref } )
86-
8786 if ( isModifierMatched ) {
8887 const modifiers = modNames . reduce ( ( acc : Dictionary , key : string ) => {
8988 if ( mod [ key ] !== '*' ) acc [ key ] = mod [ key ]
@@ -92,8 +91,8 @@ export function withBemMod<T, U extends IClassNameProps = {}>(
9291 } , { } )
9392 modifierClassName = modifierClassName || entity ( modifiers )
9493
95- nextProps . className = classnames ( modifierClassName , props . className )
96- // Replace first entityClassName for remove duplcates from className.
94+ const className = classnames ( modifierClassName , props . className )
95+ // Replace first entityClassName for remove duplicates from className.
9796 . replace ( `${ entityClassName } ` , '' )
9897
9998 if ( typeof enhance === 'function' ) {
@@ -111,13 +110,11 @@ export function withBemMod<T, U extends IClassNameProps = {}>(
111110 ModifiedComponent = WrappedComponent as any
112111 }
113112
114- // Use createElement instead of jsx to avoid __assign from tslib.
115- return createElement ( ModifiedComponent , nextProps )
113+ return jsx ( ModifiedComponent , Object . assign ( { } , props , { className } ) )
116114 }
117115
118- // Use createElement instead of jsx to avoid __assign from tslib.
119- return createElement ( WrappedComponent , nextProps )
120- } )
116+ return jsx ( WrappedComponent , props )
117+ }
121118
122119 if ( __DEV__ ) {
123120 setDisplayName ( BemMod , {
@@ -130,11 +127,8 @@ export function withBemMod<T, U extends IClassNameProps = {}>(
130127 return BemMod
131128 }
132129
133- // Ignore `forwardRef` typings to keep compatibility with `HOC<T>`
134- const withMod = ( WithBemMod as any ) as {
135- < K extends IClassNameProps = { } > ( WrappedComponent : ComponentType < T & K > ) : (
136- props : T & K ,
137- ) => React . ReactElement
130+ const withMod = WithBemMod as any as {
131+ < K extends IClassNameProps = { } > ( WrappedComponent : ComponentType < T & K > ) : FC < T & K >
138132
139133 __isSimple : boolean
140134 __blockName : string
@@ -194,7 +188,7 @@ function composeSimple(mods: any[]) {
194188 return ( Base : ComponentType < any > ) => {
195189 function SimpleComposeWrapper ( props : Record < string , any > ) {
196190 const modifiers : NoStrictEntityMods = { }
197- const newProps : any = { ... props }
191+ const newProps : any = Object . assign ( { } , props )
198192
199193 for ( let key of modNames ) {
200194 const modValues = allMods [ key ]
@@ -221,7 +215,7 @@ function composeSimple(mods: any[]) {
221215
222216 newProps . className = entity ( modifiers , [ props . className ] )
223217
224- return createElement ( Base , newProps )
218+ return jsx ( Base , newProps )
225219 }
226220 if ( __DEV__ ) {
227221 const allModsFormatted = Object . keys ( allMods )
@@ -320,9 +314,9 @@ export function compose() {
320314 f . __isSimple ? simple . push ( f ) : enhanced . push ( f )
321315 }
322316
323- const oprimizedFns = simple . length ? [ composeSimple ( simple ) , ...enhanced ] : enhanced
317+ const optimizedFns = simple . length ? [ composeSimple ( simple ) , ...enhanced ] : enhanced
324318
325- return oprimizedFns . reduce (
319+ return optimizedFns . reduce (
326320 ( a , b ) => {
327321 return function ( ) {
328322 return a ( b . apply ( 0 , arguments ) )
0 commit comments