UNPKG

@wordpress/components

Version:
138 lines (115 loc) 3.87 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = BoxInputControls; var _element = require("@wordpress/element"); var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _unitControl = _interopRequireDefault(require("./unit-control")); var _utils = require("../unit-control/utils"); var _utils2 = require("./utils"); var _boxControlStyles = require("./styles/box-control-styles"); /** * Internal dependencies */ const noop = () => {}; function BoxInputControls(_ref) { let { onChange = noop, onFocus = noop, onHoverOn = noop, onHoverOff = noop, values, selectedUnits, setSelectedUnits, sides, ...props } = _ref; const createHandleOnFocus = side => event => { onFocus(event, { side }); }; const createHandleOnHoverOn = side => () => { onHoverOn({ [side]: true }); }; const createHandleOnHoverOff = side => () => { onHoverOff({ [side]: false }); }; const handleOnChange = nextValues => { onChange(nextValues); }; const createHandleOnChange = side => (next, _ref2) => { let { event } = _ref2; const nextValues = { ...values }; const isNumeric = next !== undefined && !isNaN(parseFloat(next)); const nextValue = isNumeric ? next : undefined; nextValues[side] = nextValue; /** * Supports changing pair sides. For example, holding the ALT key * when changing the TOP will also update BOTTOM. */ // @ts-expect-error - TODO: event.altKey is only present when the change event was // triggered by a keyboard event. Should this feature be implemented differently so // it also works with drag events? if (event.altKey) { switch (side) { case 'top': nextValues.bottom = nextValue; break; case 'bottom': nextValues.top = nextValue; break; case 'left': nextValues.right = nextValue; break; case 'right': nextValues.left = nextValue; break; } } handleOnChange(nextValues); }; const createHandleOnUnitChange = side => next => { const newUnits = { ...selectedUnits }; newUnits[side] = next; setSelectedUnits(newUnits); }; // Filter sides if custom configuration provided, maintaining default order. const filteredSides = sides !== null && sides !== void 0 && sides.length ? _utils2.ALL_SIDES.filter(side => sides.includes(side)) : _utils2.ALL_SIDES; const first = filteredSides[0]; const last = filteredSides[filteredSides.length - 1]; const only = first === last && first; return (0, _element.createElement)(_boxControlStyles.LayoutContainer, { className: "component-box-control__input-controls-wrapper" }, (0, _element.createElement)(_boxControlStyles.Layout, { gap: 0, align: "top", className: "component-box-control__input-controls" }, filteredSides.map(side => { const [parsedQuantity, parsedUnit] = (0, _utils.parseQuantityAndUnitFromRawValue)(values[side]); const computedUnit = values[side] ? parsedUnit : selectedUnits[side]; return (0, _element.createElement)(_unitControl.default, (0, _extends2.default)({}, props, { isFirst: first === side, isLast: last === side, isOnly: only === side, value: [parsedQuantity, computedUnit].join(''), onChange: createHandleOnChange(side), onUnitChange: createHandleOnUnitChange(side), onFocus: createHandleOnFocus(side), onHoverOn: createHandleOnHoverOn(side), onHoverOff: createHandleOnHoverOff(side), label: _utils2.LABELS[side], key: `box-control-${side}` })); }))); } //# sourceMappingURL=input-controls.js.map