UNPKG

@fruits-chain/react-native-xiaoshu

Version:
46 lines (44 loc) 1.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = require("react"); const isLastCol = (total, colCount, index) => { const totalCol = Math.ceil(total / colCount); const col = Math.ceil((index + 1) / colCount); return totalCol === col; }; const useImageLayout = () => { const [width, setWidth] = (0, _react.useState)(0); const Size = (0, _react.useRef)({}); const Margin = (0, _react.useRef)({}); const onLayout = (0, _react.useCallback)(e => { setWidth(Math.floor(e.nativeEvent.layout.width)); }, []); const getSize = (0, _react.useCallback)((colCount, colGap) => { const sizeKey = [width, colCount, colGap].join('_'); if (!Size.current[sizeKey]) { Size.current[sizeKey] = (width - (colCount - 1) * colGap) / colCount; } return Size.current[sizeKey]; }, [width]); /** * 获取当前的尺寸、边距 */ const getMargin = (0, _react.useCallback)((total, colCount, index) => { const cacheKey = [total, colCount, index].join('_'); if (!Margin.current[cacheKey]) { const marginRight = (index + 1) % colCount === 0 ? false : true; const marginBottom = !isLastCol(total, colCount, index); Margin.current[cacheKey] = { marginRight, marginBottom }; } return Margin.current[cacheKey]; }, []); return [onLayout, getSize, getMargin]; }; var _default = exports.default = useImageLayout; //# sourceMappingURL=useImageLayout.js.map