Writing your own Components

Adding custom components

AdminJS handles all properties and actions using its own, built-in components. These are the familiar text inputs, checkboxes, paginated lists, edit forms etc. However, you can define a custom component for any single property or action instead. This gives you great control over how the data is being displayed and modified.

In general, you need to create an instance of ComponentLoader, add your custom components there, pass it in the AdminJS options object and specify which properties/actions are using your custom components and where.

./components.ts:

import { ComponentLoader } from 'adminjs'

const componentLoader = new ComponentLoader()

const Components = {
    MyInput: componentLoader.add('MyInput', './my-input'),
    // other custom components
}

export { componentLoader, Components }

./my-input.tsx

import React from 'react'

// just some regular React component
const MyInputComponent = () => <input />

export default MyInputComponent

./some-resource.ts

import { Components } from './components.js'

export const SomeResource = {
  resource: Something, // database model
  options: {
    properties: {
      someText: {
        type: 'string',
        components: {
          edit: Components.MyInput, // this is our custom component
        },
      },
    },
  },
}

./index.ts

import { AdminJS } from 'adminjs'
import { componentLoader } from './components.js'
import { SomeResource } from './some-resource.js'

const admin = new AdminJS({
    resources: [SomeResource],
    componentLoader, // the loader needs to be added here
    // other options
})

admin.watch() // this builds your frontend code in development environment

// rest of the adapter and plugin code

Components for actions are added in the exact same way, only in the action section of a resource instead of properties. Refer to Action with custom component and Creating custom properties pages for more information.

Custom Component Structure

Files added to ComponentLoader need to expose the component function as a default export. Both .jsx and .tsx formats are supported.

Advanced usage: The path passed in the second argument to the ComponentLoader.add() and ComponentLoader.override() functions needs to be relative to the file where it was called. If you want to wrap these calls in another function you might find that AdminJS cannot find the correct files. To fix that, pass a third argument with the name of the caller function, like this:

import { ComponentLoader } from 'adminjs'

const loader = new ComponentLoader()

export const bundleFile = (key: string, path: string) => {
    loader.add(key, path, 'bundleFile') // `bundleFile` is the name of this function
}

Dependencies

AdminJS uses these dependencies internally, so they are exposed for your code without the need to require them in the package.json file:

State management

Routing

Styling

Other

Props passed to components

In your property and action components, you can use props passed by their controlling components.

Currently we have 2 controlling components:

Check out their documentation to see available props

Other, internal components (like Dashboard) have either no or different props, see the source code in each case.

Overriding internal AdminJS components

ComponentLoader also has an .override() method that lets you replace components used by AdminJS internally by your own custom components. This is useful in cases where you want to change or add behavior to the entire AdminJS app, for example:

  • Adding a custom dashboard

  • Changing the app layout

  • Overriding entire controls (like replacing boolean checkboxes with toggles)

  • Customizing component look and feel when theming is insufficient

Overriding component works exactly the same way as adding custom components, but you need to specify the matching name of the component (here's the list).

The methods are split into .add() and .override() as a safety layer, so you don't accidentally override an internal component with a custom component of the same name - the functions will throw an error when used for conflicting components. In short, .add() won't let you use internal component names and .override() requires an internal component name.

Other customizations

Theming

We support Theme compatible with https://system-ui.com/theme standard.

In order to override default colors, fonts, sizes etc., you can put your values in AdminJSOptions.branding.

Using style props

AdminJS components are supercharged with multiple props controlling styles. For instance in order to change color of a module:@adminjs/design-system.Button you can pass backgroundColor (bg) from the module:@adminjs/design-system.Theme like that:

<Button bg="primary60"></Button>

For all possible options visit the Theme description.

Adding custom css to components

If using style props is not enough - you can always pass your custom CSS. So for instance let's assume that you would like to overwrite CSS in a Button component. You can do this like that:

import { Button } from '@adminjs/design-system'

const MyButton = styled(Button)`
  background-color: #ccc;
  color: ${({theme}) => theme.colors.grey100};
  ...
`

We use styled-components under the hood so make sure to check out their docs.

Reusing UI Components of AdminJS

AdminJS gives you the ability to reuse its component library:

import { Label } from '@adminjs/design-system'

const YourComponent (props) => {(
  <Label>Some styled text<Label>
)}

We divide components internally to 2 groups:

  • application components - which requires AdminJS, you can think about them as "smart components"

  • and design system components - they don't require AdminJS and you can use them outside of the AdminJS setup.

That is why sometimes you have to import components from 'adminjs' package and sometimes from '@adminjs/design-system'.

Each of the components is described with the playground option, so make sure to check out all the documentation of all the components.

One of the most versatile component is a BasePropertyComponent. It allows you to render any property. Combined with useRecord is a powerful tool for building forms.

Creating Custom Pages

You can also use custom components as full pages by specifying their name in the pages object in AdminJS options:

import AdminJS from 'adminjs'
import { Components } from './components.js'

new AdminJS({
    pages: {
        myPage: { // name, will be used to build an URL
            handler: // handler code,
            component: Components.MyPage,
            icon: // page icon name
        }
    }
})

Using other AdminJS frontend classes and objects

AdminJS also exposes following classes:

You can use them like this:

import { ApiClient, ViewHelpers } from 'adminjs'

Last updated