Merge pull request #5140 from strapi/fix/select-relations

Fix select relations
This commit is contained in:
cyril lopez 2020-02-06 17:57:15 +01:00 committed by GitHub
commit 8fabde104d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 12 deletions

View File

@ -25,8 +25,10 @@
"postal_coder": { "postal_coder": {
"type": "string" "type": "string"
}, },
"category": { "categories": {
"model": "category" "collection": "category",
"via": "addresses",
"dominant": true
}, },
"cover": { "cover": {
"model": "file", "model": "file",

View File

@ -13,6 +13,10 @@
"attributes": { "attributes": {
"name": { "name": {
"type": "string" "type": "string"
},
"addresses": {
"via": "categories",
"collection": "address"
} }
} }
} }

View File

@ -45,6 +45,7 @@ function SelectWrapper({
const { signal } = abortController; const { signal } = abortController;
const ref = useRef(); const ref = useRef();
const startRef = useRef(); const startRef = useRef();
startRef.current = state._start; startRef.current = state._start;
ref.current = async () => { ref.current = async () => {
@ -55,6 +56,9 @@ function SelectWrapper({
if (isEmpty(params._q)) { if (isEmpty(params._q)) {
delete params._q; delete params._q;
} else {
delete params._limit;
delete params._start;
} }
const data = await request(requestUrl, { const data = await request(requestUrl, {
@ -69,6 +73,7 @@ function SelectWrapper({
if (!isEmpty(params._q)) { if (!isEmpty(params._q)) {
setOptions(formattedData); setOptions(formattedData);
setState(prev => ({ ...prev, _start: 0 }));
return; return;
} }
@ -97,19 +102,17 @@ function SelectWrapper({
} }
}; };
useEffect(() => {
ref.current();
return () => {
abortController.abort();
};
}, [ref]);
useEffect(() => { useEffect(() => {
if (state._q !== '') { if (state._q !== '') {
ref.current(); let timer = setTimeout(() => {
ref.current();
}, 300);
return () => clearTimeout(timer);
} }
ref.current();
return () => { return () => {
abortController.abort(); abortController.abort();
}; };
@ -140,7 +143,7 @@ function SelectWrapper({
}; };
const onMenuScrollToBottom = () => { const onMenuScrollToBottom = () => {
setState(prevState => ({ ...prevState, _start: prevState._start + 1 })); setState(prevState => ({ ...prevState, _start: prevState._start + 20 }));
}; };
const isSingle = [ const isSingle = [