@base-ui-components/react
Version:
Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.
57 lines (56 loc) • 2.69 kB
JavaScript
import { createSelector } from '@base-ui-components/utils/store';
import { compareItemEquality } from "../utils/itemEquality.js";
import { stringifyAsValue } from "../utils/resolveValueLabel.js";
export const selectors = {
id: createSelector(state => state.id),
modal: createSelector(state => state.modal),
multiple: createSelector(state => state.multiple),
items: createSelector(state => state.items),
itemToStringLabel: createSelector(state => state.itemToStringLabel),
itemToStringValue: createSelector(state => state.itemToStringValue),
isItemEqualToValue: createSelector(state => state.isItemEqualToValue),
value: createSelector(state => state.value),
open: createSelector(state => state.open),
mounted: createSelector(state => state.mounted),
forceMount: createSelector(state => state.forceMount),
transitionStatus: createSelector(state => state.transitionStatus),
touchModality: createSelector(state => state.touchModality),
activeIndex: createSelector(state => state.activeIndex),
selectedIndex: createSelector(state => state.selectedIndex),
isActive: createSelector((state, index) => state.activeIndex === index),
isSelected: createSelector((state, index, candidate) => {
const comparer = state.isItemEqualToValue;
const storeValue = state.value;
if (state.multiple) {
return Array.isArray(storeValue) && storeValue.some(item => compareItemEquality(item, candidate, comparer));
}
// `selectedIndex` is only updated after the items mount for the first time,
// the value check avoids a re-render for the initially selected item.
if (state.selectedIndex === index && state.selectedIndex !== null) {
return true;
}
return compareItemEquality(storeValue, candidate, comparer);
}),
isSelectedByFocus: createSelector((state, index) => {
return state.selectedIndex === index;
}),
popupProps: createSelector(state => state.popupProps),
triggerProps: createSelector(state => state.triggerProps),
triggerElement: createSelector(state => state.triggerElement),
positionerElement: createSelector(state => state.positionerElement),
listElement: createSelector(state => state.listElement),
scrollUpArrowVisible: createSelector(state => state.scrollUpArrowVisible),
scrollDownArrowVisible: createSelector(state => state.scrollDownArrowVisible),
hasScrollArrows: createSelector(state => state.hasScrollArrows),
serializedValue: createSelector(state => {
const {
multiple,
value,
itemToStringValue
} = state;
if (multiple && Array.isArray(value) && value.length === 0) {
return '';
}
return stringifyAsValue(value, itemToStringValue);
})
};