UNPKG

@gechiui/components

Version:
123 lines (108 loc) 3.13 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import { createElement } from "@gechiui/element"; /** * External dependencies */ import { noop } from 'lodash'; /** * Internal dependencies */ import UnitControl from './unit-control'; import { ALL_SIDES, LABELS } from './utils'; import { LayoutContainer, Layout } from './styles/box-control-styles'; export default 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 { altKey } = event; const nextValues = { ...values }; const isNumeric = !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. */ if (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 ? ALL_SIDES.filter(side => sides.includes(side)) : ALL_SIDES; const first = filteredSides[0]; const last = filteredSides[filteredSides.length - 1]; const only = first === last && first; return createElement(LayoutContainer, { className: "component-box-control__input-controls-wrapper" }, createElement(Layout, { gap: 0, align: "top", className: "component-box-control__input-controls" }, filteredSides.map(side => createElement(UnitControl, _extends({}, props, { isFirst: first === side, isLast: last === side, isOnly: only === side, value: values[side], unit: values[side] ? undefined : selectedUnits[side], onChange: createHandleOnChange(side), onUnitChange: createHandleOnUnitChange(side), onFocus: createHandleOnFocus(side), onHoverOn: createHandleOnHoverOn(side), onHoverOff: createHandleOnHoverOff(side), label: LABELS[side], key: `box-control-${side}` }))))); } //# sourceMappingURL=input-controls.js.map