-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtext-input-modal.tsx
64 lines (58 loc) · 1.62 KB
/
text-input-modal.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import {
Button,
HelperText,
Paragraph,
TextInput,
Title,
} from 'react-native-paper';
import {StyleSheet, View} from 'react-native';
import React, {useCallback, useState} from 'react';
import {Space} from '../view';
import _ from 'lodash';
import {createUseModal} from 'react-native-use-modal';
const isNameValid = (value: string) => value.length > 2;
export const useTextInputModal = createUseModal<string>(({confirm, cancel}) => {
const [name, setName] = useState('');
const [errorMessage, setErrorMessage] = useState('');
const handlePressConfirm = useCallback(() => {
if (isNameValid(name)) {
setErrorMessage('');
confirm(name);
} else {
setErrorMessage('Name must be at least three letters long');
}
}, [confirm, name]);
return (
<View style={styles.container}>
<Title>Enter your name</Title>
<Paragraph>Will display your name on the screen.</Paragraph>
<Space height={16} />
<TextInput
label={'Name'}
value={name}
onChangeText={setName}
mode={'outlined'}
error={!_.isEmpty(errorMessage)}
/>
<HelperText type={'error'}>{errorMessage}</HelperText>
<Space height={16} />
<View style={styles.buttonContainer}>
<Button onPress={handlePressConfirm}>Confirm</Button>
<Button onPress={cancel}>Cancel</Button>
</View>
</View>
);
});
const styles = StyleSheet.create({
container: {
backgroundColor: '#fff',
margin: 16,
borderRadius: 12,
paddingHorizontal: 16,
paddingVertical: 8,
},
buttonContainer: {
flexDirection: 'row',
alignSelf: 'flex-end',
},
});