circle-ihk
Advanced tools
Comparing version 1.0.4 to 1.0.5
/// <reference types="react" /> | ||
import { Match } from '../interface'; | ||
interface IData { | ||
start: number; | ||
limit: number; | ||
data: Array<any>; | ||
} | ||
interface IProps { | ||
@@ -17,3 +22,4 @@ field?: string; | ||
app: import("../interface").App; | ||
data: any[]; | ||
data: IData; | ||
done: boolean; | ||
query: { | ||
@@ -29,2 +35,3 @@ field?: string | undefined; | ||
}; | ||
onDone: import("react").Dispatch<import("react").SetStateAction<boolean>>; | ||
onQuery: import("react").Dispatch<import("react").SetStateAction<{ | ||
@@ -44,3 +51,4 @@ field?: string | undefined; | ||
container: HTMLElement; | ||
loadMore: (() => void) | null; | ||
}; | ||
export {}; |
@@ -1,2 +0,2 @@ | ||
import { useRef, useState, useEffect } from 'react'; | ||
import { useState, useEffect } from 'react'; | ||
import useApp from './useApp'; | ||
@@ -7,6 +7,10 @@ import { isUndefined, isFunction } from '../utils/is'; | ||
const { app, me, container } = useApp(); | ||
const done = useRef(false); | ||
const [done, onDone] = useState(false); | ||
const [loading, setLoading] = useState(false); | ||
const [query, onQuery] = useState(prop); | ||
const [data, setData] = useState([]); | ||
const [data, setData] = useState({ | ||
data: [], | ||
start: query.start || 1, | ||
limit: query.limit || 10, | ||
}); | ||
const refetch = (callback) => { | ||
@@ -17,14 +21,15 @@ setLoading(true); | ||
.list(reset, { start, limit }, table) | ||
.then((results = []) => { | ||
if (!Array.isArray(results)) { | ||
return; | ||
.then((res) => { | ||
if (res.data.length <= 0 || res.data.length < limit) { | ||
onDone(true); | ||
} | ||
if (results.length <= 0 || results.length < limit) { | ||
done.current = true; | ||
} | ||
if (start <= 1) { | ||
setData(results); | ||
setData(res); | ||
} | ||
else { | ||
setData((lastData) => [...lastData, ...results]); | ||
setData((lastData) => ({ | ||
start: res.start, | ||
limit: res.limit, | ||
data: [...lastData.data, ...res.data], | ||
})); | ||
} | ||
@@ -48,15 +53,16 @@ // @ts-ignore | ||
.set(value, key, table) | ||
.then((id) => { | ||
app.get(id, table).then((val) => { | ||
const index = data.findIndex((prop) => prop.id === key); | ||
if (index >= 0) { | ||
data[index] = val; | ||
} | ||
else { | ||
data.push(val); | ||
} | ||
setData([...data]); | ||
// @ts-ignore | ||
isFunction(callback) && callback(); | ||
.then(() => { | ||
const index = data.data.findIndex((prop) => prop.id === key); | ||
if (index >= 0) { | ||
data.data[index] = value; | ||
} | ||
else { | ||
data.data.push(value); | ||
} | ||
setData({ | ||
...data, | ||
data: [...data.data], | ||
}); | ||
// @ts-ignore | ||
isFunction(callback) && callback(); | ||
}) | ||
@@ -73,3 +79,5 @@ .catch((err) => { | ||
data, | ||
done, | ||
query, | ||
onDone, | ||
onQuery, | ||
@@ -80,3 +88,14 @@ onChange, | ||
container, | ||
loadMore: done | ||
? null | ||
: () => { | ||
if (loading) { | ||
return; | ||
} | ||
onQuery({ | ||
...query, | ||
start: data.start, | ||
}); | ||
}, | ||
}; | ||
} |
{ | ||
"name": "circle-ihk", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Circle Reader Component Library", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47889
1394