Skip to content

Commit

Permalink
fix: add copy of object in use-input-state.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenesius committed May 13, 2023
1 parent 278e672 commit 07d0226
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 17 deletions.
7 changes: 5 additions & 2 deletions plugin/classes/FormProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import getPropFromObject from "../utils/get-prop-from-object";
* For example, it is used for the address composite element, which itself consists of child elements.
* */
export default class FormProxy extends Form{
/**
* @override
* */
name: string;

constructor(p:any) {
super(p);

this.name = p.name;
}

Expand Down
2 changes: 1 addition & 1 deletion plugin/hooks/use-form-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {reactive} from "vue";
* @description Return dynamic form values.
* */
export default function useFormValues(form: Form) {
const values = reactive(form.values);
const values = reactive(form.values || {});
form.oninput(data => {
const newValues = {
[data.name]: data.newValue
Expand Down
8 changes: 5 additions & 3 deletions plugin/hooks/use-input-state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {onUnmounted, reactive} from "vue";
import Input from "../classes/Input";
import debug from "../debug/debug";
import copyObject from "../utils/copy-object";

export default function useInputState(name: string, validation: any[] = []) {

Expand All @@ -15,7 +16,6 @@ export default function useInputState(name: string, validation: any[] = []) {
}

function useInputController(input: Input) {

const state = reactive<{
value: any,
disabled: boolean,
Expand All @@ -39,11 +39,13 @@ function useInputController(input: Input) {

const controls = {
change: (v:any) => {
state.value = v;
debug.msg(`New FormField(${input.name}) Value`, v);
state.value = (Object.isFrozen(v)) ? v :copyObject(v);
updateChanged();
},
setValues(v: any) {
state.value = v;
debug.msg(`New FormField(${input.name}) Value`, v);
state.value = (Object.isFrozen(v)) ? v :copyObject(v);
updateChanged();
},
disable: () => {
Expand Down
4 changes: 3 additions & 1 deletion plugin/hooks/use-proxy-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ export default function useProxyState(name: string) {
form.parentForm?.unsubscribe(form);
})

return {}
return {
form
}
}
16 changes: 7 additions & 9 deletions src/pages/test/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

<input-field type = "text" name = "sun" label = "Username" prefix = "MMM" :modify = "splitPoint"/>

<widget-child/>
<input-field type = "coord" name = "coordinate"/>

<input-coord name="coordinate" />

<input-field name = "file" type = "file"/>

</div>
</template>

Expand All @@ -13,19 +18,12 @@ import InputField from "../../../plugin/widgets/input-field.vue";
import {config, Form, useInputState} from "../../../plugin";
import {onMounted, ref} from "vue";
import WidgetChild from "@/pages/test/widget-child.vue";
import InputCoord from "@/pages/test/input-coord.vue";
const form = new Form();
config({
debug: false
})
onMounted(() => setTimeout(() => form.validate(), 1000))
function m1(a: string) {
return a.replace(/\D/,'')
}
function test() {
throw new Error('test')
Expand Down
23 changes: 23 additions & 0 deletions src/pages/test/input-coord.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<template>
<div>
<input-field name = "X" label = "X"/>
<input-field name = "Y" label = "Y"/>

{{values}}
</div>
</template>

<script setup lang = "ts">
import {InputField, useFormValues, useProxyState} from "../../../plugin";
const props = defineProps<{
name: string,
}>()
const {form} = useProxyState(props.name)
const values = useFormValues(form)
</script>

<style scoped>
</style>
79 changes: 79 additions & 0 deletions src/pages/test/input-file.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<template>
<div class="container-input-file">
<p>UPLOADING FILE</p>
<input class="widget-input-file" type="file" @change="onInput"/>
{{modelValue}}
</div>
</template>

<script setup lang="ts">
interface Props {
modelValue: any
}
const props = defineProps<Props>()
const emit = defineEmits<{
(event: 'update:modelValue', file: any):void
}>();
function onInput(event: any) {
const file = event.target.files[0];
console.log(file)
emit('update:modelValue', Object.freeze(file))
}
</script>

<style scoped>
.container-input-file {
display: flex;
background-color: red;
}
.button-upload-file {
display: grid;
place-content: center;
border: 2px dashed gray;
height: 35px;
width: 100px;
position: relative;
cursor: pointer;
border-radius: 4px;
}
.widget-input-file {
opacity: 0;
position: absolute;
width: 100%;
height: 100%;
z-index: 1;
cursor: pointer;
}
.widget-input-file::-webkit-file-upload-button {
display: none;
color: gray;
}
.button-upload-file__label {
font-size: 14px;
}
.button-upload-file_wait {
cursor: default;
border-color: blue;
}
.button-upload-file_error {
border-color: red;
}
.button-upload-file_wait:after {
content: "";
position: absolute;
z-index: 1;
height: 100%;
width: 100%;
top: 0;
left: 0;
}
</style>
6 changes: 5 additions & 1 deletion src/pages/test/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import {config} from "../../../plugin";

import country from "./widget-input-country.vue";
import accountType from "@/pages/test/widget-input-account-type.vue";
import InputCoord from "@/pages/test/input-coord.vue";
import InputFile from "@/pages/test/input-file.vue";

config({
inputTypes: {
country,
"account-type": accountType
"account-type": accountType,
coord: InputCoord,
file: InputFile
},
debug: true
})
Expand Down

0 comments on commit 07d0226

Please sign in to comment.