From eac9d30d42e49963b5e8833e06e1ea9255d96e59 Mon Sep 17 00:00:00 2001 From: Lena Morita Date: Thu, 14 Jul 2022 09:50:17 +0900 Subject: [PATCH] Memoize and simplify context value --- .../src/input-control/input-base.tsx | 38 ++++++++----------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/packages/components/src/input-control/input-base.tsx b/packages/components/src/input-control/input-base.tsx index eeb7f7ce98c9b3..931a9d3427cb61 100644 --- a/packages/components/src/input-control/input-base.tsx +++ b/packages/components/src/input-control/input-base.tsx @@ -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 @@ -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. @@ -107,33 +113,19 @@ export function InputBase( hideLabel={ hideLabel } labelPosition={ labelPosition } > - { prefix && ( - + + { prefix && ( { prefix } - - ) } - { children } - { suffix && ( - + ) } + { children } + { suffix && ( { suffix } - - ) } + ) } +