Skip to content

Commit

Permalink
Merge pull request #43 from klm-lab/dev
Browse files Browse the repository at this point in the history
feat: Add getErroneousInput
  • Loading branch information
klm-lab authored May 19, 2024
2 parents b51ca40 + 927f7a0 commit 153b843
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/inputs/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const createForm = (initialState: Unknown, config: InputConfig) => {
each,
onSubmit,
showError: () => touchInput(st),
getErroneousInput: () => st.get(`i.${st.get("e")}`),
get: (name: string) => {
const r: Input[] = [];
st.ev[name].o.forEach((k) => {
Expand Down
4 changes: 3 additions & 1 deletion src/inputs/handlers/changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ export const syncChanges = (store: InputStore, data: ObjectInputs<string>) => {
// isTouched
ref.t = true;
// new valid state
ref.iv = validateState(data);
const valid = validateState(data);
ref.iv = valid.v;
ref.e = valid.e;
});
};
4 changes: 3 additions & 1 deletion src/inputs/handlers/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ export const parseFile = (
input.valid = !em;
input.errorMessage = em;
// Validate form
ref.iv = validateState(ref.i);
const valid = validateState(ref.i);
ref.iv = valid.v;
ref.e = valid.e;
});
}
} as ParsedFile;
Expand Down
12 changes: 8 additions & 4 deletions src/inputs/validations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,18 @@ const validate = (

// Validate the state
const validateState = (data: ObjectInputs<string>) => {
let valid = true;
// valid status
let v = true;
// erroneous key
let e = "";
for (const formKey in data) {
if (!valid) {
v = data[formKey].valid;
if (!v) {
e = data[formKey].touched ? formKey : "";
break;
}
valid = data[formKey].valid;
}
return valid;
return { v, e };
};

export { validate, validateState };
4 changes: 4 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ interface Form {

showError(): void;

getErroneousInput(): Input | undefined;

get(name: string): Input[];

getValues(): Unknown;
Expand All @@ -187,6 +189,8 @@ type IPS = {
inv: boolean;
// asyncDelay: number;
c: InputConfig;
// Erroneous key
e: string;
};
type InputStore = StoreType<IPS> & {
fc: GetFile;
Expand Down
7 changes: 4 additions & 3 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ const touchInput = (store: InputStore) => {
const data = store.get("i");
for (const key in data) {
if (!data[key].valid) {
store.set((ref) =>
store.set((ref) => {
setTouchedEm(
ref.i,
key,
Expand All @@ -221,8 +221,9 @@ const touchInput = (store: InputStore) => {
// (data[key] as Input & GetValue).g(data[key].value, true)
(data[key] as Input & GetValue).g("", true)
)
)
);
);
ref.e = key;
});
break;
}
}
Expand Down

0 comments on commit 153b843

Please sign in to comment.