| 1 |
import { CSSProperties, FocusEvent } from 'react'; |
| 2 |
import Grid from '@mui/material/Grid'; |
| 3 |
import TextField from '@mui/material/TextField'; |
| 4 |
import { |
| 5 |
ADDITIONAL_PROPERTY_FLAG, |
| 6 |
FormContextType, |
| 7 |
RJSFSchema, |
| 8 |
StrictRJSFSchema, |
| 9 |
TranslatableString, |
| 10 |
WrapIfAdditionalTemplateProps, |
| 11 |
} from '@rjsf/utils'; |
| 12 |
|
| 13 |
/** The `WrapIfAdditional` component is used by the `FieldTemplate` to rename, or remove properties that are |
| 14 |
* part of an `additionalProperties` part of a schema. |
| 15 |
* |
| 16 |
* @param props - The `WrapIfAdditionalProps` for this component |
| 17 |
*/ |
| 18 |
export default function WrapIfAdditionalTemplate< |
| 19 |
T = any, |
| 20 |
S extends StrictRJSFSchema = RJSFSchema, |
| 21 |
F extends FormContextType = any |
| 22 |
>(props: WrapIfAdditionalTemplateProps<T, S, F>) { |
| 23 |
const { |
| 24 |
children, |
| 25 |
classNames, |
| 26 |
style, |
| 27 |
disabled, |
| 28 |
id, |
| 29 |
label, |
| 30 |
onDropPropertyClick, |
| 31 |
onKeyChange, |
| 32 |
readonly, |
| 33 |
required, |
| 34 |
schema, |
| 35 |
uiSchema, |
| 36 |
registry, |
| 37 |
} = props; |
| 38 |
const { templates, translateString } = registry; |
| 39 |
// Button templates are not overridden in the uiSchema |
| 40 |
const { RemoveButton } = templates.ButtonTemplates; |
| 41 |
const keyLabel = translateString(TranslatableString.KeyLabel, [label]); |
| 42 |
const additional = ADDITIONAL_PROPERTY_FLAG in schema; |
| 43 |
const btnStyle: CSSProperties = { |
| 44 |
flex: 1, |
| 45 |
paddingLeft: 6, |
| 46 |
paddingRight: 6, |
| 47 |
fontWeight: 'bold', |
| 48 |
}; |
| 49 |
|
| 50 |
if (!additional) { |
| 51 |
return ( |
| 52 |
<div className={classNames} style={style}> |
| 53 |
{children} |
| 54 |
</div> |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
const handleBlur = ({ target }: FocusEvent<HTMLInputElement>) => onKeyChange(target.value); |
| 59 |
|
| 60 |
return ( |
| 61 |
<Grid container key={`${id}-key`} alignItems='center' spacing={2} className={classNames} style={style}> |
| 62 |
<Grid item xs> |
| 63 |
<TextField |
| 64 |
fullWidth={true} |
| 65 |
required={required} |
| 66 |
label={keyLabel} |
| 67 |
defaultValue={label} |
| 68 |
disabled={disabled || readonly} |
| 69 |
id={`${id}-key`} |
| 70 |
name={`${id}-key`} |
| 71 |
onBlur={!readonly ? handleBlur : undefined} |
| 72 |
type='text' |
| 73 |
/> |
| 74 |
</Grid> |
| 75 |
<Grid item={true} xs> |
| 76 |
{children} |
| 77 |
</Grid> |
| 78 |
<Grid item={true}> |
| 79 |
<RemoveButton |
| 80 |
iconType='default' |
| 81 |
style={btnStyle} |
| 82 |
disabled={disabled || readonly} |
| 83 |
onClick={onDropPropertyClick(label)} |
| 84 |
uiSchema={uiSchema} |
| 85 |
registry={registry} |
| 86 |
/> |
| 87 |
</Grid> |
| 88 |
</Grid> |
| 89 |
); |
| 90 |
} |