fakeSyntheticFormEvent function

SyntheticFormEvent fakeSyntheticFormEvent(
  1. Element element,
  2. String type
)

If the consumer specifies a callback like onChange on one of our custom form components that are not actually form elements - we still need a valid SyntheticFormEvent to pass as the expected parameter to that callback.

This helper method generates a "fake" SyntheticFormEvent, with nothing but the target set to element, type set to type and timeStamp set to the current time. All other arguments are noop, false or null.

Implementation

SyntheticFormEvent fakeSyntheticFormEvent(Element element, String type) {
  return jsifyAndAllowInterop({
    // SyntheticEvent fields
    'bubbles': false,
    'cancelable': false,
    'currentTarget': element,
    'defaultPrevented': false,
    'eventPhase': Event.AT_TARGET,
    'isTrusted': false,
    'nativeEvent': null,
    'target': element,
    'timeStamp': DateTime.now().millisecondsSinceEpoch,
    'type': type,
    // SyntheticEvent methods
    'stopPropagation': () {},
    'preventDefault': () {},
    'persist': () {},
    'isPersistent': () => true,
  }) as SyntheticFormEvent;
}