|
| 1 | +import {NativeLazyVGridProps, SwiftUI} from '@mgcrea/react-native-swiftui/src'; |
| 2 | +import {useState, type FunctionComponent} from 'react'; |
| 3 | +import {View} from 'react-native'; |
| 4 | +import logoImage from '../assets/logo.png'; |
| 5 | + |
| 6 | +export const LayoutExample: FunctionComponent = () => { |
| 7 | + return ( |
| 8 | + <View style={{flex: 1}}> |
| 9 | + <SwiftUI style={{flex: 1}}> |
| 10 | + <SwiftUI.Text text="LayoutExample" /> |
| 11 | + <SwiftUI.Form> |
| 12 | + <LazyVGridSection /> |
| 13 | + <ImageSection /> |
| 14 | + <RectangleSection /> |
| 15 | + </SwiftUI.Form> |
| 16 | + </SwiftUI> |
| 17 | + </View> |
| 18 | + ); |
| 19 | +}; |
| 20 | + |
| 21 | +const RectangleSection: FunctionComponent = () => { |
| 22 | + const [color, setColor] = useState('blue'); |
| 23 | + |
| 24 | + return ( |
| 25 | + <SwiftUI.Section header="Rectangle Example"> |
| 26 | + <SwiftUI.HStack |
| 27 | + spacing={25} |
| 28 | + style={{ |
| 29 | + backgroundColor: 'white', |
| 30 | + borderWidth: 1, |
| 31 | + borderColor: 'black', |
| 32 | + }}> |
| 33 | + <SwiftUI.Rectangle |
| 34 | + style={{backgroundColor: color, width: 25, height: 50}} |
| 35 | + /> |
| 36 | + <SwiftUI.Rectangle |
| 37 | + style={{backgroundColor: 'red', width: 25, height: 50}} |
| 38 | + /> |
| 39 | + </SwiftUI.HStack> |
| 40 | + |
| 41 | + <SwiftUI.Button |
| 42 | + title={`Change flag to ${color === 'blue' ? 'Italy' : 'France'}`} |
| 43 | + onPress={() => setColor(color === 'blue' ? 'green' : 'blue')} |
| 44 | + /> |
| 45 | + </SwiftUI.Section> |
| 46 | + ); |
| 47 | +}; |
| 48 | + |
| 49 | +const ImageSection: FunctionComponent = () => { |
| 50 | + const [icon, setIcon] = useState('iphone'); |
| 51 | + |
| 52 | + return ( |
| 53 | + <SwiftUI.Section header="Image Example"> |
| 54 | + <SwiftUI.HStack> |
| 55 | + <SwiftUI.Image |
| 56 | + source={logoImage} |
| 57 | + resizeMode="contain" |
| 58 | + style={{ |
| 59 | + width: 128, |
| 60 | + height: 128, |
| 61 | + borderWidth: 1, |
| 62 | + borderColor: 'blue', |
| 63 | + borderRadius: 64, |
| 64 | + }} |
| 65 | + /> |
| 66 | + <SwiftUI.Image |
| 67 | + name={`system:${icon}`} |
| 68 | + // tintColor="#FF0000" |
| 69 | + style={{width: 128, height: 128, fontSize: 64, color: 'blue'}} |
| 70 | + /> |
| 71 | + </SwiftUI.HStack> |
| 72 | + <SwiftUI.Button |
| 73 | + title={`Change icon to ${icon === 'iphone' ? 'ipad' : 'iphone'}`} |
| 74 | + onPress={() => setIcon(icon === 'iphone' ? 'ipad' : 'iphone')} |
| 75 | + /> |
| 76 | + </SwiftUI.Section> |
| 77 | + ); |
| 78 | +}; |
| 79 | + |
| 80 | +const LazyVGridSection: FunctionComponent = () => { |
| 81 | + const alignmentValues: NativeLazyVGridProps['alignment'][] = [ |
| 82 | + 'leading', |
| 83 | + 'center', |
| 84 | + 'trailing', |
| 85 | + ]; |
| 86 | + const [index, setIndex] = useState(1); |
| 87 | + |
| 88 | + return ( |
| 89 | + <SwiftUI.Section header="LazyVGrid Example"> |
| 90 | + <SwiftUI.LazyVGrid |
| 91 | + columns={[ |
| 92 | + {type: 'flexible', minimum: 100}, |
| 93 | + {type: 'flexible', minimum: 100}, |
| 94 | + ]} |
| 95 | + spacing={10} |
| 96 | + alignment={alignmentValues[index]}> |
| 97 | + <SwiftUI.Text text="Item 1" /> |
| 98 | + <SwiftUI.Text text="Item 2" /> |
| 99 | + <SwiftUI.Text text="Item 3" /> |
| 100 | + <SwiftUI.Text text="Item 4" /> |
| 101 | + </SwiftUI.LazyVGrid> |
| 102 | + <SwiftUI.Button |
| 103 | + title={`Change alignment to ${ |
| 104 | + alignmentValues[(index + 1) % alignmentValues.length] |
| 105 | + }`} |
| 106 | + onPress={() => { |
| 107 | + setIndex((index + 1) % alignmentValues.length); |
| 108 | + }} |
| 109 | + /> |
| 110 | + </SwiftUI.Section> |
| 111 | + ); |
| 112 | +}; |
0 commit comments