Skip to content

Refactor Search Style and more #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 70 additions & 31 deletions src/components/Dropdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const DropdownComponent: <T>(
inverted = true,
renderLeftIcon,
renderRightIcon,
renderSelectedItem,
renderItem,
renderInputSearch,
onFocus,
Expand Down Expand Up @@ -450,33 +451,37 @@ const DropdownComponent: <T>(
accessibilityLabel={accessibilityLabel}
onPress={showOrClose}
>
<View style={styles.dropdown}>
{renderLeftIcon?.(visible)}
<Text
style={[
styles.textItem,
isSelected !== null ? selectedTextStyle : placeholderStyle,
font(),
]}
{...selectedTextProps}
>
{isSelected !== null
? _get(currentValue, labelField)
: placeholder}
</Text>
{renderRightIcon ? (
renderRightIcon(visible)
) : (
<Image
source={ic_down}
style={StyleSheet.flatten([
styles.icon,
{ tintColor: iconColor },
iconStyle,
])}
/>
)}
</View>
{renderSelectedItem ? (
renderSelectedItem(visible)
) : (
<View style={styles.dropdown}>
{renderLeftIcon?.(visible)}
<Text
style={[
styles.textItem,
isSelected !== null ? selectedTextStyle : placeholderStyle,
font(),
]}
{...selectedTextProps}
>
{isSelected !== null
? _get(currentValue, labelField)
: placeholder}
</Text>
{renderRightIcon ? (
renderRightIcon(visible)
) : (
<Image
source={ic_down}
style={StyleSheet.flatten([
styles.icon,
{ tintColor: iconColor },
iconStyle,
])}
/>
)}
</View>
)}
</TouchableWithoutFeedback>
);
};
Expand Down Expand Up @@ -552,11 +557,13 @@ const DropdownComponent: <T>(
onSearch(text);
});
} else {
const { height } = position;

return (
<CInput
testID={testID + ' input'}
accessibilityLabel={accessibilityLabel + ' input'}
style={[styles.input, inputSearchStyle]}
style={[styles.input, inputSearchStyle, { height: height }]}
inputStyle={[inputSearchStyle, font()]}
value={searchText}
autoCorrect={false}
Expand Down Expand Up @@ -590,13 +597,23 @@ const DropdownComponent: <T>(
searchPlaceholderTextColor,
testID,
searchText,
position,
]);

const _renderList = useCallback(
(isTopPosition: boolean) => {
const isInverted = !inverted ? false : isTopPosition;

const _renderListHelper = () => {
let customList = listData;
if (customList.length === 0) {
let customNotFound: any = { _index: 0 };
customNotFound[valueField] = null;
customNotFound[labelField] = 'Not Found';

customList.push(customNotFound);
}

return (
<FlatList
testID={testID + ' flatlist'}
Expand All @@ -605,7 +622,7 @@ const DropdownComponent: <T>(
keyboardShouldPersistTaps="handled"
ref={refList}
onScrollToIndexFailed={scrollIndex}
data={listData}
data={customList}
inverted={isTopPosition ? inverted : false}
renderItem={_renderItem}
keyExtractor={(_item, index) => index.toString()}
Expand All @@ -618,7 +635,7 @@ const DropdownComponent: <T>(
<TouchableWithoutFeedback>
<View style={styles.flexShrink}>
{isInverted && _renderListHelper()}
{renderSearch()}
{/* {renderSearch()} */}
{!isInverted && _renderListHelper()}
</View>
</TouchableWithoutFeedback>
Expand All @@ -630,16 +647,19 @@ const DropdownComponent: <T>(
flatListProps,
listData,
inverted,
renderSearch,
// renderSearch,
scrollIndex,
showsVerticalScrollIndicator,
testID,
labelField,
valueField,
]
);

const _renderModal = useCallback(() => {
if (visible && position) {
const { isFull, width, height, top, bottom, left } = position;
// console.log(position);

const onAutoPosition = () => {
if (keyboardHeight > 0) {
Expand All @@ -663,6 +683,12 @@ const DropdownComponent: <T>(
let keyboardStyle: ViewStyle = {};

let extendHeight = !isTopPosition ? top : bottom;
if (search) {
extendHeight = !isTopPosition
? top - height - 2
: bottom - height * 0.5;
}

if (
keyboardAvoiding &&
keyboardHeight > 0 &&
Expand Down Expand Up @@ -701,6 +727,18 @@ const DropdownComponent: <T>(
isFull && styles.fullScreen,
])}
>
<View
style={StyleSheet.flatten([
// styles.container,
// isFull ? styleHorizontal : styleVertical,
{
left: left,
width: width,
},
])}
>
{renderSearch()}
</View>
<View
style={StyleSheet.flatten([
styles.container,
Expand Down Expand Up @@ -737,6 +775,7 @@ const DropdownComponent: <T>(
containerStyle,
styleHorizontal,
_renderList,
renderSearch,
]);

return (
Expand Down
1 change: 1 addition & 0 deletions src/components/Dropdown/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface DropdownProps<T> {
excludeItems?: T[];
excludeSearchItems?: T[];
onChange: (item: T) => void;
renderSelectedItem?: (visible?: boolean) => JSX.Element | null | undefined;
renderLeftIcon?: (visible?: boolean) => JSX.Element | null | undefined;
renderRightIcon?: (visible?: boolean) => JSX.Element | null | undefined;
renderItem?: (item: T, selected?: boolean) => JSX.Element | null | undefined;
Expand Down
9 changes: 6 additions & 3 deletions src/components/Dropdown/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const styles = StyleSheet.create({
flexDirection: I18nManager.isRTL ? 'row-reverse' : 'row',
justifyContent: 'space-between',
alignItems: 'center',
columnGap: 10,
},
title: {
marginVertical: 5,
Expand All @@ -53,11 +54,13 @@ export const styles = StyleSheet.create({
height: 20,
},
input: {
borderWidth: 0.5,
borderColor: '#DDDDDD',
borderColor: 'gray',
borderWidth: 1,
borderRadius: 8,
paddingHorizontal: 8,
marginBottom: 8,
margin: 6,
// margin: 6,
backgroundColor: 'white',
height: 45,
},
fullScreen: {
Expand Down