@@ -5,7 +5,8 @@ import React, {
55 type RefAttributes ,
66 type MemoExoticComponent ,
77 type ForwardRefExoticComponent ,
8- type CSSProperties
8+ type CSSProperties ,
9+ type ComponentProps
910} from "react" ;
1011import { fr } from "./fr" ;
1112import { cx } from "./tools/cx" ;
@@ -15,12 +16,14 @@ import type { RegisteredLinkProps } from "./link";
1516import { assert } from "tsafe/assert" ;
1617import type { Equals } from "tsafe" ;
1718import { symToStr } from "tsafe/symToStr" ;
19+ import { useAnalyticsId } from "./tools/useAnalyticsId" ;
1820
1921export type ButtonProps = ButtonProps . Common &
2022 ( ButtonProps . IconOnly | ButtonProps . WithIcon | ButtonProps . WithoutIcon ) &
2123 ( ButtonProps . AsAnchor | ButtonProps . AsButton ) ;
2224export namespace ButtonProps {
2325 export type Common = {
26+ id ?: string ;
2427 className ?: string ;
2528 /** Default primary */
2629 priority ?: "primary" | "secondary" | "tertiary" | "tertiary no outline" ;
@@ -64,21 +67,19 @@ export namespace ButtonProps {
6467 export type AsButton = {
6568 linkProps ?: never ;
6669 onClick ?: React . MouseEventHandler < HTMLButtonElement > ;
67- nativeButtonProps ?: React . DetailedHTMLProps <
68- React . ButtonHTMLAttributes < HTMLButtonElement > ,
69- HTMLButtonElement
70- > &
71- Record < `data-${string } `, string | boolean | null | undefined > ;
70+ nativeButtonProps ?: ComponentProps < "button" > & Record < `data-${string } `, string | boolean | null | undefined > ;
7271 disabled ?: boolean ;
7372 /** Default "button" */
7473 type ?: "button" | "submit" | "reset" ;
7574 } ;
7675}
7776
77+
7878/** @see <https://components.react-dsfr.fr/?path=/docs/components-button> */
7979export const Button = memo (
8080 forwardRef < HTMLButtonElement | HTMLAnchorElement , ButtonProps > ( ( props , ref ) => {
8181 const {
82+ id : props_id ,
8283 className : prop_className ,
8384 children,
8485 title,
@@ -97,6 +98,11 @@ export const Button = memo(
9798
9899 assert < Equals < keyof typeof rest , never > > ( ) ;
99100
101+ const id = useAnalyticsId ( {
102+ "defaultIdPrefix" : "fr-button" ,
103+ "explicitlyProvidedId" : props_id
104+ } ) ;
105+
100106 const { Link } = getLink ( ) ;
101107
102108 const className = cx (
@@ -126,6 +132,7 @@ export const Button = memo(
126132 return linkProps !== undefined ? (
127133 < Link
128134 { ...linkProps }
135+ id = { props_id ?? linkProps . id ?? id }
129136 title = { title ?? linkProps . title }
130137 className = { cx ( linkProps ?. className , className ) }
131138 style = { {
@@ -140,6 +147,7 @@ export const Button = memo(
140147 ) : (
141148 < button
142149 { ...nativeButtonProps }
150+ id = { props_id ?? nativeButtonProps ?. id ?? id }
143151 className = { cx ( nativeButtonProps ?. className , className ) }
144152 style = { {
145153 ...nativeButtonProps ?. style ,
0 commit comments