UNPKG

@wordpress/components

Version:
84 lines (83 loc) • 2.71 kB
import clsx from "clsx"; import { useInstanceId } from "@wordpress/compose"; import { forwardRef } from "@wordpress/element"; import InputBase from "./input-base.mjs"; import InputField from "./input-field.mjs"; import { useDraft } from "./utils.mjs"; import BaseControl from "../base-control/index.mjs"; import { jsx } from "react/jsx-runtime"; // packages/components/src/input-control/index.tsx const noop = () => {}; function useUniqueId(idProp) { const id = `inspector-input-control-${useInstanceId(InputControl)}`; return idProp || id; } function UnforwardedInputControl(props, ref) { const { __next40pxDefaultSize: _next40pxDefaultSize, __next36pxDefaultSize: _next36pxDefaultSize, __unstableStateReducer: stateReducer = (state) => state, __unstableInputWidth, className, disabled = false, help, hideLabelFromVision = false, id: idProp, isPressEnterToChange = false, label, labelPosition = "top", onChange = noop, onValidate = noop, onKeyDown = noop, prefix, size = "default", style, suffix, value, ...restProps } = props; const id = useUniqueId(idProp); const classes = clsx("components-input-control", className); const draftHookProps = useDraft({ value, onBlur: restProps.onBlur, onChange }); const helpProp = !!help ? { "aria-describedby": `${id}__help` } : {}; return /* @__PURE__ */ jsx(BaseControl, { className: classes, help, id, children: /* @__PURE__ */ jsx(InputBase, { __unstableInputWidth, disabled, gap: 3, hideLabelFromVision, id, justify: "left", label, labelPosition, prefix, size, style, suffix, children: /* @__PURE__ */ jsx(InputField, { ...restProps, ...helpProp, className: "components-input-control__input", disabled, id, isPressEnterToChange, onKeyDown, onValidate, paddingInlineStart: prefix ? "var(--wpds-dimension-padding-xs, 4px)" : void 0, paddingInlineEnd: suffix ? "var(--wpds-dimension-padding-xs, 4px)" : void 0, ref, size, stateReducer, ...draftHookProps }) }) }); } /** * InputControl components let users enter and edit text. This is an experimental component * intended to (in time) merge with or replace `TextControl`. * * ```jsx * import { __experimentalInputControl as InputControl } from '@wordpress/components'; * import { useState } from 'react'; * * const Example = () => { * const [ value, setValue ] = useState( '' ); * * return ( * <InputControl * value={ value } * onChange={ ( nextValue ) => setValue( nextValue ?? '' ) } * /> * ); * }; * ``` */ const InputControl = forwardRef(UnforwardedInputControl); export { InputControl, InputControl as default, UnforwardedInputControl }; //# sourceMappingURL=index.mjs.map