Skip to content

Commit

Permalink
Memoize and simplify context value
Browse files Browse the repository at this point in the history
  • Loading branch information
mirka committed Jul 15, 2022
1 parent b692990 commit eac9d30
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions packages/components/src/input-control/input-base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { ForwardedRef } from 'react';
* WordPress dependencies
*/
import { useInstanceId } from '@wordpress/compose';
import { forwardRef } from '@wordpress/element';
import { forwardRef, useMemo } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -74,10 +74,16 @@ export function InputBase(
const id = useUniqueId( idProp );
const hideLabel = hideLabelFromVision || ! label;

const sizeConfig = getSizeConfig( {
const { paddingLeft, paddingRight } = getSizeConfig( {
inputSize: size,
__next36pxDefaultSize,
} );
const prefixSuffixContextValue = useMemo( () => {
return {
InputControlPrefix: { paddingLeft },
InputControlSuffix: { paddingRight },
};
}, [ paddingLeft, paddingRight ] );

return (
// @ts-expect-error The `direction` prop from Flex (FlexDirection) conflicts with legacy SVGAttributes `direction` (string) that come from React intrinsic prop definitions.
Expand Down Expand Up @@ -107,33 +113,19 @@ export function InputBase(
hideLabel={ hideLabel }
labelPosition={ labelPosition }
>
{ prefix && (
<ContextSystemProvider
value={ {
InputControlPrefix: {
paddingLeft: sizeConfig.paddingLeft,
},
} }
>
<ContextSystemProvider value={ prefixSuffixContextValue }>
{ prefix && (
<Prefix className="components-input-control__prefix">
{ prefix }
</Prefix>
</ContextSystemProvider>
) }
{ children }
{ suffix && (
<ContextSystemProvider
value={ {
InputControlSuffix: {
paddingRight: sizeConfig.paddingRight,
},
} }
>
) }
{ children }
{ suffix && (
<Suffix className="components-input-control__suffix">
{ suffix }
</Suffix>
</ContextSystemProvider>
) }
) }
</ContextSystemProvider>
<Backdrop disabled={ disabled } isFocused={ isFocused } />
</Container>
</Root>
Expand Down

0 comments on commit eac9d30

Please sign in to comment.