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

fix(InputMasked): expose inner_ref input element when useRef is used #2042

Merged
merged 1 commit into from
Mar 3, 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import InputMaskedElement from './InputMaskedElement'
import Input, { inputPropTypes } from '../input/Input'
import Context from '../../shared/Context'

const InputMasked = React.forwardRef((props, ref) => {
const InputMasked = (props) => {
const context = React.useContext(Context)

// Remove masks defined in Provider/Context, because it overwrites a custom mask
Expand All @@ -37,15 +37,14 @@ const InputMasked = React.forwardRef((props, ref) => {
return (
<InputMaskedContext.Provider
value={{
inner_ref: ref,
props: contextAndProps,
context,
}}
>
<InputMaskedElement />
</InputMaskedContext.Provider>
)
})
}

export default InputMasked

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,6 @@ export const useFilteredProps = () => {
return { props, htmlAttributes: Object.freeze(attributes) }
}

/**
* Returns either given component property ref or new internal ref
*
* @returns React ref
*/
export const useInputElementRef = () => {
const { props } = React.useContext(InputMaskedContext)
return typeof props?.inner_ref?.current !== 'undefined'
? props?.inner_ref
: React.createRef()
}

/**
* Returns locale from either component or context
*
Expand Down Expand Up @@ -217,7 +205,6 @@ export const useMaskParams = () => {
* @returns React Element
*/
export const useInputElement = () => {
const ref = useInputElementRef()
const { props } = React.useContext(InputMaskedContext)

const { pipe } = props
Expand All @@ -226,6 +213,9 @@ export const useInputElement = () => {
const { showMask, showGuide, placeholderChar, keepCharPositions } =
useMaskParams()

const _ref = React.useRef()
const ref = props?.inner_ref || _ref

// Create the actual input element
const inputElementRef = React.useRef(<input ref={ref} />)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,18 @@ describe('InputMasked component', () => {
})

it('gets valid ref element', () => {
const ref = React.createRef()
mount(<Component {...props} inner_ref={ref} />)
let ref

function MockComponent() {
ref = React.useRef()
return <Component {...props} inner_ref={ref} />
}

render(<MockComponent />)

expect(ref.current instanceof window.HTMLInputElement).toBe(true)
expect(ref.current.id).toBe(props.id)
expect(ref.current.tagName).toBe('INPUT')
})

it('event "on_change" gets emmited with correct value', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`InputMasked component have to match type="text" snapshot 1`] = `
<ForwardRef
<InputMasked
align={null}
as_currency={null}
as_number={null}
Expand Down Expand Up @@ -232,7 +232,7 @@ exports[`InputMasked component have to match type="text" snapshot 1`] = `
</span>
</Input>
</InputMaskedElement>
</ForwardRef>
</InputMasked>
`;

exports[`InputMasked scss have to match snapshot 1`] = `
Expand Down