@@ -11,6 +11,8 @@ export const VALID_MAX_VOTES_VALUE_REGEX = /^([2-9]|10)$/;
11
11
12
12
export const MAX_POLL_OPTIONS = 100 as const ;
13
13
14
+ const textFieldIsEmpty = ( text : string ) => ! text . trim ( ) ;
15
+
14
16
export type PollStateValidationOutput = Partial <
15
17
Omit < Record < keyof PollComposerState [ 'data' ] , string > , 'options' > & {
16
18
options ?: Record < string , string > ;
@@ -74,9 +76,20 @@ export const defaultPollFieldBlurEventValidators: Partial<
74
76
return { max_votes_allowed : undefined } ;
75
77
} ,
76
78
name : ( { value } ) => {
77
- if ( ! value ) return { name : 'Name is required' } ;
79
+ if ( textFieldIsEmpty ( value ) ) return { name : 'Question is required' } ;
78
80
return { name : undefined } ;
79
81
} ,
82
+ options : ( params ) => {
83
+ const defaultResult = pollStateChangeValidators . options ?.( params ) ;
84
+ const errors = defaultResult ?. options ?? { } ;
85
+ params . value . forEach ( ( option : { id : string ; text : string } , index : number ) => {
86
+ const isTheLastOption = index === params . value . length - 1 ;
87
+ if ( textFieldIsEmpty ( option . text ) && ! isTheLastOption ) {
88
+ errors [ option . id ] = 'Option is empty' ;
89
+ }
90
+ } ) ;
91
+ return Object . keys ( errors ) . length > 0 ? { options : errors } : { options : undefined } ;
92
+ } ,
80
93
} ;
81
94
82
95
export type PollCompositionStateProcessorOutput = Partial < PollComposerState [ 'data' ] > ;
@@ -168,15 +181,19 @@ export const createPollComposerStateMiddleware = ({
168
181
processors : customProcessors ,
169
182
validators : customValidators ,
170
183
} : PollComposerStateMiddlewareFactoryOptions = { } ) : PollComposerStateMiddleware => {
171
- const universalHandler = (
172
- state : PollComposerStateChangeMiddlewareValue ,
184
+ const universalHandler = ( {
185
+ state,
186
+ validators,
187
+ processors,
188
+ } : {
189
+ state : PollComposerStateChangeMiddlewareValue ;
173
190
validators : Partial <
174
191
Record < keyof PollComposerState [ 'data' ] , PollStateChangeValidator >
175
- > ,
192
+ > ;
176
193
processors ?: Partial <
177
194
Record < keyof PollComposerState [ 'data' ] , PollCompositionStateProcessor >
178
- > ,
179
- ) => {
195
+ > ;
196
+ } ) => {
180
197
const { previousState, targetFields } = state ;
181
198
182
199
let newData : Partial < PollComposerState [ 'data' ] > ;
@@ -210,9 +227,9 @@ export const createPollComposerStateMiddleware = ({
210
227
const validator = validators [ key as keyof PollComposerState [ 'data' ] ] ;
211
228
if ( validator ) {
212
229
const error = validator ( {
230
+ currentError : previousState . errors [ key as keyof PollComposerState [ 'data' ] ] ,
213
231
data : previousState . data ,
214
232
value : newData [ key as keyof PollComposerState [ 'data' ] ] ,
215
- currentError : previousState . errors [ key as keyof PollComposerState [ 'data' ] ] ,
216
233
} ) ;
217
234
acc = { ...acc , ...error } ;
218
235
}
@@ -232,21 +249,19 @@ export const createPollComposerStateMiddleware = ({
232
249
} : MiddlewareHandlerParams < PollComposerStateChangeMiddlewareValue > ) => {
233
250
if ( ! state . targetFields ) return forward ( ) ;
234
251
const { previousState, injectedFieldErrors } = state ;
235
- const finalValidators = {
236
- ...pollStateChangeValidators ,
237
- ...defaultPollFieldChangeEventValidators ,
238
- ...customValidators ?. handleFieldChange ,
239
- } ;
240
- const finalProcessors = {
241
- ...pollCompositionStateProcessors ,
242
- ...customProcessors ?. handleFieldChange ,
243
- } ;
244
-
245
- const { newData, newErrors } = universalHandler (
252
+
253
+ const { newData, newErrors } = universalHandler ( {
254
+ processors : {
255
+ ...pollCompositionStateProcessors ,
256
+ ...customProcessors ?. handleFieldChange ,
257
+ } ,
246
258
state,
247
- finalValidators ,
248
- finalProcessors ,
249
- ) ;
259
+ validators : {
260
+ ...pollStateChangeValidators ,
261
+ ...defaultPollFieldChangeEventValidators ,
262
+ ...customValidators ?. handleFieldChange ,
263
+ } ,
264
+ } ) ;
250
265
251
266
return next ( {
252
267
...state ,
@@ -265,16 +280,15 @@ export const createPollComposerStateMiddleware = ({
265
280
if ( ! state . targetFields ) return forward ( ) ;
266
281
267
282
const { previousState } = state ;
268
- const finalValidators = {
269
- ...pollStateChangeValidators ,
270
- ...defaultPollFieldBlurEventValidators ,
271
- ...customValidators ?. handleFieldBlur ,
272
- } ;
273
- const { newData, newErrors } = universalHandler (
283
+ const { newData, newErrors } = universalHandler ( {
284
+ processors : customProcessors ?. handleFieldBlur ,
274
285
state,
275
- finalValidators ,
276
- customProcessors ?. handleFieldBlur ,
277
- ) ;
286
+ validators : {
287
+ ...pollStateChangeValidators ,
288
+ ...defaultPollFieldBlurEventValidators ,
289
+ ...customValidators ?. handleFieldBlur ,
290
+ } ,
291
+ } ) ;
278
292
279
293
return next ( {
280
294
...state ,
0 commit comments