Skip to content

Commit

Permalink
farrah/76186/currency selector component (#6512)
Browse files Browse the repository at this point in the history
* currency dropdown component

* refactor

* fixed bugs
  • Loading branch information
farrah-deriv committed Sep 21, 2022
1 parent fc8c9fd commit 5e32328
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const Autocomplete = React.memo(props => {
has_updating_list = true,
input_id,
is_alignment_top,
is_list_visible = false,
list_items,
list_portal_id,
onHideDropdownList,
Expand Down Expand Up @@ -299,7 +300,7 @@ const Autocomplete = React.memo(props => {
// marginTop: form.errors[field.name] ? 'calc(4px - 18px)' : '4px', // 4px is the standard margin. In case of error, the list should overlap the error
}),
}}
is_visible={should_show_list}
is_visible={should_show_list || is_list_visible}
list_items={filtered_items}
list_height={props.list_height}
// Autocomplete must use the `text` property and not the `value`, however DropdownList provides access to both
Expand All @@ -321,6 +322,7 @@ Autocomplete.defaultProps = {

Autocomplete.propTypes = {
className: PropTypes.string,
is_list_visible: PropTypes.bool,
list_items: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.string),
PropTypes.arrayOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ const DropdownList = React.forwardRef((props, ref) => {

const el_dropdown_list = (
<CSSTransition
appear={is_visible}
in={is_visible}
timeout={100}
classNames={{
Expand Down
69 changes: 69 additions & 0 deletions packages/p2p/src/components/buy-sell/currency-selector.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import React from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import { Field, Formik } from 'formik';
import { Autocomplete, Icon } from '@deriv/components';
import { localize } from 'Components/i18next';
import './currency-selector.scss';

const CurrencySelector = ({ className, default_value, list, onSelect }) => {
const filtered_currency_list = list.filter(item => item.has_adverts);

return (
<Formik enableReinitialize initialValues={{ currency: '' }}>
{({ setFieldValue }) => (
<Field name='currency'>
{({ field }) => (
<Autocomplete
{...field}
autoComplete='off'
className={classNames('currency-selector', className)}
data-lpignore='true'
historyValue={default_value}
is_list_visible
leading_icon={<Icon className='currency-selector__search-icon' icon='IcSearch' />}
list_items={filtered_currency_list}
not_found_text={
list.filter(
item => item.text.toLowerCase() === field.value.toLowerCase() && !item.has_adverts
).length === 1
? localize('No ads for this currency.')
: localize('No results for "{{value}}".', {
value: field.value,
interpolation: { escapeValue: false },
})
}
onItemSelection={({ value }) => {
if (value) onSelect?.(value);
}}
placeholder={localize('Search currency')}
trailing_icon={
field.value ? (
<Icon
color='secondary'
icon='IcCloseCircle'
onClick={() => {
setFieldValue('currency', '');
}}
/>
) : (
<></>
)
}
type='text'
/>
)}
</Field>
)}
</Formik>
);
};

CurrencySelector.propTypes = {
className: PropTypes.string,
default_value: PropTypes.string,
list: PropTypes.array,
onSelect: PropTypes.func,
};

export default CurrencySelector;
24 changes: 24 additions & 0 deletions packages/p2p/src/components/buy-sell/currency-selector.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.currency-selector {
&__search-icon {
margin-right: 1rem;
}

.dc-dropdown-list {
margin-top: 0;
border-top-left-radius: 0;
border-top-right-radius: 0;
}

.dc-input {
border: 0;
border-bottom: 1px solid var(--border-normal);
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
box-shadow: 0 8px 16px 0 var(--shadow-menu);
background: var(--general-main-2);

&__field {
padding-left: 3.4rem;
}
}
}

0 comments on commit 5e32328

Please sign in to comment.