Skip to content

chore: Fix useBaseComponent types #3742

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 9 additions & 5 deletions src/alert/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
'use client';
import React, { useEffect } from 'react';
import React, { useEffect, useRef } from 'react';

import { useUniqueId } from '@cloudscape-design/component-toolkit/internal';
import { useMergeRefs, useUniqueId } from '@cloudscape-design/component-toolkit/internal';
import { getAnalyticsMetadataAttribute } from '@cloudscape-design/component-toolkit/internal/analytics-metadata';

import { FunnelMetrics } from '../internal/analytics';
Expand All @@ -23,13 +23,16 @@ export { AlertProps };
const Alert = React.forwardRef(
({ type = 'info', visible = true, style, ...props }: AlertProps, ref: React.Ref<AlertProps.Ref>) => {
const analyticsMetadata = getAnalyticsMetadataProps(props as BasePropsWithAnalyticsMetadata);
const baseComponentProps = useBaseComponent<HTMLDivElement>(

const baseComponentProps = useBaseComponent(
'Alert',
{
props: { type, visible, dismissible: props.dismissible },
},
analyticsMetadata
);
const rootRef = useRef<HTMLElement>(null);
const __internalRootRef = useMergeRefs(rootRef, baseComponentProps.__internalRootRef);

const { funnelIdentifier, funnelInteractionId, funnelErrorContext, submissionAttempt, funnelState, errorCount } =
useFunnel();
Expand All @@ -47,7 +50,7 @@ const Alert = React.forwardRef(
errorCount.current++;

// We don't want to report an error if it is hidden, e.g. inside an Expandable Section.
const errorIsVisible = (baseComponentProps.__internalRootRef.current?.getBoundingClientRect()?.width ?? 0) > 0;
const errorIsVisible = (rootRef.current?.getBoundingClientRect()?.width ?? 0) > 0;

if (errorIsVisible) {
if (subStepSelector) {
Expand All @@ -72,7 +75,7 @@ const Alert = React.forwardRef(
stepNameSelector,
stepName,
stepIdentifier,
currentDocument: baseComponentProps.__internalRootRef.current?.ownerDocument,
currentDocument: rootRef.current?.ownerDocument,
totalSubSteps: subStepCount.current,
funnelIdentifier,
subStepAllSelector: getSubStepAllSelector(),
Expand Down Expand Up @@ -112,6 +115,7 @@ const Alert = React.forwardRef(
visible={visible}
{...props}
{...baseComponentProps}
__internalRootRef={__internalRootRef}
ref={ref}
messageSlotId={messageSlotId}
style={style}
Expand Down
4 changes: 2 additions & 2 deletions src/alert/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const typeToIcon: Record<AlertProps.Type, IconProps['name']> = {
};

type InternalAlertProps = SomeRequired<AlertProps, 'type'> &
InternalBaseComponentProps<HTMLDivElement> & {
InternalBaseComponentProps & {
messageSlotId?: string;
style?: AlertProps['style'];
};
Expand All @@ -59,7 +59,7 @@ const InternalAlert = React.forwardRef(
action,
onDismiss,
onButtonClick,
__internalRootRef = null,
__internalRootRef,
statusIconAriaLabel: deprecatedStatusIconAriaLabel,
dismissAriaLabel: deprecatedDismissAriaLabel,
messageSlotId,
Expand Down
2 changes: 1 addition & 1 deletion src/anchor-navigation/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function InternalAnchorNavigation({
onFollow,
onActiveHrefChange,
activeHref = '',
__internalRootRef = null,
__internalRootRef,
scrollSpyOffset = 0,
...props
}: AnchorNavigationProps & InternalBaseComponentProps) {
Expand Down
2 changes: 1 addition & 1 deletion src/app-layout-toolbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const AppLayoutToolbar = React.forwardRef(
);
}
}
const { __internalRootRef } = useBaseComponent<HTMLDivElement>(
const { __internalRootRef } = useBaseComponent(
'AppLayoutToolbar',
{
props: {
Expand Down
2 changes: 1 addition & 1 deletion src/app-layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const AppLayout = React.forwardRef(
);
}
}
const { __internalRootRef } = useBaseComponent<HTMLDivElement>(
const { __internalRootRef } = useBaseComponent(
'AppLayout',
{
props: {
Expand Down
2 changes: 1 addition & 1 deletion src/area-chart/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default function InternalAreaChart<T extends AreaChartProps.DataTypes>({
loadingText,
recoveryText,
onRecoveryClick,
__internalRootRef = null,
__internalRootRef,
...props
}: InternalAreaChartProps<T>) {
const baseProps = getBaseProps(props);
Expand Down
2 changes: 1 addition & 1 deletion src/attribute-editor/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const InternalAttributeEditor = React.forwardRef(
i18nStrings,
onAddButtonClick,
onRemoveButtonClick,
__internalRootRef = null,
__internalRootRef,
...props
}: InternalAttributeEditorProps<T>,
ref: React.Ref<AttributeEditorProps.Ref>
Expand Down
2 changes: 1 addition & 1 deletion src/box/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function InternalBox({
fontWeight,
color,
children,
__internalRootRef = null,
__internalRootRef,
...props
}: InternalBoxProps) {
const baseProps = getBaseProps(props);
Expand Down
2 changes: 1 addition & 1 deletion src/button-group/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const InternalButtonGroup = forwardRef(
onFilesChange,
ariaLabel,
dropdownExpandToViewport,
__internalRootRef = null,
__internalRootRef,
...props
}: InternalButtonGroupProps,
ref: React.Ref<ButtonGroupProps.Ref>
Expand Down
4 changes: 2 additions & 2 deletions src/button/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export type InternalButtonProps = Omit<ButtonProps, 'variant'> & {
__title?: string;
__emitPerformanceMarks?: boolean;
__skipNativeAttributesWarnings?: boolean;
} & InternalBaseComponentProps<HTMLAnchorElement | HTMLButtonElement>;
} & InternalBaseComponentProps;

export const InternalButton = React.forwardRef(
(
Expand Down Expand Up @@ -93,7 +93,7 @@ export const InternalButton = React.forwardRef(
style,
nativeButtonAttributes,
nativeAnchorAttributes,
__internalRootRef = null,
__internalRootRef,
__focusable = false,
__injectAnalyticsComponentMetadata = false,
__title,
Expand Down
2 changes: 1 addition & 1 deletion src/container/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default function InternalContainer({
__stickyOffset,
__mobileStickyOffset,
__stickyHeader = false,
__internalRootRef = null,
__internalRootRef,
__disableFooterDivider = false,
__hiddenContent = false,
__headerRef,
Expand Down
2 changes: 1 addition & 1 deletion src/copy-to-clipboard/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function InternalCopyToClipboard({
popoverRenderWithPortal,
disabled,
disabledReason,
__internalRootRef = null,
__internalRootRef,
...restProps
}: InternalCopyToClipboardProps) {
const [status, setStatus] = useState<'pending' | 'success' | 'error'>('pending');
Expand Down
2 changes: 1 addition & 1 deletion src/date-input/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const InternalDateInput = React.forwardRef(
locale,
format = 'slashed',
inputFormat = 'slashed',
__internalRootRef = null,
__internalRootRef,
...props
}: InternalDateInputProps,
ref: Ref<DateInputProps.Ref>
Expand Down
2 changes: 1 addition & 1 deletion src/file-dropzone/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import styles from './styles.css.js';
export default function InternalFileDropzone({
onChange,
children,
__internalRootRef = null,
__internalRootRef,
...restProps
}: FileDropzoneProps & InternalBaseComponentProps) {
const [isDropzoneHovered, setDropzoneHovered] = useState(false);
Expand Down
2 changes: 1 addition & 1 deletion src/file-input/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const InternalFileInput = React.forwardRef(
onChange,
variant = 'button',
children,
__internalRootRef = null,
__internalRootRef,
__inputClassName,
__inputNativeAttributes,
__injectAnalyticsComponentMetadata,
Expand Down
2 changes: 1 addition & 1 deletion src/file-upload/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function InternalFileUpload(
showFileLastModified,
showFileThumbnail,
i18nStrings,
__internalRootRef = null,
__internalRootRef,
constraintText,
errorText,
warningText,
Expand Down
2 changes: 1 addition & 1 deletion src/form-field/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export namespace FormFieldProps {
}
}

export interface InternalFormFieldProps extends FormFieldProps, InternalBaseComponentProps<HTMLDivElement> {
export interface InternalFormFieldProps extends FormFieldProps, InternalBaseComponentProps {
/**
* Visually hide the label.
*/
Expand Down
10 changes: 6 additions & 4 deletions src/form-field/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React, { useEffect, useRef } from 'react';
import clsx from 'clsx';

import { useUniqueId, warnOnce } from '@cloudscape-design/component-toolkit/internal';
import { useMergeRefs, useUniqueId, warnOnce } from '@cloudscape-design/component-toolkit/internal';
import { copyAnalyticsMetadataAttribute } from '@cloudscape-design/component-toolkit/internal/analytics-metadata';

import InternalGrid from '../grid/internal';
Expand Down Expand Up @@ -117,12 +117,14 @@ export default function InternalFormField({
errorText,
warningText,
__hideLabel,
__internalRootRef = null,
__internalRootRef,
__disableGutters = false,
__analyticsMetadata = undefined,
__style = {},
...rest
}: InternalFormFieldProps) {
const rootRef = useRef<HTMLElement>();
const ref = useMergeRefs(rootRef, __internalRootRef);
const baseProps = getBaseProps(rest);
const isRefresh = useVisualRefresh();

Expand Down Expand Up @@ -180,7 +182,7 @@ export default function InternalFormField({
errorCount.current++;

// We don't want to report an error if it is hidden, e.g. inside an Expandable Section.
const errorIsVisible = (__internalRootRef?.current?.getBoundingClientRect()?.width ?? 0) > 0;
const errorIsVisible = (rootRef.current?.getBoundingClientRect()?.width ?? 0) > 0;

if (errorIsVisible) {
FunnelMetrics.funnelSubStepError({
Expand Down Expand Up @@ -216,7 +218,7 @@ export default function InternalFormField({
{...baseProps}
className={clsx(baseProps.className, styles.root)}
style={__style}
ref={__internalRootRef}
ref={ref}
{...analyticsAttributes}
{...copyAnalyticsMetadataAttribute(rest)}
>
Expand Down
17 changes: 9 additions & 8 deletions src/form/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
'use client';
import React, { useEffect } from 'react';
import React, { useEffect, useRef } from 'react';

import { useUniqueId } from '@cloudscape-design/component-toolkit/internal';
import { useMergeRefs, useUniqueId } from '@cloudscape-design/component-toolkit/internal';

import { FunnelMetrics } from '../internal/analytics';
import { AnalyticsFunnel, AnalyticsFunnelStep } from '../internal/analytics/components/analytics-funnel';
import { useFunnel, useFunnelNameSelector, useFunnelStepRef } from '../internal/analytics/hooks/use-funnel';
import { getSubStepAllSelector, getTextFromSelector } from '../internal/analytics/selectors';
import { BasePropsWithAnalyticsMetadata, getAnalyticsMetadataProps } from '../internal/base-component';
import { ButtonContext, ButtonContextProps } from '../internal/context/button-context';
import useBaseComponent from '../internal/hooks/use-base-component';
import useBaseComponent, { InternalBaseComponentProps } from '../internal/hooks/use-base-component';
import { applyDisplayName } from '../internal/utils/apply-display-name';
import { FormProps } from './interfaces';
import InternalForm from './internal';
Expand All @@ -27,7 +27,9 @@ const FormWithAnalytics = ({
errorText,
__internalRootRef,
...props
}: FormProps & ReturnType<typeof useBaseComponent<HTMLElement>>) => {
}: FormProps & InternalBaseComponentProps) => {
const rootRef = useRef<HTMLElement>(null);
const ref = useMergeRefs(rootRef, __internalRootRef);
const {
funnelIdentifier,
funnelInteractionId,
Expand Down Expand Up @@ -62,7 +64,7 @@ const FormWithAnalytics = ({
stepNameSelector: funnelStepInfo.current.stepNameSelector,
stepName,
stepIdentifier: funnelStepInfo.current.stepIdentifier,
currentDocument: __internalRootRef.current?.ownerDocument,
currentDocument: rootRef.current?.ownerDocument,
totalSubSteps: funnelStepInfo.current.subStepCount.current,
funnelIdentifier,
subStepAllSelector: getSubStepAllSelector(),
Expand All @@ -86,7 +88,6 @@ const FormWithAnalytics = ({
errorCount,
funnelErrorContext,
errorSlotId,
__internalRootRef,
funnelStepInfo,
]);

Expand All @@ -100,7 +101,7 @@ const FormWithAnalytics = ({
{...props}
{...funnelProps}
{...funnelStepInfo.current.funnelStepProps}
__internalRootRef={__internalRootRef}
__internalRootRef={ref}
__injectAnalyticsComponentMetadata={true}
/>
</ButtonContext.Provider>
Expand All @@ -109,7 +110,7 @@ const FormWithAnalytics = ({

export default function Form({ variant = 'full-page', ...props }: FormProps) {
const analyticsMetadata = getAnalyticsMetadataProps(props as BasePropsWithAnalyticsMetadata);
const baseComponentProps = useBaseComponent<HTMLElement>(
const baseComponentProps = useBaseComponent(
'Form',
{
props: {
Expand Down
2 changes: 1 addition & 1 deletion src/grid/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const InternalGrid = React.forwardRef(
children,
__tagOverride,
__responsiveClassName,
__internalRootRef = null,
__internalRootRef,
...restProps
}: InternalGridProps,
ref: React.Ref<HTMLDivElement>
Expand Down
2 changes: 1 addition & 1 deletion src/header/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function InternalHeader({
counter,
description,
info,
__internalRootRef = null,
__internalRootRef,
__disableActionsWrapping,
__headingTagRef,
__headingTagTabIndex,
Expand Down
2 changes: 1 addition & 1 deletion src/icon/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const InternalIcon = ({
ariaLabel,
svg,
badge,
__internalRootRef = null,
__internalRootRef,
...props
}: InternalIconProps) => {
const icons = useContext(InternalIconContext);
Expand Down
Loading
Loading