From 37ef6df3b2ae33a8ef778c2274eb37fa1fb3be0f Mon Sep 17 00:00:00 2001 From: Ashley Harrison Date: Tue, 7 May 2024 10:07:16 +0100 Subject: [PATCH] Chore: replace calls to `defaultProps` (#87310) * replace calls to defaultProps * make linksGetter optional --- .../src/components/FormField/FormField.tsx | 10 ++---- .../components/Layers/LayerDragDropList.tsx | 4 --- .../PageActionBar/PageActionBar.tsx | 2 +- .../SpanDetail/AccordianKeyValues.tsx | 32 +++++++++---------- .../SpanDetail/AccordianLogs.tsx | 22 ++++++------- .../SpanDetail/AccordianText.tsx | 29 +++++++---------- .../SpanDetail/KeyValuesTable.tsx | 20 ++++++------ .../components/TraceTimelineViewer/Ticks.tsx | 10 +----- .../TraceTimelineViewer/TimelineRow.tsx | 12 ++----- .../components/common/BreakableText.tsx | 7 +--- .../TraceView/components/common/CopyIcon.tsx | 13 +++----- .../components/common/NewWindowIcon.tsx | 9 ++---- 12 files changed, 62 insertions(+), 108 deletions(-) diff --git a/packages/grafana-ui/src/components/FormField/FormField.tsx b/packages/grafana-ui/src/components/FormField/FormField.tsx index 27e2e75ab23..a872ff27461 100644 --- a/packages/grafana-ui/src/components/FormField/FormField.tsx +++ b/packages/grafana-ui/src/components/FormField/FormField.tsx @@ -17,11 +17,6 @@ export interface Props extends InputHTMLAttributes { interactive?: boolean; } -const defaultProps = { - labelWidth: 6, - inputWidth: 12, -}; - /** * Default form field including label used in Grafana UI. Default input element is simple . You can also pass * custom inputEl if required in which case inputWidth and inputProps are ignored. @@ -31,8 +26,8 @@ const defaultProps = { export const FormField = ({ label, tooltip, - labelWidth, - inputWidth, + labelWidth = 6, + inputWidth = 12, inputEl, className, interactive, @@ -57,7 +52,6 @@ export const FormField = ({ }; FormField.displayName = 'FormField'; -FormField.defaultProps = defaultProps; const getStyles = () => { return { diff --git a/public/app/core/components/Layers/LayerDragDropList.tsx b/public/app/core/components/Layers/LayerDragDropList.tsx index 1dc2ff5dc66..7589575c7f2 100644 --- a/public/app/core/components/Layers/LayerDragDropList.tsx +++ b/public/app/core/components/Layers/LayerDragDropList.tsx @@ -122,10 +122,6 @@ export const LayerDragDropList = ({ ); }; -LayerDragDropList.defaultProps = { - isGroup: () => false, -}; - const getStyles = (theme: GrafanaTheme2) => ({ wrapper: css({ marginBottom: theme.spacing(2), diff --git a/public/app/core/components/PageActionBar/PageActionBar.tsx b/public/app/core/components/PageActionBar/PageActionBar.tsx index 4e3f814ae5d..48fda4cc01f 100644 --- a/public/app/core/components/PageActionBar/PageActionBar.tsx +++ b/public/app/core/components/PageActionBar/PageActionBar.tsx @@ -28,7 +28,7 @@ export default class PageActionBar extends PureComponent { placeholder = 'Search by name or type', sortPicker, } = this.props; - const linkProps: typeof LinkButton.defaultProps = { href: linkButton?.href, disabled: linkButton?.disabled }; + const linkProps: Parameters[0] = { href: linkButton?.href, disabled: linkButton?.disabled }; if (target) { linkProps.target = target; diff --git a/public/app/features/explore/TraceView/components/TraceTimelineViewer/SpanDetail/AccordianKeyValues.tsx b/public/app/features/explore/TraceView/components/TraceTimelineViewer/SpanDetail/AccordianKeyValues.tsx index d03bbb0fdb0..f2d0a4d9be2 100644 --- a/public/app/features/explore/TraceView/components/TraceTimelineViewer/SpanDetail/AccordianKeyValues.tsx +++ b/public/app/features/explore/TraceView/components/TraceTimelineViewer/SpanDetail/AccordianKeyValues.tsx @@ -94,13 +94,16 @@ export type AccordianKeyValuesProps = { interactive?: boolean; isOpen: boolean; label: string; - linksGetter: ((pairs: TraceKeyValuePair[], index: number) => TraceLink[]) | TNil; + linksGetter?: ((pairs: TraceKeyValuePair[], index: number) => TraceLink[]) | TNil; onToggle?: null | (() => void); }; +interface KeyValuesSummaryProps { + data?: TraceKeyValuePair[] | null; +} + // export for tests -export function KeyValuesSummary(props: { data?: TraceKeyValuePair[] }) { - const { data } = props; +export function KeyValuesSummary({ data = null }: KeyValuesSummaryProps) { const styles = useStyles2(getStyles); if (!Array.isArray(data) || !data.length) { @@ -121,12 +124,16 @@ export function KeyValuesSummary(props: { data?: TraceKeyValuePair[] }) { ); } -KeyValuesSummary.defaultProps = { - data: null, -}; - -export default function AccordianKeyValues(props: AccordianKeyValuesProps) { - const { className, data, highContrast, interactive, isOpen, label, linksGetter, onToggle } = props; +export default function AccordianKeyValues({ + className = null, + data, + highContrast = false, + interactive = true, + isOpen, + label, + linksGetter, + onToggle = null, +}: AccordianKeyValuesProps) { const isEmpty = !Array.isArray(data) || !data.length; const styles = useStyles2(getStyles); const iconCls = cx(alignIcon, { [styles.emptyIcon]: isEmpty }); @@ -166,10 +173,3 @@ export default function AccordianKeyValues(props: AccordianKeyValuesProps) { ); } - -AccordianKeyValues.defaultProps = { - className: null, - highContrast: false, - interactive: true, - onToggle: null, -}; diff --git a/public/app/features/explore/TraceView/components/TraceTimelineViewer/SpanDetail/AccordianLogs.tsx b/public/app/features/explore/TraceView/components/TraceTimelineViewer/SpanDetail/AccordianLogs.tsx index e627d199be2..9e8cefccd8c 100644 --- a/public/app/features/explore/TraceView/components/TraceTimelineViewer/SpanDetail/AccordianLogs.tsx +++ b/public/app/features/explore/TraceView/components/TraceTimelineViewer/SpanDetail/AccordianLogs.tsx @@ -65,7 +65,7 @@ const getStyles = (theme: GrafanaTheme2) => { export type AccordianLogsProps = { interactive?: boolean; isOpen: boolean; - linksGetter: ((pairs: TraceKeyValuePair[], index: number) => TraceLink[]) | TNil; + linksGetter?: ((pairs: TraceKeyValuePair[], index: number) => TraceLink[]) | TNil; logs: TraceLog[]; onItemToggle?: (log: TraceLog) => void; onToggle?: () => void; @@ -73,8 +73,16 @@ export type AccordianLogsProps = { timestamp: number; }; -export default function AccordianLogs(props: AccordianLogsProps) { - const { interactive, isOpen, linksGetter, logs, openedItems, onItemToggle, onToggle, timestamp } = props; +export default function AccordianLogs({ + interactive = true, + isOpen, + linksGetter, + logs, + openedItems, + onItemToggle, + onToggle, + timestamp, +}: AccordianLogsProps) { let arrow: React.ReactNode | null = null; let HeaderComponent: 'span' | 'a' = 'span'; let headerProps: {} | null = null; @@ -122,11 +130,3 @@ export default function AccordianLogs(props: AccordianLogsProps) { ); } - -AccordianLogs.defaultProps = { - interactive: true, - linksGetter: undefined, - onItemToggle: undefined, - onToggle: undefined, - openedItems: undefined, -}; diff --git a/public/app/features/explore/TraceView/components/TraceTimelineViewer/SpanDetail/AccordianText.tsx b/public/app/features/explore/TraceView/components/TraceTimelineViewer/SpanDetail/AccordianText.tsx index 07ffdb08549..c9c7bda92dd 100644 --- a/public/app/features/explore/TraceView/components/TraceTimelineViewer/SpanDetail/AccordianText.tsx +++ b/public/app/features/explore/TraceView/components/TraceTimelineViewer/SpanDetail/AccordianText.tsx @@ -58,17 +58,17 @@ function DefaultTextComponent({ data }: { data: string[] }) { return ; } -export default function AccordianText(props: AccordianTextProps) { - const { - className, - data, - headerClassName, - interactive, - isOpen, - label, - onToggle, - TextComponent = DefaultTextComponent, - } = props; +export default function AccordianText({ + className = null, + data, + headerClassName, + highContrast = false, + interactive = true, + isOpen, + label, + onToggle = null, + TextComponent = DefaultTextComponent, +}: AccordianTextProps) { const isEmpty = !Array.isArray(data) || !data.length; const accordianKeyValuesStyles = useStyles2(getAccordianKeyValuesStyles); const iconCls = cx(alignIcon, { [accordianKeyValuesStyles.emptyIcon]: isEmpty }); @@ -97,10 +97,3 @@ export default function AccordianText(props: AccordianTextProps) { ); } - -AccordianText.defaultProps = { - className: null, - highContrast: false, - interactive: true, - onToggle: null, -}; diff --git a/public/app/features/explore/TraceView/components/TraceTimelineViewer/SpanDetail/KeyValuesTable.tsx b/public/app/features/explore/TraceView/components/TraceTimelineViewer/SpanDetail/KeyValuesTable.tsx index 6e80b457e27..4cab6366f46 100644 --- a/public/app/features/explore/TraceView/components/TraceTimelineViewer/SpanDetail/KeyValuesTable.tsx +++ b/public/app/features/explore/TraceView/components/TraceTimelineViewer/SpanDetail/KeyValuesTable.tsx @@ -14,7 +14,7 @@ import { css } from '@emotion/css'; import cx from 'classnames'; -import * as React from 'react'; +import React, { PropsWithChildren } from 'react'; import { GrafanaTheme2 } from '@grafana/data'; import { Icon, useStyles2 } from '@grafana/ui'; @@ -92,21 +92,23 @@ function parseIfComplexJson(value: unknown) { return value; } -export const LinkValue = (props: { href: string; title?: string; children: React.ReactNode }) => { +interface LinkValueProps { + href: string; + title?: string; + children: React.ReactNode; +} + +export const LinkValue = ({ href, title = '', children }: PropsWithChildren) => { return ( - - {props.children} + + {children} ); }; -LinkValue.defaultProps = { - title: '', -}; - export type KeyValuesTableProps = { data: TraceKeyValuePair[]; - linksGetter: ((pairs: TraceKeyValuePair[], index: number) => TraceLink[]) | TNil; + linksGetter?: ((pairs: TraceKeyValuePair[], index: number) => TraceLink[]) | TNil; }; export default function KeyValuesTable(props: KeyValuesTableProps) { diff --git a/public/app/features/explore/TraceView/components/TraceTimelineViewer/Ticks.tsx b/public/app/features/explore/TraceView/components/TraceTimelineViewer/Ticks.tsx index 6d1c1195f4d..53fd755b21d 100644 --- a/public/app/features/explore/TraceView/components/TraceTimelineViewer/Ticks.tsx +++ b/public/app/features/explore/TraceView/components/TraceTimelineViewer/Ticks.tsx @@ -60,9 +60,7 @@ type TicksProps = { startTime?: number | TNil; }; -export default function Ticks(props: TicksProps) { - const { endTime, numTicks, showLabels, startTime } = props; - +export default function Ticks({ endTime = null, numTicks, showLabels = null, startTime = null }: TicksProps) { let labels: undefined | string[]; if (showLabels) { labels = []; @@ -95,9 +93,3 @@ export default function Ticks(props: TicksProps) { } return
{ticks}
; } - -Ticks.defaultProps = { - endTime: null, - showLabels: null, - startTime: null, -}; diff --git a/public/app/features/explore/TraceView/components/TraceTimelineViewer/TimelineRow.tsx b/public/app/features/explore/TraceView/components/TraceTimelineViewer/TimelineRow.tsx index c957f08e7d6..abb071fde4d 100644 --- a/public/app/features/explore/TraceView/components/TraceTimelineViewer/TimelineRow.tsx +++ b/public/app/features/explore/TraceView/components/TraceTimelineViewer/TimelineRow.tsx @@ -43,8 +43,7 @@ interface TimelineRowCellProps extends React.HTMLAttributes { style?: {}; } -export default function TimelineRow(props: TTimelineRowProps) { - const { children, className = '', ...rest } = props; +export default function TimelineRow({ children, className = '', ...rest }: TTimelineRowProps) { const styles = useStyles2(getStyles); return (
@@ -53,12 +52,7 @@ export default function TimelineRow(props: TTimelineRowProps) { ); } -TimelineRow.defaultProps = { - className: '', -}; - -export function TimelineRowCell(props: TimelineRowCellProps) { - const { children, className = '', width, style, ...rest } = props; +export function TimelineRowCell({ children, className = '', width, style = {}, ...rest }: TimelineRowCellProps) { const widthPercent = `${width * 100}%`; const mergedStyle = { ...style, flexBasis: widthPercent, maxWidth: widthPercent }; const styles = useStyles2(getStyles); @@ -69,6 +63,4 @@ export function TimelineRowCell(props: TimelineRowCellProps) { ); } -TimelineRowCell.defaultProps = { className: '', style: {} }; - TimelineRow.Cell = TimelineRowCell; diff --git a/public/app/features/explore/TraceView/components/common/BreakableText.tsx b/public/app/features/explore/TraceView/components/common/BreakableText.tsx index 2893bcf119f..3d65aae87ff 100644 --- a/public/app/features/explore/TraceView/components/common/BreakableText.tsx +++ b/public/app/features/explore/TraceView/components/common/BreakableText.tsx @@ -35,8 +35,7 @@ type Props = { wordRegexp?: RegExp; }; -export default function BreakableText(props: Props): React.ReactElement | null { - const { className, text, wordRegexp = WORD_RX } = props; +export default function BreakableText({ text, className, wordRegexp = WORD_RX }: Props): React.ReactElement | null { const styles = useStyles2(getStyles); if (!text) { return null; @@ -55,7 +54,3 @@ export default function BreakableText(props: Props): React.ReactElement | null { } return <>{spans}; } - -BreakableText.defaultProps = { - wordRegexp: WORD_RX, -}; diff --git a/public/app/features/explore/TraceView/components/common/CopyIcon.tsx b/public/app/features/explore/TraceView/components/common/CopyIcon.tsx index f2d01c21c00..43164f85e1f 100644 --- a/public/app/features/explore/TraceView/components/common/CopyIcon.tsx +++ b/public/app/features/explore/TraceView/components/common/CopyIcon.tsx @@ -41,24 +41,19 @@ type PropsType = { tooltipTitle: string; }; -export default function CopyIcon(props: PropsType) { +export default function CopyIcon({ copyText, icon = 'copy', tooltipTitle }: PropsType) { const styles = useStyles2(getStyles); const [hasCopied, setHasCopied] = useState(false); const handleClick = () => { - navigator.clipboard.writeText(props.copyText); + navigator.clipboard.writeText(copyText); setHasCopied(true); }; return ( - -