Skip to content

Commit

Permalink
filter with Items' text
Browse files Browse the repository at this point in the history
  • Loading branch information
anubra266 committed Jul 15, 2021
1 parent c7cff01 commit 565c716
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
19 changes: 12 additions & 7 deletions example/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const App = () => {

return (
<Flex justify="center" pt="150px">
<AutoComplete rollNavigation>
<AutoComplete rollNavigation openOnFocus>
{({ isOpen }) => (
<>
<InputGroup>
Expand All @@ -57,7 +57,6 @@ const App = () => {
{option}
</AutoCompleteItem>
))} */}

<AutoCompleteGroup title="Title" showDivider>
<AutoCompleteItem key="2" value="Apple">
Apple
Expand All @@ -66,11 +65,17 @@ const App = () => {
Appoint
</AutoCompleteItem>
</AutoCompleteGroup>
<AutoCompleteFixedItem value="F1">
Fixed Item 1
</AutoCompleteFixedItem>
<AutoCompleteFixedItem value="F2">
Fixed Item 2
<AutoCompleteGroup title="Titles" showDivider>
<AutoCompleteItem key="2g" value="f2">
Apple
</AutoCompleteItem>
<AutoCompleteItem key="3g" value="appoin">
Appoint
</AutoCompleteItem>
</AutoCompleteGroup>

<AutoCompleteFixedItem value="F3">
Fixed Item 3
</AutoCompleteFixedItem>
</AutoCompleteList>
</>
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,7 @@
"publishConfig": {
"access": "public"
},
"dependencies": {}
"dependencies": {
"react-innertext": "^1.1.5"
}
}
11 changes: 9 additions & 2 deletions src/helpers/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const useOptionsFilter = (props: AutoCompleteInputProps) => {
const filteredItems = options
.filter(
opt =>
opt.value.toLowerCase().indexOf(inputValue.toLowerCase()) > -1 ||
opt.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1 ||
opt.value.trim().length === 0 ||
opt.fixed
)
Expand All @@ -40,7 +40,14 @@ export const useOptionsFilter = (props: AutoCompleteInputProps) => {

useEffect(() => {
const filterPayload = creatable
? [...filteredItems, creatable && { key: 'newInput', value: inputValue }]
? [
...filteredItems,
creatable && {
key: 'newInput',
value: inputValue,
label: inputValue,
},
]
: filteredItems;
dispatch({ type: ItemAction.SetFiltered, payload: filterPayload });
dispatch({ type: ItemAction.ResetActive, payload: false });
Expand Down
3 changes: 3 additions & 0 deletions src/helpers/list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React, { ReactNode, useState } from 'react';
import innerText from 'react-innertext';

import { State, StoreDispatch } from '../store';
import { Item } from '../store/reducers/item';
import { ListAction } from '../store/reducers/list';
Expand Down Expand Up @@ -66,6 +68,7 @@ export const getItemKeys: string[] | any = (children: ReactNode) => {
const getChildProps = (child: any, fixed?: boolean) => ({
key: child.key || child.props.children.toString(),
value: child.props.value || '',
label: innerText(child.props.children),
fixed,
});

Expand Down
1 change: 1 addition & 0 deletions src/store/reducers/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ActionMap, State } from '..';
export interface Item {
key: string;
value: string;
label: string;
fixed?: boolean;
}

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7706,6 +7706,11 @@ react-focus-lock@2.5.0:
use-callback-ref "^1.2.1"
use-sidecar "^1.0.1"

react-innertext@^1.1.5:
version "1.1.5"
resolved "https://registry.yarnpkg.com/react-innertext/-/react-innertext-1.1.5.tgz#8147ac54db3f7067d95f49e2d2c05a720d27d8d0"
integrity sha512-PWAqdqhxhHIv80dT9znP2KvS+hfkbRovFp4zFYHFFlOoQLRiawIic81gKb3U1wEyJZgMwgs3JoLtwryASRWP3Q==

react-is@^16.12.0, react-is@^16.7.0, react-is@^16.8.1:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
Expand Down

0 comments on commit 565c716

Please sign in to comment.