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: support nativeElement #68

Merged
merged 2 commits into from
May 17, 2024
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
20 changes: 16 additions & 4 deletions src/BaseInput.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import clsx from 'classnames';
import type { FC, ReactElement, ReactNode } from 'react';
import type { ReactElement, ReactNode } from 'react';
import React, { cloneElement, useRef } from 'react';
import type { BaseInputProps } from './interface';
import { hasAddon, hasPrefixSuffix } from './utils/commonUtils';

const BaseInput: FC<BaseInputProps> = (props) => {
export interface HolderRef {
/** Provider holder ref. Will return `null` if not wrap anything */
nativeElement: HTMLElement | null;
}

const BaseInput = React.forwardRef<HolderRef, BaseInputProps>((props, ref) => {
const {
inputElement: inputEl,
children,
Expand Down Expand Up @@ -54,6 +59,13 @@ const BaseInput: FC<BaseInputProps> = (props) => {
null,
});

// ======================== Ref ======================== //
const groupRef = useRef<HTMLDivElement>(null);

React.useImperativeHandle(ref, () => ({
nativeElement: groupRef.current || containerRef.current,
}));

// ================== Prefix & Suffix ================== //
if (hasAffix) {
// ================== Clear Icon ================== //
Expand Down Expand Up @@ -157,7 +169,7 @@ const BaseInput: FC<BaseInputProps> = (props) => {
// Need another wrapper for changing display:table to display:inline-block
// and put style prop in wrapper
element = (
<GroupWrapperComponent className={mergedGroupClassName}>
<GroupWrapperComponent className={mergedGroupClassName} ref={groupRef}>
<WrapperComponent className={mergedWrapperClassName}>
{addonBefore && (
<GroupAddonComponent className={addonCls}>
Expand All @@ -184,6 +196,6 @@ const BaseInput: FC<BaseInputProps> = (props) => {
},
hidden,
});
};
});

export default BaseInput;
4 changes: 3 additions & 1 deletion src/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import React, {
useRef,
useState,
} from 'react';
import BaseInput from './BaseInput';
import BaseInput, { HolderRef } from './BaseInput';
import useCount from './hooks/useCount';
import type { ChangeEventInfo, InputProps, InputRef } from './interface';
import type { InputFocusOptions } from './utils/commonUtils';
Expand Down Expand Up @@ -43,6 +43,7 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => {
const compositionRef = useRef(false);

const inputRef = useRef<HTMLInputElement>(null);
const holderRef = useRef<HolderRef>(null);

const focus = (option?: InputFocusOptions) => {
if (inputRef.current) {
Expand Down Expand Up @@ -86,6 +87,7 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => {
inputRef.current?.select();
},
input: inputRef.current,
nativeElement: holderRef.current?.nativeElement || inputRef.current,
}));

useEffect(() => {
Expand Down
1 change: 1 addition & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export interface InputRef {
) => void;
select: () => void;
input: HTMLInputElement | null;
nativeElement: HTMLElement | null;
}

export interface ChangeEventInfo {
Expand Down
47 changes: 46 additions & 1 deletion tests/BaseInput.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fireEvent, render } from '@testing-library/react';
import type { ChangeEvent, FC } from 'react';
import React, { useState } from 'react';
import BaseInput from '../src/BaseInput';
import BaseInput, { type HolderRef } from '../src/BaseInput';

describe('BaseInput', () => {
it('should render perfectly', () => {
Expand Down Expand Up @@ -267,4 +267,49 @@ describe('BaseInput', () => {
expect(container.querySelector('.rc-input-affix-wrapper')).toBeFalsy();
expect(container.querySelector('input')).toHaveClass('test-variant');
});

describe('ref', () => {
it('prefix', () => {
const holderRef = React.createRef<HolderRef>();
const { container } = render(
<BaseInput prefixCls="rc-input" prefix="prefix" ref={holderRef}>
<input />
</BaseInput>,
);
expect(holderRef.current?.nativeElement).toBe(
container.querySelector('.rc-input-affix-wrapper'),
);
});

it('addon', () => {
const holderRef = React.createRef<HolderRef>();
const { container } = render(
<BaseInput prefixCls="rc-input" addonAfter="after" ref={holderRef}>
<input />
</BaseInput>,
);

expect(holderRef.current?.nativeElement).toBe(
container.querySelector('.rc-input-group-wrapper'),
);
});

it('mix', () => {
const holderRef = React.createRef<HolderRef>();
const { container } = render(
<BaseInput
prefixCls="rc-input"
suffix="suffix"
addonAfter="after"
ref={holderRef}
>
<input />
</BaseInput>,
);

expect(holderRef.current?.nativeElement).toBe(
container.querySelector('.rc-input-group-wrapper'),
);
});
});
});
1 change: 1 addition & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ describe('Input ref', () => {
const { container } = render(<Input ref={ref} defaultValue="light" />);
const inputEl = container.querySelector('input')!;
expect(ref.current?.input).toBe(inputEl);
expect(ref.current?.nativeElement).toBe(inputEl);
});
});

Expand Down
Loading