Skip to content

Commit 74918f1

Browse files
committed
feat(update): minor changes
1 parent 054db75 commit 74918f1

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

example/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { TanStackFormExample } from './src/views/TanStackFormExample';
88
import { AdvancedFormExample } from './src/views/AdvancedFormExample.private';
99

1010
function App(): React.JSX.Element {
11-
return <ReactHookFormExample />;
11+
return <TanStackFormExample />;
1212
}
1313

1414
export default App;

example/src/views/TanStackFormExample.tsx

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,50 @@ import { SwiftUI } from '@mgcrea/react-native-swiftui/src';
1010
import { Alert, Text, TextInput, View } from 'react-native';
1111
import { type FunctionComponent } from 'react';
1212

13+
export const GENDERS = {
14+
male: 'Male',
15+
female: 'Female',
16+
} as const;
17+
export const GENDER_OPTIONS = toLabelledOptions(GENDERS);
18+
export type Gender = keyof typeof GENDERS;
19+
20+
export const LEVELS = {
21+
B: 'Beginner',
22+
N: 'Novice',
23+
I: 'Intermediate',
24+
A: 'Advanced',
25+
E: 'Elite',
26+
} as const;
27+
export const LEVEL_OPTIONS = toLabelledOptions(LEVELS);
28+
export type Level = keyof typeof LEVELS;
29+
1330
type FormData = {
1431
firstName: string;
1532
lastName: string;
33+
gender: Gender;
34+
level: Level;
1635
birthDate: Date;
1736
};
1837

38+
const defaultValues: FormData = {
39+
firstName: 'Olivier',
40+
lastName: '',
41+
gender: 'male',
42+
level: 'B',
43+
birthDate: new Date('1990-02-22T00:00:00Z'),
44+
};
45+
1946
export const TanStackFormExample: FunctionComponent = () => {
2047
const form = useForm({
21-
defaultValues: {
22-
firstName: 'Olivier',
23-
lastName: '',
24-
birthDate: new Date('1990-02-22T00:00:00Z'),
25-
},
48+
defaultValues,
2649
onSubmit: ({ value }) => {
2750
console.log('Submitted with data:', JSON.stringify(value, null, 2));
2851
},
2952
});
3053

3154
return (
3255
<View style={{ flex: 1 }}>
33-
<SwiftUI style={{ flex: 1 }}>
56+
<SwiftUI style={{ flex: 1 }} debug>
3457
<SwiftUI.VStack>
3558
<SwiftUI.Text text="TanStackFormExample" />
3659
<SwiftUI.Form>
@@ -75,6 +98,30 @@ export const TanStackFormExample: FunctionComponent = () => {
7598
/>
7699
)}
77100
</form.Field>
101+
<form.Field name="gender">
102+
{field => (
103+
<SwiftUI.Picker
104+
label="Gender"
105+
options={GENDER_OPTIONS}
106+
pickerStyle="menu"
107+
onBlur={field.handleBlur}
108+
onChange={field.handleChange}
109+
selection={field.state.value}
110+
/>
111+
)}
112+
</form.Field>
113+
<form.Field name="level">
114+
{field => (
115+
<SwiftUI.Picker
116+
label="Level"
117+
options={LEVEL_OPTIONS}
118+
pickerStyle="menu"
119+
onBlur={field.handleBlur}
120+
onChange={field.handleChange}
121+
selection={field.state.value}
122+
/>
123+
)}
124+
</form.Field>
78125
<form.Field
79126
name="birthDate"
80127
validators={{
@@ -106,3 +153,13 @@ export const TanStackFormExample: FunctionComponent = () => {
106153
</View>
107154
);
108155
};
156+
157+
function toLabelledOptions<
158+
K extends keyof T,
159+
T extends Record<string | number, string>,
160+
>(options: T) {
161+
return Object.entries(options).map(([value, label]) => ({
162+
value: value as K,
163+
label,
164+
}));
165+
}

0 commit comments

Comments
 (0)