-
Notifications
You must be signed in to change notification settings - Fork 22
[FEQ]Yaswanth/FEQ-1440/Added Dropdown component #27
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
Merged
shayan-deriv
merged 9 commits into
deriv-com:main
from
yaswanth-deriv:yaswanth/FEQ-1440_Add-DropDown-Component
Jan 31, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
82dd5af
refactor: Added dropdown component
yaswanth-deriv 3ddfcd7
refactor: added storybook for dropdown
yaswanth-deriv 9f63186
refactor: added storybook for dropdown
yaswanth-deriv 0d1b9d3
Merge remote-tracking branch 'origin' into yaswanth/FEQ-1440_Add-Drop…
yaswanth-deriv c4f1302
Merge branch 'deriv-com:main' into yaswanth/FEQ-1440_Add-DropDown-Com…
yaswanth-deriv 75b99bc
refactor: To remove exported types
yaswanth-deriv 912cee8
Merge branch 'deriv-com:main' into yaswanth/FEQ-1440_Add-DropDown-Com…
yaswanth-deriv 61e32b7
refactor: To remove exported types
yaswanth-deriv bc36051
refactor: To remove exported types
yaswanth-deriv 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
This file contains hidden or 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,145 @@ | ||
.deriv-dropdown { | ||
width: 100%; | ||
position: relative; | ||
cursor: pointer; | ||
|
||
&--disabled { | ||
pointer-events: none; | ||
|
||
& label { | ||
color: var(--system-light-5-active-background, #999); | ||
} | ||
} | ||
|
||
&__button { | ||
all: unset; | ||
right: 1.6rem; | ||
transform: rotate(0); | ||
transform-origin: 50% 45%; | ||
transition: transform 0.2s cubic-bezier(0.25, 0.1, 0.25, 1); | ||
|
||
&--active { | ||
transform: rotate(180deg); | ||
} | ||
} | ||
&__content { | ||
width: 100%; | ||
background: var(--system-light-8-primary-background, #fff); | ||
display: flex; | ||
align-items: center; | ||
|
||
.deriv-textfield__field { | ||
cursor: pointer; | ||
} | ||
} | ||
|
||
&__field { | ||
position: absolute; | ||
inset: 0; | ||
min-width: 0; /* this is required to reset input's default width */ | ||
padding-left: 2rem; | ||
display: flex; | ||
flex-grow: 1; | ||
font-family: inherit; | ||
outline: 0; | ||
font-size: 1.4rem; | ||
background-color: transparent; | ||
color: var(--system-light-2-general-text, #333); | ||
transition: border-color 0.2s; | ||
cursor: unset; | ||
user-select: none; | ||
&::selection { | ||
background-color: transparent; | ||
} | ||
|
||
&::placeholder { | ||
color: transparent; | ||
} | ||
} | ||
|
||
&__field:placeholder-shown ~ &__label { | ||
font-size: 1.4rem; | ||
cursor: text; | ||
top: 30%; | ||
padding: 0; | ||
} | ||
|
||
&__field:placeholder-shown ~ &__label--with-icon { | ||
left: 4.4rem; | ||
} | ||
|
||
label, | ||
&__field:focus ~ &__label { | ||
display: inline-block; | ||
position: absolute; | ||
top: 0; | ||
bottom: 0; | ||
left: 16px; | ||
display: flex; | ||
align-items: center; | ||
pointer-events: none; | ||
text-transform: capitalize; | ||
transition: all 0.15s ease-out; | ||
padding: 0; | ||
} | ||
|
||
&__field:focus ~ &__label { | ||
color: var(--brand-blue, #85acb0); | ||
} | ||
|
||
&__items { | ||
position: absolute; | ||
top: 2.7rem; | ||
width: 100%; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: flex-start; | ||
z-index: 2; | ||
border-radius: 0.4rem; | ||
background: var(--system-light-8-primary-background, #fff); | ||
box-shadow: 0 3.2rem 6.4rem 0 rgba(14, 14, 14, 0.14); | ||
overflow-y: auto; | ||
|
||
& > :first-child { | ||
border-radius: 0.4rem 0.4rem 0 0; | ||
} | ||
|
||
& > :last-child { | ||
border-radius: 0 0 0.4rem 0.4rem; | ||
} | ||
|
||
&--sm { | ||
max-height: 22rem; | ||
} | ||
|
||
&--md { | ||
max-height: 42rem; | ||
} | ||
|
||
&--lg { | ||
max-height: 66rem; | ||
} | ||
} | ||
|
||
&__icon { | ||
position: absolute; | ||
left: 1.6rem; | ||
width: 1.6rem; | ||
height: 1.6rem; | ||
} | ||
|
||
&__item { | ||
padding: 10px 16px; | ||
width: 100%; | ||
z-index: 2; | ||
|
||
&:hover:not(&--active) { | ||
cursor: pointer; | ||
background: var(--system-light-6-hover-background, #e6e9e9); | ||
} | ||
|
||
&--active { | ||
background: var(--system-light-5-active-background, #d6dadb); | ||
} | ||
} | ||
} |
This file contains hidden or 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,158 @@ | ||
import React, { isValidElement, useCallback, useEffect, useState } from 'react'; | ||
import clsx from 'clsx'; | ||
import { useCombobox } from 'downshift'; | ||
import { TGenericSizes } from "../../types"; | ||
import { Text } from '../Text'; | ||
import {Input } from '../Input/index'; | ||
import './Dropdown.scss'; | ||
|
||
type InputProps = React.ComponentProps<typeof Input>; | ||
type TProps = { | ||
disabled?: boolean; | ||
dropdownIcon: React.ReactNode; | ||
errorMessage?: InputProps['message']; | ||
icon?: React.ReactNode; | ||
isRequired?: boolean; | ||
label?: InputProps['label']; | ||
list: { | ||
text?: React.ReactNode; | ||
value?: string; | ||
}[]; | ||
listHeight?: Extract<TGenericSizes, 'lg' | 'md' | 'sm'>; | ||
shayan-deriv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
name: InputProps['name']; | ||
onChange?: (inputValue: string) => void; | ||
onSelect: (value: string) => void; | ||
value?: InputProps['value']; | ||
variant?: 'comboBox' | 'prompt'; | ||
}; | ||
|
||
export const Dropdown = ({ | ||
disabled, | ||
dropdownIcon, | ||
errorMessage, | ||
icon = false, | ||
label, | ||
list, | ||
listHeight = 'md', | ||
name, | ||
onChange, | ||
onSelect, | ||
value, | ||
variant = 'prompt', | ||
}:TProps) => { | ||
const [items, setItems] = useState(list); | ||
const [shouldFilterList, setShouldFilterList] = useState(false); | ||
const clearFilter = useCallback(() => { | ||
setShouldFilterList(false); | ||
setItems(list); | ||
}, [list]); | ||
const reactNodeToString = function (reactNode: React.ReactNode): string { | ||
let string = ''; | ||
if (typeof reactNode === 'string') { | ||
string = reactNode; | ||
} else if (typeof reactNode === 'number') { | ||
string = reactNode.toString(); | ||
} else if (reactNode instanceof Array) { | ||
reactNode.forEach(function (child) { | ||
string += reactNodeToString(child); | ||
}); | ||
} else if (isValidElement(reactNode)) { | ||
string += reactNodeToString(reactNode.props.children); | ||
} | ||
return string; | ||
}; | ||
const { closeMenu, getInputProps, getItemProps, getMenuProps, getToggleButtonProps, isOpen, openMenu } = | ||
useCombobox({ | ||
shayan-deriv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
defaultSelectedItem: items.find(item => item.value === value) ?? null, | ||
items, | ||
itemToString(item) { | ||
return item ? reactNodeToString(item.text) : ''; | ||
}, | ||
onInputValueChange({ inputValue }) { | ||
onChange?.(inputValue ?? ''); | ||
if (shouldFilterList) { | ||
setItems( | ||
list.filter(item => | ||
reactNodeToString(item.text) | ||
.toLowerCase() | ||
.includes(inputValue?.toLowerCase() ?? '') | ||
) | ||
); | ||
} | ||
}, | ||
onIsOpenChange({ isOpen }) { | ||
if (!isOpen) { | ||
clearFilter(); | ||
} | ||
}, | ||
onSelectedItemChange({ selectedItem }) { | ||
onSelect(selectedItem?.value ?? ''); | ||
closeMenu(); | ||
}, | ||
}); | ||
|
||
const handleInputClick = useCallback(() => { | ||
variant === 'comboBox' && setShouldFilterList(true); | ||
|
||
if (isOpen) { | ||
closeMenu(); | ||
} else { | ||
openMenu(); | ||
} | ||
}, [closeMenu, isOpen, openMenu, variant]); | ||
|
||
useEffect(() => { | ||
setItems(list); | ||
}, [list]); | ||
|
||
return ( | ||
<div | ||
className={clsx('deriv-dropdown', { | ||
'deriv-dropdown--disabled': disabled, | ||
})} | ||
{...getToggleButtonProps()} | ||
> | ||
<div className='deriv-dropdown__content'> | ||
<Input | ||
disabled={disabled} | ||
message={errorMessage} | ||
label={reactNodeToString(label)} | ||
name={name} | ||
onClickCapture={handleInputClick} | ||
onKeyUp={() => setShouldFilterList(true)} | ||
readOnly={variant !== 'comboBox'} | ||
leftPlaceholder={icon ? icon : undefined} | ||
rightPlaceholder={ ( | ||
<button | ||
className={clsx('deriv-dropdown__button', { | ||
'deriv-dropdown__button--active': isOpen, | ||
})} | ||
> | ||
{dropdownIcon} | ||
</button> | ||
)} | ||
type='text' | ||
value={value} | ||
{...getInputProps()} | ||
/> | ||
</div> | ||
<ul className={`deriv-dropdown__items deriv-dropdown__items--${listHeight}`} {...getMenuProps()}> | ||
{isOpen && | ||
items.map((item, index) => ( | ||
<li | ||
className={clsx('deriv-dropdown__item', { | ||
'deriv-dropdown__item--active': value === item.value, | ||
})} | ||
key={item.value} | ||
onClick={() => clearFilter()} | ||
{...getItemProps({ index, item })} | ||
> | ||
<Text size='sm' weight={value === item.value ? 'bold' : 'normal'}> | ||
{item.text} | ||
</Text> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
}; |
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -43,4 +43,3 @@ export const Text = ({ | |
|
||
return <Tag className={textClassNames}>{children}</Tag>; | ||
}; | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.