@ariakit/react-core
Version:
Ariakit React core
157 lines (128 loc) • 4.56 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true});
var _WZB3FBKIcjs = require('./WZB3FBKI.cjs');
var _YTEJF6TYcjs = require('./YTEJF6TY.cjs');
var _25BPIGZHcjs = require('./25BPIGZH.cjs');
var _WULEED4Qcjs = require('./WULEED4Q.cjs');
var _OZM4QA2Vcjs = require('./OZM4QA2V.cjs');
var _7EQBAZ46cjs = require('./7EQBAZ46.cjs');
// src/radio/radio.tsx
var _misc = require('@ariakit/core/utils/misc');
var _react = require('react');
var TagName = "input";
function getIsChecked(value, storeValue) {
if (storeValue === void 0) return;
if (value != null && storeValue != null) {
return storeValue === value;
}
return !!storeValue;
}
function isNativeRadio(tagName, type) {
return tagName === "input" && (!type || type === "radio");
}
var useRadio = _WULEED4Qcjs.createHook.call(void 0, function useRadio2(_a) {
var _b = _a, {
store,
name,
value,
checked
} = _b, props = _7EQBAZ46cjs.__objRest.call(void 0, _b, [
"store",
"name",
"value",
"checked"
]);
const context = _WZB3FBKIcjs.useRadioContext.call(void 0, );
store = store || context;
const id = _OZM4QA2Vcjs.useId.call(void 0, props.id);
const ref = _react.useRef.call(void 0, null);
const isChecked = _25BPIGZHcjs.useStoreState.call(void 0,
store,
(state) => checked != null ? checked : getIsChecked(value, state == null ? void 0 : state.value)
);
_react.useEffect.call(void 0, () => {
if (!id) return;
if (!isChecked) return;
const isActiveItem = (store == null ? void 0 : store.getState().activeId) === id;
if (isActiveItem) return;
store == null ? void 0 : store.setActiveId(id);
}, [store, isChecked, id]);
const onChangeProp = props.onChange;
const tagName = _OZM4QA2Vcjs.useTagName.call(void 0, ref, TagName);
const nativeRadio = isNativeRadio(tagName, props.type);
const disabled = _misc.disabledFromProps.call(void 0, props);
const [propertyUpdated, schedulePropertyUpdate] = _OZM4QA2Vcjs.useForceUpdate.call(void 0, );
_react.useEffect.call(void 0, () => {
const element = ref.current;
if (!element) return;
if (nativeRadio) return;
if (isChecked !== void 0) {
element.checked = isChecked;
}
if (name !== void 0) {
element.name = name;
}
if (value !== void 0) {
element.value = `${value}`;
}
}, [propertyUpdated, nativeRadio, isChecked, name, value]);
const onChange = _OZM4QA2Vcjs.useEvent.call(void 0, (event) => {
if (disabled) {
event.preventDefault();
event.stopPropagation();
return;
}
if ((store == null ? void 0 : store.getState().value) === value) return;
if (!nativeRadio) {
event.currentTarget.checked = true;
schedulePropertyUpdate();
}
onChangeProp == null ? void 0 : onChangeProp(event);
if (event.defaultPrevented) return;
store == null ? void 0 : store.setValue(value);
});
const onClickProp = props.onClick;
const onClick = _OZM4QA2Vcjs.useEvent.call(void 0, (event) => {
onClickProp == null ? void 0 : onClickProp(event);
if (event.defaultPrevented) return;
if (nativeRadio) return;
onChange(event);
});
const onFocusProp = props.onFocus;
const onFocus = _OZM4QA2Vcjs.useEvent.call(void 0, (event) => {
onFocusProp == null ? void 0 : onFocusProp(event);
if (event.defaultPrevented) return;
if (!nativeRadio) return;
if (!store) return;
const { moves, activeId } = store.getState();
if (!moves) return;
if (id && activeId !== id) return;
onChange(event);
});
props = _7EQBAZ46cjs.__spreadProps.call(void 0, _7EQBAZ46cjs.__spreadValues.call(void 0, {
id,
role: !nativeRadio ? "radio" : void 0,
type: nativeRadio ? "radio" : void 0,
"aria-checked": isChecked
}, props), {
ref: _OZM4QA2Vcjs.useMergeRefs.call(void 0, ref, props.ref),
onChange,
onClick,
onFocus
});
props = _YTEJF6TYcjs.useCompositeItem.call(void 0, _7EQBAZ46cjs.__spreadValues.call(void 0, {
store,
clickOnEnter: !nativeRadio
}, props));
return _misc.removeUndefinedValues.call(void 0, _7EQBAZ46cjs.__spreadValues.call(void 0, {
name: nativeRadio ? name : void 0,
value: nativeRadio ? value : void 0,
checked: isChecked
}, props));
});
var Radio = _WULEED4Qcjs.memo.call(void 0,
_WULEED4Qcjs.forwardRef.call(void 0, function Radio2(props) {
const htmlProps = useRadio(props);
return _WULEED4Qcjs.createElement.call(void 0, TagName, htmlProps);
})
);
exports.useRadio = useRadio; exports.Radio = Radio;