@farm-investimentos/front-mfe-components 9.3.2 → 9.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/front-mfe-components.common.js +249 -126
- package/dist/front-mfe-components.common.js.map +1 -1
- package/dist/front-mfe-components.css +2 -2
- package/dist/front-mfe-components.umd.js +249 -126
- package/dist/front-mfe-components.umd.js.map +1 -1
- package/dist/front-mfe-components.umd.min.js +1 -1
- package/dist/front-mfe-components.umd.min.js.map +1 -1
- package/package.json +1 -1
- package/src/components/Card/Card.stories.js +2 -1
- package/src/components/Checkbox/Checkbox.stories.js +2 -1
- package/src/components/Checkbox/Checkbox.vue +57 -4
- package/src/components/Checkbox/__tests__/Checkbox.spec.js +5 -1
- package/src/components/Chip/Chip.stories.js +11 -0
- package/src/components/Form/Form.stories.js +25 -13
- package/src/components/Form/Form.vue +2 -0
- package/src/components/Icon/Icon.stories.js +3 -1
- package/src/components/Loader/Loader.stories.ts +2 -1
- package/src/components/Logger/Logger.stories.js +2 -1
- package/src/components/Modal/Modal.stories.js +2 -1
- package/src/components/layout/Row/Row.stories.js +1 -0
- package/src/composition/__tests__/deepEqual.spec.js +13 -0
- package/src/composition/__tests__/validateFormFieldBuilder.spec.js +8 -0
- package/src/composition/__tests__/validateFormStateBuilder.spec.js +8 -0
- package/src/composition/deepEqual.ts +24 -0
- package/src/composition/validateFormFieldBuilder.ts +19 -0
- package/src/composition/validateFormMethodBuilder.ts +9 -0
- package/src/composition/validateFormStateBuilder.ts +13 -0
package/package.json
CHANGED
|
@@ -18,7 +18,11 @@
|
|
|
18
18
|
</div>
|
|
19
19
|
</template>
|
|
20
20
|
<script lang="ts">
|
|
21
|
-
import Vue, { ref, toRefs, watch } from 'vue';
|
|
21
|
+
import Vue, { computed, onBeforeMount, PropType, ref, toRefs, watch } from 'vue';
|
|
22
|
+
import validateFormStateBuilder from '../../composition/validateFormStateBuilder';
|
|
23
|
+
import validateFormFieldBuilder from '../../composition/validateFormFieldBuilder';
|
|
24
|
+
import validateFormMethodBuilder from '../../composition/validateFormMethodBuilder';
|
|
25
|
+
import deepEqual from '../../composition/deepEqual';
|
|
22
26
|
|
|
23
27
|
export default Vue.extend({
|
|
24
28
|
name: 'farm-checkbox',
|
|
@@ -32,21 +36,34 @@ export default Vue.extend({
|
|
|
32
36
|
*/
|
|
33
37
|
label: { type: String, default: null },
|
|
34
38
|
/**
|
|
35
|
-
* disabled
|
|
39
|
+
* Is disabled?
|
|
36
40
|
*/
|
|
37
41
|
disabled: { type: Boolean, default: false },
|
|
42
|
+
/**
|
|
43
|
+
* Variation
|
|
44
|
+
*/
|
|
38
45
|
variation: {
|
|
39
|
-
type: String
|
|
46
|
+
type: String as PropType<'' | 'lighten' | 'darken'>,
|
|
40
47
|
default: '',
|
|
41
48
|
},
|
|
42
49
|
color: {
|
|
43
50
|
type: String,
|
|
44
51
|
default: 'primary',
|
|
45
52
|
},
|
|
53
|
+
/**
|
|
54
|
+
* Array of rules used for validation
|
|
55
|
+
*/
|
|
56
|
+
rules: {
|
|
57
|
+
type: Array as PropType<Array<Function>>,
|
|
58
|
+
default: () => [],
|
|
59
|
+
},
|
|
46
60
|
},
|
|
47
61
|
setup(props, { emit }) {
|
|
48
62
|
const innerValue = ref(props.value);
|
|
49
|
-
const { label, disabled } = toRefs(props);
|
|
63
|
+
const { label, disabled, rules } = toRefs(props);
|
|
64
|
+
const { errorBucket, valid, validatable } = validateFormStateBuilder();
|
|
65
|
+
|
|
66
|
+
let fieldValidator = validateFormFieldBuilder(rules.value);
|
|
50
67
|
|
|
51
68
|
const toggleValue = () => {
|
|
52
69
|
if (disabled.value) {
|
|
@@ -54,20 +71,56 @@ export default Vue.extend({
|
|
|
54
71
|
}
|
|
55
72
|
innerValue.value = !innerValue.value;
|
|
56
73
|
emit('input', innerValue.value);
|
|
74
|
+
validate(innerValue.value);
|
|
57
75
|
};
|
|
58
76
|
|
|
77
|
+
const hasError = computed(() => {
|
|
78
|
+
return errorBucket.value.length > 0;
|
|
79
|
+
});
|
|
80
|
+
|
|
59
81
|
watch(
|
|
60
82
|
() => props.value,
|
|
61
83
|
() => {
|
|
62
84
|
innerValue.value = props.value;
|
|
85
|
+
validate(innerValue.value);
|
|
86
|
+
}
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
watch(
|
|
90
|
+
() => props.rules,
|
|
91
|
+
(newVal, oldVal) => {
|
|
92
|
+
if (deepEqual(newVal, oldVal)) return;
|
|
93
|
+
fieldValidator = validateFormFieldBuilder(rules.value);
|
|
94
|
+
validate = validateFormMethodBuilder(errorBucket, valid, fieldValidator);
|
|
95
|
+
validate(innerValue.value);
|
|
63
96
|
}
|
|
64
97
|
);
|
|
65
98
|
|
|
99
|
+
const reset = () => {
|
|
100
|
+
if (disabled.value) {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
innerValue.value = false;
|
|
104
|
+
emit('input', innerValue.value);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
onBeforeMount(() => {
|
|
108
|
+
validate(innerValue.value);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
let validate = validateFormMethodBuilder(errorBucket, valid, fieldValidator);
|
|
112
|
+
|
|
66
113
|
return {
|
|
67
114
|
innerValue,
|
|
68
115
|
label,
|
|
69
116
|
disabled,
|
|
117
|
+
errorBucket,
|
|
118
|
+
valid,
|
|
119
|
+
validatable,
|
|
120
|
+
hasError,
|
|
70
121
|
toggleValue,
|
|
122
|
+
reset,
|
|
123
|
+
validate,
|
|
71
124
|
};
|
|
72
125
|
},
|
|
73
126
|
});
|
|
@@ -8,6 +8,17 @@ const variations = ['', 'darken', 'lighten'];
|
|
|
8
8
|
export default {
|
|
9
9
|
title: 'Display/Chips',
|
|
10
10
|
component: Chip,
|
|
11
|
+
parameters: {
|
|
12
|
+
docs: {
|
|
13
|
+
description: {
|
|
14
|
+
component: `Chip<br />
|
|
15
|
+
selector: <em>farm-chip</em><br />
|
|
16
|
+
<span style="color: green;">ready for use</span>
|
|
17
|
+
`,
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
viewMode: 'docs',
|
|
21
|
+
},
|
|
11
22
|
};
|
|
12
23
|
|
|
13
24
|
export const Primary = () => ({
|
|
@@ -7,7 +7,8 @@ export default {
|
|
|
7
7
|
docs: {
|
|
8
8
|
description: {
|
|
9
9
|
component: `Form<br />
|
|
10
|
-
selector: <em>farm-form</em
|
|
10
|
+
selector: <em>farm-form</em><br />
|
|
11
|
+
<span style="color: green;">ready for use</span>
|
|
11
12
|
`,
|
|
12
13
|
},
|
|
13
14
|
},
|
|
@@ -31,28 +32,28 @@ export const Primary = () => ({
|
|
|
31
32
|
form: {
|
|
32
33
|
document: 'Document',
|
|
33
34
|
name: 'Name',
|
|
35
|
+
checkbox: true,
|
|
34
36
|
},
|
|
35
37
|
validForm: false,
|
|
36
38
|
rules: {
|
|
37
39
|
required: value => !!value || 'Campo obrigatório',
|
|
40
|
+
checked: value => !!value || 'Deve estar marcado',
|
|
38
41
|
},
|
|
39
42
|
styles,
|
|
40
43
|
};
|
|
41
44
|
},
|
|
42
45
|
template: `
|
|
43
46
|
<farm-form v-model="validForm" :style="[styles.vForm]" ref="form">
|
|
44
|
-
|
|
45
|
-
<
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
</div>
|
|
55
|
-
</section>
|
|
47
|
+
<div>
|
|
48
|
+
<farm-label :required="true">Documento</farm-label>
|
|
49
|
+
<farm-textfield v-model="form.document" :rules="[rules.required]" />
|
|
50
|
+
</div>
|
|
51
|
+
<farm-label :required="true">Nome</farm-label>
|
|
52
|
+
<farm-textfield v-model="form.name" :rules="[rules.required]" />
|
|
53
|
+
|
|
54
|
+
<farm-label :required="true">True/false</farm-label>
|
|
55
|
+
<farm-checkbox v-model="form.checkbox" :rules="[rules.checked]" />
|
|
56
|
+
|
|
56
57
|
<div class="footer" :style="[styles.footer]">
|
|
57
58
|
<farm-btn color="secondary" outlined @click="$refs.form.reset()" class="mr-3">Reset</farm-btn>
|
|
58
59
|
<farm-btn color="secondary" :disabled="!validForm">Salvar</farm-btn>
|
|
@@ -67,6 +68,7 @@ export const Secondary = () => ({
|
|
|
67
68
|
form: {
|
|
68
69
|
document: 'Document',
|
|
69
70
|
not: 'Not required field',
|
|
71
|
+
checkbox: false,
|
|
70
72
|
},
|
|
71
73
|
validForm: false,
|
|
72
74
|
rules: {
|
|
@@ -89,6 +91,10 @@ export const Secondary = () => ({
|
|
|
89
91
|
<farm-textfield v-model="form.not" />
|
|
90
92
|
</div>
|
|
91
93
|
</section>
|
|
94
|
+
|
|
95
|
+
<farm-label>Not required</farm-label>
|
|
96
|
+
<farm-checkbox v-model="form.checkbox" />
|
|
97
|
+
|
|
92
98
|
<div class="footer" :style="[styles.footer]">
|
|
93
99
|
<farm-btn color="secondary" outlined @click="$refs.form.reset()" class="mr-3">Reset</farm-btn>
|
|
94
100
|
<farm-btn color="secondary" :disabled="!validForm">Salvar</farm-btn>
|
|
@@ -103,10 +109,12 @@ export const InitInvalid = () => ({
|
|
|
103
109
|
form: {
|
|
104
110
|
document: '',
|
|
105
111
|
name: '',
|
|
112
|
+
checkbox: false,
|
|
106
113
|
},
|
|
107
114
|
validForm: false,
|
|
108
115
|
rules: {
|
|
109
116
|
required: value => !!value || 'Campo obrigatório',
|
|
117
|
+
checked: value => !!value || 'Deve estar marcado',
|
|
110
118
|
},
|
|
111
119
|
styles,
|
|
112
120
|
};
|
|
@@ -125,6 +133,10 @@ export const InitInvalid = () => ({
|
|
|
125
133
|
<farm-textfield v-model="form.name" :rules="[rules.required]" />
|
|
126
134
|
</div>
|
|
127
135
|
</section>
|
|
136
|
+
|
|
137
|
+
<farm-label :required="true">True/false</farm-label>
|
|
138
|
+
<farm-checkbox v-model="form.checkbox" :rules="[rules.checked]" />
|
|
139
|
+
|
|
128
140
|
<div class="footer" :style="[styles.footer]">
|
|
129
141
|
<farm-btn color="secondary" outlined @click="$refs.form.reset()" class="mr-3">Reset</farm-btn>
|
|
130
142
|
<farm-btn color="secondary" :disabled="!validForm">Salvar</farm-btn>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import deepEqual from '../deepEqual';
|
|
2
|
+
|
|
3
|
+
describe('deepEqual', () => {
|
|
4
|
+
it('Should return true for same object', () => {
|
|
5
|
+
const r = deepEqual({ a: 1 }, { a: 1 });
|
|
6
|
+
expect(r).toBeTruthy();
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it('Should return true for different object', () => {
|
|
10
|
+
const r = deepEqual({ a: 1 }, { a: '1' });
|
|
11
|
+
expect(r).toBeFalsy();
|
|
12
|
+
});
|
|
13
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
function deepEqual(a: any, b: any): boolean {
|
|
2
|
+
if (a === b) return true;
|
|
3
|
+
|
|
4
|
+
if (a instanceof Date && b instanceof Date && a.getTime() !== b.getTime()) {
|
|
5
|
+
// If the values are Date, compare them as timestamps
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
if (a !== Object(a) || b !== Object(b)) {
|
|
10
|
+
// If the values aren't objects, they were already checked for equality
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const props = Object.keys(a);
|
|
15
|
+
|
|
16
|
+
if (props.length !== Object.keys(b).length) {
|
|
17
|
+
// Different number of props, don't bother to check
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return props.every(p => deepEqual(a[p], b[p]));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default deepEqual;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export default function (rules) {
|
|
2
|
+
return value => {
|
|
3
|
+
const innerErrorBucket = [];
|
|
4
|
+
for (let index = 0; index < rules.length; index++) {
|
|
5
|
+
const rule = rules[index];
|
|
6
|
+
const valid = typeof rule === 'function' ? rule(value) : rule;
|
|
7
|
+
|
|
8
|
+
if (valid === false || typeof valid === 'string') {
|
|
9
|
+
innerErrorBucket.push(valid || '');
|
|
10
|
+
} else if (typeof valid !== 'boolean') {
|
|
11
|
+
console.error(
|
|
12
|
+
`Rules should return a string or boolean, received '${typeof valid}' instead`,
|
|
13
|
+
this
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return innerErrorBucket;
|
|
18
|
+
};
|
|
19
|
+
}
|