Skip to content
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

fix: add Select Input and update inputs style #647

Merged
merged 2 commits into from
Aug 3, 2023
Merged
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
3 changes: 3 additions & 0 deletions assets/icons/lock.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
232 changes: 232 additions & 0 deletions packages/components/inputs/SelectInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
import React, { ReactElement, useState } from "react";
import {
View,
ScrollView,
StyleSheet,
StyleProp,
ViewStyle,
} from "react-native";

import { Label } from "./TextInputCustom";
import chevronDownSVG from "../../../assets/icons/chevron-down.svg";
import chevronUpSVG from "../../../assets/icons/chevron-up.svg";
import lockSVG from "../../../assets/icons/lock.svg";
import {
neutral00,
neutral33,
neutral77,
neutralA3,
secondaryColor,
} from "../../utils/style/colors";
import { fontMedium13, fontSemibold14 } from "../../utils/style/fonts";
import { layout } from "../../utils/style/layout";
import { BrandText } from "../BrandText";
import { SVG } from "../SVG";
import { CustomPressable } from "../buttons/CustomPressable";
import { SpacerColumn, SpacerRow } from "../spacer";

export type SelectInputDataValue = string | number;

export type SelectInputData = {
label: string;
value: SelectInputDataValue;
iconComponent?: ReactElement;
};

type Props = {
data: SelectInputData[];
placeHolder?: string;
selectedData: SelectInputData;
setData: (data: SelectInputData) => void;
disabled?: boolean;
style?: StyleProp<ViewStyle>;
boxStyle?: StyleProp<ViewStyle>;
label?: string;
isRequired?: boolean;
};

export const SelectInput: React.FC<Props> = ({
data,
placeHolder,
selectedData,
setData,
disabled,
style,
boxStyle,

Check warning on line 55 in packages/components/inputs/SelectInput.tsx

View workflow job for this annotation

GitHub Actions / lint-and-build

Insert `·`
label,
isRequired,
}) => {
const [openMenu, setOpenMenu] = useState<boolean>(false);
const [hoveredIndex, setHoveredIndex] = useState<number>(0);
const [hovered, setHovered] = useState(false);

const getScrollViewStyle = () => {
if (data.length > 5) {
return [styles.dropdownMenu, { height: 200 }];
}
return styles.dropdownMenu;
};

return (
<CustomPressable
style={[style, { position: "relative", zIndex: 999 }]}
onHoverIn={() => setHovered(true)}
onHoverOut={() => setHovered(false)}
onPress={() => {
if (!disabled || data.length) setOpenMenu((value) => !value);
}}
disabled={disabled || !data.length}
>
{label && (
<>
<Label
hovered={hovered}
style={{ color: neutralA3 }}
isRequired={isRequired}
>
{label}
</Label>
<SpacerColumn size={1.5} />
</>
)}
<View>
<View
style={[
styles.selectInput,
hovered && { borderColor: secondaryColor },
boxStyle,
]}
>
<View style={styles.iconLabel}>
{selectedData.iconComponent && (
<>
{selectedData.iconComponent}
<SpacerRow size={1} />
</>
)}

<BrandText
style={[
fontSemibold14,
{ color: selectedData ? secondaryColor : neutral77 },
]}
>
{selectedData?.label ? selectedData.label : placeHolder}
</BrandText>
</View>

<SVG
source={
!data.length || disabled
? lockSVG
: openMenu
? chevronUpSVG
: chevronDownSVG
}
width={16}
height={16}
color={secondaryColor}
/>
</View>

{/*TODO: If the opened menu appears under other elements, you'll may need to set zIndex:-1 or something to these elements*/}
{openMenu && (
<ScrollView style={getScrollViewStyle()}>
{data.map((item, index) => (
<CustomPressable
onHoverIn={() => {
setHoveredIndex(index + 1);
setHovered(true);
}}
onHoverOut={() => {
setHoveredIndex(0);
setHovered(false);
}}
onPress={() => {
setData(item);
setOpenMenu(false);
}}
key={index}
style={styles.dropdownMenuRow}
>
<View
style={[
styles.iconLabel,
hoveredIndex === index + 1 && { opacity: 0.5 },
]}
>
{item.iconComponent && (
<>
{item.iconComponent}
<SpacerRow size={1.5} />
</>
)}

<BrandText style={styles.dropdownMenuText}>
{item.label}
</BrandText>
</View>
</CustomPressable>
))}
</ScrollView>
)}
</View>
</CustomPressable>
);
};

const styles = StyleSheet.create({
selectInputLabel: StyleSheet.flatten([fontSemibold14, { color: neutralA3 }]),
selectInput: {
backgroundColor: neutral00,
fontSize: 14,
fontWeight: 600,
color: secondaryColor,
borderColor: neutral33,
borderWidth: 1,
borderRadius: 12,
padding: layout.padding_x1_5,
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
},
inputContainer: {
backgroundColor: neutral00,
borderWidth: 1,
borderColor: neutral33,
borderRadius: 12,
paddingHorizontal: layout.padding_x1_5,
},
inputItemStyle: {
backgroundColor: "#292929",
color: neutralA3,
paddingVertical: layout.padding_x1_5,
paddingHorizontal: layout.padding_x1,
},
iconLabel: {
flexDirection: "row",
alignItems: "center",
},

dropdownMenu: {
backgroundColor: "#292929",
borderWidth: 1,
borderColor: neutral33,
borderRadius: 12,
padding: layout.padding_x1,
position: "absolute",
top: 52,
width: "100%",
zIndex: 10,
},
dropdownMenuText: StyleSheet.flatten([fontMedium13]),
dropdownMenuRow: {
borderRadius: 6,
padding: layout.padding_x1,
},
// dropdownMenuRow: {
// backgroundColor: neutral00,
// borderRadius: 6,
// padding: layout.padding_x1,
// },
});
Loading
Loading