Files
lettersoup-online/002_source/ionic_mobile/src/data/utils.tsx
louiscklaw 49189a532e ```
update Add development environment configuration, I18n support, route adjustments, and various hooks refactoring
```
2025-05-16 17:47:05 +08:00

39 lines
1002 B
TypeScript

import { useState } from 'react';
export const useFormInput = (initialValue = '') => {
const [value, setValue] = useState(initialValue);
const handleChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
const tempValue = await e.currentTarget.value;
setValue(tempValue);
};
return {
value,
reset: (newValue: string) => setValue(newValue),
onIonChange: handleChange,
onKeyUp: handleChange,
};
};
export const validateForm = (fields: { required: boolean; id: string; input: { state: { value: string } } }[]) => {
let errors: { id: string; message: string }[] = [];
fields.forEach((field: { required: boolean; id: string; input: { state: { value: string } } }) => {
if (field.required) {
const fieldValue = field.input.state.value;
if (fieldValue === '') {
const error = {
id: field.id,
message: `Please check your ${field.id}`,
};
errors.push(error);
}
}
});
return errors;
};