Files
react-native/packages/rn-tester/js/examples/OSSLibraryExample/OSSLibraryExample.js
Tim Yung 1977dd6596 RN: Sort Pragmas in Headers (#51554)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/51554

Sorts pragma directives file headers in React Native.

Changelog:
[Internal]

Reviewed By: SamChou19815

Differential Revision: D75264593

fbshipit-source-id: 9e4b253dd0fc94dc2fc469d7114b93a8aae305f4
2025-05-22 21:18:53 -07:00

130 lines
3.3 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
import type {NativeComponentType} from '@react-native/oss-library-example';
import RNTesterText from '../../components/RNTesterText';
import {
SampleNativeComponent,
SampleNativeComponentCommands,
} from '@react-native/oss-library-example';
import {NativeSampleModule} from '@react-native/oss-library-example';
import * as React from 'react';
import {useRef, useState} from 'react';
import {Button, View} from 'react-native';
import {StyleSheet} from 'react-native';
const colors = [
'#0000FF',
'#FF0000',
'#00FF00',
'#003300',
'#330000',
'#000033',
];
// $FlowFixMe[value-as-type]
const styles: StyleSheet = StyleSheet.create({
container: {
flex: 1,
},
column: {
flex: 2,
justifyContent: 'center',
paddingLeft: 5,
paddingRight: 5,
},
});
function SampleNativeComponentContainer(props: {}): React.Node {
const ref = useRef<React.ElementRef<NativeComponentType> | null>(null);
const [opacity, setOpacity] = useState(1.0);
const [arrayValues, setArrayValues] = useState([1, 2, 3]);
return (
<View style={{flex: 1}}>
<SampleNativeComponent
ref={ref}
style={{flex: 1}}
opacity={opacity}
values={arrayValues}
onIntArrayChanged={event => {
console.log(event.nativeEvent.values);
console.log(event.nativeEvent.boolValues);
console.log(event.nativeEvent.floats);
console.log(event.nativeEvent.doubles);
console.log(event.nativeEvent.yesNos);
console.log(event.nativeEvent.strings);
console.log(event.nativeEvent.latLons);
console.log(event.nativeEvent.multiArrays);
}}
/>
<Button
title="Change Background"
onPress={() => {
let newColor = colors[Math.floor(Math.random() * 5)];
SampleNativeComponentCommands.changeBackgroundColor(
// $FlowFixMe[incompatible-call]
ref.current,
newColor,
);
}}
/>
<Button
title="Set Opacity"
onPress={() => {
setOpacity(Math.random());
setArrayValues([
Math.floor(Math.random() * 100),
Math.floor(Math.random() * 100),
Math.floor(Math.random() * 100),
]);
}}
/>
</View>
);
}
function NativeSampleModuleWrapper(props: {}): React.Node {
const [randomNumber, setRandomNumber] = useState(0);
return (
<View style={styles.container}>
<Button
title="Get Random Number"
onPress={() => {
const x = NativeSampleModule?.getRandomNumber();
if (x != null) {
setRandomNumber(x);
}
}}
/>
<RNTesterText style={styles.column}>{randomNumber}</RNTesterText>
</View>
);
}
exports.title = 'OSS Library Example';
exports.description = 'OSS Library Example.';
exports.examples = [
{
title: 'OSS Library Example',
description: 'Click to change background and opacity',
render(): React.MixedElement {
return (
<>
<SampleNativeComponentContainer />
<NativeSampleModuleWrapper />
</>
);
},
},
];