Custom editors
Write your own editor, or change how a built-in editor renders
Create a basic custom editor
Even though StellarAdmin provides editors for most requirements, you may run into a situation where you need to create a custom editor. A custom editor has two parts: a class that derives from FieldEditor, and a Razor template that renders the editor. You link the editor to its associated template by returning the template name from the TemplateName property.
The example below demonstrates a simple color picker editor:
using StellarAdmin.Resources.Editors;
public class ColorEditor : FieldEditor
{
public override string TemplateName => "Color";
}An editor template is a Razor partial you must create in the Areas/StellarAdmin/Views/Shared/EditorTemplates/ directory. Name the file with the name returned from the TemplateName property using the .cshtml extension, for example Color.cshtml in the example above. The template must inherit from FieldEditorView<TEditor>.
The bound value is passed as the model, so you can use data binding using the asp-for="@Model" syntax.
@using StellarAdmin.Areas.StellarAdmin
@inherits FieldEditorView<ColorEditor>
<sa-input asp-for="@Model" type="color" label="@Title" description="@Description"
readonly="@IsReadOnly" disabled="@IsDisabled" />The sa-* Tag Helpers used in these examples need a _ViewImports.cshtml in Areas/StellarAdmin/Views/.
To use the editor for a field, call UseEditor() with the editor's type:
fields.Add(trip => trip.BrandColor).UseEditor<ColorEditor>();Template properties
Because your template inherits from FieldEditorView<TEditor>, it has access to the following properties:
| Property | Description |
|---|---|
Model | The value of the field. |
Editor | The editor instance with your settings. See adding settings below. |
Title, Description | The field's label and help text. |
IsReadOnly, IsDisabled | Whether the field is read-only or disabled. See rendering read-only and disabled fields. |
EditorData | The data loaded by the editor's handler. See loading data for the editor below. |
Add additional settings
You can expose additional properties that allow other developers to configure your editor. The example below adds a setting that specifies whether the editor my display the color's hex value:
using StellarAdmin.Resources.Editors;
public class ColorEditor : FieldEditor
{
public override string TemplateName => "Color";
public bool ShowHex { get; set; }
}In the template, you can read the setting from the Editor property:
@using StellarAdmin.Areas.StellarAdmin
@inherits FieldEditorView<ColorEditor>
<sa-input asp-for="@Model" type="color" label="@Title" description="@Description"
readonly="@IsReadOnly" disabled="@IsDisabled" />
@if (Editor.ShowHex)
{
<sa-kbd>@Model</sa-kbd>
}You can then configure this setting by passing a callback to UseEditor():
fields.Add(trip => trip.BrandColor).UseEditor<ColorEditor>(color => color.ShowHex = true);Rendering read-only and disabled fields
A field can be read-only or disabled, so the template must support both:
- When
IsReadOnlyistrue, render the input withreadonly, so that it still posts its value. - When
IsDisabledistrue, render the input withdisabled, so that it doesn't post.
StellarAdmin ignores any value posted for a read-only or disabled field, so these only change how the editor looks and behaves.
Loading data for the editor
Some editors may require loading additional data or bootstrapping. For example, you may want to load data required by the editor from a service registered with dependency injection. For these situations, you can create an additional editor handler that derives from FieldEditorHandler<TEditor>, and returns the data from its PrepareAsync() method implementation.
The constructor must take the editor instance as a parameter and any additional parameters you want to inject. The example below loads the brand's colors from an IBrandPalette service, so that the color editor can list them under the input:
using StellarAdmin.Resources.Editors;
public class ColorEditorHandler(ColorEditor editor, IBrandPalette palette)
: FieldEditorHandler<ColorEditor>(editor)
{
public override async Task<object?> PrepareAsync(
FieldEditorContext context,
CancellationToken cancellationToken)
{
return await palette.GetColorsAsync(cancellationToken);
}
}To connect the handler to the editor, implement IFieldEditor<THandler> on the editor:
public class ColorEditor : FieldEditor, IFieldEditor<ColorEditorHandler>
{
public override string TemplateName => "Color";
public bool ShowHex { get; set; }
}The template reads the data from the EditorData property, which has the type object?, so cast it to the type the handler returns:
@using StellarAdmin.Areas.StellarAdmin
@inherits FieldEditorView<ColorEditor>
@{
var palette = EditorData as IReadOnlyList<string> ?? [];
}
<sa-input asp-for="@Model" type="color" label="@Title" description="@Description"
readonly="@IsReadOnly" disabled="@IsDisabled" />
@foreach (var color in palette)
{
<sa-kbd>@color</sa-kbd>
}The FieldEditorContext passed to PrepareAsync() contains the field's name, the record the form is busy editing, and the field's current value, for a handler that loads data based on them.
Editing in an inline row
For the editor to work in an inline row, add a second template with the same name in Areas/StellarAdmin/Views/Shared/GridEditorTemplates/. Render one compact input, without a label, because the cell provides the label for screen readers.