Skip to content

Commit c8c3629

Browse files
authored
Merge pull request #101 from kube-js/develop
fix: added initial autocomplete functionality
2 parents 5f55e71 + 37f686d commit c8c3629

File tree

16 files changed

+436
-119
lines changed

16 files changed

+436
-119
lines changed

assets/jscpd-badge.svg

Lines changed: 2 additions & 2 deletions
Loading

k8s/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apiVersion: v1
22
description: A Helm chart for kube-ts-react-client
33
name: kube-ts-react-client
44
version: 1.0.0
5-
appVersion: 1.5.9
5+
appVersion: 1.5.14
66
home: https://cloud.docker.com/u/kubejs/repository/docker/kubejs/kube-ts-react-client
77
icon: https://avatars2.githubusercontent.com/u/47761918?s=200&v=4
88
sources:

k8s/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ replicaCount: 2
66

77
image:
88
repository: kubejs/kube-ts-react-client
9-
tag: 1.5.9
9+
tag: 1.5.14
1010
pullPolicy: Always
1111
containerPort: 80
1212

src/api/index.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
ResetPasswordOptions,
1010
VerifyAccountOptions,
1111
} from '../redux/auth/actionCreators';
12+
import { AutocompleteResults } from '../redux/autocomplete/actionCreators';
1213
import { CourseDetailsResult } from '../redux/courseDetails/actionCreators';
1314
import { DiscoveryItemsResult } from '../redux/discoveryItems/actionCreators';
1415
import http from '../services/http';
@@ -63,18 +64,29 @@ export type SearchParams =
6364

6465
export type DiscoveryType = 'homepage' | 'course';
6566
export interface BaseGetOptions {
66-
readonly type: DiscoveryType
67+
readonly type: DiscoveryType;
6768
readonly searchParams?: SearchParams;
6869
}
6970

7071
export type GetDiscoveryItems<T> = (options: BaseGetOptions) => Promise<T>;
7172

73+
export interface AutocompleteOptions {
74+
readonly term: string;
75+
}
76+
77+
export type Autocomplete = (
78+
options: AutocompleteOptions
79+
) => Promise<AutocompleteResults>;
80+
7281
export interface Api {
7382
readonly auth: AuthApi;
7483
readonly categories: Facade<Category>;
7584
readonly courses: Facade<Course>;
7685
readonly users: Facade<User>;
77-
readonly getDiscoveryItems: GetDiscoveryItems<CourseDetailsResult | DiscoveryItemsResult>;
86+
readonly getDiscoveryItems: GetDiscoveryItems<
87+
CourseDetailsResult | DiscoveryItemsResult
88+
>;
89+
readonly autocomplete: Autocomplete;
7890
}
7991

8092
export const normalisePromise = <T>(promise: ResponsePromise): Promise<T> =>
@@ -118,6 +130,10 @@ const createApi = ({ httpClient, token }: Options): Api => {
118130
})
119131
),
120132
},
133+
autocomplete: ({ term }: AutocompleteOptions) =>
134+
normalisePromise<AutocompleteResults>(
135+
httpClient.get('autocomplete', { searchParams: { q: term } })
136+
),
121137
categories: categoriesFactory({
122138
kyConfig: {
123139
...baseConfig,

src/atoms/Autocomplete/index.tsx

Lines changed: 5 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,6 @@ import Downshift from 'downshift';
99
import React from 'react';
1010
import useStyles from './styles';
1111

12-
const suggestions = [
13-
{ label: 'Afghanistan' },
14-
{ label: 'Aland Islands' },
15-
{ label: 'Albania' },
16-
{ label: 'Algeria' },
17-
{ label: 'American Samoa' },
18-
{ label: 'Andorra' },
19-
{ label: 'Angola' },
20-
{ label: 'Anguilla' },
21-
{ label: 'Antarctica' },
22-
{ label: 'Antigua and Barbuda' },
23-
{ label: 'Argentina' },
24-
{ label: 'Armenia' },
25-
{ label: 'Aruba' },
26-
{ label: 'Australia' },
27-
{ label: 'Austria' },
28-
{ label: 'Azerbaijan' },
29-
{ label: 'Bahamas' },
30-
{ label: 'Bahrain' },
31-
{ label: 'Bangladesh' },
32-
{ label: 'Barbados' },
33-
{ label: 'Belarus' },
34-
{ label: 'Belgium' },
35-
{ label: 'Belize' },
36-
{ label: 'Benin' },
37-
{ label: 'Bermuda' },
38-
{ label: 'Bhutan' },
39-
{ label: 'Bolivia, Plurinational State of' },
40-
{ label: 'Bonaire, Sint Eustatius and Saba' },
41-
{ label: 'Bosnia and Herzegovina' },
42-
{ label: 'Botswana' },
43-
{ label: 'Bouvet Island' },
44-
{ label: 'Brazil' },
45-
{ label: 'British Indian Ocean Territory' },
46-
{ label: 'Brunei Darussalam' },
47-
];
48-
4912
function renderInput(inputProps: any) {
5013
const { InputProps, classes, ref, ...other } = inputProps;
5114

@@ -91,46 +54,21 @@ function renderSuggestion(suggestionProps: any) {
9154
);
9255
}
9356

94-
function getSuggestions(
95-
value: string | null,
96-
{ showEmpty }: { showEmpty: boolean } = { showEmpty: false }
97-
) {
98-
const inputValue = String(value)
99-
.trim()
100-
.toLowerCase();
101-
const inputLength = inputValue.length;
102-
let count = 0;
103-
104-
return inputLength === 0 && !showEmpty
105-
? []
106-
: suggestions.filter(suggestion => {
107-
const keep =
108-
count < 5 &&
109-
suggestion.label.slice(0, inputLength).toLowerCase() === inputValue;
110-
111-
if (keep) {
112-
count += 1;
113-
}
114-
115-
return keep;
116-
});
117-
}
118-
11957
let popperNode: any;
12058

121-
const Autocomplete = () => {
59+
const Autocomplete = ({value, onChange, suggestions}: any) => {
12260
const classes = useStyles();
12361

12462
return (
12563
<div className={classes.root}>
126-
<Downshift id="downshift-popper">
64+
<Downshift id="downshift-popper" selectedItem={value} onStateChange={onChange}>
12765
{({
66+
clearSelection,
12867
getInputProps,
12968
getItemProps,
13069
getLabelProps,
13170
getMenuProps,
13271
highlightedIndex,
133-
inputValue,
13472
isOpen,
13573
selectedItem,
13674
}) => {
@@ -155,7 +93,7 @@ const Autocomplete = () => {
15593
})}
15694
{String(inputProps.value).length > 0 && (
15795
<div className={[classes.icon, classes.closeIcon].join(' ')}>
158-
<CloseIcon />
96+
<CloseIcon onClick={clearSelection as any} />
15997
</div>
16098
)}
16199
<Popper open={isOpen} anchorEl={popperNode}>
@@ -171,7 +109,7 @@ const Autocomplete = () => {
171109
width: popperNode ? popperNode.clientWidth : undefined,
172110
}}
173111
>
174-
{(getSuggestions(inputValue) as any).map(
112+
{suggestions.map(
175113
(suggestion: any, index: number) =>
176114
renderSuggestion({
177115
highlightedIndex,

src/components/HeroContent/index.jsx

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)