feat(#526): add onchange to search country card

pull/164/head
Andrei Dekterev 3 years ago
parent d5cf526725
commit 7681462c83
  1. 12
      src/features/AddCardForm/components/Form/hooks/index.tsx
  2. 4
      src/features/AddCardForm/components/Form/hooks/useCountries.tsx
  3. 27
      src/features/AddCardForm/components/Form/index.tsx
  4. 6
      src/features/AddCardForm/styled.tsx
  5. 7
      src/features/Combobox/hooks/index.tsx
  6. 3
      src/features/Combobox/index.tsx
  7. 5
      src/features/Combobox/styled.tsx
  8. 1
      src/features/Combobox/types.tsx
  9. 1
      src/features/Common/Input/styled.tsx

@ -104,6 +104,8 @@ export const useFormSubmit = ({
const [inputStates, setInputStates] = useObjectState(initialState) const [inputStates, setInputStates] = useObjectState(initialState)
const [errorMessage, setErrorMessage] = useState('') const [errorMessage, setErrorMessage] = useState('')
const [loader, setLoader] = useState(false) const [loader, setLoader] = useState(false)
const [typedCountry, setTypedCountry] = useState<string>('')
const dataHighlights = useRecoilValue(dataForPayHighlights) const dataHighlights = useRecoilValue(dataForPayHighlights)
const { const {
@ -161,7 +163,7 @@ export const useFormSubmit = ({
} }
} }
const onCountryChange = useCallback((country: SelectedCountry) => { const onCountrySelect = useCallback((country: SelectedCountry) => {
resetErrors() resetErrors()
if (country?.id === selectedCountry?.id) return if (country?.id === selectedCountry?.id) return
setSelectedCountry(country) setSelectedCountry(country)
@ -178,6 +180,10 @@ export const useFormSubmit = ({
setSelectedState, setSelectedState,
]) ])
const onChangeCountry = (e: ChangeEvent<HTMLInputElement>) => {
setTypedCountry(e.target.value)
}
const onStateSelect = useCallback((state: SelectedState) => { const onStateSelect = useCallback((state: SelectedState) => {
resetErrors() resetErrors()
if (state?.id === selectedState?.id) return if (state?.id === selectedState?.id) return
@ -334,8 +340,9 @@ export const useFormSubmit = ({
loader, loader,
name, name,
onAddressChange, onAddressChange,
onChangeCountry,
onCityChange, onCityChange,
onCountryChange, onCountrySelect,
onInputsBlur, onInputsBlur,
onInputsChange, onInputsChange,
onInputsFocus, onInputsFocus,
@ -344,6 +351,7 @@ export const useFormSubmit = ({
onStateSelect, onStateSelect,
selectedCountry, selectedCountry,
selectedState, selectedState,
typedCountry,
usaStateValue, usaStateValue,
usaStates, usaStates,
} }

@ -19,6 +19,8 @@ export type SelectedState = (UsaStateType & {
name: string, name: string,
}) | null }) | null
export const USA_ID = 194
const useCountriesList = () => { const useCountriesList = () => {
const [countries, setCountries] = useState<Countries>([]) const [countries, setCountries] = useState<Countries>([])
@ -47,7 +49,7 @@ export const useCountries = () => {
) )
useEffect(() => { useEffect(() => {
if (selectedCountry?.id === 194) { if (selectedCountry?.id === USA_ID) {
(async () => { (async () => {
const states = await getUsaStates() const states = await getUsaStates()
setUsaStates(states) setUsaStates(states)

@ -1,8 +1,8 @@
import { useRouteMatch } from 'react-router-dom' import { useRouteMatch } from 'react-router-dom'
import { import {
CardNumberElement,
CardExpiryElement,
CardCvcElement, CardCvcElement,
CardExpiryElement,
CardNumberElement,
} from '@stripe/react-stripe-js' } from '@stripe/react-stripe-js'
import { isMobileDevice } from 'config/userAgent' import { isMobileDevice } from 'config/userAgent'
@ -16,7 +16,8 @@ import { SolidButton } from 'features/UserAccount/styled'
import { ElementContainer } from '../ElementContainer' import { ElementContainer } from '../ElementContainer'
import type { Props } from './hooks' import type { Props } from './hooks'
import { useFormSubmit, ElementTypes } from './hooks' import { ElementTypes, useFormSubmit } from './hooks'
import { USA_ID } from './hooks/useCountries'
import { import {
Form, Form,
@ -81,8 +82,9 @@ export const AddCardFormInner = (props: Props) => {
loader, loader,
name, name,
onAddressChange, onAddressChange,
onChangeCountry,
onCityChange, onCityChange,
onCountryChange, onCountrySelect,
onInputsBlur, onInputsBlur,
onInputsChange, onInputsChange,
onInputsFocus, onInputsFocus,
@ -91,11 +93,12 @@ export const AddCardFormInner = (props: Props) => {
onStateSelect, onStateSelect,
selectedCountry, selectedCountry,
selectedState, selectedState,
typedCountry,
usaStates, usaStates,
usaStateValue, usaStateValue,
} = useFormSubmit(props) } = useFormSubmit(props)
const isUsaCountry = selectedCountry?.id === 194 const isUsaCountry = selectedCountry?.id === USA_ID
return ( return (
<Form onSubmit={handleSubmit}> <Form onSubmit={handleSubmit}>
@ -166,14 +169,20 @@ export const AddCardFormInner = (props: Props) => {
<Column> <Column>
<CountryWrapper> <CountryWrapper>
<CustomCombobox <CustomCombobox
value={selectedCountry?.name || ''} value={selectedCountry?.name || typedCountry}
labelLexic={selectedCountry?.name ? '' : 'country'} labelLexic={
onSelect={onCountryChange} (selectedCountry?.name || typedCountry)
|| !isLabelVisible(ElementTypes.CardCountry)
? ''
: 'country'
}
onSelect={onCountrySelect}
onBlur={onInputsBlur(ElementTypes.CardCountry)} onBlur={onInputsBlur(ElementTypes.CardCountry)}
options={countries} options={countries}
withError={false} withError={false}
selected={Boolean(selectedCountry?.name)} selected={Boolean(selectedCountry?.name)}
noSearch onChange={onChangeCountry}
onFocus={onInputsFocus(ElementTypes.CardCountry)}
/> />
</CountryWrapper> </CountryWrapper>
{isUsaCountry && ( {isUsaCountry && (

@ -133,5 +133,11 @@ export const CustomCombobox = styled(Combobox)`
${PopOver}{ ${PopOver}{
top: 55px; top: 55px;
max-height: 300px; max-height: 300px;
${isMobileDevice
? css`
top: 30px;
`
: ''};
} }
` as typeof Combobox ` as typeof Combobox

@ -32,6 +32,7 @@ export const useCombobox = <T extends Option>({
noSearch, noSearch,
onBlur, onBlur,
onChange, onChange,
onFocus,
onSelect, onSelect,
options, options,
selected, selected,
@ -104,6 +105,11 @@ export const useCombobox = <T extends Option>({
onBlur?.(event) onBlur?.(event)
} }
const onInputFocus = (event: FocusEvent<HTMLInputElement>) => {
open()
onFocus?.(event)
}
useEffect(() => { useEffect(() => {
const selectedIndex = findIndex(filteredOptions, ({ name }) => name === query) const selectedIndex = findIndex(filteredOptions, ({ name }) => name === query)
setIndex(selectedIndex) setIndex(selectedIndex)
@ -138,6 +144,7 @@ export const useCombobox = <T extends Option>({
inputFieldRef, inputFieldRef,
isOpen, isOpen,
onInputBlur, onInputBlur,
onInputFocus,
onKeyDown, onKeyDown,
onOptionSelect, onOptionSelect,
onOutsideClick, onOutsideClick,

@ -61,6 +61,7 @@ export const Combobox = <T extends Option>(props: Props<T>) => {
inputFieldRef, inputFieldRef,
isOpen, isOpen,
onInputBlur, onInputBlur,
onInputFocus,
onKeyDown, onKeyDown,
onOptionSelect, onOptionSelect,
onOutsideClick, onOutsideClick,
@ -102,7 +103,7 @@ export const Combobox = <T extends Option>(props: Props<T>) => {
<InputStyled <InputStyled
maxLength={maxLength} maxLength={maxLength}
onBlur={onInputBlur} onBlur={onInputBlur}
onFocus={open} onFocus={onInputFocus || open}
ref={inputFieldRef} ref={inputFieldRef}
autoComplete='off' autoComplete='off'
title={title} title={title}

@ -36,7 +36,6 @@ export const ListOption = styled.li.attrs(() => ({
padding-left: 24px; padding-left: 24px;
color: ${({ theme }) => theme.colors.white70}; color: ${({ theme }) => theme.colors.white70};
background-color: transparent; background-color: transparent;
text-transform: capitalize;
${({ isHighlighted }) => isHighlighted && css` ${({ isHighlighted }) => isHighlighted && css`
color: ${({ theme }) => theme.colors.white}; color: ${({ theme }) => theme.colors.white};
@ -74,10 +73,6 @@ export const WrapperIcon = styled.span`
: ''}; : ''};
` `
export const ScAudioWrap = styled.div`
margin-left: 20px;
`
export const ScLoaderWrapper = styled.div` export const ScLoaderWrapper = styled.div`
border-radius: 10px; border-radius: 10px;
display: flex; display: flex;

@ -18,6 +18,7 @@ export type Props<T> = Pick<InputHTMLAttributes<HTMLInputElement>, (
| 'title' | 'title'
| 'placeholder' | 'placeholder'
| 'onBlur' | 'onBlur'
| 'onFocus'
)> & { )> & {
className?: string, className?: string,
customListStyles?: CustomStyles, customListStyles?: CustomStyles,

@ -129,7 +129,6 @@ const inputStyles = css<InputProps>`
export const InputStyled = styled.input<InputProps>` export const InputStyled = styled.input<InputProps>`
${inputStyles} ${inputStyles}
opacity: ${({ disabled }) => (disabled ? 0.5 : 1)}; opacity: ${({ disabled }) => (disabled ? 0.5 : 1)};
text-transform: capitalize;
::placeholder { ::placeholder {
opacity: 0; opacity: 0;

Loading…
Cancel
Save