Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 2f8debe

Browse files
authored
Merge pull request #225 from asigloo/docs/add-visibility-demo
Docs/add visibility demo
2 parents 1109611 + 1ed02f6 commit 2f8debe

File tree

7 files changed

+158
-15
lines changed

7 files changed

+158
-15
lines changed

demos/vue-3/src/router/index.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,21 @@ const routes: Array<RouteRecordRaw> = [
9292
component: () =>
9393
import(/* webpackChunkName: "campaigns" */ '../views/Login.vue'),
9494
},
95+
{
96+
path: '/toggle-visibility',
97+
name: 'Toggle Visibility',
98+
meta: {
99+
title: 'Toggle Visibility',
100+
},
101+
component: () =>
102+
import(
103+
/* webpackChunkName: "toggle-visibility" */ '../views/ToggleVisibility.vue'
104+
),
105+
},
95106
];
96107

97108
const router = createRouter({
98-
history: createWebHistory(process.env.BASE_URL),
109+
history: createWebHistory(),
99110
routes,
100111
});
101112

demos/vue-3/src/views/Home.vue

+5
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ export default defineComponent({
9999
route: '/custom-fields',
100100
tags: ['custom-field'],
101101
},
102+
{
103+
name: 'Toggle Visibility',
104+
route: '/toggle-visibility',
105+
tags: ['visibility', 'custom-class', 'custom-styles'],
106+
},
102107
],
103108
];
104109
}, 1000);
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<template>
2+
<div class="page container">
3+
<div class="mx-auto w-full sm:w-1/2 relative mb-24">
4+
<div
5+
class="absolute inset-0 bg-gradient-to-r from-blue-400 to-green-200 shadow-xl transform -skew-y-3 sm:skew-y-0 sm:-rotate-3 sm:rounded-3xl"
6+
></div>
7+
<div class="relative card p-6 bg-white">
8+
<dynamic-form
9+
:form="form"
10+
@submitted="handleSubmit"
11+
@change="valueChanged"
12+
@error="handleError"
13+
/>
14+
<button
15+
data-cy="submit"
16+
class="btn bg-green-500 text-white hover:bg-green-700 mt-4"
17+
submit="true"
18+
:form="form?.id"
19+
>
20+
Try me
21+
</button>
22+
</div>
23+
</div>
24+
<div class="mx-auto w-full sm:w-1/2"><Console :content="formValues" /></div>
25+
</div>
26+
</template>
27+
28+
<script lang="ts">
29+
import { CheckboxField, TextField } from '@/';
30+
import { computed, defineComponent, reactive } from 'vue';
31+
import Console from '../components/Console.vue';
32+
33+
const components = {
34+
Console,
35+
};
36+
/* } from '../../dist/as-dynamic-forms.esm'; */
37+
export default defineComponent({
38+
name: 'BasicDemo',
39+
components,
40+
setup() {
41+
const formValues = reactive({
42+
hideUsingCustomClass: false,
43+
hideUsingCustomStyles: false,
44+
});
45+
46+
const form = computed(() => ({
47+
id: 'basic-demo',
48+
fields: {
49+
hideUsingCustomClass: CheckboxField({
50+
label: 'Toggle visibility next field with CustomClass',
51+
}),
52+
hideMe: TextField({
53+
label: 'Using CustomClass',
54+
customClass: formValues.hideUsingCustomClass
55+
? 'invisible'
56+
: 'visible',
57+
}),
58+
hideUsingCustomStyles: CheckboxField({
59+
label: 'Toggle visibility next field with CustomStyles',
60+
}),
61+
hideMe2: TextField({
62+
label: 'Using CustomStyles',
63+
customStyles: {
64+
visibility: formValues.hideUsingCustomStyles ? 'hidden' : 'visible',
65+
},
66+
}),
67+
},
68+
}));
69+
70+
function handleSubmit(values) {
71+
console.log('Values Submitted', values);
72+
}
73+
74+
function valueChanged(values) {
75+
Object.assign(formValues, values);
76+
console.log('Values', values);
77+
}
78+
79+
function handleError(errors) {
80+
console.error('Errors', errors);
81+
}
82+
83+
return {
84+
form,
85+
formValues,
86+
handleSubmit,
87+
valueChanged,
88+
handleError,
89+
};
90+
},
91+
});
92+
</script>
93+
<style lang="scss"></style>

src/components/dynamic-form/DynamicForm.vue

+11-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
</template>
3737

