UNPKG

chayns-components

Version:

A set of beautiful React components for developing chayns® applications.

57 lines (56 loc) 1.53 kB
/* eslint-disable react/forbid-prop-types,react-hooks/exhaustive-deps */ import React, { createContext, useState, useCallback } from 'react'; import PropTypes from 'prop-types'; const SimpleWrapperContext = /*#__PURE__*/createContext({ value: null }); const SimpleWrapperStateProvider = _ref => { let { children, data = [], hasMore = false, onLoadMore, onInput } = _ref; const [isLoading, setIsLoading] = useState(false); const loadMore = useCallback(async () => { if (isLoading || !onLoadMore) return; setIsLoading(true); try { await onLoadMore(); } finally { setIsLoading(false); } }, [onLoadMore, isLoading]); const onChange = useCallback(async value => { setIsLoading(true); try { await onInput(value); } finally { setIsLoading(false); } }, [onInput]); return /*#__PURE__*/React.createElement(SimpleWrapperContext.Provider, { value: { data, showWaitCursor: isLoading, isLoading, hasMore, onLoadMore: loadMore, onChange } }, children); }; SimpleWrapperStateProvider.propTypes = { children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.node), PropTypes.node]), data: PropTypes.array, hasMore: PropTypes.bool, onLoadMore: PropTypes.func, onInput: PropTypes.func }; export default (objectMapping => ({ Consumer: SimpleWrapperContext.Consumer, Provider: SimpleWrapperStateProvider, ObjectMapping: objectMapping })); //# sourceMappingURL=SimpleWrapperContext.js.map