Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offer pipeline builders for Field Attributes #49

Open
MangelMaxime opened this issue Mar 5, 2024 · 0 comments
Open

Offer pipeline builders for Field Attributes #49

MangelMaxime opened this issue Mar 5, 2024 · 0 comments

Comments

@MangelMaxime
Copy link
Owner

MangelMaxime commented Mar 5, 2024

Currently, attributes are exposed using records only meaning that we need to provides all the properties all the time.

Attributes = {
	Label = "Order Value"
	Placeholder = "Order Value"
	HtmlAttributes = []
}

This is annoying because we need to provide information that we don't care about.

For example, if you have a DatePickerField you could have attributes looking like that:

type Attributes = {
	Label: string
	DisplayMode: DisplayMode
	ShouldDisableDate: System.DateTime -> bool
	ShouldDisableMonth: System.DateTime -> bool
	ShouldDisableYear: System.DateTime -> bool
}

Forcing the user to provide all the ShouldDisableXX functions, even when he don't want to disable any.

Exposing an alternative API would allow us to work around this problem:

  1. We could use classes to have optional arguments but discovery is not that great in IDE IHMO
  2. We could use pipeline builders, making discovering API easier
Form.datePickerField {
    Parser =
        fun value ->
            match value.SelectedDate with
            | Some date -> Ok date
            | None -> Error "This field is required"
    Value = _.CollectionDate
    Update =
        fun newValue values -> {
            values with
                CollectionDate = newValue
                // Reset the time slot when the date changes
                CollectionTimeSlot = ""
        }
    Error = fun _ -> None
    Attributes =
        DatePickerField.Attributes.create "Collection Date"
        |> DatePickerField.Attributes.withDisplayMode DatePickerField.DisplayMode.Default
        |> DatePickerField.Attributes.withShouldDisableDate (fun date ->
            timeSlots
            |> List.exists (fun slot -> Date.getDayKey slot.Start = Date.getDayKey date)
            |> not
        )
}
  1. Another solution is to use fluent API like I did in the past for Thoth.Elmish.FormBuilder old comparaison of the different style

Today, I think I lean more toward pipeline builders.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant