@ariakit/react
Version:
Toolkit for building accessible web apps with React
186 lines (150 loc) • 6.51 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true});
var _X4U3XKYQcjs = require('./X4U3XKYQ.cjs');
var _5FQZLBJ7cjs = require('./5FQZLBJ7.cjs');
var _LF4ENSO4cjs = require('./LF4ENSO4.cjs');
var _LZ6L3ECGcjs = require('./LZ6L3ECG.cjs');
// ../ariakit-react-components/dist/checkbox/checkbox-context.js
var ctx = _LZ6L3ECGcjs.createStoreContext.call(void 0, );
var useCheckboxContext = ctx.useContext;
var useCheckboxScopedContext = ctx.useScopedContext;
var useCheckboxProviderContext = ctx.useProviderContext;
var CheckboxContextProvider = ctx.ContextProvider;
var CheckboxScopedContextProvider = ctx.ScopedContextProvider;
// ../ariakit-react-components/dist/__chunks/Bz1czXRN.js
var _react = require('react');
var _jsxruntime = require('react/jsx-runtime');
function getPrimitiveValue(value) {
if (Array.isArray(value)) return value.toString();
return value;
}
var TagName = "input";
function setMixed(element, mixed) {
if (mixed) element.indeterminate = true;
else if (element.indeterminate) element.indeterminate = false;
}
function isNativeCheckbox(tagName, type) {
return tagName === "input" && (!type || type === "checkbox");
}
var useCheckbox = _LZ6L3ECGcjs.createHook.call(void 0, function useCheckbox2({ store, name, value: valueProp, checked: checkedProp, defaultChecked, ...props }) {
const context = useCheckboxContext();
store = store || context;
const [_checked, setChecked] = _react.useState.call(void 0, defaultChecked != null ? defaultChecked : false);
const checked = _5FQZLBJ7cjs.useStoreState.call(void 0, store, (state) => {
if (checkedProp !== void 0) return checkedProp;
if ((state == null ? void 0 : state.value) === void 0) return _checked;
if (valueProp != null) {
if (Array.isArray(state.value)) {
const primitiveValue = getPrimitiveValue(valueProp);
return state.value.includes(primitiveValue);
}
return state.value === valueProp;
}
if (Array.isArray(state.value)) return false;
if (typeof state.value === "boolean") return state.value;
return false;
});
const ref = _react.useRef.call(void 0, null);
const nativeCheckbox = isNativeCheckbox(_LZ6L3ECGcjs.useTagName.call(void 0, ref, TagName), props.type);
const mixed = checked ? checked === "mixed" : void 0;
const isChecked = checked === "mixed" ? false : checked;
const disabled = _LZ6L3ECGcjs.disabledFromProps.call(void 0, props);
const [propertyUpdated, schedulePropertyUpdate] = _LZ6L3ECGcjs.useForceUpdate.call(void 0, );
_react.useEffect.call(void 0, () => {
const element = ref.current;
if (!element) return;
setMixed(element, mixed);
if (nativeCheckbox) return;
element.checked = isChecked;
if (name !== void 0) element.name = name;
if (valueProp !== void 0) element.value = String(valueProp);
}, [
propertyUpdated,
mixed,
nativeCheckbox,
isChecked,
name,
valueProp
]);
const onChangeProp = props.onChange;
const onChange = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
if (disabled) {
event.stopPropagation();
event.preventDefault();
return;
}
setMixed(event.currentTarget, mixed);
if (!nativeCheckbox) {
event.currentTarget.checked = !event.currentTarget.checked;
schedulePropertyUpdate();
}
onChangeProp == null ? void 0 : onChangeProp(event);
if (event.defaultPrevented) return;
const elementChecked = event.currentTarget.checked;
setChecked(elementChecked);
store == null ? void 0 : store.setValue((prevValue) => {
if (valueProp == null) return elementChecked;
const primitiveValue = getPrimitiveValue(valueProp);
if (!Array.isArray(prevValue)) return prevValue === primitiveValue ? false : primitiveValue;
if (elementChecked) {
if (prevValue.includes(primitiveValue)) return prevValue;
return [...prevValue, primitiveValue];
}
return prevValue.filter((v) => v !== primitiveValue);
});
});
const onClickProp = props.onClick;
const onClick = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
onClickProp == null ? void 0 : onClickProp(event);
if (event.defaultPrevented) return;
if (nativeCheckbox) return;
onChange(event);
});
props = _LZ6L3ECGcjs.useWrapElement.call(void 0, props, (element) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _X4U3XKYQcjs.CheckboxCheckedContext.Provider, {
value: isChecked,
children: element
}), [isChecked]);
props = {
role: !nativeCheckbox ? "checkbox" : void 0,
type: nativeCheckbox ? "checkbox" : void 0,
"aria-checked": checked,
...props,
ref: _LZ6L3ECGcjs.useMergeRefs.call(void 0, ref, props.ref),
onChange,
onClick
};
props = _LF4ENSO4cjs.useCommand.call(void 0, {
clickOnEnter: !nativeCheckbox,
...props
});
return _LZ6L3ECGcjs.removeUndefinedValues.call(void 0, {
name: nativeCheckbox ? name : void 0,
value: nativeCheckbox ? valueProp : void 0,
checked: isChecked,
...props
});
});
var Checkbox = _LZ6L3ECGcjs.forwardRef.call(void 0, function Checkbox2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName, useCheckbox(props));
});
// ../ariakit-components/dist/checkbox/checkbox-store.js
function createCheckboxStore(props = {}) {
var _a;
_5FQZLBJ7cjs.throwOnConflictingProps.call(void 0, props, props.store);
const syncState = (_a = props.store) == null ? void 0 : _a.getState();
const checkbox = _5FQZLBJ7cjs.createStore.call(void 0, { value: _LZ6L3ECGcjs.defaultValue.call(void 0, props.value, syncState == null ? void 0 : syncState.value, props.defaultValue, false) }, props.store);
return {
...checkbox,
setValue: (value) => checkbox.setState("value", value)
};
}
// ../ariakit-react-components/dist/checkbox/checkbox-store.js
function useCheckboxStoreProps(store, update, props) {
_LZ6L3ECGcjs.useUpdateEffect.call(void 0, update, [props.store]);
_5FQZLBJ7cjs.useStoreProps.call(void 0, store, props, "value", "setValue");
return store;
}
function useCheckboxStore(props = {}) {
const [store, update] = _5FQZLBJ7cjs.useStore.call(void 0, createCheckboxStore, props);
return useCheckboxStoreProps(store, update, props);
}
exports.useCheckboxContext = useCheckboxContext; exports.CheckboxContextProvider = CheckboxContextProvider; exports.getPrimitiveValue = getPrimitiveValue; exports.useCheckbox = useCheckbox; exports.Checkbox = Checkbox; exports.useCheckboxStore = useCheckboxStore;