Bradleycorn
05/26/2023, 3:53 PMValidatingOffsetMapping
class that was added to text fields. I understand what it does, and why it’s there, but I have a question about how we (developers) should implement our own OffsetMapping
instances to return proper offsets and avoid errors thrown by the new(ish) validator. I’ll post more in the 🧵transformedToOriginal
implementation could sometimes return an offset value that is outside the bounds of the original/current text length. Using the above example, if the field currently has a value of “072” and is displaying “07/2d/yyyy”, and then the user clicks to place their cursor somewhere near the end of the mask (say, after the 2nd “y”), our transform would account for the 2 slashes that we insert, but then return an offset that is higher than 3 (the length of the actual text value). Whoops.
To fix this, we create an implementation of OffsetMapping that takes in an “originalTextLength” property, and we can use that when calculating the offset and make sure we produce a proper value. The hassle is, it seems like we’ll have to do this for EVERY OffsetMapping implementation that we create.
Here is where my question comes in … I see that the ValidatingOffsetMapping
class is setup in a similar way (it gets passed the original text length and the transformed text length) … I’m not sure where/how instances of that class get created and passed those values … But would it be possible to update the methods in the OffsetMapping
interface so that they get passed those values (along with the offset that is already passed)? That way ANY implementation of OffsetMapping
would have those length values available so we can ensure we are producing a valid offset. I haven’t traced far enough to see where those methods are called from and if that information is available at that point, but … 🤞Zach Klippenstein (he/him) [MOD]
05/26/2023, 4:50 PMVisualTransformation
is kind of a terrible API, and we’re working on an improved alternative design. We are putting most of our effort into that instead of trying to keep plugging holes in the current one.Bradleycorn
05/26/2023, 6:01 PMColton Idle
06/01/2023, 1:43 PMis kind of a terrible APIso nice to hear that. i thought i was just an idiot