-
Notifications
You must be signed in to change notification settings - Fork 750
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
feat: Implement dropdown component with search #13
Closed
memoyil
wants to merge
9
commits into
pancakeswap:master
from
memoyil:feat/implement_dropdown_search_component
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
81ac2e9
feat: Implement dropdown component with search
memoyil 58ece8a
fix: Input
memoyil fef93a8
fix: Null assertions
memoyil cc84f7d
fix: Format
memoyil 71591a7
fix: Format
memoyil f11c32e
fix: Format
memoyil 3aac0c3
fix: Change name
memoyil f1412fe
fix: On change
memoyil 717e43f
fix: Visit comments
memoyil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
172 changes: 172 additions & 0 deletions
172
packages/pancake-uikit/src/components/DropdownSearch/DropdownSearch.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
import React, { useState, useRef, useEffect } from "react"; | ||
import styled, { css } from "styled-components"; | ||
import { Input } from "../Input"; | ||
import { Text } from "../Text"; | ||
import { ArrowDropDownIcon } from "../Svg"; | ||
import { DropdownSearchOptionProps, DropdownSearchProps } from "./types"; | ||
|
||
const StyledInput = styled(Input)` | ||
border: 1px solid ${({ theme }) => theme.colors.inputSecondary}; | ||
box-shadow: ${({ theme }) => theme.tooltip.boxShadow}; | ||
margin-left: auto; | ||
`; | ||
|
||
const DropDownHeader = styled.div` | ||
width: 100%; | ||
height: 40px; | ||
display: flex; | ||
align-items: center; | ||
justify-content: space-between; | ||
padding: 0 16px; | ||
box-shadow: ${({ theme }) => theme.shadows.inset}; | ||
border: 1px solid ${({ theme }) => theme.colors.inputSecondary}; | ||
border-radius: 16px; | ||
background: ${({ theme }) => theme.colors.input}; | ||
transition: border-radius 0.15s; | ||
`; | ||
|
||
const DropDownListContainer = styled.div` | ||
min-width: 136px; | ||
height: 0; | ||
position: absolute; | ||
overflow: hidden; | ||
background: ${({ theme }) => theme.colors.input}; | ||
z-index: ${({ theme }) => theme.zIndices.dropdown}; | ||
transition: transform 0.15s, opacity 0.15s; | ||
transform: scaleY(0); | ||
transform-origin: top; | ||
opacity: 0; | ||
|
||
${({ theme }) => theme.mediaQueries.sm} { | ||
min-width: 168px; | ||
} | ||
`; | ||
|
||
const DropDownContainer = styled.div<{ isOpen: boolean; width: number; height: number }>` | ||
cursor: pointer; | ||
width: ${({ width }) => width}px; | ||
position: relative; | ||
background: ${({ theme }) => theme.colors.input}; | ||
border-radius: 16px; | ||
height: 40px; | ||
min-width: 136px; | ||
|
||
${({ theme }) => theme.mediaQueries.sm} { | ||
min-width: 168px; | ||
} | ||
|
||
${(props) => | ||
props.isOpen && | ||
css` | ||
${DropDownHeader} { | ||
border-bottom: 1px solid ${({ theme }) => theme.colors.inputSecondary}; | ||
box-shadow: ${({ theme }) => theme.tooltip.boxShadow}; | ||
border-radius: 16px 16px 0 0; | ||
} | ||
|
||
${DropDownListContainer} { | ||
height: auto; | ||
transform: scaleY(1); | ||
opacity: 1; | ||
border: 1px solid ${({ theme }) => theme.colors.inputSecondary}; | ||
border-top-width: 0; | ||
border-radius: 0 0 16px 16px; | ||
box-shadow: ${({ theme }) => theme.tooltip.boxShadow}; | ||
} | ||
|
||
${StyledInput} { | ||
border-radius: 16px 16px 0 0; | ||
} | ||
`} | ||
svg { | ||
position: absolute; | ||
right: 16px; | ||
top: 50%; | ||
transform: translateY(-50%); | ||
} | ||
`; | ||
|
||
const DropDownList = styled.ul` | ||
padding: 0; | ||
margin: 0; | ||
box-sizing: border-box; | ||
z-index: ${({ theme }) => theme.zIndices.dropdown}; | ||
`; | ||
|
||
const ListItem = styled.li` | ||
list-style: none; | ||
padding: 8px 16px; | ||
|
||
&:hover { | ||
background: ${({ theme }) => theme.colors.inputSecondary}; | ||
} | ||
`; | ||
|
||
const DropdownSearch: React.FunctionComponent<DropdownSearchProps> = ({ options, onChange }) => { | ||
const dropdownRef = useRef<HTMLUListElement>(null); | ||
const searchRef = useRef<HTMLInputElement>(null); | ||
const [isOpen, setIsOpen] = useState(false); | ||
const [selectedOption, setSelectedOption] = useState(options[0]); | ||
const [containerSize, setContainerSize] = useState({ width: 0, height: 0 }); | ||
const [content, setContent] = useState(options); | ||
|
||
const toggling = () => setIsOpen(!isOpen); | ||
|
||
const onOptionClicked = (option: DropdownSearchOptionProps) => () => { | ||
setSelectedOption(option); | ||
setIsOpen(false); | ||
|
||
if (onChange) { | ||
onChange(option); | ||
} | ||
setContent(options); | ||
if (searchRef.current) { | ||
searchRef.current.value = option.label; | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (dropdownRef.current) { | ||
setContainerSize({ | ||
width: dropdownRef.current.offsetWidth, // Consider border | ||
height: dropdownRef.current.offsetHeight, | ||
}); | ||
} | ||
}, [dropdownRef]); | ||
|
||
const handleChangeQuery = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const filteredContent = options.filter((option) => | ||
option.label.toLowerCase().includes(event.target.value.toLowerCase()) | ||
); | ||
|
||
if (filteredContent !== undefined) { | ||
setIsOpen(true); | ||
setContent(filteredContent); | ||
} | ||
}; | ||
|
||
return ( | ||
<DropDownContainer isOpen={isOpen} {...containerSize}> | ||
{containerSize.width !== 0 && ( | ||
<StyledInput | ||
ref={searchRef} | ||
defaultValue={selectedOption.label} | ||
onChange={handleChangeQuery} | ||
placeholder="Hint Text" | ||
/> | ||
)} | ||
<ArrowDropDownIcon color="text" onClick={toggling} /> | ||
<DropDownListContainer> | ||
<DropDownList ref={dropdownRef}> | ||
{content.map((option) => ( | ||
<ListItem onClick={onOptionClicked(option)} key={option.label}> | ||
<Text>{option.label}</Text> | ||
</ListItem> | ||
))} | ||
</DropDownList> | ||
</DropDownListContainer> | ||
</DropDownContainer> | ||
); | ||
}; | ||
|
||
export default DropdownSearch; |
45 changes: 45 additions & 0 deletions
45
packages/pancake-uikit/src/components/DropdownSearch/index.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React from "react"; | ||
import Button from "../Button/Button"; | ||
import DropdownSearch from "./DropdownSearch"; | ||
|
||
export default { | ||
title: "Components/DropdownSearch", | ||
component: DropdownSearch, | ||
argTypes: {}, | ||
}; | ||
|
||
export const Default: React.FC = () => { | ||
return ( | ||
<div> | ||
<DropdownSearch | ||
options={[ | ||
{ | ||
label: "Hot", | ||
value: "hot", | ||
}, | ||
{ | ||
label: "APR", | ||
value: "apr", | ||
}, | ||
{ | ||
label: "Multiplier", | ||
value: "multiplier", | ||
}, | ||
{ | ||
label: "Earned", | ||
value: "earned", | ||
}, | ||
{ | ||
label: "Liquidity", | ||
value: "liquidity", | ||
}, | ||
]} | ||
target={<Button>Hover</Button>} | ||
> | ||
{[...Array(30)].map(() => ( | ||
<div>Content</div> | ||
))} | ||
</DropdownSearch> | ||
</div> | ||
); | ||
}; |
2 changes: 2 additions & 0 deletions
2
packages/pancake-uikit/src/components/DropdownSearch/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as Dropdown } from "./DropdownSearch"; | ||
export type { DropdownSearchProps, DropdownSearchOptionProps } from "./types"; |
11 changes: 11 additions & 0 deletions
11
packages/pancake-uikit/src/components/DropdownSearch/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { ReactText } from "react"; | ||
|
||
export interface DropdownSearchProps { | ||
options: DropdownSearchOptionProps[]; | ||
onChange?: (option: DropdownSearchOptionProps) => void; | ||
} | ||
|
||
export interface DropdownSearchOptionProps { | ||
label: string; | ||
value: ReactText; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please put
dropdownRef
in the dependency listThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added