react-native-macos/Libraries/Types/CodegenTypes.js

41 строка
1.3 KiB
JavaScript
Исходник Обычный вид История

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
*/
'use strict';
import type {SyntheticEvent} from './CoreEventTypes';
// Event types
Add paperTopLevelNameDeprecated Summary: This diff adds a way for the codegen to handle view configs with non-standard top event names by adding a `paperTopLevelNameDeprecated` field to events in the schema. ## The problem The problem this is solving is that Android host components build their own options for event settings in the view config. So instead of enforcing `onChange` to use the top level event name `topChange` like iOS does, Android can use `change` or `forbarChange` or anything the person adding the component wanted at the time: ``` // Expected topChange: { registrationName: 'onChange', }, // Android bringBackStargateYouCowards: { registrationName: 'onChange', }, ``` This is possible because of the way Android builds these settings ``` // On iOS, notice that there's no option to change the top level name: RCT_EXPORT_VIEW_PROPERTY(onChange, RCTDirectEventBlock); // On Android, you provide the top level event name Override public Map getExportedCustomDirectEventTypeConstants() { return MapBuilder.of( "bringBackStargateYouCowards", MapBuilder.of("registrationName", "onChange")); } ``` Since the codegen does not allow us to specify the top level event name (similar to iOS), we don't have a way to customize the names to support android ## The solution To fix this, we're adding an extra option the event flow types: ``` onBubblingChange: (event: BubblingEvent<Event, 'customBubblingName'>) => void, onDirectChange: (event: DirectEvent<Event, 'customDirectName'>) => void, ``` These will register **both** top level names in the view config: ``` { directEventTypes: { // Notice the same registration name is configured for different top level events topChange: { registrationName: 'onChange', }, bringBackStargateYouCowards: { registrationName: 'onChange', }, } } ``` This means when either `topChange` or `bringBackStargateYouCowards` fires it will call the onChange listener. **This gives us the flexibility to rename the native event name without breaking backwards compatibility.** Old apps will work when `bringBackStargateYouCowards` is fired, and new apps with an update will work when `topChange` fires. Note: only the correct name will be generated for Fabric so technically we don't even really need to migrate the paper names and this prop can be deleted when paper is gone. Reviewed By: cpojer Differential Revision: D16042065 fbshipit-source-id: 40d076b43ffbbfc6c65c3c19de481d922a2add62
2019-06-28 16:46:01 +03:00
// We're not using the PaperName, it is only used to codegen view config settings
export type BubblingEventHandler<
Add paperTopLevelNameDeprecated Summary: This diff adds a way for the codegen to handle view configs with non-standard top event names by adding a `paperTopLevelNameDeprecated` field to events in the schema. ## The problem The problem this is solving is that Android host components build their own options for event settings in the view config. So instead of enforcing `onChange` to use the top level event name `topChange` like iOS does, Android can use `change` or `forbarChange` or anything the person adding the component wanted at the time: ``` // Expected topChange: { registrationName: 'onChange', }, // Android bringBackStargateYouCowards: { registrationName: 'onChange', }, ``` This is possible because of the way Android builds these settings ``` // On iOS, notice that there's no option to change the top level name: RCT_EXPORT_VIEW_PROPERTY(onChange, RCTDirectEventBlock); // On Android, you provide the top level event name Override public Map getExportedCustomDirectEventTypeConstants() { return MapBuilder.of( "bringBackStargateYouCowards", MapBuilder.of("registrationName", "onChange")); } ``` Since the codegen does not allow us to specify the top level event name (similar to iOS), we don't have a way to customize the names to support android ## The solution To fix this, we're adding an extra option the event flow types: ``` onBubblingChange: (event: BubblingEvent<Event, 'customBubblingName'>) => void, onDirectChange: (event: DirectEvent<Event, 'customDirectName'>) => void, ``` These will register **both** top level names in the view config: ``` { directEventTypes: { // Notice the same registration name is configured for different top level events topChange: { registrationName: 'onChange', }, bringBackStargateYouCowards: { registrationName: 'onChange', }, } } ``` This means when either `topChange` or `bringBackStargateYouCowards` fires it will call the onChange listener. **This gives us the flexibility to rename the native event name without breaking backwards compatibility.** Old apps will work when `bringBackStargateYouCowards` is fired, and new apps with an update will work when `topChange` fires. Note: only the correct name will be generated for Fabric so technically we don't even really need to migrate the paper names and this prop can be deleted when paper is gone. Reviewed By: cpojer Differential Revision: D16042065 fbshipit-source-id: 40d076b43ffbbfc6c65c3c19de481d922a2add62
2019-06-28 16:46:01 +03:00
T,
PaperName: string | empty = empty, // eslint-disable-line no-unused-vars
> = (event: SyntheticEvent<T>) => void | Promise<void>;
export type DirectEventHandler<
Add paperTopLevelNameDeprecated Summary: This diff adds a way for the codegen to handle view configs with non-standard top event names by adding a `paperTopLevelNameDeprecated` field to events in the schema. ## The problem The problem this is solving is that Android host components build their own options for event settings in the view config. So instead of enforcing `onChange` to use the top level event name `topChange` like iOS does, Android can use `change` or `forbarChange` or anything the person adding the component wanted at the time: ``` // Expected topChange: { registrationName: 'onChange', }, // Android bringBackStargateYouCowards: { registrationName: 'onChange', }, ``` This is possible because of the way Android builds these settings ``` // On iOS, notice that there's no option to change the top level name: RCT_EXPORT_VIEW_PROPERTY(onChange, RCTDirectEventBlock); // On Android, you provide the top level event name Override public Map getExportedCustomDirectEventTypeConstants() { return MapBuilder.of( "bringBackStargateYouCowards", MapBuilder.of("registrationName", "onChange")); } ``` Since the codegen does not allow us to specify the top level event name (similar to iOS), we don't have a way to customize the names to support android ## The solution To fix this, we're adding an extra option the event flow types: ``` onBubblingChange: (event: BubblingEvent<Event, 'customBubblingName'>) => void, onDirectChange: (event: DirectEvent<Event, 'customDirectName'>) => void, ``` These will register **both** top level names in the view config: ``` { directEventTypes: { // Notice the same registration name is configured for different top level events topChange: { registrationName: 'onChange', }, bringBackStargateYouCowards: { registrationName: 'onChange', }, } } ``` This means when either `topChange` or `bringBackStargateYouCowards` fires it will call the onChange listener. **This gives us the flexibility to rename the native event name without breaking backwards compatibility.** Old apps will work when `bringBackStargateYouCowards` is fired, and new apps with an update will work when `topChange` fires. Note: only the correct name will be generated for Fabric so technically we don't even really need to migrate the paper names and this prop can be deleted when paper is gone. Reviewed By: cpojer Differential Revision: D16042065 fbshipit-source-id: 40d076b43ffbbfc6c65c3c19de481d922a2add62
2019-06-28 16:46:01 +03:00
T,
PaperName: string | empty = empty, // eslint-disable-line no-unused-vars
> = (event: SyntheticEvent<T>) => void | Promise<void>;
// Prop types
export type Double = number;
export type Float = number;
export type Int32 = number;
type DefaultTypes = number | boolean | string | $ReadOnlyArray<string>;
// Default handling, ignore the unused value
// we're only using it for type checking
//
// TODO: (rickhanlonii) T44881457 If a default is provided, it should always be optional
// but that is currently not supported in the codegen since we require a default
//
// eslint-disable-next-line no-unused-vars
export type WithDefault<Type: DefaultTypes, Value: ?Type | string> = ?Type;