Skip to content

Commit b5a9319

Browse files
fix: refactor
1 parent 2b84f50 commit b5a9319

File tree

8 files changed

+2321
-3519
lines changed

8 files changed

+2321
-3519
lines changed

example/package.json

+10-10
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
"web": "expo start --web"
1010
},
1111
"dependencies": {
12-
"expo": "~47.0.12",
13-
"expo-status-bar": "~1.4.2",
14-
"react": "18.1.0",
15-
"react-dom": "18.1.0",
16-
"react-native": "0.70.5",
17-
"react-native-paper": "^5.2.0",
18-
"react-native-safe-area-context": "^4.5.0",
19-
"react-native-web": "~0.18.9"
12+
"expo": "^48.0.0",
13+
"expo-status-bar": "~1.4.4",
14+
"react": "18.2.0",
15+
"react-dom": "18.2.0",
16+
"react-native": "0.71.3",
17+
"react-native-paper": "^5.8.0",
18+
"react-native-safe-area-context": "4.5.0",
19+
"react-native-web": "~0.18.11"
2020
},
2121
"devDependencies": {
22-
"@babel/core": "^7.12.9",
23-
"@expo/webpack-config": "^0.17.2",
22+
"@babel/core": "^7.20.0",
23+
"@expo/webpack-config": "^18.0.1",
2424
"babel-loader": "^8.1.0",
2525
"babel-plugin-module-resolver": "^4.1.0"
2626
},

example/src/App.tsx

+16-10
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type FormType = {
3636
address?: AddressType | null;
3737
};
3838
export default function App() {
39+
const [hideRequiredField, setHideRequiredField] = React.useState(false);
3940
const scrollViewRef = useRef<ScrollView>(null);
4041
const [{ submit, formProps, hasError }, fh] = useFormState<FormType>(
4142
{
@@ -83,16 +84,21 @@ export default function App() {
8384
label: 'Email',
8485
})}
8586
/>
86-
<TextInputWithError
87-
mode="outlined"
88-
{...fh.telephone('telephone', {
89-
required: true,
90-
minLength: 3,
91-
maxLength: 10,
92-
shouldFollowRegexes: [telephoneRegex],
93-
label: 'Telephone',
94-
})}
95-
/>
87+
<Button onPress={() => setHideRequiredField((prev) => !prev)}>
88+
hide required field
89+
</Button>
90+
{!hideRequiredField && (
91+
<TextInputWithError
92+
mode="outlined"
93+
{...fh.telephone('telephone', {
94+
required: true,
95+
minLength: 3,
96+
maxLength: 10,
97+
shouldFollowRegexes: [telephoneRegex],
98+
label: 'Telephone',
99+
})}
100+
/>
101+
)}
96102
<TextInputWithError
97103
mode="outlined"
98104
{...fh.text('postalCode', {

example/yarn.lock

+2,250-3,492
Large diffs are not rendered by default.

src/useFocusedOnce.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import useRefState from './useRefState';
2+
import type { BooleanUtility, DotNestedKeys } from './types';
3+
import * as React from 'react';
4+
import { deepSet } from './objectPath';
5+
6+
export type UseTouchedReturnType<T> = ReturnType<typeof useTouched<T>>;
7+
export default function useTouched<T>() {
8+
const [touched, sTouched] = useRefState<BooleanUtility<T>>({});
9+
const setTouchedField = React.useCallback(
10+
<K extends DotNestedKeys<T>>(k: K, v: boolean) => {
11+
sTouched((p) => deepSet(p, k, v) as any);
12+
},
13+
[sTouched]
14+
);
15+
return { touched, setTouchedField };
16+
}

src/layout.ts renamed to src/useLayout.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { deepGet } from './objectPath';
21
import type {
32
Customizing,
43
DotNestedKeys,
@@ -17,7 +16,10 @@ export default function useLayout<T>({
1716
scrollViewRef: ScrollViewRef;
1817
}) {
1918
const layoutsRef = React.useRef<Record<string, LayoutRectangle>>({});
20-
return <K extends DotNestedKeys<T>>(k: K, h?: Customizing<T, K>) =>
19+
const onLayoutKey = <K extends DotNestedKeys<T>>(
20+
k: K,
21+
h?: Customizing<T, K>
22+
) =>
2123
referencedCallback(`layout.${k}`, (e: LayoutChangeEvent) => {
2224
h?.onLayout?.(e);
2325

@@ -31,10 +33,14 @@ export default function useLayout<T>({
3133
});
3234
}
3335

34-
const value = deepGet(values.current, k);
35-
// this is not ideal, but we need to check the error after the layout is
36-
// calculated because it's the only way to check if the value has an
37-
// error since we don't have the handlers at mount time
38-
checkError(k, h, value as any, values.current, true);
36+
// const value = deepGet(values.current, k);
37+
// // this is not ideal, but we need to check the error after the layout is
38+
// // calculated because it's the only way to check if the value has an
39+
// // error since we don't have the handlers at mount time
40+
// checkError(k, h, value as any, values.current, true);
3941
});
42+
return {
43+
onLayoutKey,
44+
layoutsRef,
45+
};
4046
}

src/useTouched.ts

Whitespace-only changes.

src/useValues.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import useRefState from './useRefState';
2+
import type { BooleanUtility, DotNestedKeys } from './types';
3+
import * as React from 'react';
4+
import { deepSet } from './objectPath';
5+
6+
export type UseTouchedReturnType<T> = ReturnType<typeof useTouched<T>>;
7+
export default function useTouched<T>() {
8+
const [touched, sTouched] = useRefState<BooleanUtility<T>>({});
9+
const setTouchedField = React.useCallback(
10+
<K extends DotNestedKeys<T>>(k: K, v: boolean) => {
11+
sTouched((p) => deepSet(p, k, v) as any);
12+
},
13+
[sTouched]
14+
);
15+
return { touched, setTouchedField };
16+
}

src/useWasSubmitted.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)