@wordpress/components
Version:
UI components for WordPress.
79 lines (69 loc) • 2.07 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import { createElement } from "@wordpress/element";
/**
* Internal dependencies
*/
import UnitControl from './unit-control';
import { LABELS, applyValueToSides, getAllValue, isValuesMixed, isValuesDefined } from './utils';
const noop = () => {};
export default function AllInputControl(_ref) {
let {
onChange = noop,
onFocus = noop,
onHoverOn = noop,
onHoverOff = noop,
values,
sides,
selectedUnits,
setSelectedUnits,
...props
} = _ref;
const allValue = getAllValue(values, selectedUnits, sides);
const hasValues = isValuesDefined(values);
const isMixed = hasValues && isValuesMixed(values, selectedUnits, sides);
const allPlaceholder = isMixed ? LABELS.mixed : undefined;
const handleOnFocus = event => {
onFocus(event, {
side: 'all'
});
};
const handleOnChange = next => {
const isNumeric = next !== undefined && !isNaN(parseFloat(next));
const nextValue = isNumeric ? next : undefined;
const nextValues = applyValueToSides(values, nextValue, sides);
onChange(nextValues);
}; // Set selected unit so it can be used as fallback by unlinked controls
// when individual sides do not have a value containing a unit.
const handleOnUnitChange = unit => {
const newUnits = applyValueToSides(selectedUnits, unit, sides);
setSelectedUnits(newUnits);
};
const handleOnHoverOn = () => {
onHoverOn({
top: true,
bottom: true,
left: true,
right: true
});
};
const handleOnHoverOff = () => {
onHoverOff({
top: false,
bottom: false,
left: false,
right: false
});
};
return createElement(UnitControl, _extends({}, props, {
disableUnits: isMixed,
isOnly: true,
value: allValue,
onChange: handleOnChange,
onUnitChange: handleOnUnitChange,
onFocus: handleOnFocus,
onHoverOn: handleOnHoverOn,
onHoverOff: handleOnHoverOff,
placeholder: allPlaceholder
}));
}
//# sourceMappingURL=all-input-control.js.map