Open elgca opened 3 weeks ago
Note: onChange
should not have the inline keyword before, maybe it's a bug in scala
Thanks for the proposal!
It's a bit complicated honestly. I'm not going to build this into ST, because it's a huge effort and very low frequency.
I think the logical thing to do here is to add a manually written, customized version of the generated code. Inline the different states, and expose different components for what has the same physical js implementation.
I don't think it should use match types, since those don't work in intellij. More explicit is better here, honestly.
I'd gladly accept such a manually tweaked facade
I just tried it on IntelliJ, it doesn't work. scala3 is still not well supported😭 Yes, it's a huge effort, if the code cannot be auto generated.
No, it's not a huge effort. This is a very rare typescript pattern, and you can perfectly model the few usecases in mui with copy/paste+customization
Thanks. If this typescript type is rare, that's good news.Manually modify it when I need to.I've designed new type inference to make this work seem easier
type TRUE = true
type FALSE = false
type BOOLEAN = TRUE | FALSE
sealed trait Match
object NotMatch extends Match
object Matched extends Match
type ??[A, B] = A match
case TRUE => B
case FALSE => NotMatch.type
case _ => ??[A, B] // for a compile error
type ::[A, B] = A match
case NotMatch.type => B
case _ => A
// export type AutocompleteFreeSoloValueMapping<FreeSolo> = FreeSolo extends true ? string : never;
type FreeSoloValue[T, FreeSolo] = FreeSolo ?? (T | String) :: T
// export type AutocompleteValue<Value, Multiple, DisableClearable, FreeSolo> = Multiple extends true
// ? Array<Value | AutocompleteFreeSoloValueMapping<FreeSolo>>
// : DisableClearable extends true
// ? NonNullable<Value | AutocompleteFreeSoloValueMapping<FreeSolo>>
// : Value | null | AutocompleteFreeSoloValueMapping<FreeSolo>;
type AutocompleteValue[T, Multiple, DisableClearable, FreeSolo] =
Multiple ?? (js.Array[FreeSoloValue[T, FreeSolo]]) ::
(DisableClearable ?? FreeSoloValue[T, FreeSolo] :: (FreeSoloValue[T, FreeSolo] | Unit))
In Scala3 we have a type inference system, that makes the mui facades more ergonomic. Its looks like this
https://docs.scala-lang.org/scala3/reference/new-types/match-types.html
I used it to rewrite
Autocomplete
theonChange
call cannot get the exact type of value,onChange
event you current value type define it as follows:If we use type match for type inference,we can get like this:
How it works
The first is to provide type definitions, as follows, this is a set of type match types that can be from a given type inference what we want
And then changed the type definition of the
Builder
andonChange
and in order for the types to be correctly inferred, I made sure that the types of
Multiple
,DisableClearable
, andFreeSolo
were explicit when using 'onChange'Of course, these types have default values, and apply needs to be redefined
Now, when calling any method that changes the 'Multiple', 'DisableClearable', and 'FreeSolo' types, onChange can now automatically inference the appropriate type。
.multiple(true)
changeMultiple
toture(as a type)