UNPKG

@wordpress/components

Version:
194 lines (187 loc) 4.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = require("@use-gesture/react"); var _element = require("@wordpress/element"); var _utils = require("./utils"); var _inputControlStyles = require("./styles/input-control-styles"); var _reducer = require("./reducer/reducer"); var _withIgnoreImeEvents = require("../utils/with-ignore-ime-events"); var _jsxRuntime = require("react/jsx-runtime"); /** * External dependencies */ /** * WordPress dependencies */ /** * Internal dependencies */ const noop = () => {}; function InputField({ disabled = false, dragDirection = 'n', dragThreshold = 10, id, isDragEnabled = false, isPressEnterToChange = false, onBlur = noop, onChange = noop, onDrag = noop, onDragEnd = noop, onDragStart = noop, onKeyDown = noop, onValidate = noop, size = 'default', stateReducer = state => state, value: valueProp, type, ...props }, ref) { const { // State. state, // Actions. change, commit, drag, dragEnd, dragStart, invalidate, pressDown, pressEnter, pressUp, reset } = (0, _reducer.useInputControlStateReducer)(stateReducer, { isDragEnabled, value: valueProp, isPressEnterToChange }, onChange); const { value, isDragging, isDirty } = state; const wasDirtyOnBlur = (0, _element.useRef)(false); const dragCursor = (0, _utils.useDragCursor)(isDragging, dragDirection); const handleOnBlur = event => { onBlur(event); /** * If isPressEnterToChange is set, this commits the value to * the onChange callback. */ if (isDirty || !event.target.validity.valid) { wasDirtyOnBlur.current = true; handleOnCommit(event); } }; const handleOnChange = event => { const nextValue = event.target.value; change(nextValue, event); }; const handleOnCommit = event => { const nextValue = event.currentTarget.value; try { onValidate(nextValue); commit(nextValue, event); } catch (err) { invalidate(err, event); } }; const handleOnKeyDown = event => { const { key } = event; onKeyDown(event); switch (key) { case 'ArrowUp': pressUp(event); break; case 'ArrowDown': pressDown(event); break; case 'Enter': pressEnter(event); if (isPressEnterToChange) { event.preventDefault(); handleOnCommit(event); } break; case 'Escape': if (isPressEnterToChange && isDirty) { event.preventDefault(); reset(valueProp, event); } break; } }; const dragGestureProps = (0, _react.useDrag)(dragProps => { const { distance, dragging, event, target } = dragProps; // The `target` prop always references the `input` element while, by // default, the `dragProps.event.target` property would reference the real // event target (i.e. any DOM element that the pointer is hovering while // dragging). Ensuring that the `target` is always the `input` element // allows consumers of `InputControl` (or any higher-level control) to // check the input's validity by accessing `event.target.validity.valid`. dragProps.event = { ...dragProps.event, target }; if (!distance) { return; } event.stopPropagation(); /** * Quick return if no longer dragging. * This prevents unnecessary value calculations. */ if (!dragging) { onDragEnd(dragProps); dragEnd(dragProps); return; } onDrag(dragProps); drag(dragProps); if (!isDragging) { onDragStart(dragProps); dragStart(dragProps); } }, { axis: dragDirection === 'e' || dragDirection === 'w' ? 'x' : 'y', threshold: dragThreshold, enabled: isDragEnabled, pointer: { capture: false } }); const dragProps = isDragEnabled ? dragGestureProps() : {}; return /*#__PURE__*/(0, _jsxRuntime.jsx)(_inputControlStyles.Input, { ...props, ...dragProps, className: "components-input-control__input", disabled: disabled, dragCursor: dragCursor, isDragging: isDragging, id: id, onBlur: handleOnBlur, onChange: handleOnChange, onKeyDown: (0, _withIgnoreImeEvents.withIgnoreIMEEvents)(handleOnKeyDown), ref: ref, inputSize: size // Fallback to `''` to avoid "uncontrolled to controlled" warning. // See https://github.com/WordPress/gutenberg/pull/47250 for details. , value: value !== null && value !== void 0 ? value : '', type: type }); } const ForwardedComponent = (0, _element.forwardRef)(InputField); var _default = exports.default = ForwardedComponent; //# sourceMappingURL=input-field.js.map