fix(workout-builder): defer TextInput updates to onBlur using local state
This commit is contained in:
@@ -16,6 +16,7 @@ export function BuilderScreen() {
|
|||||||
const navigation = useNavigation<NativeStackNavigationProp<RootStackParamList>>();
|
const navigation = useNavigation<NativeStackNavigationProp<RootStackParamList>>();
|
||||||
const { poolLength: defaultPoolLength } = useSettings();
|
const { poolLength: defaultPoolLength } = useSettings();
|
||||||
const [poolLength, setPoolLength] = useState(defaultPoolLength);
|
const [poolLength, setPoolLength] = useState(defaultPoolLength);
|
||||||
|
const [fieldValues, setFieldValues] = useState<Record<string, string>>({});
|
||||||
const {
|
const {
|
||||||
sets,
|
sets,
|
||||||
workoutName,
|
workoutName,
|
||||||
@@ -124,8 +125,19 @@ export function BuilderScreen() {
|
|||||||
<Text className="text-xs font-medium text-slate-500 mb-1">{t("distanceM")}</Text>
|
<Text className="text-xs font-medium text-slate-500 mb-1">{t("distanceM")}</Text>
|
||||||
<TextInput
|
<TextInput
|
||||||
keyboardType="numeric"
|
keyboardType="numeric"
|
||||||
value={String(set.distancePerRep)}
|
value={fieldValues[`${index}-distance`] ?? String(set.distancePerRep)}
|
||||||
onChangeText={(val) => updateSet(index, { distancePerRep: Number(val) || 0 })}
|
onChangeText={(val) => setFieldValues((prev) => ({ ...prev, [`${index}-distance`]: val }))}
|
||||||
|
onBlur={() => {
|
||||||
|
const num = Number(fieldValues[`${index}-distance`] ?? set.distancePerRep);
|
||||||
|
updateSet(index, { distancePerRep: isNaN(num) || num < poolLength ? poolLength : num });
|
||||||
|
setFieldValues((prev) => {
|
||||||
|
const next = { ...prev };
|
||||||
|
delete next[`${index}-distance`];
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
placeholder={String(poolLength)}
|
||||||
|
placeholderTextColor="#94a3b8"
|
||||||
className="bg-white border border-slate-300 rounded-lg px-3 py-2 text-slate-900"
|
className="bg-white border border-slate-300 rounded-lg px-3 py-2 text-slate-900"
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
@@ -133,8 +145,19 @@ export function BuilderScreen() {
|
|||||||
<Text className="text-xs font-medium text-slate-500 mb-1">{t("reps")}</Text>
|
<Text className="text-xs font-medium text-slate-500 mb-1">{t("reps")}</Text>
|
||||||
<TextInput
|
<TextInput
|
||||||
keyboardType="numeric"
|
keyboardType="numeric"
|
||||||
value={String(set.repCount)}
|
value={fieldValues[`${index}-rep`] ?? String(set.repCount)}
|
||||||
onChangeText={(val) => updateSet(index, { repCount: Number(val) || 1 })}
|
onChangeText={(val) => setFieldValues((prev) => ({ ...prev, [`${index}-rep`]: val }))}
|
||||||
|
onBlur={() => {
|
||||||
|
const num = Number(fieldValues[`${index}-rep`] ?? set.repCount);
|
||||||
|
updateSet(index, { repCount: isNaN(num) || num < 1 ? 1 : num });
|
||||||
|
setFieldValues((prev) => {
|
||||||
|
const next = { ...prev };
|
||||||
|
delete next[`${index}-rep`];
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
placeholder="1"
|
||||||
|
placeholderTextColor="#94a3b8"
|
||||||
className="bg-white border border-slate-300 rounded-lg px-3 py-2 text-slate-900"
|
className="bg-white border border-slate-300 rounded-lg px-3 py-2 text-slate-900"
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
@@ -142,8 +165,19 @@ export function BuilderScreen() {
|
|||||||
<Text className="text-xs font-medium text-slate-500 mb-1">{t("restS")}</Text>
|
<Text className="text-xs font-medium text-slate-500 mb-1">{t("restS")}</Text>
|
||||||
<TextInput
|
<TextInput
|
||||||
keyboardType="numeric"
|
keyboardType="numeric"
|
||||||
value={String(set.restDuration)}
|
value={fieldValues[`${index}-rest`] ?? String(set.restDuration)}
|
||||||
onChangeText={(val) => updateSet(index, { restDuration: Number(val) || 0 })}
|
onChangeText={(val) => setFieldValues((prev) => ({ ...prev, [`${index}-rest`]: val }))}
|
||||||
|
onBlur={() => {
|
||||||
|
const num = Number(fieldValues[`${index}-rest`] ?? set.restDuration);
|
||||||
|
updateSet(index, { restDuration: isNaN(num) || num < 0 ? 0 : num });
|
||||||
|
setFieldValues((prev) => {
|
||||||
|
const next = { ...prev };
|
||||||
|
delete next[`${index}-rest`];
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
placeholder="0"
|
||||||
|
placeholderTextColor="#94a3b8"
|
||||||
className="bg-white border border-slate-300 rounded-lg px-3 py-2 text-slate-900"
|
className="bg-white border border-slate-300 rounded-lg px-3 py-2 text-slate-900"
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
|
|||||||
Reference in New Issue
Block a user