@ariakit/core
Version:
Ariakit core
173 lines (148 loc) • 6.26 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true});
var _WOLZY4WCcjs = require('../__chunks/WOLZY4WC.cjs');
var _BVPEDDW6cjs = require('../__chunks/BVPEDDW6.cjs');
var _RNIVVQWXcjs = require('../__chunks/RNIVVQWX.cjs');
require('../__chunks/GDZQUFNP.cjs');
require('../__chunks/YMCSKJHR.cjs');
require('../__chunks/V4YF2QA6.cjs');
require('../__chunks/MNBBM5CR.cjs');
require('../__chunks/726BNPQZ.cjs');
var _ASR6PAFNcjs = require('../__chunks/ASR6PAFN.cjs');
var _MLCPLY2Pcjs = require('../__chunks/MLCPLY2P.cjs');
var _7EQBAZ46cjs = require('../__chunks/7EQBAZ46.cjs');
// src/combobox/combobox-store.ts
var isTouchSafari = _WOLZY4WCcjs.isSafari.call(void 0, ) && _WOLZY4WCcjs.isTouchDevice.call(void 0, );
function createComboboxStore(_a = {}) {
var _b = _a, {
tag
} = _b, props = _7EQBAZ46cjs.__objRest.call(void 0, _b, [
"tag"
]);
const store = _ASR6PAFNcjs.mergeStore.call(void 0, props.store, _ASR6PAFNcjs.pick.call(void 0, tag, ["value", "rtl"]));
_ASR6PAFNcjs.throwOnConflictingProps.call(void 0, props, store);
const tagState = tag == null ? void 0 : tag.getState();
const syncState = store == null ? void 0 : store.getState();
const activeId = _MLCPLY2Pcjs.defaultValue.call(void 0,
props.activeId,
syncState == null ? void 0 : syncState.activeId,
props.defaultActiveId,
null
);
const composite = _RNIVVQWXcjs.createCompositeStore.call(void 0, _7EQBAZ46cjs.__spreadProps.call(void 0, _7EQBAZ46cjs.__spreadValues.call(void 0, {}, props), {
activeId,
includesBaseElement: _MLCPLY2Pcjs.defaultValue.call(void 0,
props.includesBaseElement,
syncState == null ? void 0 : syncState.includesBaseElement,
true
),
orientation: _MLCPLY2Pcjs.defaultValue.call(void 0,
props.orientation,
syncState == null ? void 0 : syncState.orientation,
"vertical"
),
focusLoop: _MLCPLY2Pcjs.defaultValue.call(void 0, props.focusLoop, syncState == null ? void 0 : syncState.focusLoop, true),
focusWrap: _MLCPLY2Pcjs.defaultValue.call(void 0, props.focusWrap, syncState == null ? void 0 : syncState.focusWrap, true),
virtualFocus: _MLCPLY2Pcjs.defaultValue.call(void 0,
props.virtualFocus,
syncState == null ? void 0 : syncState.virtualFocus,
true
)
}));
const popover = _BVPEDDW6cjs.createPopoverStore.call(void 0, _7EQBAZ46cjs.__spreadProps.call(void 0, _7EQBAZ46cjs.__spreadValues.call(void 0, {}, props), {
placement: _MLCPLY2Pcjs.defaultValue.call(void 0,
props.placement,
syncState == null ? void 0 : syncState.placement,
"bottom-start"
)
}));
const value = _MLCPLY2Pcjs.defaultValue.call(void 0,
props.value,
syncState == null ? void 0 : syncState.value,
props.defaultValue,
""
);
const selectedValue = _MLCPLY2Pcjs.defaultValue.call(void 0,
props.selectedValue,
syncState == null ? void 0 : syncState.selectedValue,
tagState == null ? void 0 : tagState.values,
props.defaultSelectedValue,
""
);
const multiSelectable = Array.isArray(selectedValue);
const initialState = _7EQBAZ46cjs.__spreadProps.call(void 0, _7EQBAZ46cjs.__spreadValues.call(void 0, _7EQBAZ46cjs.__spreadValues.call(void 0, {}, composite.getState()), popover.getState()), {
value,
selectedValue,
resetValueOnSelect: _MLCPLY2Pcjs.defaultValue.call(void 0,
props.resetValueOnSelect,
syncState == null ? void 0 : syncState.resetValueOnSelect,
multiSelectable
),
resetValueOnHide: _MLCPLY2Pcjs.defaultValue.call(void 0,
props.resetValueOnHide,
syncState == null ? void 0 : syncState.resetValueOnHide,
multiSelectable && !tag
),
activeValue: syncState == null ? void 0 : syncState.activeValue
});
const combobox = _ASR6PAFNcjs.createStore.call(void 0, initialState, composite, popover, store);
if (isTouchSafari) {
_ASR6PAFNcjs.setup.call(void 0,
combobox,
() => _ASR6PAFNcjs.sync.call(void 0, combobox, ["virtualFocus"], () => {
combobox.setState("virtualFocus", false);
})
);
}
_ASR6PAFNcjs.setup.call(void 0, combobox, () => {
if (!tag) return;
return _MLCPLY2Pcjs.chain.call(void 0,
_ASR6PAFNcjs.sync.call(void 0, combobox, ["selectedValue"], (state) => {
if (!Array.isArray(state.selectedValue)) return;
tag.setValues(state.selectedValue);
}),
_ASR6PAFNcjs.sync.call(void 0, tag, ["values"], (state) => {
combobox.setState("selectedValue", state.values);
})
);
});
_ASR6PAFNcjs.setup.call(void 0,
combobox,
() => _ASR6PAFNcjs.sync.call(void 0, combobox, ["resetValueOnHide", "mounted"], (state) => {
if (!state.resetValueOnHide) return;
if (state.mounted) return;
combobox.setState("value", value);
})
);
_ASR6PAFNcjs.setup.call(void 0,
combobox,
() => _ASR6PAFNcjs.sync.call(void 0, combobox, ["open"], (state) => {
if (state.open) return;
combobox.setState("activeId", activeId);
combobox.setState("moves", 0);
})
);
_ASR6PAFNcjs.setup.call(void 0,
combobox,
() => _ASR6PAFNcjs.sync.call(void 0, combobox, ["moves", "activeId"], (state, prevState) => {
if (state.moves === prevState.moves) {
combobox.setState("activeValue", void 0);
}
})
);
_ASR6PAFNcjs.setup.call(void 0,
combobox,
() => _ASR6PAFNcjs.batch.call(void 0, combobox, ["moves", "renderedItems"], (state, prev) => {
if (state.moves === prev.moves) return;
const { activeId: activeId2 } = combobox.getState();
const activeItem = composite.item(activeId2);
combobox.setState("activeValue", activeItem == null ? void 0 : activeItem.value);
})
);
return _7EQBAZ46cjs.__spreadProps.call(void 0, _7EQBAZ46cjs.__spreadValues.call(void 0, _7EQBAZ46cjs.__spreadValues.call(void 0, _7EQBAZ46cjs.__spreadValues.call(void 0, {}, popover), composite), combobox), {
tag,
setValue: (value2) => combobox.setState("value", value2),
resetValue: () => combobox.setState("value", initialState.value),
setSelectedValue: (selectedValue2) => combobox.setState("selectedValue", selectedValue2)
});
}
exports.createComboboxStore = createComboboxStore;