11<script >
2+ import { ref , watch , computed } from ' vue' ;
23import debounce from ' lodash/debounce' ;
34import { _EDIT , _VIEW } from ' @shell/config/query-params' ;
45import { removeAt } from ' @shell/utils/array' ;
@@ -99,32 +100,85 @@ export default {
99100 default: ' ' ,
100101 },
101102 },
102- data () {
103- const input = (Array .isArray (this .value ) ? this .value : []).slice ();
104- const rows = [];
103+
104+ setup (props , { emit }) {
105+ const input = (Array .isArray (props .value ) ? props .value : []).slice ();
106+ const rows = ref ([]);
105107
106108 for ( const value of input ) {
107- rows .push ({ value });
109+ rows .value . push ({ value });
108110 }
109- if ( ! rows .length && this .initialEmptyRow ) {
110- const value = this .defaultAddValue ? clone (this .defaultAddValue ) : ' ' ;
111+ if ( ! rows .value . length && props .initialEmptyRow ) {
112+ const value = props .defaultAddValue ? clone (props .defaultAddValue ) : ' ' ;
111113
112- rows .push ({ value });
114+ rows .value . push ({ value });
113115 }
114116
115- return { rows, lastUpdateWasFromValue: false };
117+ const isView = computed (() => {
118+ return props .mode === _VIEW ;
119+ });
120+
121+ /**
122+ * Cleanup rows and emit input
123+ */
124+ const update = () => {
125+ if ( isView .value ) {
126+ return ;
127+ }
128+ const out = [];
129+
130+ for ( const row of rows .value ) {
131+ const trim = ! props .valueMultiline && (typeof row .value === ' string' );
132+ const value = trim ? row .value .trim () : row .value ;
133+
134+ if ( typeof value !== ' undefined' ) {
135+ out .push (value);
136+ }
137+ }
138+ emit (' update:value' , out);
139+ };
140+
141+ const lastUpdateWasFromValue = ref (false );
142+ const queueUpdate = debounce (update, 50 );
143+
144+ watch (
145+ rows,
146+ () => {
147+ // lastUpdateWasFromValue is used to break a cycle where when rows are updated
148+ // this was called which then forced rows to updated again
149+ if (! lastUpdateWasFromValue .value ) {
150+ queueUpdate ();
151+ }
152+ lastUpdateWasFromValue .value = false ;
153+ },
154+ { deep: true }
155+ );
156+
157+ watch (
158+ () => props .value ,
159+ () => {
160+ lastUpdateWasFromValue .value = true ;
161+ rows .value = (props .value || []).map ((v ) => ({ value: v }));
162+ },
163+ { deep: true }
164+ );
165+
166+ return {
167+ rows,
168+ lastUpdateWasFromValue,
169+ queueUpdate,
170+ isView,
171+ update,
172+ };
116173 },
174+
117175 computed: {
118176 _addLabel () {
119177 return this .addLabel || this .t (' generic.add' );
120178 },
121179 _removeLabel () {
122180 return this .removeLabel || this .t (' generic.remove' );
123181 },
124-
125- isView () {
126- return this .mode === _VIEW ;
127- },
128182 showAdd () {
129183 return this .addAllowed ;
130184 },
@@ -145,29 +199,7 @@ export default {
145199 return ! this .valueMultiline && this .protip ;
146200 }
147201 },
148- watch: {
149- value: {
150- deep: true ,
151- handler () {
152- this .lastUpdateWasFromValue = true ;
153- this .rows = (this .value || []).map ((v ) => ({ value: v }));
154- }
155- },
156-
157- rows: {
158- deep: true ,
159- handler (newValue , oldValue ) {
160- // lastUpdateWasFromValue is used to break a cycle where when rows are updated
161- // this was called which then forced rows to updated again
162- if (! this .lastUpdateWasFromValue ) {
163- this .queueUpdate ();
164- }
165- this .lastUpdateWasFromValue = false ;
166- }
167- }
168- },
169202 created () {
170- this .queueUpdate = debounce (this .update , 50 );
171203 },
172204 methods: {
173205 add () {
@@ -193,26 +225,6 @@ export default {
193225 this .queueUpdate ();
194226 },
195227
196- /**
197- * Cleanup rows and emit input
198- */
199- update () {
200- if ( this .isView ) {
201- return ;
202- }
203- const out = [];
204-
205- for ( const row of this .rows ) {
206- const trim = ! this .valueMultiline && (typeof row .value === ' string' );
207- const value = trim ? row .value .trim () : row .value ;
208-
209- if ( typeof value !== ' undefined' ) {
210- out .push (value);
211- }
212- }
213- this .$emit (' update:value' , out);
214- },
215-
216228 /**
217229 * Handle paste event, e.g. split multiple lines in rows
218230 */
0 commit comments