3838
<script lang="ts">
39-
import { defineComponent, inject, PropType } from 'vue';
39+
import { defineComponent, inject, PropType, watch } from 'vue';
4040
4141
import DynamicInput from '../dynamic-input/DynamicInput.vue';
4242
@@ -76,8 +76,18 @@ export default defineComponent({
7676
onBlur,
7777
onValidate,
7878
forceValidation,
79+
detectChanges,
7980
} = useDynamicForm(props.form as DynamicForm, ctx, options);
8081
82+
watch(
83+
() => props.form.fields,
84+
fields => {
85+
detectChanges(fields);
86+
},
87+
{
88+
deep: true,
89+
},
90+
);
8191
return {
8292
controls,
8393
valueChange,

src/components/dynamic-input/DynamicInput.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default defineComponent({
7171
const attributes = computed(() => {
7272
return {
7373
control: props?.control,
74-
style: props?.control.customStyles,
74+
7575
onChange: valueChange,
7676
onBlur: (e: InputEvent) => emit('blur', e),
7777
onFocus: (e: InputEvent) => emit('focus', e),
@@ -203,6 +203,7 @@ export default defineComponent({
203203
isFieldSet.value ? 'fieldset' : 'div',
204204
{
205205
class: getClasses.value,
206+
style: props?.control.customStyles,
206207
role: isFieldSet.value ? undefined : 'group',
207208
},
208209
[

src/composables/useDynamicForm.ts

+4-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ValidationEvent,
88
InputEvent,
99
DynamicForm,
10+
FormFields,
1011
} from '@/core/models';
1112
import {
1213
computed,
@@ -16,7 +17,6 @@ import {
1617
ref,
1718
Ref,
1819
toRaw,
19-
watch,
2020
} from 'vue';
2121
import { deepClone, hasValue, removeEmpty } from '@/core/utils/helpers';
2222
import { FieldControl } from '@/core/factories';
@@ -41,6 +41,7 @@ interface DynamicFormComposition {
4141
mapControls: (empty?: boolean) => void;
4242
findControlByName: (name: string | unknown) => FormControl<InputType>;
4343
resetForm: () => void;
44+
detectChanges: (fields: FormFields) => void;
4445
}
4546

4647
export function useDynamicForm(
@@ -216,7 +217,7 @@ export function useDynamicForm(
216217
});
217218
}
218219
});
219-
cache = deepClone(toRaw(form.fields));
220+
cache = deepClone(toRaw(fields));
220221
}
221222

222223
function resetForm() {
@@ -241,16 +242,6 @@ export function useDynamicForm(
241242
forceValidation.value = true;
242243
}
243244

244-
watch(
245-
() => form.fields,
246-
fields => {
247-
detectChanges(fields);
248-
},
249-
{
250-
deep: true,
251-
},
252-
);
253-
254245
onMounted(() => {
255246
mapControls();
256247
});
@@ -272,5 +263,6 @@ export function useDynamicForm(
272263
validateAll,
273264
findControlByName,
274265
resetForm,
266+
detectChanges,
275267
};
276268
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
describe('Toggle Visibility', () => {
2+
beforeEach(() => {
3+
cy.visit('/toggle-visibility'); // "baseUrl" is defined in cypress.json file
4+
});
5+
6+
context('Basic Events', () => {
7+
it('first input should be invisible when toggle visibility is false using CustomClass property', () => {
8+
cy.get('input[name="hideUsingCustomClass"]').check();
9+
cy.get('input[name="hideMe"]').should('not.be.visible');
10+
});
11+
12+
it('first input should be visible again when toggle visibility is true using CustomClass property', () => {
13+
cy.get('input[name="hideUsingCustomClass"]').check();
14+
cy.get('input[name="hideMe"]').should('not.be.visible');
15+
cy.get('input[name="hideUsingCustomClass"]').uncheck();
16+
cy.get('input[name="hideMe"]').should('be.visible');
17+
});
18+
19+
it('second input should be invisible when toggle visibility is false using CustomStyle property', () => {
20+
cy.get('input[name="hideUsingCustomStyles"]').check();
21+
cy.get('input[name="hideMe2"]').should('not.be.visible');
22+
});
23+
24+
it('second input should be visible again when toggle visibility is true using CustomClass property', () => {
25+
cy.get('input[name="hideUsingCustomStyles"]').check();
26+
cy.get('input[name="hideMe2"]').should('not.be.visible');
27+
cy.get('input[name="hideUsingCustomStyles"]').uncheck();
28+
cy.get('input[name="hideMe2"]').should('be.visible');
29+
});
30+
});
31+
});

0 commit comments

Comments
 (0)