Skip to content

Commit

Permalink
Fix #2963: Expose ref and element (#3077)
Browse files Browse the repository at this point in the history
* Fix #2963: Expose ref and element

* Fix #2963: DomHandler check for Ref object

* Fix #2963: InputText/InputTextArea/Button remove imperative handle

Co-authored-by: mertsincan <[email protected]>
  • Loading branch information
melloware and mertsincan authored Jul 18, 2022
1 parent 04e70dd commit 4fe9817
Show file tree
Hide file tree
Showing 172 changed files with 752 additions and 337 deletions.
9 changes: 4 additions & 5 deletions components/lib/accordion/Accordion.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ interface AccordionEventParams {
index: number;
}

export interface AccordionProps {
id?: string;
export interface AccordionProps extends Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, 'ref'> {
activeIndex?: AccordionActiveIndexType;
className?: string;
style?: object;
multiple?: boolean;
expandIcon?: IconType<AccordionProps>;
collapseIcon?: IconType<AccordionProps>;
Expand All @@ -42,4 +39,6 @@ export interface AccordionProps {
}

// tslint:disable-next-line:max-classes-per-file
export declare class Accordion extends React.Component<AccordionProps, any> { }
export declare class Accordion extends React.Component<AccordionProps, any> {
public getElement(): HTMLDivElement;
}
8 changes: 7 additions & 1 deletion components/lib/accordion/Accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const AccordionTab = () => { }
export const Accordion = React.forwardRef((props, ref) => {
const [idState, setIdState] = React.useState(props.id);
const [activeIndexState, setActiveIndexState] = React.useState(props.activeIndex);
const elementRef = React.useRef(null);
const activeIndex = props.onTabChange ? props.activeIndex : activeIndexState;

const shouldUseTab = (tab) => tab && tab.props.__TYPE === 'AccordionTab';
Expand Down Expand Up @@ -46,6 +47,11 @@ export const Accordion = React.forwardRef((props, ref) => {
return props.multiple ? (activeIndex && activeIndex.some(i => i === index)) : activeIndex === index;
}

React.useImperativeHandle(ref, () => ({
getElement: () => elementRef.current,
...props
}));

useMountEffect(() => {
if (!idState) {
setIdState(UniqueComponentId());
Expand Down Expand Up @@ -123,7 +129,7 @@ export const Accordion = React.forwardRef((props, ref) => {
const tabs = createTabs();

return (
<div id={idState} className={className} style={props.style} {...otherProps}>
<div id={idState} ref={elementRef} className={className} style={props.style} {...otherProps}>
{tabs}
</div>
)
Expand Down
6 changes: 5 additions & 1 deletion components/lib/autocomplete/AutoComplete.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import TooltipOptions from '../tooltip/tooltipoptions';
import { VirtualScrollerProps } from '../virtualscroller';
import { VirtualScrollerProps, VirtualScroller } from '../virtualscroller';
import { CSSTransitionProps } from '../csstransition';
import { IconType } from '../utils';

Expand Down Expand Up @@ -109,4 +109,8 @@ export interface AutoCompleteProps extends Omit<React.DetailedHTMLProps<React.In

export declare class AutoComplete extends React.Component<AutoCompleteProps, any> {
public search(event:React.SyntheticEvent, query:string, source: AutoCompleteSourceType): void;
public getElement(): HTMLSpanElement;
public getInput(): HTMLInputElement;
public getOverlay(): HTMLElement;
public getVirtualScroller(): VirtualScroller;
}
4 changes: 4 additions & 0 deletions components/lib/autocomplete/AutoComplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,10 @@ export const AutoComplete = React.memo(React.forwardRef((props, ref) => {

React.useImperativeHandle(ref, () => ({
search,
getElement: () => elementRef.current,
getOverlay: () => overlayRef.current,
getInput: () => inputRef.current,
getVirtualScroller: () => virtualScrollerRef.current,
...props
}));

Expand Down
4 changes: 3 additions & 1 deletion components/lib/avatar/Avatar.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ export interface AvatarProps extends Omit<React.DetailedHTMLProps<React.HTMLAttr
children?: React.ReactNode;
}

export declare class Avatar extends React.Component<AvatarProps, any> { }
export declare class Avatar extends React.Component<AvatarProps, any> {
public getElement(): HTMLDivElement;
}
8 changes: 7 additions & 1 deletion components/lib/avatar/Avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import { classNames, IconUtils, ObjectUtils } from '../utils/Utils';

export const Avatar = React.forwardRef((props, ref) => {
const elementRef = React.useRef(null);

const createContent = () => {
if (props.image) {
Expand All @@ -17,6 +18,11 @@ export const Avatar = React.forwardRef((props, ref) => {
return null;
}

React.useImperativeHandle(ref, () => ({
getElement: () => elementRef.current,
...props
}));

const otherProps = ObjectUtils.findDiffKeys(props, Avatar.defaultProps);
const containerClassName = classNames('p-avatar p-component', {
'p-avatar-image': props.image != null,
Expand All @@ -29,7 +35,7 @@ export const Avatar = React.forwardRef((props, ref) => {
const content = props.template ? ObjectUtils.getJSXElement(props.template, props) : createContent();

return (
<div className={containerClassName} style={props.style} {...otherProps}>
<div ref={elementRef} className={containerClassName} style={props.style} {...otherProps}>
{content}
{props.children}
</div>
Expand Down
8 changes: 4 additions & 4 deletions components/lib/avatargroup/AvatarGroup.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as React from 'react';

export interface AvatarGroupProps {
style?: object;
className?: string;
export interface AvatarGroupProps extends Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, 'ref'> {
children?: React.ReactNode;
}

export declare class AvatarGroup extends React.Component<AvatarGroupProps, any> { }
export declare class AvatarGroup extends React.Component<AvatarGroupProps, any> {
public getElement(): HTMLDivElement;
}
8 changes: 7 additions & 1 deletion components/lib/avatargroup/AvatarGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ import * as React from 'react';
import { classNames, ObjectUtils } from '../utils/Utils';

export const AvatarGroup = React.forwardRef((props, ref) => {
const elementRef = React.useRef(null);
const otherProps = ObjectUtils.findDiffKeys(props, AvatarGroup.defaultProps);
const className = classNames('p-avatar-group p-component', props.className);

React.useImperativeHandle(ref, () => ({
getElement: () => elementRef.current,
...props
}));

return (
<div className={className} style={props.style} {...otherProps}>
<div ref={elementRef} className={className} style={props.style} {...otherProps}>
{props.children}
</div>
)
Expand Down
4 changes: 3 additions & 1 deletion components/lib/badge/Badge.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ export interface BadgeProps extends Omit<React.DetailedHTMLProps<React.HTMLAttri
children?: React.ReactNode;
}

export declare class Badge extends React.Component<BadgeProps, any> { }
export declare class Badge extends React.Component<BadgeProps, any> {
public getElement(): HTMLSpanElement;
}
8 changes: 7 additions & 1 deletion components/lib/badge/Badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import { classNames, ObjectUtils } from '../utils/Utils';

export const Badge = React.memo(React.forwardRef((props, ref) => {
const elementRef = React.useRef(null);
const otherProps = ObjectUtils.findDiffKeys(props, Badge.defaultProps);
const className = classNames('p-badge p-component', {
'p-badge-no-gutter': ObjectUtils.isNotEmpty(props.value) && String(props.value).length === 1,
Expand All @@ -11,8 +12,13 @@ export const Badge = React.memo(React.forwardRef((props, ref) => {
[`p-badge-${props.severity}`]: props.severity !== null
}, props.className);

React.useImperativeHandle(ref, () => ({
getElement: () => elementRef.current,
...props
}));

return (
<span className={className} style={props.style} {...otherProps}>
<span ref={elementRef} className={className} style={props.style} {...otherProps}>
{props.value}
</span>
)
Expand Down
6 changes: 2 additions & 4 deletions components/lib/blockui/BlockUI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ import * as React from 'react';

type BlockUITemplateType = React.ReactNode | ((props: BlockUIProps) => React.ReactNode);

export interface BlockUIProps {
id?: string;
export interface BlockUIProps extends Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, 'ref'> {
blocked?: boolean;
fullScreen?: boolean;
baseZIndex?: number;
autoZIndex?: boolean;
style?: object;
className?: string;
template?: BlockUITemplateType;
onBlocked?(): void;
onUnblocked?(): void;
Expand All @@ -19,4 +16,5 @@ export interface BlockUIProps {
export declare class BlockUI extends React.Component<BlockUIProps, any> {
public block(): void;
public unblock(): void;
public getElement(): HTMLDivElement;
}
4 changes: 3 additions & 1 deletion components/lib/blockui/BlockUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { classNames, DomHandler, ObjectUtils, ZIndexUtils } from '../utils/Utils

export const BlockUI = React.forwardRef((props, ref) => {
const [visibleState, setVisibleState] = React.useState(props.blocked);
const elementRef = React.useRef(null);
const maskRef = React.useRef(null);

const block = () => {
Expand Down Expand Up @@ -65,6 +66,7 @@ export const BlockUI = React.forwardRef((props, ref) => {
React.useImperativeHandle(ref, () => ({
block,
unblock,
getElement: () => elementRef.current,
...props
}));

Expand All @@ -91,7 +93,7 @@ export const BlockUI = React.forwardRef((props, ref) => {
const mask = createMask();

return (
<div id={props.id} className="p-blockui-container" {...otherProps}>
<div id={props.id} ref={elementRef} className="p-blockui-container" {...otherProps}>
{props.children}
{mask}
</div>
Expand Down
9 changes: 4 additions & 5 deletions components/lib/breadcrumb/BreadCrumb.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import * as React from 'react';
import { MenuItem } from '../menuitem';

export interface BreadCrumbProps {
id?: string;
export interface BreadCrumbProps extends Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>, 'ref'> {
model?: MenuItem[];
home?: MenuItem;
style?: object;
className?: string;
children?: React.ReactNode;
}

export declare class BreadCrumb extends React.Component<BreadCrumbProps, any> { }
export declare class BreadCrumb extends React.Component<BreadCrumbProps, any> {
public getElement(): HTMLElement;
}
8 changes: 7 additions & 1 deletion components/lib/breadcrumb/BreadCrumb.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import { classNames, IconUtils, ObjectUtils } from '../utils/Utils';

export const BreadCrumb = React.memo(React.forwardRef((props, ref) => {
const elementRef = React.useRef(null);

const itemClick = (event, item) => {
if (item.disabled) {
Expand Down Expand Up @@ -110,14 +111,19 @@ export const BreadCrumb = React.memo(React.forwardRef((props, ref) => {
return null;
}

React.useImperativeHandle(ref, () => ({
getElement: () => elementRef.current,
...props
}));

const otherProps = ObjectUtils.findDiffKeys(props, BreadCrumb.defaultProps);
const className = classNames('p-breadcrumb p-component', props.className);
const home = createHome();
const items = createMenuitems();
const separator = createSeparator();

return (
<nav id={props.id} className={className} style={props.style} aria-label="Breadcrumb" {...otherProps}>
<nav id={props.id} ref={elementRef} className={className} style={props.style} aria-label="Breadcrumb" {...otherProps}>
<ul>
{home}
{separator}
Expand Down
4 changes: 3 additions & 1 deletion components/lib/button/Button.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ export interface ButtonProps extends Omit<React.DetailedHTMLProps<React.ButtonHT
children?: React.ReactNode;
}

export declare class Button extends React.Component<ButtonProps, any> { }
export declare class Button extends React.Component<ButtonProps, any> {
public getElement(): HTMLButtonElement;
}
3 changes: 3 additions & 0 deletions components/lib/calendar/Calendar.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,8 @@ export declare class Calendar extends React.Component<CalendarProps, any> {
public hide(): void;
public getCurrentDateTime(): Date | Date[];
public getViewDate(): Date | Date[];
public getElement(): HTMLSpanElement;
public getInput(): HTMLInputElement;
public getOverlay(): HTMLElement;
public updateViewDate(event: CalendarEventType, value: Date | Date[]): void;
}
3 changes: 3 additions & 0 deletions components/lib/calendar/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2491,6 +2491,9 @@ export const Calendar = React.memo(React.forwardRef((props, ref) => {
getCurrentDateTime,
getViewDate,
updateViewDate,
getElement: () => elementRef.current,
getOverlay: () => overlayRef.current,
getInput: () => inputRef.current,
...props
}));

Expand Down
5 changes: 2 additions & 3 deletions components/lib/captcha/Captcha.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import * as React from 'react';

export interface CaptchaProps {
id?: string;
export interface CaptchaProps extends Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, 'ref'> {
siteKey?: string;
theme?: string;
type?: string;
size?: string;
tabIndex?: number;
language?: string;
onResponse?(response: any): void;
onExpire?(): void;
Expand All @@ -16,4 +14,5 @@ export interface CaptchaProps {
export declare class Captcha extends React.Component<CaptchaProps, any> {
public reset(): void;
public getResponse(): any;
public getElement(): HTMLDivElement;
}
1 change: 1 addition & 0 deletions components/lib/captcha/Captcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const Captcha = React.memo(React.forwardRef((props, ref) => {
React.useImperativeHandle(ref, () => ({
reset,
getResponse,
getElement: () => elementRef.current,
...props
}));

Expand Down
9 changes: 4 additions & 5 deletions components/lib/carousel/Carousel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ interface CarouselPageChangeParams {
page: number;
}

export interface CarouselProps {
id?: string;
export interface CarouselProps extends Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, 'ref'> {
value?: any[];
page?: number;
header?: React.ReactNode;
footer?: React.ReactNode;
style?: object;
className?: string;
itemTemplate?(item: any): React.ReactNode;
circular?: boolean;
autoplayInterval?: number;
Expand All @@ -35,4 +32,6 @@ export interface CarouselProps {
children?: React.ReactNode;
}

export declare class Carousel extends React.Component<CarouselProps, any> { }
export declare class Carousel extends React.Component<CarouselProps, any> {
public getElement(): HTMLDivElement;
}
5 changes: 5 additions & 0 deletions components/lib/carousel/Carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ export const Carousel = React.memo(React.forwardRef((props, ref) => {
}
}

React.useImperativeHandle(ref, () => ({
getElement: () => elementRef.current,
...props
}));

useMountEffect(() => {
if (elementRef.current) {
attributeSelector.current = UniqueComponentId();
Expand Down
7 changes: 6 additions & 1 deletion components/lib/cascadeselect/CascadeSelect.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ export interface CascadeSelectProps extends Omit<React.DetailedHTMLProps<React.I
children?: React.ReactNode;
}

export declare class CascadeSelect extends React.Component<CascadeSelectProps, any> { }
export declare class CascadeSelect extends React.Component<CascadeSelectProps, any> {
public getElement(): HTMLDivElement;
public getInput(): HTMLInputElement;
public getOverlay(): HTMLElement;
public getLabel(): HTMLSpanElement;
}
8 changes: 8 additions & 0 deletions components/lib/cascadeselect/CascadeSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ export const CascadeSelect = React.memo(React.forwardRef((props, ref) => {
DomHandler.alignOverlay(overlayRef.current, labelRef.current.parentElement, props.appendTo || PrimeReact.appendTo);
}

React.useImperativeHandle(ref, () => ({
getElement: () => elementRef.current,
getOverlay: () => overlayRef.current,
getInput: () => inputRef.current,
getLabel: () => labelRef.current,
...props
}));

React.useEffect(() => {
ObjectUtils.combinedRefs(inputRef, props.inputRef);
}, [inputRef, props.inputRef]);
Expand Down
1 change: 1 addition & 0 deletions components/lib/chart/Chart.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export declare class Chart extends React.Component<ChartProps, any> {
public getBase64Image(): any;
public generateLegend(): string;
public refresh(): void;
public getElement(): HTMLDivElement;
}
Loading

0 comments on commit 4fe9817

Please sign in to comment.