Skip to content

Commit

Permalink
Merge pull request #108 from cervisebas/main
Browse files Browse the repository at this point in the history
feat: add focus and blur methods by reference
  • Loading branch information
fateh999 authored Jul 21, 2024
2 parents 9d5baad + e169b9b commit 5141683
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 10 deletions.
25 changes: 24 additions & 1 deletion Example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useMemo, useState } from 'react';
import { useMemo, useRef, useState } from 'react';
import { ScrollView, StyleSheet, View, ViewStyle } from 'react-native';
import {
Appbar,
Button,
Divider,
Headline,
MD3DarkTheme,
Expand All @@ -17,6 +18,7 @@ import {
MultiSelectDropdown,
DropdownInputProps,
DropdownItemProps,
DropdownRef,
} from 'react-native-paper-dropdown';

const OPTIONS = [
Expand Down Expand Up @@ -119,6 +121,7 @@ export default function App() {
const [nightMode, setNightmode] = useState(false);
const [gender, setGender] = useState<string>();
const [colors, setColors] = useState<string[]>([]);
const refDropdown1 = useRef<DropdownRef>(null);
const Theme = nightMode ? MD3DarkTheme : MD3LightTheme;

return (
Expand Down Expand Up @@ -146,6 +149,7 @@ export default function App() {
<View style={styles.spacer} />
<Paragraph>Default Dropdown</Paragraph>
<Dropdown
ref={refDropdown1}
label={'Gender'}
placeholder="Select Gender"
options={OPTIONS}
Expand Down Expand Up @@ -214,6 +218,25 @@ export default function App() {
onSelect={setColors}
mode={'outlined'}
/>

<View style={styles.spacer} />
<View style={styles.spacer} />

<Headline>References</Headline>
<View style={styles.spacer} />
<Button
mode={'contained'}
onPress={() => refDropdown1.current?.focus()}
>
Focus
</Button>
<View style={styles.spacer} />
<Button
mode={'contained'}
onPress={() => refDropdown1.current?.blur()}
>
Blur
</Button>
</View>
</ScrollView>
</View>
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ export default function App() {
| `disabled` | `boolean` | Whether the dropdown is disabled. |
| `error` | `boolean` | Whether the dropdown has an error. |

## Methods

| Method | Return | Description |
| ----------- | ------ | ------------------------------ |
| `focus()` | `void` | Open the dropdown manually. |
| `blur()` | `void` | Close the dropdown manually. |

## Latest Documentation

- [https://fateh999.github.io/react-native-paper-dropdown](https://fateh999.github.io/react-native-paper-dropdown)
Expand Down
7 changes: 7 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,13 @@ const styles = StyleSheet.create({
| `disabled` | `boolean` | Whether the dropdown is disabled. |
| `error` | `boolean` | Whether the dropdown has an error. |
## Methods
| Method | Return | Description |
| ----------- | ------ | ------------------------------ |
| `focus()` | `void` | Open the dropdown manually. |
| `blur()` | `void` | Close the dropdown manually. |
## Customization
You can customize the appearance and behavior of the dropdowns by using the provided props like `menuUpIcon`, `menuDownIcon`, `CustomDropdownItem`, `CustomDropdownInput`, and `menuContentStyle`.
Expand Down
12 changes: 10 additions & 2 deletions src/dropdown-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ import { TextInput } from 'react-native-paper';
import { DropdownInputProps } from './types';

function DropdownInput(props: DropdownInputProps) {
const { placeholder, label, rightIcon, selectedLabel, mode, disabled } =
props;
const {
placeholder,
label,
rightIcon,
selectedLabel,
mode,
disabled,
error,
} = props;

return (
<TextInput
Expand All @@ -14,6 +21,7 @@ function DropdownInput(props: DropdownInputProps) {
mode={mode}
editable={false}
disabled={disabled}
error={error}
/>
);
}
Expand Down
17 changes: 14 additions & 3 deletions src/dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { ScrollView, View } from 'react-native';
import { Menu, TextInput, TouchableRipple } from 'react-native-paper';
import DropdownItem from './dropdown-item';
import DropdownInput from './dropdown-input';
import { DropdownProps } from './types';
import { DropdownProps, DropdownRef } from './types';
import useDropdown from './use-dropdown';
import { forwardRef, useImperativeHandle } from 'react';

function Dropdown(props: DropdownProps) {
function Dropdown(props: DropdownProps, ref: React.Ref<DropdownRef>) {
const {
options,
mode,
Expand All @@ -28,6 +29,7 @@ function Dropdown(props: DropdownProps) {
const selectedLabel = options.find((option) => option.value === value)?.label;
const {
enable,
setEnable,
toggleMenu,
onLayout,
menuStyle,
Expand All @@ -36,6 +38,15 @@ function Dropdown(props: DropdownProps) {
} = useDropdown(maxMenuHeight);
const rightIcon = enable ? menuUpIcon : menuDownIcon;

useImperativeHandle(ref, () => ({
focus() {
setEnable(true);
},
blur() {
setEnable(false);
},
}));

return (
<Menu
visible={enable}
Expand Down Expand Up @@ -86,4 +97,4 @@ function Dropdown(props: DropdownProps) {
);
}

export default Dropdown;
export default forwardRef(Dropdown);
2 changes: 2 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Option,
MultiSelectDropdownProps,
MultiSelectDropdownItemProps,
DropdownRef,
} from './types';

export {
Expand All @@ -24,4 +25,5 @@ export {
Option,
MultiSelectDropdownProps,
MultiSelectDropdownItemProps,
DropdownRef,
};
21 changes: 17 additions & 4 deletions src/multi-select-dropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { useMemo } from 'react';
import { forwardRef, useImperativeHandle, useMemo } from 'react';
import { ScrollView, View } from 'react-native';
import { Menu, TextInput, TouchableRipple } from 'react-native-paper';
import DropdownInput from './dropdown-input';
import MultiSelectDropdownItem from './multi-select-dropdown-item';
import { MultiSelectDropdownProps } from './types';
import { DropdownRef, MultiSelectDropdownProps } from './types';
import useDropdown from './use-dropdown';

function MultiSelectDropdown(props: MultiSelectDropdownProps) {
function MultiSelectDropdown(
props: MultiSelectDropdownProps,
ref: React.Ref<DropdownRef>
) {
const {
options,
mode,
Expand Down Expand Up @@ -37,6 +40,7 @@ function MultiSelectDropdown(props: MultiSelectDropdownProps) {
);
const {
enable,
setEnable,
toggleMenu,
onLayout,
menuStyle,
Expand All @@ -45,6 +49,15 @@ function MultiSelectDropdown(props: MultiSelectDropdownProps) {
} = useDropdown(maxMenuHeight);
const rightIcon = enable ? menuUpIcon : menuDownIcon;

useImperativeHandle(ref, () => ({
focus() {
setEnable(true);
},
blur() {
setEnable(false);
},
}));

return (
<Menu
testID={menuTestID}
Expand Down Expand Up @@ -94,4 +107,4 @@ function MultiSelectDropdown(props: MultiSelectDropdownProps) {
);
}

export default MultiSelectDropdown;
export default forwardRef(MultiSelectDropdown);
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,8 @@ export type MultiSelectDropdownItemProps = {
isLast: boolean;
menuItemTestID?: string;
};

export type DropdownRef = {
blur: () => void;
focus: () => void;
};
1 change: 1 addition & 0 deletions src/use-dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function useDropdown(maxMenuHeight?: number, maxHeightFraction: number = 2.5) {

return {
enable,
setEnable,
toggleMenu,
onLayout,
menuStyle,
Expand Down

0 comments on commit 5141683

Please sign in to comment.