mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 07:49:52 +03:00
This commit is contained in:
@@ -25,6 +25,7 @@ export const extractEventValue = (
|
|||||||
normalizedValue.endsWith('.') ||
|
normalizedValue.endsWith('.') ||
|
||||||
normalizedValue === '-' ||
|
normalizedValue === '-' ||
|
||||||
normalizedValue === '-.' ||
|
normalizedValue === '-.' ||
|
||||||
|
normalizedValue === '-0' ||
|
||||||
endsWithDecimalAndZeros;
|
endsWithDecimalAndZeros;
|
||||||
|
|
||||||
if (isIncomplete) {
|
if (isIncomplete) {
|
||||||
@@ -45,15 +46,15 @@ type UpdateEntity<S> = (state: (prevState: Readonly<S>) => S) => void;
|
|||||||
*/
|
*/
|
||||||
export const updateValue =
|
export const updateValue =
|
||||||
<S extends Record<string, unknown>>(updateEntity: UpdateEntity<S>) =>
|
<S extends Record<string, unknown>>(updateEntity: UpdateEntity<S>) =>
|
||||||
(event: React.ChangeEvent<HTMLInputElement>): void => {
|
(event: React.ChangeEvent<HTMLInputElement>): void => {
|
||||||
const { name } = event.target;
|
const { name } = event.target;
|
||||||
const value = extractEventValue(event);
|
const value = extractEventValue(event);
|
||||||
|
|
||||||
updateEntity((prevState) => ({
|
updateEntity((prevState) => ({
|
||||||
...prevState,
|
...prevState,
|
||||||
[name]: value
|
[name]: value
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an event handler that tracks dirty flags for modified fields.
|
* Creates an event handler that tracks dirty flags for modified fields.
|
||||||
@@ -66,22 +67,22 @@ export const updateValueDirty =
|
|||||||
setDirtyFlags: React.Dispatch<React.SetStateAction<string[]>>,
|
setDirtyFlags: React.Dispatch<React.SetStateAction<string[]>>,
|
||||||
updateDataValue: (updater: (prevState: T) => T) => void
|
updateDataValue: (updater: (prevState: T) => T) => void
|
||||||
) =>
|
) =>
|
||||||
(event: React.ChangeEvent<HTMLInputElement>): void => {
|
(event: React.ChangeEvent<HTMLInputElement>): void => {
|
||||||
const { name } = event.target;
|
const { name } = event.target;
|
||||||
const updatedValue = extractEventValue(event);
|
const updatedValue = extractEventValue(event);
|
||||||
|
|
||||||
updateDataValue((prevState) => ({
|
updateDataValue((prevState) => ({
|
||||||
...prevState,
|
...prevState,
|
||||||
[name]: updatedValue
|
[name]: updatedValue
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const isDirty = origData[name] !== updatedValue;
|
const isDirty = origData[name] !== updatedValue;
|
||||||
const wasDirty = dirtyFlags.includes(name);
|
const wasDirty = dirtyFlags.includes(name);
|
||||||
|
|
||||||
// Only update dirty flags if the state changed
|
// Only update dirty flags if the state changed
|
||||||
if (isDirty !== wasDirty) {
|
if (isDirty !== wasDirty) {
|
||||||
setDirtyFlags(
|
setDirtyFlags(
|
||||||
isDirty ? [...dirtyFlags, name] : dirtyFlags.filter((f) => f !== name)
|
isDirty ? [...dirtyFlags, name] : dirtyFlags.filter((f) => f !== name)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user