Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input component 86937xxay #2

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/components/CustomInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React, { useState } from 'react';
import { StyleSheet, Text, TextInput, View } from 'react-native';

interface CustomInputProps {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zamiast interface stosujmy type

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

każdy wie, że ten typ jest napisany do propsów komponentu poniżej, więc nie ma potrzeby pisać CustomInputProps. Wystarczy Props :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dobrą techniką tutaj byłoby przyjęcie przez propsów dokładnie takich samych typów jak komponent TextInput. W ten sposób mamy pewność, że inputowi można przekazać jego wszystkie propsy a wyglądałoby to tak:
type Props = React.ComponentsPropsWithRef<typeof TextInput> & { jakieś nasze propsy, których TextInput nie zawiera, takie jak pewnie label }

label: string;
secureTextEntry?: boolean;
onSubmit: (text: string) => void;
}

const CustomInput: React.FC<CustomInputProps> = ({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ogólnie są dwie opcje typowania komponentu. W zasadzie obie są okej pod warunkiem, że stosuje je każdy w aplikacji spójnie.
W tym przypadku ja proponuję taki schemat, bo po prostu sam taki używam na daily basis w pracy i się przyzwyczaiłem XD

const CustomInput = ({ ...propsy }: CustomInputProps) => ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nazwa zamiast CustomInput to Input

label,
secureTextEntry,
onSubmit,
}) => {
const [text, setText] = useState('');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jeśli już byś chciał użyć useState, co w tym przypadku jest zbędne, to na przyszłość zauważ, że w takim przypadku jak tutaj mamy czasami do czynienia z inputami, które mają domyślnie wpisaną jakąś wartość. Wówczas useState powinien przyjąć tą domyślną wartość jako wartość początkową, a nie pustego stringa


const handleSubmit = () => {
onSubmit(text);
};

return (
<View style={styles.container}>
<Text style={styles.label}>{label}</Text>
<TextInput
style={styles.input}
value={text}
secureTextEntry={secureTextEntry}
onChangeText={setText}
onSubmitEditing={handleSubmit}
Comment on lines +24 to +29
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

brakuje propsa defaultValue.
Ogólnie wydaje mi się, że raczej tutaj nie będzie potrzeby użycia hooka. Sprawdź czy przypadkiem metoda onSubmitEditing nie zawiera eventu jako 1 argumentu, z którego możesz wyciągnąć wartość inputa.

/>
</View>
);
};

const styles = StyleSheet.create({
container: {
marginBottom: 10,
},
label: {
fontSize: 16,
marginBottom: 5,
fontFamily: '',
},
input: {
borderWidth: 1,
borderColor: '#c7c4d6',
borderRadius: 10,
width: 300,
height: 40,
padding: 10,
fontSize: 16,
},
});
Comment on lines +35 to +53
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

w projekcie można już korzystać ze stylowania zgodnie z dokumentacją. Docelowo chcemy tworzyć wersję w motywie jasnym i ciemnym i takie też inputy potrzebujemy w tym zadaniu


export default CustomInput;