@farm-investimentos/front-mfe-components 3.0.0 → 3.2.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@farm-investimentos/front-mfe-components",
3
- "version": "3.0.0",
3
+ "version": "3.2.0",
4
4
  "author": "farm investimentos",
5
5
  "private": false,
6
6
  "main": "./dist/front-mfe-components.common.js",
@@ -0,0 +1,37 @@
1
+ import MultipleSelectShortener from './MultipleSelectShortener';
2
+
3
+ export default {
4
+ title: 'Example/MultipleSelectShortener',
5
+ component: MultipleSelectShortener,
6
+ };
7
+
8
+ export const Primary = () => ({
9
+ components: { MultipleSelectShortener },
10
+ data() {
11
+ return {
12
+ items: {
13
+ id: 0,
14
+ label: `Convidar`,
15
+ },
16
+ };
17
+ },
18
+ template: '<MultipleSelectShortener :item="items" :index="0" :itemsLength="1"/>',
19
+ });
20
+
21
+ export const Secondary = () => ({
22
+ components: { MultipleSelectShortener },
23
+ data() {
24
+ return {
25
+ items: [
26
+ {
27
+ id: 0,
28
+ label: `Convidar`
29
+ },
30
+ ],
31
+ };
32
+ },
33
+ template: '<MultipleSelectShortener :item="{...items}" :index="1" :itemsLength="3"/>',
34
+ });
35
+
36
+ Primary.storyName = 'Básico';
37
+ Secondary.storyName = 'Multiplos';
@@ -0,0 +1,48 @@
1
+ <template>
2
+ <div class="align-center">
3
+ <span class="mr-1 label" v-if="index === 0">{{ item[labelKey] }}</span>
4
+ <span class="label" v-if="index === 1">
5
+ (+{{ itemsLength - 1 }} {{ itemsLength > 2 ? 'outros' : 'outro' }} )
6
+ </span>
7
+ </div>
8
+ </template>
9
+
10
+ <script>
11
+ import Vue from 'vue';
12
+ export default Vue.extend({
13
+ name: 'farm-multiple-select-shortener',
14
+ props: {
15
+ /**
16
+ * Key used to get value from item
17
+ */
18
+ labelKey: {
19
+ type: String,
20
+ default: 'label',
21
+ },
22
+ /**
23
+ * Item from v-select
24
+ */
25
+ item: {
26
+ type: Object,
27
+ },
28
+ /**
29
+ * Index from slot
30
+ */
31
+ index: {
32
+ type: Number,
33
+ },
34
+ /**
35
+ * Items count
36
+ */
37
+ itemsLength: {
38
+ type: Number,
39
+ },
40
+ },
41
+ });
42
+ </script>
43
+
44
+ <style scoped>
45
+ .label {
46
+ font-size: 0.75rem;
47
+ }
48
+ </style>
@@ -0,0 +1,21 @@
1
+ import { shallowMount } from '@vue/test-utils';
2
+ import MultipleSelectShortener from '../index';
3
+
4
+ describe('MultipleSelectShortener component', () => {
5
+ let wrapper;
6
+
7
+ beforeEach(() => {
8
+ wrapper = shallowMount(MultipleSelectShortener, {
9
+ propsData: {
10
+ item: {},
11
+ index: 0,
12
+ labelKey: '',
13
+ itemsLength: 0,
14
+ },
15
+ });
16
+ });
17
+
18
+ test('Created hook', () => {
19
+ expect(wrapper).toBeDefined();
20
+ });
21
+ });
@@ -0,0 +1,4 @@
1
+ import MultipleSelectShortener from './MultipleSelectShortener';
2
+
3
+ export { MultipleSelectShortener };
4
+ export default MultipleSelectShortener;
@@ -12,7 +12,7 @@ import Vue from 'vue';
12
12
  import VBtn from 'vuetify/lib/components/VBtn';
13
13
  import VIcon from 'vuetify/lib/components/VIcon';
