An open API service indexing awesome lists of open source software.

https://github.com/fenrisdotio/fenris.oneofcontrib.blazor

A collection of Blazor components for working with OneOf discriminated unions
https://github.com/fenrisdotio/fenris.oneofcontrib.blazor

blazor c-sharp discriminated-unions oneof

Last synced: 1 day ago
JSON representation

A collection of Blazor components for working with OneOf discriminated unions

Awesome Lists containing this project

README

          

# Fenris.OneOfContrib.Blazor

A collection of Blazor components for working with [OneOf](https://github.com/mcintyre321/OneOf) types in your Blazor
project.

## 🤔 Why?

I was working on a project where I needed to render different components based on the value of a `OneOf`.
I couldn't get the `OneOf.Switch()` method to work in Blazor, and writing `switch` statements over and over again
was annoying and looked messy.

Instead I made a component library, which is actually just a switch statement in disguise,
and found it made my Blazor components much more readable and in general nicer to work with.

## ⭐ Features

- Blazor components for `OneOf` types with up to **8** type parameters.
- Blazor components for types that inherit from `OneOfBase`, also with up to **8** type parameters.

That's it. It's a pretty simple library 😁

## 💿 Installation

Install the [Fenris.OneOfContrib.Blazor](https://www.nuget.org/packages/Fenris.OneOfContrib.Blazor/) NuGet package.

```bash
dotnet add package Fenris.OneOfContrib.Blazor
```

## 🚀 Usage

See the [Examples](examples/OneOfContrib.Blazor.Examples/Pages/Index.razor) project for a full, simple example.

Let's make a small example where we have to deal with a `OneOf`.

First, add the using either to your `_Imports.razor` file, or directly in your specific pages/components:

```csharp
@using Fenris.OneOfContrib.Blazor.Components
```

Then use the component in your blazor pages or components

```csharp


It's a string and the value is @context!




It's an int and the value is @context!


@code {
private OneOf myOneOf = "Hello World!";
}
```

If you are using a `OneOfBase` type, you need to use the `` component instead. Everything else is
identical.

If your union contains more than two types, you need to use the `` or `` components,
where `N` is the number of types in your union. E.g. ``

## 🐛 Bugs, feature requests, and contributions

If you find a bug or have a feature request, please open an issue.

Pull requests are also welcome 😊