diff options
Diffstat (limited to 'src/view/com/util/forms/Button.tsx')
-rw-r--r-- | src/view/com/util/forms/Button.tsx | 45 |
1 files changed, 34 insertions, 11 deletions
diff --git a/src/view/com/util/forms/Button.tsx b/src/view/com/util/forms/Button.tsx index 8049d2243..076fa1baa 100644 --- a/src/view/com/util/forms/Button.tsx +++ b/src/view/com/util/forms/Button.tsx @@ -7,6 +7,8 @@ import { Pressable, ViewStyle, PressableStateCallbackType, + ActivityIndicator, + View, } from 'react-native' import {Text} from '../text/Text' import {useTheme} from 'lib/ThemeContext' @@ -48,17 +50,19 @@ export function Button({ accessibilityHint, accessibilityLabelledBy, onAccessibilityEscape, + withLoading = false, }: React.PropsWithChildren<{ type?: ButtonType label?: string style?: StyleProp<ViewStyle> labelStyle?: StyleProp<TextStyle> - onPress?: () => void + onPress?: () => void | Promise<void> testID?: string accessibilityLabel?: string accessibilityHint?: string accessibilityLabelledBy?: string onAccessibilityEscape?: () => void + withLoading?: boolean }>) { const theme = useTheme() const typeOuterStyle = choose<ViewStyle, Record<ButtonType, ViewStyle>>( @@ -138,13 +142,16 @@ export function Button({ }, ) + const [isLoading, setIsLoading] = React.useState(false) const onPressWrapped = React.useCallback( - (event: Event) => { + async (event: Event) => { event.stopPropagation() event.preventDefault() - onPress?.() + withLoading && setIsLoading(true) + await onPress?.() + withLoading && setIsLoading(false) }, - [onPress], + [onPress, withLoading], ) const getStyle = React.useCallback( @@ -160,23 +167,35 @@ export function Button({ [typeOuterStyle, style], ) + const renderChildern = React.useCallback(() => { + if (!label) { + return children + } + + return ( + <View style={styles.labelContainer}> + {label && withLoading && isLoading ? ( + <ActivityIndicator size={12} color={typeLabelStyle.color} /> + ) : null} + <Text type="button" style={[typeLabelStyle, labelStyle]}> + {label} + </Text> + </View> + ) + }, [children, label, withLoading, isLoading, typeLabelStyle, labelStyle]) + return ( <Pressable style={getStyle} onPress={onPressWrapped} + disabled={isLoading} testID={testID} accessibilityRole="button" accessibilityLabel={accessibilityLabel} accessibilityHint={accessibilityHint} accessibilityLabelledBy={accessibilityLabelledBy} onAccessibilityEscape={onAccessibilityEscape}> - {label ? ( - <Text type="button" style={[typeLabelStyle, labelStyle]}> - {label} - </Text> - ) : ( - children - )} + {renderChildern} </Pressable> ) } @@ -187,4 +206,8 @@ const styles = StyleSheet.create({ paddingVertical: 8, borderRadius: 24, }, + labelContainer: { + flexDirection: 'row', + gap: 8, + }, }) |