14
14
  export default Vue.extend({
15
- // name: 'farm-tablerowselection',
15
+ name: 'farm-tablerowselection',
16
16
  components: {
17
17
  VBtn,
18
18
  VIcon,
@@ -0,0 +1,29 @@
1
+ import SelectModalOptions from './SelectModalOptions';
2
+
3
+ export default {
4
+ title: 'Example/SelectModalOptions',
5
+ component: SelectModalOptions,
6
+ };
7
+
8
+ export const Primary = () => ({
9
+ components: { SelectModalOptions },
10
+ data() {
11
+ return {
12
+ items: [
13
+ { id: 1, name: 'name 1' },
14
+ { id: 2, name: 'name 2' },
15
+ ],
16
+ selectedValue: null,
17
+ };
18
+ },
19
+ template: `<div>
20
+ <SelectModalOptions
21
+ :items="items"
22
+ v-model="selectedValue"
23
+ label="label"
24
+ inputId="some-id" />
25
+ <br />selected value: {{ selectedValue }}
26
+ </div>`,
27
+ });
28
+
29
+ Primary.storyName = 'Básico';
@@ -0,0 +1,273 @@
1
+ <template>
2
+ <v-col cols="12" sm="12" :md="config.md ? config.md : 2" class="v-col-fieldset-default">
3
+ <label :for="inputId">
4
+ {{ label }}
5
+ <span class="required" v-if="config.required">*</span>
6
+ </label>
7
+ <v-text-field
8
+ append-icon="mdi-magnify "
9
+ color="secondary"
10
+ outlined
11
+ dense
12
+ readonly
13
+ :id="inputId"
14
+ :value="selectedValueText"
15
+ @click="openModal"
16
+ ></v-text-field>
17
+
18
+ <v-dialog content-class="modal-default modal-default-small" v-model="showModal">
19
+ <DialogHeader class="dialog-header" :title="modalTitle" @onClose="closeModal" />
20
+
21
+ <v-main class="mt-9">
22
+ <Loader class="text-center mb-2" v-if="isLoading" />
23
+ <fieldset class="fieldset-default mx-4 mb-3" v-if="!isLoading">
24
+ <label for="searchInput"> {{ label }} </label>
25
+ <v-text-field
26
+ color="secondary"
27
+ id="searchInput"
28
+ outlined
29
+ dense
30
+ hide-details
31
+ :placeholder="placeholder"
32
+ v-model="searchValue"
33
+ />
34
+ </fieldset>
35
+
36
+ <v-data-table
37
+ v-if="!isLoading"
38
+ id="inputModalOptionsTable"
39
+ class="v-data-table__clickable v-data-table__select-modal"
40
+ hide-default-footer
41
+ hide-default-header
42
+ :items="items"
43
+ :headers="headers"
44
+ :options.sync="pagination"
45
+ :search="searchValue"
46
+ :custom-filter="customFilter"
47
+ @click:row="handleClick"
48
+ @pagination="handlePagination"
49
+ >
50
+ <template slot="no-data">
51
+ <DataTableEmptyWrapper />
52
+ </template>
53
+ <template slot="no-results">
54
+ <DataTableEmptyWrapper />
55
+ </template>
56
+ <template v-slot:[`item.label`]="{ item }">
57
+ <td :title="getItemLabel(item)" aria-role="button">
58
+ {{ getItemLabel(item) }}
59
+ </td>
60
+ </template>
61
+
62
+ <template v-slot:footer>
63
+ <DataTablePaginator
64
+ class="my-6"
65
+ hidePerPageOptions
66
+ :initialLimitPerPage="pagination.itemsPerPage"
67
+ :page="pagination.page"
68
+ :totalPages="pagination.pages"
69
+ @onChangePage="onChangePage"
70
+ />
71
+ </template>
72
+ </v-data-table>
73
+ </v-main>
74
+ <DialogFooter :hasConfirm="false" @onClose="closeModal" />
75
+ </v-dialog>
76
+ </v-col>
77
+ </template>
78
+ <script>
79
+ import Vue from 'vue';
80
+ import VTextField from 'vuetify/lib/components/VTextField';
81
+ import { VCol } from 'vuetify/lib/components/VGrid';
82
+ import { VMain } from 'vuetify/lib/components/VMain';
83
+ import { VDataTable } from 'vuetify/lib/components/VDataTable/';
84
+ import { VDialog } from 'vuetify/lib/components/VDialog/';
85
+ import { DialogHeader, DialogFooter, DataTableEmptyWrapper, DataTablePaginator } from '../../main';
86
+
87
+ export default Vue.extend({
88
+ name: 'farm-select-modal-options',
89
+ components: {
90
+ VTextField,
91
+ VCol,
92
+ VMain,
93
+ VDataTable,
94
+ VDialog,
95
+ DialogHeader,
96
+ DialogFooter,
97
+ DataTableEmptyWrapper,
98
+ DataTablePaginator,
99
+ },
100
+ props: {
101
+ /**
102
+ * Input Label
103
+ */
104
+ label: {
105
+ type: String,
106
+ required: true,
107
+ },
108
+ /**
109
+ * Input ID
110
+ */
111
+ inputId: {
112
+ type: String,
113
+ required: true,
114
+ },
115
+ /**
116
+ * Key used to get value from selected item
117
+ */
118
+ selectIdentifier: {
119
+ type: String,
120
+ default: 'id',
121
+ },
122
+ /**
123
+ * Format label from each item
124
+ * If value is a function, it is evaluted (use it to build complex strings)
125
+ * Otherwise, will get item value from the key with the prop value
126
+ */
127
+ itemLabelFormatter: {
128
+ default: 'name',
129
+ },
130
+ /**
131
+ * Items to be shown in the table
132
+ */
133
+ items: {
134
+ type: Array,
135
+ default: () => [],
136
+ },
137
+ /**
138
+ * Function that will be called when user focus/click the input
139
+ */
140
+ loadFn: {
141
+ type: Function,
142
+ default: () => {},
143
+ },
144
+ /**
145
+ * Request Status from the loader function
146
+ */
147
+ loadingRequestStatus: {
148
+ default: 'IDLE',
149
+ },
150
+ /**
151
+ * Title from the modal
152
+ */
153
+ modalTitle: {
154
+ type: String,
155
+ default: 'Selecione um item',
156
+ },
157
+ /**
158
+ * General configuration
159
+ */
160
+ config: {
161
+ default: () => ({ md: 2, required: false }),
162
+ },
163
+ /**
164
+ * Placeholder from the search input
165
+ */
166
+ placeholder: {
167
+ type: String,
168
+ default: 'Pesquise',
169
+ },
170
+ /**
171
+ * v-model
172
+ */
173
+ value: {
174
+ required: true,
175
+ },
176
+ },
177
+ computed: {
178
+ inputVal: {
179
+ get() {
180
+ return this.value;
181
+ },
182
+ set(val) {
183
+ this.$emit('input', val);
184
+ },
185
+ },
186
+ isLoading() {
187
+ return this.loadingRequestStatus === 'START';
188
+ },
189
+ selectedValueText: {
190
+ get() {
191
+ return this.selectedItem ? this.getItemLabel(this.selectedItem) : '';
192
+ },
193
+ },
194
+ totalPages() {
195
+ if (this.items !== undefined) {
196
+ return Math.ceil(this.items.length / 10);
197
+ }
198
+ return 0;
199
+ },
200
+ },
201
+ data() {
202
+ return {
203
+ selectedValue: {},
204
+ showModal: false,
205
+ selectedItem: null,
206
+ searchValue: '',
207
+ headers: [
208
+ {
209
+ text: 'Label',
210
+ sortable: false,
211
+ value: 'label',
212
+ align: 'left',
213
+ },
214
+ ],
215
+ pagination: {
216
+ page: 1,
217
+ itemsPerPage: 10,
218
+ pages: 0,
219
+ },
220
+ };
221
+ },
222
+ watch: {
223
+ value(newValue) {
224
+ if (!newValue || newValue === undefined) {
225
+ this.selectedItem = null;
226
+ }
227
+ },
228
+ items(newValue) {
229
+ this.pagination.pages = Math.ceil(newValue.length / 10);
230
+ },
231
+ },
232
+ methods: {
233
+ handlePagination(tablePagination) {
234
+ if (
235
+ tablePagination.page === 1 &&
236
+ tablePagination.itemsLength !== this.pagination.pages
237
+ ) {
238
+ this.pagination.pages = Math.ceil(tablePagination.itemsLength / 10);
239
+ }
240
+ },
241
+ openModal() {
242
+ this.loadFn();
243
+ this.showModal = true;
244
+ },
245
+ closeModal() {
246
+ this.showModal = false;
247
+ },
248
+ onChangePage(newPage) {
249
+ this.pagination.page = newPage;
250
+ },
251
+ getItemLabel(item) {
252
+ if (typeof this.itemLabelFormatter === 'function') {
253
+ return this.itemLabelFormatter(item);
254
+ }
255
+ return item[this.itemLabelFormatter];
256
+ },
257
+ handleClick(item) {
258
+ this.inputVal = item[this.selectIdentifier];
259
+ this.showModal = false;
260
+ this.selectedItem = item;
261
+ },
262
+ customFilter(_, search, item) {
263
+ const label = this.getItemLabel(item);
264
+ return label.toLowerCase().includes(search.toLowerCase());
265
+ },
266
+ },
267
+ });
268
+ </script>
269
+ <style lang="scss" scoped>
270
+ .v-text-field {
271
+ cursor: pointer;
272
+ }
273
+ </style>
@@ -0,0 +1,130 @@
1
+ import { shallowMount } from '@vue/test-utils';
2
+ import SelectModalOptions from '../SelectModalOptions';
3
+
4
+ describe('SelectModalOptions component', () => {
5
+ let wrapper;
6
+ let component;
7
+
8
+ beforeEach(() => {
9
+ wrapper = shallowMount(SelectModalOptions, {
10
+ propsData: {
11
+ value: 1,
12
+ inputId: 'some-id',
13
+ label: '',
14
+ pagination: {
15
+ page: 1,
16
+ itemsPerPage: 10,
17
+ pages: 0,
18
+ },
19
+ },
20
+ });
21
+ component = wrapper.vm;
22
+ });
23
+
24
+ test('SelectModalOptions created', () => {
25
+ expect(wrapper).toBeDefined();
26
+ });
27
+
28
+ describe('Methods', () => {
29
+ it('Should open modal', () => {
30
+ component.openModal();
31
+ expect(component.showModal).toBeTruthy();
32
+ });
33
+
34
+ it('Should close close', () => {
35
+ component.closeModal();
36
+ expect(component.showModal).toBeFalsy();
37
+ });
38
+
39
+ it('Should get item value', async () => {
40
+ expect(component.getItemLabel({ name: 'a' })).toEqual('a');
41
+
42
+ await wrapper.setProps({ itemLabelFormatter: item => item.name + ' ' + item.id });
43
+ expect(component.getItemLabel({ name: 'a', id: 'b' })).toEqual('a b');
44
+ });
45
+
46
+ it('Should handle click', () => {
47
+ component.handleClick({ name: 'a', id: 1 });
48
+ expect(component.inputVal).toEqual(1);
49
+ expect(component.selectedItem.id).toEqual(1);
50
+ });
51
+ });
52
+
53
+ describe('Computed properties', () => {
54
+ it('Should check if is loading', () => {
55
+ expect(component.isLoading).toBeFalsy();
56
+ });
57
+
58
+ it('Should return 0 when items are empty', () => {
59
+ expect(component.totalPages).toBe(0);
60
+ });
61
+
62
+ it('Should return 0 when items are empty', async () => {
63
+ await wrapper.setProps({
64
+ items: new Array(30).fill(0),
65
+ });
66
+ expect(component.totalPages).toBe(3);
67
+ });
68
+
69
+ it('Should return 0 when items are empty', async () => {
70
+ await wrapper.setProps({
71
+ items: new Array(30).fill(0),
72
+ });
73
+ expect(component.totalPages).toBe(3);
74
+ });
75
+ });
76
+
77
+ describe('Watch methods', () => {
78
+ it('Should reset selectedItem', () => {
79
+ component.$options.watch.value.call(component, undefined);
80
+ expect(component.selectedItem).toEqual(null);
81
+ });
82
+ it('Should reset selectedItem', () => {
83
+ component.$options.watch.items.call(component, new Array(50));
84
+
85
+ expect(component.pagination.pages).toBe(5);
86
+ });
87
+ it('Should change pagination', () => {
88
+ expect(component.pagination).toEqual({
89
+ page: 1,
90
+ itemsPerPage: 10,
91
+ pages: 0,
92
+ });
93
+ component.handlePagination({
94
+ page: 1,
95
+ itemsLength: 1,
96
+ });
97
+ expect(component.pagination).toEqual({
98
+ page: 1,
99
+ itemsPerPage: 10,
100
+ pages: 1,
101
+ });
102
+ });
103
+ it('Should not change pagination', () => {
104
+ expect(component.pagination).toEqual({
105
+ page: 1,
106
+ itemsPerPage: 10,
107
+ pages: 0,
108
+ });
109
+ component.handlePagination({
110
+ page: 2,
111
+ itemsLength: 30,
112
+ });
113
+ expect(component.pagination).toEqual({
114
+ page: 1,
115
+ itemsPerPage: 10,
116
+ pages: 0,
117
+ });
118
+ });
119
+
120
+ it('Should change page', () => {
121
+ component.onChangePage(2);
122
+ expect(component.pagination.page).toBe(2);
123
+ });
124
+
125
+ it('Should do customFilter', () => {
126
+ expect(component.customFilter({}, 'key', { name: '21321' })).toBeFalsy();
127
+ expect(component.customFilter({}, 'key', { name: 'aS KEY SDSA' })).toBeTruthy();
128
+ });
129
+ });
130
+ });
@@ -0,0 +1,4 @@
1
+ import SelectModalOptions from './SelectModalOptions';
2
+
3
+ export { SelectModalOptions };
4
+ export default SelectModalOptions;
package/src/main.js CHANGED
@@ -54,3 +54,5 @@ export * from './components/Buttons/RemoveButton/';
54
54
  export * from './components/Logos/ProductLogo/';
55
55
  export * from './components/Logos/OriginatorLogo/';
56
56
  export * from './components/ResetTableRowSelection/';
57
+ export * from './components/MultipleSelectShortener/';
58
+ export * from './components/SelectModalOptions/';