Skip to content

Commit

Permalink
feat(autocomplete): add shouldRenderSuggestions prop
Browse files Browse the repository at this point in the history
  • Loading branch information
anubra266 committed Jun 6, 2021
1 parent cbc1515 commit f2588b8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,27 @@ type onSelectOption= (params: {
<td>If true, the menu will close when an item is selected, by mouse or keyboard.</td>
<td>No</td>
<td>true</td>
</tr>
<tr>
<td>shouldRenderSuggestions</td>
<td>

`(value: string) => boolean`

</td>
<td>

By default, suggestions are rendered when the input isn't blank. Feel free to override this behaviour. This function gets the current value of the input

e.g. The following function is to show the suggestions only if the input has more than two characters.
```js
function shouldRenderSuggestions(value) {
return value.trim().length > 2;
}
```
</td>
<td>No</td>
<td>&mdash;&mdash;&mdash;</td>
</tr>
</tbody>
</table></div>
Expand Down
8 changes: 7 additions & 1 deletion example/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
AutoCompleteList,
AutoCompleteChildProps,
AutoCompleteFixedItem,
AutoCompleteProps,
} from '../.';
import { Button, Flex, InputGroup, InputRightElement } from '@chakra-ui/react';
import { useTheme } from '@chakra-ui/system';
Expand All @@ -19,6 +20,10 @@ const App = () => {
// console.log(e.target.value);
// setOutValue(e.target.value);
// };
const shouldRenderSuggestions: AutoCompleteProps['shouldRenderSuggestions'] = value => {
return value.trim().length > 2;
};

return (
<Flex justify="center" pt="150px">
<AutoComplete
Expand All @@ -28,8 +33,9 @@ const App = () => {
emphasize
// freeSolo
// creatable
// suggestWhenEmpty
suggestWhenEmpty
// closeOnselect={false}
shouldRenderSuggestions={shouldRenderSuggestions}
>
<AutoCompleteInput variant="filled" placeholder="Search..." autoFocus />
<AutoCompleteList>
Expand Down
8 changes: 6 additions & 2 deletions src/auto-complete-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const AutoCompleteInput = forwardRef<AutoCompleteInputProps, 'input'>(
closeOnBlur,
closeOnselect,
value: autoCompleteValue,
shouldRenderSuggestions,
} = autocomplete;

const isEmpty = item.filtered.length < 1 && !emptyState;
Expand All @@ -57,8 +58,11 @@ export const AutoCompleteInput = forwardRef<AutoCompleteInputProps, 'input'>(
if (freeSolo) dispatch({ type: AutoCompleteAction.Set, payload: value });
const inputIsEmpty = value?.length < 1;
if (!isEmpty) {
if (!inputIsEmpty) dispatch({ type: ListAction.Show });
else if (!suggestWhenEmpty) hideList();
if (!inputIsEmpty) {
if (runIfFn(shouldRenderSuggestions, value))
dispatch({ type: ListAction.Show });
else dispatch({ type: ListAction.Hide });
} else if (!suggestWhenEmpty) hideList();
}
};

Expand Down
7 changes: 5 additions & 2 deletions src/auto-complete-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import { inputReducer } from './store/reducers/input';
import { itemReducer } from './store/reducers/item';
import { listReducer } from './store/reducers/list';

export type ChildrenProps = {
export type AutoCompleteChildProps = {
isOpen: boolean;
onClose: () => void;
inputIsEmpty: boolean;
resetInput: () => void;
};

export interface AutoComplete extends Omit<BoxProps, 'onChange'> {
children?: MaybeRenderProp<ChildrenProps>;
children?: MaybeRenderProp<AutoCompleteChildProps>;
onChange?: (value: string) => void;
emptyState?: boolean | ReactNode;
rollNavigation?: boolean;
Expand All @@ -36,6 +36,7 @@ export interface AutoComplete extends Omit<BoxProps, 'onChange'> {
suggestWhenEmpty?: boolean;
closeOnselect?: boolean;
closeOnBlur?: boolean;
shouldRenderSuggestions?: (value: string) => boolean;
}

export type AutoCompleteProps = AutoComplete;
Expand All @@ -57,6 +58,7 @@ export const AutoComplete = forwardRef<AutoCompleteProps, 'div'>(
suggestWhenEmpty,
closeOnselect = true,
closeOnBlur = true,
shouldRenderSuggestions,
...rest
} = props;

Expand All @@ -76,6 +78,7 @@ export const AutoComplete = forwardRef<AutoCompleteProps, 'div'>(
suggestWhenEmpty,
closeOnselect,
closeOnBlur,
shouldRenderSuggestions,
},
input: {
value: '',
Expand Down
5 changes: 1 addition & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
export {
AutoComplete,
ChildrenProps as AutoCompleteChildProps,
} from './auto-complete-provider';
export * from './auto-complete-provider';
export * from './auto-complete-input';
export * from './auto-complete-list';
export * from './auto-complete-item';
Expand Down

0 comments on commit f2588b8

Please sign in to comment.