@ariakit/react-core
Version:
Ariakit React core
96 lines (75 loc) • 3.3 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true});
var _MIBORXQWcjs = require('../__chunks/MIBORXQW.cjs');
var _WULEED4Qcjs = require('../__chunks/WULEED4Q.cjs');
var _OZM4QA2Vcjs = require('../__chunks/OZM4QA2V.cjs');
require('../__chunks/FDRJDQ5Y.cjs');
var _7EQBAZ46cjs = require('../__chunks/7EQBAZ46.cjs');
// src/composite/composite-input.tsx
var _dom = require('@ariakit/core/utils/dom');
var _misc = require('@ariakit/core/utils/misc');
var _react = require('react');
var TagName = "input";
function getValueLength(element) {
if (_dom.isTextField.call(void 0, element)) {
return element.value.length;
} else if (element.isContentEditable) {
const range = _dom.getDocument.call(void 0, element).createRange();
range.selectNodeContents(element);
return range.toString().length;
}
return 0;
}
var useCompositeInput = _WULEED4Qcjs.createHook.call(void 0,
function useCompositeInput2(_a) {
var _b = _a, { store } = _b, props = _7EQBAZ46cjs.__objRest.call(void 0, _b, ["store"]);
const onKeyDownCaptureProp = props.onKeyDownCapture;
if (process.env.NODE_ENV !== "production") {
_react.useEffect.call(void 0, () => {
console.warn(
"CompositeInput is deprecated. Use `<CompositeItem render={<input />}>` instead."
);
}, []);
}
const onKeyDownCapture = _OZM4QA2Vcjs.useEvent.call(void 0, (event) => {
onKeyDownCaptureProp == null ? void 0 : onKeyDownCaptureProp(event);
if (event.defaultPrevented) return;
const element = event.currentTarget;
if (!element.isContentEditable && !_dom.isTextField.call(void 0, element)) return;
const selection = _dom.getTextboxSelection.call(void 0, element);
const { orientation } = (store == null ? void 0 : store.getState()) || {};
const isHorizontal = orientation !== "vertical";
const isVertical = orientation !== "horizontal";
const isLeft = isHorizontal && event.key === "ArrowLeft";
const isRight = isHorizontal && event.key === "ArrowRight";
const isUp = isVertical && event.key === "ArrowUp";
const isDown = isVertical && event.key === "ArrowDown";
if (isRight || isDown) {
if (selection.end !== getValueLength(element)) {
event.stopPropagation();
}
} else if (isLeft || isUp) {
if (selection.start !== 0) {
event.stopPropagation();
}
}
});
const onFocusProp = props.onFocus;
const onFocus = _OZM4QA2Vcjs.useEvent.call(void 0, (event) => {
onFocusProp == null ? void 0 : onFocusProp(event);
if (event.defaultPrevented) return;
_MIBORXQWcjs.selectTextField.call(void 0, event.currentTarget);
});
props = _7EQBAZ46cjs.__spreadProps.call(void 0, _7EQBAZ46cjs.__spreadValues.call(void 0, {}, props), {
onKeyDownCapture,
onFocus
});
return _misc.removeUndefinedValues.call(void 0, props);
}
);
var CompositeInput = _WULEED4Qcjs.memo.call(void 0,
_WULEED4Qcjs.forwardRef.call(void 0, function CompositeInput2(props) {
const htmlProps = useCompositeInput(props);
return _WULEED4Qcjs.createElement.call(void 0, TagName, htmlProps);
})
);
exports.CompositeInput = CompositeInput; exports.useCompositeInput = useCompositeInput;