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

feat(components/molecule/phoneInput): add onPrefixChange prop #2642

Merged
merged 1 commit into from
Sep 28, 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
13 changes: 12 additions & 1 deletion components/molecule/phoneInput/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import MoleculeInputField from '@s-ui/react-molecule-input-field'
import {phoneValidationType} from './settings.js'

const BASE_CLASS = 'sui-MoleculePhoneInput'
const NOOP = () => {}

export {PREFIXES} from './settings.js'
export default function MoleculePhoneInput({
Expand All @@ -28,6 +29,7 @@ export default function MoleculePhoneInput({
label,
name,
onChange,
onPrefixChange = NOOP,
placeholder,
prefixes = [],
initialSelectedPrefix = prefixes[0],
Expand Down Expand Up @@ -77,6 +79,11 @@ export default function MoleculePhoneInput({
}
}, [])

useEffect(() => {
onPrefixChange(selectedPrefix)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tal vez si que haria que en el nuevo onPrefixChange, devolviera el mismo objeto que devuelve el onChange, es decir {name, prefix, value}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedPrefix])

const handlePhoneChange = (e, {value}) => {
setIsLandLine(selectedPrefix.landlinePrefixs.includes(value[0]))

Expand Down Expand Up @@ -198,6 +205,7 @@ MoleculePhoneInput.propTypes = {
}),
hasError: PropTypes.bool,
visiblePrefixes: PropTypes.bool,

/** Boolean to decide if helptext should be auto hide */
autoHideHelpText: PropTypes.bool,
/** Success message to display when success state */
Expand All @@ -219,5 +227,8 @@ MoleculePhoneInput.propTypes = {
id: PropTypes.string,

/** Label to set in the molecule field */
label: PropTypes.string
label: PropTypes.string,

/** Callback dispatch when selected prefix changes */
onPrefixChange: PropTypes.func
}
26 changes: 26 additions & 0 deletions components/molecule/phoneInput/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,32 @@ describe('MoleculePhoneInput', () => {
expect(phonePrefix).to.be.equal(PREFIXES[0].countryCode)
})

it('should call onPrefixChange when prefix changes value', () => {
const phoneValue = '666'
let phonePrefix = ''
// Given
const props = {
value: phoneValue,
prefixes: PREFIXES,
initialSelectedPrefix: PREFIXES[0],
onChange: () => {},
onPrefixChange: prefix => (phonePrefix = prefix)
}

const {container} = setup(props)
const prefix = container.querySelector(
'div.sui-MoleculePhoneInput-input-prefix'
)
// When
prefix.click()
const options = container.querySelectorAll('.sui-MoleculeDropdownOption')
const option = options[1]
option.click()

// Then
expect(phonePrefix).to.be.equal(PREFIXES[1])
})

it('should add error class when error', () => {
// Given
const props = {
Expand Down