UNPKG

@stratakit/bricks

Version:

Small, modular components for StrataKit

45 lines (44 loc) 1.52 kB
import { jsx } from "react/jsx-runtime"; import * as React from "react"; import { Collection, useCollectionStore } from "@ariakit/react/collection"; import { useStoreState } from "@ariakit/react/store"; function FieldCollection(props) { const fieldElementCollection = useCollectionStore({ defaultItems: [] }); const renderedItems = useStoreState(fieldElementCollection, "renderedItems"); const [controlType, controlIndex] = React.useMemo(() => { const controlIndex2 = renderedItems.findIndex( (item) => item.elementType === "control" ); return [renderedItems[controlIndex2]?.controlType, controlIndex2]; }, [renderedItems]); const labelPlacement = React.useMemo(() => { const labelIndex = renderedItems.findIndex( (item) => item.elementType === "label" ); if (controlIndex === -1 || labelIndex === -1) return; return labelIndex < controlIndex ? "before" : "after"; }, [renderedItems, controlIndex]); return /* @__PURE__ */ jsx( Collection, { ...props, store: fieldElementCollection, "data-kiwi-label-placement": labelPlacement, "data-kiwi-control-type": controlType } ); } const FieldControlTypeContext = React.createContext(void 0); function useFieldControlType(controlType) { const setControlType = React.useContext(FieldControlTypeContext); React.useEffect(() => { setControlType?.(controlType); }, [controlType, setControlType]); } export { FieldCollection, FieldControlTypeContext, useFieldControlType };