Skip to content

Commit 061e123

Browse files
committed
Implement a register function to register search input easily
1 parent bd77c2e commit 061e123

File tree

5 files changed

+39
-7
lines changed

5 files changed

+39
-7
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ function App() {
6767
```jsx
6868
import { useSearch } from 'react-search-hook'
6969
function MyExampleComponent() {
70-
const { search, setSearch } = useSearch('products')
70+
const { register } = useSearch('products')
7171

7272
return (
7373
<div>
74-
<input onChange={(e) => setSearch(e.target.value)} />
74+
<input {...register()} />
7575
<span>{search}</span>
7676
</div>
7777
)
@@ -86,11 +86,11 @@ function MyExampleComponent() {
8686
import { useSearch } from 'react-search-hook'
8787
function MyExampleComponent() {
8888
const items = ['text1', 'text2', ...]
89-
const { search, setSearch, searchResult } = useSearch('products', { items })
89+
const { register, searchResult } = useSearch('products', { items })
9090

9191
return (
9292
<div>
93-
<input onChange={(e) => setSearch(e.target.value)} />
93+
<input {...register()} />
9494
{searchResult.map((item, key) => (
9595
<li>{JSON.stringify(item)}</li>
9696
))}
@@ -115,6 +115,15 @@ function MyExampleComponent() {
115115
| items | array of strings or objects | | The array of strings or objects to be filtered |
116116
| searchProp | string | yes if each item is object | If each item is an object, it specifies the desired property of the filter |
117117

118+
#### useSearch responses
119+
120+
| Name | Type | Description |
121+
| ------------ | -------------- | ---------------------------------------------------------------------------------------------------------- |
122+
| seach | string | The current value of the specified store |
123+
| setSearch | function | function that updates the specified store |
124+
| register | function | This function returns an object with properties required for registering an input |
125+
| searchResult | array of items | If options include items (and a search property for array of objects), items will filter with search value |
126+
118127
## Contributor ✨
119128

120129
[![Contributors](https://contrib.rocks/image?repo=DevAnsar/react-search-hook)](https://github.com/DevAnsar/react-search-hook/graphs/contributors)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-search-hook",
3-
"version": "0.2.1",
3+
"version": "0.3.0",
44
"description": "Search Library for React",
55
"keywords": [
66
"react",

src/__tests__/hook/useSearch.test.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react'
2-
import { renderHook } from '@testing-library/react'
2+
import { render, renderHook, screen, fireEvent } from '@testing-library/react'
33
import useSearch from '../../hooks/useSearch'
44
import SearchProvider from '../../SearchProvider'
55
import SearchContext from '../../SearchContext'
@@ -67,6 +67,23 @@ describe('useSearch', () => {
6767
expect(result.current.search).toEqual('product_search_value')
6868
})
6969

70+
it('should update search state with register and input', () => {
71+
const fakeSearchState = {
72+
stores: {},
73+
changeStoreValue: jest.fn(),
74+
}
75+
const wrapper = ({ children }: { children: React.ReactNode }) => (
76+
<SearchContext.Provider value={fakeSearchState}>
77+
<SearchProvider stores={['product']}>{children}</SearchProvider>
78+
</SearchContext.Provider>
79+
)
80+
const { result } = renderHook(() => useSearch('product'), { wrapper })
81+
82+
render(<input {...result.current.register()} />)
83+
fireEvent.change(screen.getByRole('textbox'), { target: { value: 'product_search_value' } })
84+
expect(result.current.search).toEqual('product_search_value')
85+
})
86+
7087
it('should filter data with search value (array of string)', () => {
7188
const fakeSearchContextValue: SearchContextType = {
7289
stores: {},

src/hooks/useSearch.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,13 @@ const useSearch = <TItem extends any>(
7171
searchResult = getfilterItems(search, options.items, options.searchProp)
7272
}
7373

74+
// This function returns an object with properties required for registering an input
75+
const register = () => {
76+
return { value: search, onChange: (e: React.ChangeEvent<HTMLInputElement>) => setSearch(e.target.value) }
77+
}
78+
7479
// Return an object containing search-related properties and functions.
75-
return { search, setSearch, searchResult }
80+
return { search, setSearch, register, searchResult }
7681
}
7782

7883
/**

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export interface StoreData<T = string> {
5050
export interface UseSearchResponse<TItem = any> {
5151
search: string
5252
setSearch: (value: string) => void
53+
register: () => {}
5354
searchResult: TItem[]
5455
}
5556

0 commit comments

Comments
 (0)