Skip to content

Commit c15e6aa

Browse files
committed
Hook up load more button
1 parent 916354a commit c15e6aa

File tree

4 files changed

+38
-32
lines changed

4 files changed

+38
-32
lines changed

packages/connect-react/examples/nextjs/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/connect-react/src/components/ControlSelect.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as React from "react";
12
import { useMemo } from "react";
23
import Select, {
34
Props as ReactSelectProps, components,
@@ -14,13 +15,12 @@ type ControlSelectProps<T> = {
1415
isCreatable?: boolean;
1516
options: {label: string; value: T;}[];
1617
selectProps?: ReactSelectProps;
17-
canLoadMore?: boolean;
1818
showLoadMoreButton?: boolean;
1919
onLoadMore?: () => void;
2020
};
2121

2222
export function ControlSelect<T>({
23-
isCreatable, options, selectProps, canLoadMore, showLoadMoreButton, onLoadMore,
23+
isCreatable, options, selectProps, showLoadMoreButton, onLoadMore,
2424
}: ControlSelectProps<T>) {
2525
const formFieldCtx = useFormFieldContext();
2626
const {
@@ -76,13 +76,14 @@ export function ControlSelect<T>({
7676
]);
7777

7878
const LoadMore = ({
79+
// eslint-disable-next-line react/prop-types
7980
children, ...props
8081
}) => {
8182
return (
8283
<components.MenuList {...props}>
83-
{children}
84+
{ children }
8485
<div className="pt-4">
85-
<LoadMoreButton enabled={canLoadMore} onClick={onLoadMore}/>
86+
<LoadMoreButton onChange={onLoadMore}/>
8687
</div>
8788
</components.MenuList>
8889
)

packages/connect-react/src/components/LoadMoreButton.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ import { useCustomize } from "../hooks/customization-context";
22
import type { CSSProperties } from "react";
33

44
export type ButtonProps = {
5-
enabled: boolean;
6-
onClick: () => void;
5+
onChange: () => void;
76
};
87

98
export const LoadMoreButton = (props: ButtonProps) => {
10-
const {
11-
enabled, onClick,
12-
} = props;
9+
const { onChange } = props;
1310
const {
1411
getProps, theme,
1512
} = useCustomize();
@@ -27,9 +24,10 @@ export const LoadMoreButton = (props: ButtonProps) => {
2724
cursor: "pointer",
2825
width: "100%",
2926
};
27+
3028
return (
31-
<button disabled={!enabled} onClick={onClick} type="button" {...getProps("loadMoreButton", baseStyles, props)}>
32-
Load More
29+
<button onClick={onChange} type="button" {...getProps("loadMoreButton", baseStyles, props)}>
30+
Load More
3331
</button>
3432
);
3533
};

packages/connect-react/src/components/RemoteOptionsContainer.tsx

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
4444
setPageable,
4545
] = useState({
4646
page: 0,
47-
prevContext: undefined,
47+
prevContext: {},
48+
data: [],
4849
})
4950

5051
const configuredPropsUpTo: Record<string, unknown> = {};
@@ -75,19 +76,22 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
7576
setError,
7677
] = useState<{ name: string; message: string; }>();
7778

78-
const [
79-
canLoadMore,
80-
setCanLoadMore,
81-
] = useState<boolean>(false);
82-
8379
const onLoadMore = () => {
8480
setPage(pageable.page)
8581
setContext(pageable.prevContext)
82+
setPageable({
83+
...pageable,
84+
prevContext: {},
85+
})
86+
}
87+
88+
const canLoadMore = () => {
89+
return Object.keys(pageable.prevContext || {}).length > 0
8690
}
8791

8892
// TODO handle error!
8993
const {
90-
isFetching, data, refetch,
94+
isFetching, refetch,
9195
} = useQuery({
9296
queryKey: [
9397
"componentConfigure",
@@ -97,15 +101,11 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
97101
setError(undefined);
98102
const res = await client.componentConfigure(componentConfigureInput);
99103

100-
// console.log("res", res)
101104
// XXX look at errors in response here too
102105
const {
103106
options, stringOptions, errors,
104107
} = res;
105-
setPageable({
106-
page: page + 1,
107-
prevContext: res.context,
108-
})
108+
109109
if (errors?.length) {
110110
// TODO field context setError? (for validity, etc.)
111111
try {
@@ -118,9 +118,9 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
118118
}
119119
return [];
120120
}
121+
let _options = []
121122
if (options?.length) {
122-
setCanLoadMore(true)
123-
return options;
123+
_options = options;
124124
}
125125
if (stringOptions?.length) {
126126
const options = [];
@@ -130,16 +130,24 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
130130
value: stringOption,
131131
});
132132
}
133-
setCanLoadMore(true)
134-
return options;
133+
_options = options;
135134
}
136-
return [];
135+
const data = [
136+
...pageable.data,
137+
..._options,
138+
]
139+
setPageable({
140+
page: page + 1,
141+
prevContext: res.context,
142+
data,
143+
})
144+
return data;
137145
},
138146
enabled: !!queryEnabled,
139147
});
140148

141149
const showLoadMoreButton = () => {
142-
return !isFetching && !error && pageable.prevContext !== undefined
150+
return !isFetching && !error && canLoadMore()
143151
}
144152

145153
// TODO show error in different spot!
@@ -156,10 +164,9 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
156164

157165
return (
158166
<ControlSelect
159-
canLoadMore={canLoadMore}
160167
showLoadMoreButton={showLoadMoreButton()}
161168
onLoadMore={onLoadMore}
162-
options={data || []}
169+
options={pageable.data}
163170
// XXX isSearchable if pageQuery? or maybe in all cases? or maybe NOT when pageQuery
164171
selectProps={{
165172
isLoading: isFetching,

0 commit comments

Comments
 (0)