Skip to content

Commit 9953ef5

Browse files
Fix 4707 - Fixed additional properties for daisyui theme
Fixes #4707 by rendering the `WrapIfAdditionalTemplate` inside of the `FieldTemplate` - Updated the `WrapIfAdditionalTemplate` to properly render the delete button and key input around the `children` when the schema element is an additional property - Updated the `FieldTemplate` to properly wrap the Field with the `WrapIfAdditionalTemplate` - Updated the snapshots accordingly - Updated the `CHANGELOG.md` accordingly
1 parent 75e1404 commit 9953ef5

File tree

6 files changed

+417596
-416515
lines changed

6 files changed

+417596
-416515
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ should change the heading of the (upcoming) version to include a major version b
2929
## @rjsf/daisyui
3030

3131
- Fixed checkbox widget to use current value instead of event target in onFocus/onBlur handlers, fixing [#4704](https://github.com/rjsf-team/react-jsonschema-form/issues/4704)
32+
- Fixed additional properties rendering by properly connecting the `FieldTemplate` and `WrapIfAdditionalTemplate`, fixing [4707](https://github.com/rjsf-team/react-jsonschema-form/issues/4707)
3233

3334
## @rjsf/fluentui-rc
3435

packages/daisyui/src/templates/FieldTemplate/FieldTemplate.tsx

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { FieldTemplateProps, StrictRJSFSchema, RJSFSchema, FormContextType } from '@rjsf/utils';
1+
import {
2+
FieldTemplateProps,
3+
StrictRJSFSchema,
4+
RJSFSchema,
5+
FormContextType,
6+
getTemplate,
7+
getUiOptions,
8+
} from '@rjsf/utils';
29

310
/** The `FieldTemplate` component provides the main layout for each form field
411
* with DaisyUI styling. It handles:
@@ -47,20 +54,40 @@ export default function FieldTemplate<
4754

4855
// Special handling for checkboxes - they should have the label after the input
4956
const isCheckbox = schema.type === 'boolean';
57+
const uiOptions = getUiOptions<T, S, F>(uiSchema);
58+
const WrapIfAdditionalTemplate = getTemplate<'WrapIfAdditionalTemplate', T, S, F>(
59+
'WrapIfAdditionalTemplate',
60+
registry,
61+
uiOptions,
62+
);
5063

5164
return (
52-
<div className={`field-template mb-3 ${classNames || ''}`} {...divProps}>
53-
{displayLabel && !isCheckbox && (
54-
<label htmlFor={id} className='label'>
55-
<span className='label-text font-medium'>
56-
{label}
57-
{required && <span className='text-error ml-1'>*</span>}
58-
</span>
59-
</label>
60-
)}
61-
{children}
62-
{errors}
63-
{help}
64-
</div>
65+
<WrapIfAdditionalTemplate
66+
classNames={classNames}
67+
disabled={divProps.disabled}
68+
id={id}
69+
label={label}
70+
onDropPropertyClick={onDropPropertyClick}
71+
onKeyChange={onKeyChange}
72+
readonly={readonly}
73+
required={required}
74+
schema={schema}
75+
uiSchema={uiSchema}
76+
registry={registry}
77+
>
78+
<div className={`field-template mb-3 ${classNames || ''}`} {...divProps}>
79+
{displayLabel && !isCheckbox && (
80+
<label htmlFor={id} className='label'>
81+
<span className='label-text font-medium'>
82+
{label}
83+
{required && <span className='text-error ml-1'>*</span>}
84+
</span>
85+
</label>
86+
)}
87+
{children}
88+
{errors}
89+
{help}
90+
</div>
91+
</WrapIfAdditionalTemplate>
6592
);
6693
}

packages/daisyui/src/templates/WrapIfAdditionalTemplate/WrapIfAdditionalTemplate.tsx

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import {
44
StrictRJSFSchema,
55
RJSFSchema,
66
FormContextType,
7-
TranslatableString,
87
buttonId,
8+
ADDITIONAL_PROPERTY_FLAG,
9+
TranslatableString,
910
} from '@rjsf/utils';
1011

1112
/** The `WrapIfAdditional` component is used by the `FieldTemplate` to rename, or remove properties that are
@@ -27,13 +28,18 @@ export default function WrapIfAdditionalTemplate<
2728
readonly,
2829
required,
2930
schema,
31+
uiSchema,
3032
onKeyChange,
3133
onDropPropertyClick,
3234
registry,
3335
...rest
3436
} = props;
3537

36-
const { translateString } = registry;
38+
const additional = ADDITIONAL_PROPERTY_FLAG in schema;
39+
const { templates, translateString } = registry;
40+
// Button templates are not overridden in the uiSchema
41+
const { RemoveButton } = templates.ButtonTemplates;
42+
const keyLabel = translateString(TranslatableString.KeyLabel, [label]);
3743

3844
const handleBlur = useCallback(
3945
(event: React.FocusEvent<HTMLInputElement>) => {
@@ -46,29 +52,38 @@ export default function WrapIfAdditionalTemplate<
4652
onDropPropertyClick(label)();
4753
}, [onDropPropertyClick, label]);
4854

55+
if (!additional) {
56+
return <div className={classNames}>{children}</div>;
57+
}
58+
4959
return (
5060
<div className={`wrap-if-additional-template ${classNames}`} {...rest}>
51-
<div className='flex items-center'>
52-
<input
53-
type='text'
54-
className='input input-bordered'
55-
id={`${id}-key`}
56-
onBlur={handleBlur}
57-
defaultValue={label}
58-
disabled={disabled || readonly}
59-
/>
60-
{schema.additionalProperties && (
61-
<button
61+
<div className='flex items-baseline' style={{ justifyContent: 'space-between' }}>
62+
<div>
63+
<label htmlFor={`${id}-key`} className='label'>
64+
<span className='label-text'>{keyLabel}</span>
65+
</label>
66+
<input
67+
type='text'
68+
className='input input-bordered'
69+
id={`${id}-key`}
70+
onBlur={handleBlur}
71+
defaultValue={label}
72+
disabled={disabled || readonly}
73+
/>
74+
</div>
75+
{children}
76+
<div className='flex self-center'>
77+
<RemoveButton
6278
id={buttonId<T>(id, 'remove')}
63-
className='rjsf-array-item-remove btn btn-danger ml-2'
64-
onClick={handleRemove}
79+
className='rjsf-object-property-remove'
6580
disabled={disabled || readonly}
66-
>
67-
{translateString(TranslatableString.RemoveButton)}
68-
</button>
69-
)}
81+
onClick={handleRemove}
82+
uiSchema={uiSchema}
83+
registry={registry}
84+
/>
85+
</div>
7086
</div>
71-
{children}
7287
</div>
7388
);
7489
}

0 commit comments

Comments
 (0)