Skip to content

Commit 5df5f44

Browse files
committed
feat: padding
1 parent 57277e8 commit 5df5f44

File tree

7 files changed

+24
-15
lines changed

7 files changed

+24
-15
lines changed

packages/carbon/src/ArrayFieldTemplate/ArrayFieldTemplate.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default function ArrayFieldTemplate<
7171
uiSchema={uiSchema}
7272
registry={registry}
7373
/>
74-
<LayerBackground>
74+
<LayerBackground padding={carbonOptions.padding}>
7575
<Stack gap={carbonOptions.gap}>
7676
{items.length > 0 && (
7777
<Stack gap={carbonOptions.gap} className='array-item-list'>

packages/carbon/src/MultiSchemaField/MultiSchemaField.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
import { LayerBackground } from '../components/Layer';
1919
import { FormGroup } from '@carbon/react';
2020
import { LabelValue } from '../components/LabelValue';
21+
import getCarbonOptions from '../utils';
2122

2223
/** Type used for the state of the `AnyOfField` component */
2324
type AnyOfFieldState<S extends StrictRJSFSchema = RJSFSchema> = {
@@ -165,6 +166,7 @@ class AnyOfField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends For
165166
title = schema.title,
166167
...uiOptions
167168
} = getUiOptions<T, S, F>(uiSchema, globalUiOptions);
169+
const carbonOptions = getCarbonOptions<T, S, F>(formContext || ({} as F), uiOptions);
168170
const Widget = getWidget<T, S, F>({ type: 'number' }, widget, widgets);
169171
const rawErrors = get(errorSchema, ERRORS_KEY, []);
170172
const fieldErrorSchema = omit(errorSchema, [ERRORS_KEY]);
@@ -197,7 +199,7 @@ class AnyOfField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends For
197199
</label>
198200
}
199201
>
200-
<LayerBackground ignoreFirstLayer={false}>
202+
<LayerBackground ignoreFirstLayer={false} padding={carbonOptions.padding}>
201203
<div className='panel panel-default panel-body'>
202204
<div className='form-group'>
203205
<Widget

packages/carbon/src/ObjectFieldTemplate/ObjectFieldTemplate.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export default function ObjectFieldTemplate<
8282
{!depth && (title || description) && (
8383
<div style={{ height: `calc(${spacing[carbonOptions.gap - 1]} - 0.5rem)` }} />
8484
)}
85-
<LayerBackground>
85+
<LayerBackground padding={carbonOptions.padding}>
8686
<Stack gap={carbonOptions.gap}>
8787
{properties.length > 0 && <Stack gap={carbonOptions.gap}>{properties.map((item) => item.content)}</Stack>}
8888
{canExpand(schema, uiSchema, formData) && (
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { FormContextType, RJSFSchema, StrictRJSFSchema, TitleFieldProps } from '@rjsf/utils';
22
import { LabelValue } from '../components/LabelValue';
3+
import { FormGroup } from '@carbon/react';
34

45
/** Implement `TitleFieldTemplate`
56
*/
@@ -9,13 +10,10 @@ export default function TitleField<T = any, S extends StrictRJSFSchema = RJSFSch
910
required,
1011
}: TitleFieldProps<T, S, F>) {
1112
return (
12-
<div
13-
id={id}
14-
style={{
15-
marginBlockEnd: '0.5rem',
16-
}}
17-
>
18-
<LabelValue label={title} required={required} hide={false} />
13+
<div className='title-field'>
14+
<FormGroup legendId={id} legendText={<LabelValue label={title} required={required} hide={false} />}>
15+
{null}
16+
</FormGroup>
1917
</div>
2018
);
2119
}

packages/carbon/src/WrapIfAdditionalTemplate/WrapIfAdditionalTemplate.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default function WrapIfAdditionalTemplate<
5454
<FormGroup legendText={<LabelValue hide={false} label={label} required={required} />}>
5555
<div style={{ display: 'flex', alignItems: 'flex-start', justifyContent: 'center' }}>
5656
<div style={{ flex: 1 }}>
57-
<LayerBackground>
57+
<LayerBackground padding={carbonOptions.padding}>
5858
<Stack gap={carbonOptions.gap}>
5959
<TextInput
6060
labelText={keyLabel}

packages/carbon/src/components/Layer.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import { Layer as CarbonLayer } from '@carbon/react';
33
import { ReactNode } from 'react';
44
import { NestDepth, useNestDepth } from '../contexts';
5+
// @ts-expect-error miss types for `@carbon/layout`
6+
import { spacing } from '@carbon/layout';
57

68
/** The `LayerBackground` render a background for [a carbon layer](https://carbondesignsystem.com/guidelines/color/usage#layering-tokens)
79
*
@@ -10,9 +12,11 @@ import { NestDepth, useNestDepth } from '../contexts';
1012
export function LayerBackground({
1113
children,
1214
ignoreFirstLayer = true,
15+
padding,
1316
}: {
1417
children: ReactNode;
1518
ignoreFirstLayer?: boolean;
19+
padding?: number;
1620
}) {
1721
const nestDepth = useNestDepth();
1822
const content = <NestDepth>{children}</NestDepth>;
@@ -25,26 +29,26 @@ export function LayerBackground({
2529
return (
2630
// To make the first layer be `gray`, we need to nest it in another layer
2731
<NestDepth>
28-
<Background>{content}</Background>
32+
<Background padding={padding}>{content}</Background>
2933
</NestDepth>
3034
);
3135
}
3236
}
3337

3438
// oneOf and anyOf need to show the first layer
3539

36-
return <Background>{content}</Background>;
40+
return <Background padding={padding}>{content}</Background>;
3741
}
3842

3943
/** Draw layer background
4044
*/
41-
function Background({ children }: { children: ReactNode }) {
45+
function Background({ children, padding = 3 }: { children: ReactNode; padding?: number }) {
4246
const nestDepth = useNestDepth();
4347
return (
4448
<CarbonLayer level={1 - (nestDepth % 2)}>
4549
<div
4650
style={{
47-
padding: '1rem',
51+
padding: spacing[padding - 1],
4852
backgroundColor: 'var(--cds-layer)',
4953
}}
5054
>

packages/carbon/src/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ export interface CarbonOptions {
77
* @see https://carbondesignsystem.com/guidelines/spacing/overview/#spacing-scale
88
*/
99
gap: number;
10+
/**Padding in layer background, default to `3` (0.5rem)
11+
* @see https://carbondesignsystem.com/guidelines/spacing/overview/#spacing-scale
12+
*/
13+
padding: number;
1014
/** Size of form item.
1115
*
1216
* Note that some of the `@carbon/react` component doesn't support `xl` and will fallback to `lg`
@@ -18,6 +22,7 @@ export interface CarbonOptions {
1822
*/
1923
const defaultCarbonOptions: CarbonOptions = {
2024
gap: 7,
25+
padding: 3,
2126
};
2227

2328
/** Get carbon theme options from `formContext` and `ui:carbon` uiSchema options.

0 commit comments

Comments
 (0)