/
githubmirror
/
gutenberg
Обзор
Документация
Войти
/
githubmirror
/
gutenberg
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
trunk
packages/components/src/input-control/input-base.tsx
126 строк
3 KB
Marco Ciampini
ESLint: Replace strict config with bulk suppressions (#81248)
07 авг 2026, 14:34
Не верифицирован
07 авг 2026, 14:34
7f43eaf
Код
Авторство
О чём код?
import type { ForwardedRef } from 'react'; import { useInstanceId } from '@wordpress/compose'; import { useMemo } from '@wordpress/element'; import Backdrop from './backdrop'; import Label from './label'; import { Container, Root, Prefix, Suffix } from './styles/input-control-styles'; import type { InputBaseProps, LabelPosition } from './types'; import type { WordPressComponentProps } from '../context'; import { ContextSystemProvider, contextConnect, useContextSystem, } from '../context'; function useUniqueId( idProp?: string ) { const instanceId = useInstanceId( InputBase ); const id = `input-base-control-${ instanceId }`; return idProp || id; } // Adapter to map props for the new ui/flex component. function getUIFlexProps( labelPosition?: LabelPosition ) { const props: { direction?: string; gap?: number; justify?: string; expanded?: boolean; } = {}; switch ( labelPosition ) { case 'top': props.direction = 'column'; props.expanded = false; props.gap = 0; break; case 'bottom': props.direction = 'column-reverse'; props.expanded = false; props.gap = 0; break; case 'edge': props.justify = 'space-between'; break; } return props; } function InputBase( props: WordPressComponentProps< InputBaseProps, 'div' >, ref: ForwardedRef< HTMLDivElement > ) { const { __unstableInputWidth, children, className, disabled = false, hideLabelFromVision = false, labelPosition, id: idProp, isBorderless = false, label, prefix, size = 'default', suffix, ...restProps } = useContextSystem( props, 'InputBase' ); const id = useUniqueId( idProp ); const hideLabel = hideLabelFromVision || ! label; const prefixSuffixContextValue = useMemo( () => { return { InputControlPrefixWrapper: { size }, InputControlSuffixWrapper: { size }, }; }, [ size ] ); return ( // @ts-expect-error The `direction` prop from Flex (FlexDirection) conflicts with legacy SVGAttributes `direction` (string) that come from React intrinsic prop definitions. <Root { ...restProps } { ...getUIFlexProps( labelPosition ) } className={ className } gap={ 2 } ref={ ref } > <Label className="components-input-control__label" hideLabelFromVision={ hideLabelFromVision } labelPosition={ labelPosition } htmlFor={ id } > { label } </Label> <Container __unstableInputWidth={ __unstableInputWidth } className="components-input-control__container" disabled={ disabled } hideLabel={ hideLabel } labelPosition={ labelPosition } > <ContextSystemProvider value={ prefixSuffixContextValue }> { prefix && ( <Prefix className="components-input-control__prefix"> { prefix } </Prefix> ) } { children } { suffix && ( <Suffix className="components-input-control__suffix"> { suffix } </Suffix> ) } </ContextSystemProvider> <Backdrop disabled={ disabled } isBorderless={ isBorderless } /> </Container> </Root> ); } /** * `InputBase` is an internal component used to style the standard borders for an input, * as well as handle the layout for prefix/suffix elements. */ export default contextConnect( InputBase, 'InputBase' );