@uncinc/uncinc-react-kitchen-sink 2.3.2 → 2.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,14 +1,29 @@
1
- import React, { useRef } from 'react';
2
- import { useSearchkit, useSearchkitQueryValue } from '@searchkit/client';
3
- import TextField from '../../Form/TextField';
1
+ import React, { useRef } from "react";
2
+ import { useSearchkit, useSearchkitQueryValue } from "@searchkit/client";
4
3
  import FormikWrapper from '../../Form/FormikWrapper';
4
+ import Button from '../../Button';
5
+ import TextField from '../../Form/TextField';
5
6
 
6
7
  const SearchField = ({
7
- timeout
8
+ timeout,
9
+ autoSearch = true,
10
+ buttonText,
11
+ placeholder
8
12
  }) => {
9
13
  const [query, setQuery] = useSearchkitQueryValue();
10
14
  const api = useSearchkit();
11
15
  const timeoutRef = useRef();
16
+ const fieldLabel = false;
17
+
18
+ const searchQuery = query => {
19
+ setQuery(query);
20
+ api.setQuery(query);
21
+ api.search();
22
+ };
23
+
24
+ const onSubmit = (values, form) => {
25
+ searchQuery(values.query);
26
+ };
12
27
 
13
28
  const onChange = e => {
14
29
  e.persist();
@@ -18,26 +33,38 @@ const SearchField = ({
18
33
  }
19
34
 
20
35
  timeoutRef.current = setTimeout(() => {
21
- setQuery(e.target.value);
22
- api.setQuery(e.target.value);
23
- api.search();
36
+ searchQuery(e.target.value);
24
37
  }, timeout);
25
38
  };
26
39
 
40
+ const handleEnter = event => {
41
+ if (event.key === 'Enter') {
42
+ onSubmit();
43
+ }
44
+ };
45
+
27
46
  return /*#__PURE__*/React.createElement(FormikWrapper, {
28
47
  initialValues: {
29
48
  query: query
30
49
  },
31
50
  displayRequiredIndicator: "required",
32
- enableReinitialize: true
51
+ enableReinitialize: true,
52
+ onSubmit: onSubmit
33
53
  }, /*#__PURE__*/React.createElement(TextField, {
34
54
  fieldName: "query",
35
- title: "Search",
36
- onChange: onChange
37
- }));
55
+ title: fieldLabel,
56
+ placeholder: placeholder,
57
+ onChange: autoSearch ? () => onChange : () => handleEnter
58
+ }), !autoSearch && /*#__PURE__*/React.createElement(Button, {
59
+ type: "submit",
60
+ primary: true
61
+ }, buttonText));
38
62
  };
39
63
 
40
64
  SearchField.defaultProps = {
41
- timeout: 1000
65
+ timeout: 1000,
66
+ autoSearch: true,
67
+ buttonText: '',
68
+ placeholder: ''
42
69
  };
43
70
  export default SearchField;
@@ -1,6 +1,7 @@
1
1
  import React, { useState, useEffect } from 'react';
2
2
  import usePrevious from '../../../hooks/usePrevious';
3
3
  import { useSearchkitVariables } from '@searchkit/client';
4
+ import PropTypes from 'prop-types';
4
5
 
