@ozen-ui/kit
Version:
React component library
41 lines (40 loc) • 1.5 kB
JavaScript
import { useEffect } from 'react';
import { useDataListBaseContext } from '../../DataListBaseContext';
export var useAsDataListBaseItem = function (_a) {
var optionId = _a.optionId, ref = _a.ref, _b = _a.disabled, disabledProp = _b === void 0 ? false : _b;
var _c = useDataListBaseContext(), optionsControl = _c.optionsControl, optionsStore = _c.optionsStore, valueControl = _c.valueControl, active = _c.active, globalDisabled = _c.disabled;
var disabled = disabledProp || globalDisabled;
var option = {
id: optionId,
disabled: disabled,
ref: ref,
};
var onClick = function (event) {
if (!active || disabled) {
return;
}
var newFocused = option.id === optionsStore.focused.current ? option.id : null;
optionsStore.setFocused(newFocused);
optionsStore.setCurrentOption(option.id);
valueControl.setValue(event, option.id);
};
optionsStore.registerOption(option);
useEffect(function () { return function () {
optionsStore.unregisterOption(option);
}; }, []);
var select = function () {
if (disabled) {
return;
}
valueControl.setValue(null, option.id);
};
var isSelected = optionsControl.isSelected(option);
var isFocused = optionsControl.isFocused(option);
return {
select: select,
selected: isSelected,
focused: isFocused,
onClick: onClick,
disabled: disabled,
};
};