You can customize the behavior and appearance of the 'Add' buttons (the + buttons) in several ways:
1. Change Labels
Update the text used for adding elements or sections via the labels object in mods.
2. Programmatic Addition
You can trigger the addition of elements or sections from anywhere in your application using addCardObj or addSectionObj. These functions require an object of type AddFormObjectParameters.
To get the necessary categoryHash required by these functions, use the onMount callback on the FormBuilder component to capture the InitParameters.
3. Override the Add Component
To completely replace the UI of the 'Add' buttons, provide a callback function to mods.components.add. This callback receives AddFormObjectParameters and should return a React component.
import {
FormBuilder,
addCardObj,
addSectionObj,
type AddFormObjectParameters,
type InitParameters
} from '@ginkgo-bioworks/react-json-schema-form-builder';
import Button from '@mui/material/Button';
import Stack from '@mui/material/Stack';
function MyFormBuilder() {
const [categoryHash, setCategoryHash] = useState('');
const mods = {
components: {
add: (addProps: AddFormObjectParameters) => (
<Stack direction="row" spacing={1}>
<Button onClick={() => addCardObj(addProps)}>Add Element</Button>
<Button onClick={() => addSectionObj(addProps)}>Add Section</Button>
</Stack>
),
},
};
return (
<FormBuilder
onMount={(params: InitParameters) => setCategoryHash(params.categoryHash || '')}
mods={mods}
// ... other props
/>
);
}