5
6
  const SearchResultItemDefault = ({
6
7
  item
@@ -14,10 +15,11 @@ const SearchResults = ({
14
15
  data,
15
16
  renderItem,
16
17
  renderHeader,
17
- appendResults = false
18
+ appendResults = false,
19
+ scrollView = null
18
20
  }) => {
19
- const SearchResultItem = renderItem || SearchResultItemDefault;
20
- const SearchResultHeader = renderHeader || SearchResultHeaderDefault;
21
+ const SearchResultItem = renderItem;
22
+ const SearchResultHeader = renderHeader;
21
23
  const variables = useSearchkitVariables();
22
24
  const [items, setItems] = useState([]);
23
25
  const [currentPage, setCurrentPage] = useState(-1);
@@ -30,6 +32,11 @@ const SearchResults = ({
30
32
  const previousItems = usePrevious(items);
31
33
  useEffect(() => {
32
34
  if (typeof data.results === 'undefined') return;
35
+ setCurrentPage(data.results.hits.page.pageNumber);
36
+ setPreviousQuery(variables.query);
37
+ setPreviousSortBy(variables.sortBy);
38
+ setPreviousActiveFilters(variables.filters);
39
+ setCurrentData(JSON.stringify(data));
33
40
 
34
41
  if (!appendResults) {
35
42
  // Set items directly if not appendResults
@@ -55,20 +62,30 @@ const SearchResults = ({
55
62
  // Append data if the page has changed and we have previousItems
56
63
  setItems([...previousItems, ...data.results.hits.items]);
57
64
  }
58
-
59
- setCurrentPage(data.results.hits.page.pageNumber);
60
- setPreviousQuery(variables.query);
61
- setPreviousSortBy(variables.sortBy);
62
- setPreviousActiveFilters(variables.filters);
63
- setCurrentData(JSON.stringify(data));
64
65
  }
65
66
  }, [data, appendResults, variables]); // Set the search results when items have changed
66
67
 
67
68
  useEffect(() => {
68
69
  setSearchResultItems(items.map(item => /*#__PURE__*/React.createElement(SearchResultItem, {
70
+ key: item.id,
69
71
  item: item
70
72
  })));
71
- }, [items]);
73
+ }, [items]); // Scroll to top on every page.
74
+
75
+ useEffect(() => {
76
+ if (!currentData) {
77
+ return;
78
+ }
79
+
80
+ const currentPageNumber = currentData.results.hits.page.pageNumber;
81
+
82
+ if (appendResults === false && scrollView && scrollView.current && currentPageNumber !== 0) {
83
+ scrollView.current.scrollIntoView({
84
+ behavior: 'smooth',
85
+ block: 'start'
86
+ });
87
+ }
88
+ }, [currentData]);
72
89
  return /*#__PURE__*/React.createElement("div", {
73
90
  className: "SearchResults"
74
91
  }, data && /*#__PURE__*/React.createElement(SearchResultHeader, {
@@ -76,4 +93,19 @@ const SearchResults = ({
76
93
  }), searchResultItems);
77
94
  };
78
95
 
96
+ SearchResults.defaultProps = {
97
+ renderItem: SearchResultItemDefault,
98
+ renderHeader: SearchResultHeaderDefault,
99
+ appendResults: false,
100
+ scrollView: null
101
+ };
102
+ SearchResults.propTypes = {
103
+ // its turned off due to complex struxture of the object
104
+ // eslint-disable-next-line react/forbid-prop-types
105
+ data: PropTypes.object.isRequired,
106
+ renderItem: PropTypes.elementType,
107
+ renderHeader: PropTypes.elementType,
108
+ appendResults: PropTypes.bool,
109
+ scrollView: PropTypes.element
110
+ };
79
111
  export default SearchResults;
@@ -0,0 +1,25 @@
1
+ import { ApolloServer, gql } from 'apollo-server-micro';
2
+ export const config = {
3
+ api: {
4
+ bodyParser: false
5
+ }
6
+ };
7
+ const typeDefs = [gql`
8
+ type Query {
9
+ root: String
10
+ }
11
+
12
+ type Mutation {
13
+ root: String
14
+ }
15
+ `];
16
+ const server = new ApolloServer({
17
+ typeDefs,
18
+ resolvers: {},
19
+ introspection: true,
20
+ playground: true,
21
+ context: {}
22
+ });
23
+ export default server.createHandler({
24
+ path: '/api/graphql'
25
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uncinc/uncinc-react-kitchen-sink",
3
- "version": "2.3.2",
3
+ "version": "2.3.3",
4
4
  "description": "Default React components that are easily extended.",
5
5
  "author": "uncinc",
6
6
  "license": "MIT",
@@ -53,11 +53,11 @@
53
53
  "next-with-apollo": "^5.2.1",
54
54
  "ola": "^1.1.1",
55
55
  "path": "^0.12.7",
56
- "react": "^16.14.0",
56
+ "react": "^16 || ^17 || ^18",
57
57
  "react-animate-height": "^2.0.23",
58
58
  "react-autosuggest": "^10.0.2",
59
59
  "react-datepicker": "^3.0.0",
60
- "react-dom": "^16.4",
60
+ "react-dom": "^16 || ^17 || ^18",
61
61
  "react-global-configuration": "^1.4.0",
62
62
  "react-html-parser": "^2.0.2",
63
63
  "react-i18next": "^11.5.0",
@@ -1,3 +0,0 @@
1
- .WysiwygField {
2
- margin-right: 200px;
3
- }
@@ -1,5 +0,0 @@
1
- const makeSafeForCSS = value => {
2
- return encodeURIComponent(value).replace(/\.|%[0-9a-z]{2}/gi, '');
3
- };
4
-
5
- export default makeSafeForCSS;