@kanda-libs/ks-component-ts 0.3.7 → 0.3.8

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kanda-libs/ks-component-ts",
3
- "version": "0.3.7",
3
+ "version": "0.3.8",
4
4
  "description": "Kanda form component library",
5
5
  "main": "dist/index.esm.js",
6
6
  "module": "dist/index.esm.js",
@@ -5,6 +5,7 @@ import InputUncontrolled, {
5
5
  } from "~/field/components/Input/InputUncontrolled";
6
6
  import { DefaultFormFieldProps } from "~/field/types";
7
7
  import FormTheme from "~/components/FormTheme";
8
+ import { useFormContext } from "react-hook-form";
8
9
 
9
10
  export interface HeaderSearchProps
10
11
  extends DefaultFormFieldProps<InputUncontrolledProps> {
@@ -12,6 +13,7 @@ export interface HeaderSearchProps
12
13
  className?: string;
13
14
  help?: boolean;
14
15
  hideNumber?: boolean;
16
+ searchName?: string;
15
17
  }
16
18
 
17
19
  const HeaderSearch: FunctionComponent<HeaderSearchProps> = function ({
@@ -19,6 +21,7 @@ const HeaderSearch: FunctionComponent<HeaderSearchProps> = function ({
19
21
  help = false,
20
22
  hideNumber = true,
21
23
  className,
24
+ searchName = "search",
22
25
  ...inputProps
23
26
  }) {
24
27
  return (
@@ -31,7 +34,7 @@ const HeaderSearch: FunctionComponent<HeaderSearchProps> = function ({
31
34
  <div className="w-full -ml-4">
32
35
  <FormTheme variant="clean">
33
36
  <InputUncontrolled
34
- name="search"
37
+ name={searchName}
35
38
  icon="search"
36
39
  iconVariant="search"
37
40
  {...inputProps}
@@ -1,4 +1,10 @@
1
- import React, { ChangeEvent, FunctionComponent, useState } from "react";
1
+ import React, {
2
+ ChangeEvent,
3
+ FunctionComponent,
4
+ useEffect,
5
+ useRef,
6
+ useState,
7
+ } from "react";
2
8
  import { useFormContext } from "react-hook-form";
3
9
  import type { SearchResultsProps } from "../types";
4
10
  import useSearch from "../useSearch";
@@ -26,11 +32,13 @@ const Container: FunctionComponent<ContainerProps> = function ({
26
32
  children,
27
33
  }) {
28
34
  const [query, setQuery] = useState("");
29
- const { setValue } = useFormContext();
35
+ const { setValue, getValues } = useFormContext();
30
36
  const { results, isLoading, searchWords } = useSearch(query);
31
37
 
32
38
  const handleSearch = (e: ChangeEvent<HTMLInputElement>) => {
33
39
  setQuery(e.target?.value as string);
40
+ if (companySearchName)
41
+ setValue(companySearchName, e.target?.value as string);
34
42
  };
35
43
 
36
44
  const showButton = !!(
@@ -44,6 +52,15 @@ const Container: FunctionComponent<ContainerProps> = function ({
44
52
  setValue(companySearchName as string, query);
45
53
  };
46
54
 
55
+ const initRef = useRef<boolean>(false);
56
+ useEffect(() => {
57
+ if (!companySearchName || initRef.current) return;
58
+ const value = getValues(companySearchName);
59
+ initRef.current = true;
60
+ if (!value) return;
61
+ setQuery(value);
62
+ }, [getValues, companySearchName]);
63
+
47
64
  return children({
48
65
  results,
49
66
  isLoading,
@@ -2,23 +2,30 @@ import { Button } from "@kanda-libs/ks-design-library";
2
2
  import React, { ChangeEvent, FunctionComponent } from "react";
3
3
  import HeaderSearch from "~/components/HeaderSearch";
4
4
  import { SEARCH_PLACEHOLDER } from "../../constants";
5
+ import { useFormContext } from "react-hook-form";
5
6
 
6
7
  export interface HeaderProps {
7
8
  id: string;
8
9
  handleClose?: () => void;
9
10
  handleSearch?: (e: ChangeEvent<HTMLInputElement>) => void;
11
+ companySearchName?: string;
10
12
  }
11
13
 
12
14
  const Header: FunctionComponent<HeaderProps> = function ({
13
15
  id,
14
16
  handleClose,
15
17
  handleSearch = () => {},
18
+ companySearchName = "companySearch",
16
19
  }) {
20
+ const { watch } = useFormContext();
21
+ const value = watch(companySearchName);
17
22
  return (
18
23
  <HeaderSearch
19
24
  autoFocus
20
25
  onChange={handleSearch as () => void}
21
26
  placeholder={SEARCH_PLACEHOLDER}
27
+ searchName={companySearchName}
28
+ value={value}
22
29
  options={[
23
30
  <Button.Icon
24
31
  id={`${id}-close`}
@@ -14,13 +14,19 @@ export type MobileProps = SearchResultsProps;
14
14
  const Mobile: FunctionComponent<MobileProps> = function ({
15
15
  modalId = "",
16
16
  handleSelect,
17
+ companySearchName,
17
18
  ...rest
18
19
  }) {
20
+ console.log({ companySearchName });
19
21
  return (
20
22
  <ModalContainer id={modalId}>
21
23
  {({ handleClose }) => (
22
24
  <>
23
- <Container handleClose={handleClose} {...rest}>
25
+ <Container
26
+ handleClose={handleClose}
27
+ companySearchName={companySearchName}
28
+ {...rest}
29
+ >
24
30
  {({
25
31
  handleSearch,
26
32
  results,
@@ -37,6 +43,7 @@ const Mobile: FunctionComponent<MobileProps> = function ({
37
43
  id={modalId}
38
44
  handleClose={handleClose}
39
45
  handleSearch={handleSearch}
46
+ companySearchName={companySearchName}
40
47
  />
41
48
  {showButton && (
42
49
  <Button.Link
@@ -36,6 +36,7 @@ const CompanyLookupInput: FunctionComponent<CompanyLookupInputProps> =
36
36
  companySelected,
37
37
  searchInputProps,
38
38
  searchResultsProps,
39
+ isDesktop,
39
40
  noCompanyCallback,
40
41
  } = useCompanyLookupInputProps({
41
42
  noCompanyCallback: initialNoCompanyCallback,
@@ -79,12 +80,12 @@ const CompanyLookupInput: FunctionComponent<CompanyLookupInputProps> =
79
80
  <div className={CLASS_NAMES.inputWrapper}>
80
81
  <Input
81
82
  {...FIELDS.search}
82
- name={companySearchName}
83
+ name={isDesktop ? companySearchName : "placeholder"}
83
84
  label={label}
84
85
  placeholder={placeholder}
85
86
  {...searchInputProps}
86
87
  />
87
- <BreakPoints desktop={soleTraderButton as JSX.Element} />
88
+ {soleTraderButton}
88
89
  </div>
89
90
  <SearchResults
90
91
  {...searchResultsProps}
@@ -99,3 +100,25 @@ const CompanyLookupInput: FunctionComponent<CompanyLookupInputProps> =
99
100
  };
100
101
 
101
102
  export default CompanyLookupInput;
103
+
104
+ // {/* <BreakPoints
105
+ // desktop={
106
+ // <Input
107
+ // {...FIELDS.search}
108
+ // name={companySearchName}
109
+ // label={label}
110
+ // placeholder={placeholder}
111
+ // {...searchInputProps}
112
+ // />
113
+ // }
114
+ // mobile={
115
+ // <button
116
+ // id="company-search"
117
+ // role="button"
118
+ // className=""
119
+ // {...mobileButtonProps}
120
+ // >
121
+ // {label}
122
+ // </button>
123
+ // }
124
+ // /> */}
@@ -10,6 +10,7 @@ import {
10
10
  import { CompanyLookupInputProps } from "./types";
11
11
  import { InputProps } from "~/field/components/Input";
12
12
  import { SearchResultsProps } from "~/field/components/CompanyLookupInput/SearchResults/types";
13
+ import { StringIndexedObject } from "~/types";
13
14
 
14
15
  export type CompanyLookupInputArgs = Omit<
15
16
  CompanyLookupInputProps,
@@ -22,7 +23,9 @@ export interface CompanyLookupInputPropsHook {
22
23
  handleUnSelect?: () => void;
23
24
  searchInputProps?: InputProps;
24
25
  searchResultsProps?: SearchResultsProps;
26
+ mobileButtonProps?: StringIndexedObject;
25
27
  noCompanyCallback?: (name?: string) => void;
28
+ isDesktop: boolean;
26
29
  }
27
30
 
28
31
  export default function useCompanyLookupInputProps({
@@ -133,6 +136,15 @@ export default function useCompanyLookupInputProps({
133
136
  }
134
137
  }, [isDesktop, modalId, showModal]);
135
138
 
139
+ /**
140
+ * Handles focus
141
+ */
142
+ const handleMobileButtonClick = useCallback(() => {
143
+ if (!isDesktop) {
144
+ showModal(modalId);
145
+ }
146
+ }, [isDesktop, modalId, showModal]);
147
+
136
148
  /**
137
149
  * Handles click
138
150
  */
@@ -162,6 +174,11 @@ export default function useCompanyLookupInputProps({
162
174
  onClick: handleClick,
163
175
  };
164
176
 
177
+ const mobileButtonProps = {
178
+ onFocus: handleFocus,
179
+ onClick: handleMobileButtonClick,
180
+ };
181
+
165
182
  const searchResultsProps = { modalId, handleSelect };
166
183
 
167
184
  const inputCallback = (enteredName?: string) => {
@@ -208,6 +225,8 @@ export default function useCompanyLookupInputProps({
208
225
  handleUnSelect,
209
226
  searchInputProps: searchInputProps as InputProps,
210
227
  searchResultsProps: searchResultsProps as SearchResultsProps,
228
+ mobileButtonProps,
211
229
  noCompanyCallback: inputCallback,
230
+ isDesktop,
212
231
  };
213
232
  }