Skip to content

Commit

Permalink
Merge pull request #7 from unimal-jp/add-input-type
Browse files Browse the repository at this point in the history
Add input type
  • Loading branch information
akoarum committed Aug 9, 2022
2 parents c7fc928 + 0597f5b commit e7ada15
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
14 changes: 7 additions & 7 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
@@ -1,6 +1,6 @@
{
"name": "@spearly/vue",
"version": "0.2.0",
"version": "0.3.0",
"description": "This package allows you to easily implement Spearly in your Vue project!",
"main": "dist/cjs/spearly-vue.js",
"module": "dist/esm/spearly-vue.js",
Expand Down Expand Up @@ -67,7 +67,7 @@
"vue-template-compiler": "^2.6.14"
},
"dependencies": {
"@spearly/sdk-js": "^1.0.0-next.3",
"@spearly/sdk-js": "^1.0.0-next.8",
"@vue/compiler-sfc": "^3.2.33",
"typescript": "^4.6.4"
},
Expand Down
40 changes: 39 additions & 1 deletion src/components/spearly-form/index.vue
Expand Up @@ -79,6 +79,19 @@
</label>
</template>

<template v-else-if="field.inputType === 'file'">
<label class="spearly-form-file">
<input
:id="field.identifier"
:required="field.required"
:accept="field.data?.fileExtensions?.map((extension) => `.${extension}`).join(',')"
type="file"
class="spearly-form-file-input"
@change="onChangeFile($event, field.identifier)"
/>
</label>
</template>
<p
v-if="state.errors.get(field.identifier)"
:id="`spearly-form-field-${field.identifier}-error`"
Expand Down Expand Up @@ -153,6 +166,7 @@ export type Props = {
export type State = {
form: { createdAt: Date | null } & Omit<SpearlyForm, 'createdAt'>
answers: { [key: string]: string | string[]; _spearly_gotcha: string }
files: { [key: string]: string }
errors: Map<string, string>
error: boolean
confirm: boolean
Expand All @@ -177,6 +191,7 @@ const state = reactive<State>({
createdAt: null,
},
answers: { _spearly_gotcha: '' },
files: {},
error: false,
errors: new Map(),
confirm: false,
Expand All @@ -194,7 +209,11 @@ const setFormData = async () => {
const setAnswersObj = () => {
state.form.fields.forEach((field) => {
state.answers[field.identifier] = field.inputType === 'checkbox' && field.data?.options.length ? [] : ''
state.answers[field.identifier] = field.inputType === 'checkbox' && field.data?.options?.length ? [] : ''
if (field.inputType === 'file') {
state.files[field.identifier] = ''
}
})
}
Expand Down Expand Up @@ -295,6 +314,25 @@ const onClick = () => {
submit(state.answers)
}
const onChangeFile = (event: Event, identifier: string) => {
const files = (event.target as HTMLInputElement).files
if (!files) return
if (!files.length) {
state.answers[identifier] = ''
state.files[identifier] = ''
}
const fileReader = new FileReader()
fileReader.onload = () => {
state.answers[identifier] = fileReader.result as string
state.files[identifier] = files[0].name
}
fileReader.readAsDataURL(files[0])
}
const submit = async (fields: { [key: string]: unknown } & { _spearly_gotcha: string }) => {
if (!$spearly) throw new Error('$spearly is not inject.')
try {
Expand Down

0 comments on commit e7ada15

Please sign in to comment.