Skip to content

Commit 04e2ff8

Browse files
(Form): apply form type to each form element
1 parent 9395f1f commit 04e2ff8

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed

src/scripts/CheckboxGroup.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import React, {
88
ReactNode,
99
} from 'react';
1010
import classnames from 'classnames';
11+
import { FormTypeContext } from './Form';
1112
import { FormElementProps } from './FormElement';
1213
import { FieldSetColumnContext } from './FieldSet';
1314
import { TooltipContent } from './TooltipContent';
@@ -63,6 +64,7 @@ export const CheckboxGroup = createFC<
6364
children,
6465
...rprops
6566
} = props;
67+
const type = useContext(FormTypeContext);
6668
const { totalCols } = useContext(FieldSetColumnContext);
6769
const controlElRef = useRef<HTMLDivElement | null>(null);
6870

@@ -88,6 +90,7 @@ export const CheckboxGroup = createFC<
8890
const grpClassNames = classnames(
8991
className,
9092
'slds-form-element',
93+
type ? `slds-form-element_${type}` : null,
9194
{
9295
'slds-has-error': error,
9396
'slds-is-required': required,

src/scripts/FieldSet.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import React, {
33
HTMLAttributes,
44
ReactNode,
55
useMemo,
6+
useContext,
67
} from 'react';
78
import classnames from 'classnames';
9+
import { FormTypeContext } from './Form';
810
import { FormElement } from './FormElement';
911
import { createFC } from './common';
1012

@@ -75,10 +77,12 @@ export const FieldSet = createFC<
7577
{ isFormElement: boolean; Row: typeof FieldSetRow }
7678
>(
7779
({ className, label, children, ...props }) => {
80+
const type = useContext(FormTypeContext) ?? 'compound';
81+
7882
const fsClassNames = classnames(
7983
className,
8084
'slds-form-element',
81-
'slds-form-element_compound'
85+
`slds-form-element_${type}`
8286
);
8387
const legendClassNames = classnames(
8488
'slds-form-element__legend',

src/scripts/Form.tsx

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,32 @@ export type FormProps = {
99
type?: 'stacked' | 'horizontal' | 'inline' | 'compound';
1010
} & HTMLAttributes<HTMLDivElement>;
1111

12+
/**
13+
*
14+
*/
15+
export const FormTypeContext =
16+
React.createContext<FormProps['type']>(undefined);
17+
1218
/**
1319
*
1420
*/
1521
export const Form: FC<FormProps> = (props) => {
1622
const { className, type = 'stacked', children, ...rprops } = props;
1723
const formClassNames = classnames(className, `slds-form_${type}`);
1824
return (
19-
<div className={formClassNames} {...rprops}>
20-
{React.Children.map(children, (child) => {
21-
if (
22-
React.isValidElement(child) &&
23-
!(child.type as unknown as { isFormElement?: boolean }).isFormElement
24-
) {
25-
return <FormElement>{child}</FormElement>;
26-
}
27-
return child;
28-
})}
29-
</div>
25+
<FormTypeContext.Provider value={type === 'compound' ? 'stacked' : type}>
26+
<div className={formClassNames} {...rprops}>
27+
{React.Children.map(children, (child) => {
28+
if (
29+
React.isValidElement(child) &&
30+
!(child.type as unknown as { isFormElement?: boolean })
31+
.isFormElement
32+
) {
33+
return <FormElement>{child}</FormElement>;
34+
}
35+
return child;
36+
})}
37+
</div>
38+
</FormTypeContext.Provider>
3039
);
3140
};

src/scripts/FormElement.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import React, {
88
useCallback,
99
} from 'react';
1010
import classnames from 'classnames';
11+
import { FormTypeContext } from './Form';
1112
import { FieldSetColumnContext } from './FieldSet';
1213
import { createFC } from './common';
1314
import { TooltipContent } from './TooltipContent';
@@ -31,6 +32,7 @@ export type FormElementProps = {
3132
children?: ReactNode;
3233
tooltip?: ReactNode;
3334
tooltipIcon?: string;
35+
type?: 'stacked' | 'horizontal' | 'inline' | 'compound';
3436
};
3537

3638
/**
@@ -56,8 +58,12 @@ export const FormElement = createFC<
5658
readOnly,
5759
tooltip,
5860
tooltipIcon,
61+
type: propsType,
5962
} = props;
6063

64+
const contextType = useContext(FormTypeContext);
65+
const type = propsType ?? contextType;
66+
6167
const controlElRef = useRef<HTMLDivElement>(null);
6268

6369
const { totalCols } = useContext(FieldSetColumnContext);
@@ -72,6 +78,7 @@ export const FormElement = createFC<
7278

7379
const formElementClassNames = classnames(
7480
'slds-form-element',
81+
type ? `slds-form-element_${type}` : null,
7582
readOnly ? 'slds-form-element_readonly' : null,
7683
error ? 'slds-has-error' : null,
7784
typeof totalCols === 'number'

src/scripts/RadioGroup.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import React, {
77
ReactNode,
88
} from 'react';
99
import classnames from 'classnames';
10+
import { FormTypeContext } from './Form';
1011
import { FieldSetColumnContext } from './FieldSet';
1112
import { TooltipContent } from './TooltipContent';
1213
import { createFC } from './common';
@@ -60,10 +61,12 @@ export const RadioGroup = createFC<RadioGroupProps, { isFormElement: boolean }>(
6061
onValueChange,
6162
...rprops
6263
} = props;
64+
const type = useContext(FormTypeContext);
6365
const { totalCols } = useContext(FieldSetColumnContext);
6466
const grpClassNames = classnames(
6567
className,
6668
'slds-form-element',
69+
type ? `slds-form-element_${type}` : null,
6770
{
6871
'slds-has-error': error,
6972
'slds-is-required': required,

0 commit comments

Comments
 (0)