11import React , {
22 Ref ,
3- createContext ,
43 useContext ,
54 useMemo ,
65 ReactNode ,
76 CSSProperties ,
7+ useRef ,
8+ useCallback ,
89} from 'react' ;
910import classnames from 'classnames' ;
1011import { FieldSetColumnContext } from './FieldSet' ;
11- import { useFormElementId } from './hooks' ;
1212import { createFC } from './common' ;
1313
1414/**
@@ -28,11 +28,6 @@ export type FormElementProps = {
2828 children ?: ReactNode ;
2929} ;
3030
31- /**
32- *
33- */
34- export const FormElementContext = createContext < { id ?: string } > ( { } ) ;
35-
3631/**
3732 *
3833 */
@@ -42,7 +37,7 @@ export const FormElement = createFC<
4237> (
4338 ( props ) => {
4439 const {
45- id : id_ ,
40+ id,
4641 className,
4742 cols = 1 ,
4843 elementRef,
@@ -54,7 +49,7 @@ export const FormElement = createFC<
5449 readOnly,
5550 } = props ;
5651
57- const id = useFormElementId ( id_ ) ;
52+ const controlElRef = useRef < HTMLDivElement > ( null ) ;
5853
5954 const { totalCols } = useContext ( FieldSetColumnContext ) ;
6055
@@ -80,31 +75,39 @@ export const FormElement = createFC<
8075 className
8176 ) ;
8277
83- const formElemCtx = useMemo ( ( ) => ( { id } ) , [ id ] ) ;
78+ const onClickLabel = useCallback ( ( ) => {
79+ if ( controlElRef . current ) {
80+ const inputEl = controlElRef . current . querySelector < HTMLElement > (
81+ 'input,select,button'
82+ ) ;
83+ inputEl ?. focus ( ) ;
84+ }
85+ } , [ ] ) ;
86+
8487 const emptyCtx = useMemo ( ( ) => ( { } ) , [ ] ) ;
8588
8689 return (
87- < FormElementContext . Provider value = { formElemCtx } >
88- < FieldSetColumnContext . Provider value = { emptyCtx } >
89- < div ref = { elementRef } className = { formElementClassNames } >
90- { label ? (
91- < label className = 'slds-form-element__label' htmlFor = { id } >
92- { label }
93- { required ? (
94- < abbr className = 'slds-required' > * </ abbr >
95- ) : undefined }
96- </ label >
97- ) : null }
98- < div className = { formElementControlClassNames } >
99- { children }
100- { dropdown }
101- { errorMessage ? (
102- < span className = 'slds-form-element__help' > { errorMessage } </ span >
103- ) : undefined }
104- </ div >
90+ < FieldSetColumnContext . Provider value = { emptyCtx } >
91+ < div ref = { elementRef } className = { formElementClassNames } >
92+ { label ? (
93+ < label
94+ className = 'slds-form-element__label'
95+ htmlFor = { id }
96+ onClick = { id ? undefined : onClickLabel }
97+ >
98+ { label }
99+ { required ? < abbr className = 'slds-required' > * </ abbr > : undefined }
100+ </ label >
101+ ) : null }
102+ < div ref = { controlElRef } className = { formElementControlClassNames } >
103+ { children }
104+ { dropdown }
105+ { errorMessage ? (
106+ < span className = 'slds-form-element__help' > { errorMessage } </ span >
107+ ) : undefined }
105108 </ div >
106- </ FieldSetColumnContext . Provider >
107- </ FormElementContext . Provider >
109+ </ div >
110+ </ FieldSetColumnContext . Provider >
108111 ) ;
109112 } ,
110113 { isFormElement : true }
0 commit comments