Skip to content

Commit

Permalink
Merge pull request #29 from klm-lab/dev
Browse files Browse the repository at this point in the history
Add new form feature
  • Loading branch information
klm-lab authored Nov 25, 2023
2 parents 139bd0b + b44c713 commit 18e615f
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 158 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ An input state management for React. It comes with useful common validations if
[MIT][license-url]


[size-shield]: https://img.shields.io/bundlephobia/minzip/aio-inputs/2.1.12?style=for-the-badge
[size-shield]: https://img.shields.io/bundlephobia/minzip/aio-inputs/2.1.13?style=for-the-badge

[license-shield]: https://img.shields.io/github/license/klm-lab/inputs?style=for-the-badge

Expand Down
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Tracking inputs [HERE][no-tracking-link]
[MIT][license-url]


[size-shield]: https://img.shields.io/bundlephobia/minzip/aio-inputs/2.1.12?style=for-the-badge
[size-shield]: https://img.shields.io/bundlephobia/minzip/aio-inputs/2.1.13?style=for-the-badge

[license-shield]: https://img.shields.io/github/license/klm-lab/inputs?style=for-the-badge

Expand Down
15 changes: 9 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aio-inputs",
"version": "2.1.12",
"version": "2.1.13",
"description": "An opinionated inputs state manager for react",
"scripts": {
"build": "npm ci && npm run build:noci",
Expand Down Expand Up @@ -79,7 +79,7 @@
"state"
],
"dependencies": {
"aio-store": "^2.4.3"
"aio-store": "^2.4.41"
},
"peerDependencies": {
"react": "^18.2.0"
Expand Down
24 changes: 14 additions & 10 deletions src/inputs/handlers/changes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type {
Config,
Helper,
InputStore,
ParsedFile,
Input,
ObjectInput
InputConfig,
InputStore,
ObjectInputs,
ParsedFile
} from "../../types";
import { AsyncValidationParams } from "../../types";
import { asyncValidation, validate } from "../../util/validation";
Expand Down Expand Up @@ -48,7 +48,7 @@ const onChange = (
input: Input,
element: HTMLInputElement | HTMLSelectElement,
store: InputStore,
config: Config,
config: InputConfig,
isEvent: boolean,
helper: Helper
) => {
Expand All @@ -66,8 +66,12 @@ const onChange = (
config,
helper
)
: input.type === "select" && input.multiple
? createSelectFiles(isEvent, element as HTMLSelectElement, clone, ID)
: input.type === "select"
? input.multiple
? createSelectFiles(isEvent, element as HTMLSelectElement, clone, ID)
: element.value !== "" && element.value !== clone[ID].placeholder
? element.value
: ""
: element.value;

const toValidate =
Expand Down Expand Up @@ -120,7 +124,7 @@ const onChange = (
syncChanges(store, clone);
};

const syncChanges = (store: InputStore, data: ObjectInput) => {
const syncChanges = (store: InputStore, data: ObjectInputs<string>) => {
store.set((ref) => {
ref.entry = data;
ref.isValid = validateState(data).isValid;
Expand All @@ -130,9 +134,9 @@ const syncChanges = (store: InputStore, data: ObjectInput) => {
export const inputChange = (
value: any,
key: string,
entry: ObjectInput,
entry: ObjectInputs<string>,
store: InputStore,
config: Config,
config: InputConfig,
helper: Helper
) => {
const isEvent =
Expand Down
4 changes: 2 additions & 2 deletions src/inputs/handlers/checkbox.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ObjectInput } from "../../types";
import type { ObjectInputs } from "../../types";

export const createCheckboxValue = (
clone: ObjectInput,
clone: ObjectInputs<string>,
ID: string,
userChange = true
) => {
Expand Down
50 changes: 18 additions & 32 deletions src/inputs/handlers/files.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import type {
Config,
InputConfig,
Helper,
InitFileConfig,
InputStore,
ParsedFile,
ObjectInput
ObjectInputs
} from "../../types";
import { validate } from "../../util/validation";
import { validateState } from "../../util";

export const createFiles = (
files: FileList | null,
clone: ObjectInput,
clone: ObjectInputs<string>,
ID: string,
store: InputStore,
config: Config,
config: InputConfig,
helper: Helper
) => {
const entry = clone[ID];
Expand Down Expand Up @@ -44,10 +44,10 @@ export const createFiles = (
};

export const parseFile = (
clone: ObjectInput,
clone: ObjectInputs<string>,
ID: string,
store: InputStore,
config: Config,
config: InputConfig,
url: string,
gettingFile: boolean,
file: File,
Expand Down Expand Up @@ -87,26 +87,19 @@ export const parseFile = (
};
};

// export const getFile = async (url: string, fileConfig: InitFileConfig) => {
// const URL = fileConfig.useDefaultProxyUrl
// ? "cors-anywhere.herokuapp.com/" + url
// : fileConfig.proxyUrl
// ? fileConfig.proxyUrl + "/" + url
// : url;
// const blob = await fetch(URL).then((r) => r.blob());
// const fileName = url.match(/([a-z0-9_-]+\.\w+)(?!.*\/)/gi);
// const name = fileName ? fileName[0] : "";
// return new File([blob], name, {
// type: blob.type
// });
// };
const getFile = (url: string, blob: Blob) => {
const fileName = url.match(/([a-z0-9_-]+\.\w+)(?!.*\/)/gi);
return new File([blob], fileName ? fileName[0] : "", {
type: blob.type
});
};

export const blobStringJob = (
value: any,
store: InputStore,
clone: ObjectInput,
clone: ObjectInputs<string>,
ID: string,
config: Config,
config: InputConfig,
fileConfig: InitFileConfig,
index: number,
valid: boolean,
Expand All @@ -126,28 +119,21 @@ export const blobStringJob = (
ref.entry[ID].valid = valid;
});
if (fileConfig.getBlob) {
Promise.resolve(fileConfig.getBlob(value)).then((file) => {
Promise.resolve(fileConfig.getBlob(value)).then((blob) => {
store.set((ref) => {
ref.entry[ID].files[index].gettingFile = false;
ref.entry[ID].files[index].file = file;
ref.entry[ID].files[index].file = getFile(value, blob);
});
});
}

// getFile(value, fileConfig).then((file) => {
// store.set((ref) => {
// ref.entry[ID].files[index].gettingFile = false;
// ref.entry[ID].files[index].file = file;
// });
// });
};

export const retrieveBlob = (
value: any,
store: InputStore,
clone: ObjectInput,
clone: ObjectInputs<string>,
ID: string,
config: Config,
config: InputConfig,
fileConfig: InitFileConfig,
valid: boolean,
helper: Helper
Expand Down
4 changes: 2 additions & 2 deletions src/inputs/handlers/radio.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ObjectInput } from "../../types";
import type { ObjectInputs } from "../../types";

export const radioIsChecked = (clone: ObjectInput, ID: string) => {
export const radioIsChecked = (clone: ObjectInputs<string>, ID: string) => {
let isSelected = false;
for (const key in clone) {
if (
Expand Down
12 changes: 8 additions & 4 deletions src/inputs/handlers/select.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import type { ObjectInput } from "../../types";
import type { ObjectInputs } from "../../types";

export const createSelectFiles = (
isEvent: boolean,
element: HTMLSelectElement,
clone: ObjectInput,
clone: ObjectInputs<string>,
ID: string
) => {
let selected = [] as string[];
if (isEvent) {
const els = element.selectedOptions;
for (let i = 0; i < els.length; i++) {
selected.push(els[i].value);
els[i].value !== "" &&
els[i].value !== clone[ID].placeholder &&
selected.push(els[i].value);
}
} else {
// Not need to clone, we keep the same reference safely
selected = clone[ID].value;
if (selected.includes(element.value)) {
selected = selected.filter((v: any) => v !== element.value);
} else {
selected.push(element.value);
element.value !== "" &&
element.value !== clone[ID].placeholder &&
selected.push(element.value);
}
}
return selected;
Expand Down
Loading

0 comments on commit 18e615f

Please sign in to comment.