@@ -10,6 +10,7 @@ import Statement from '../Statement/index'
10
10
import { Labels , Operator } from './typings'
11
11
import { DEFAULT_LABELS , MEDIUM_ICON_SIZE } from './constants'
12
12
import { SubjectOptions } from '../Statement/Atoms/SubjectAtom'
13
+ import { StatementProp } from '../Statement/typings'
13
14
14
15
type Props = {
15
16
canDelete ?: boolean
@@ -21,16 +22,11 @@ type Props = {
21
22
operator : Operator
22
23
options : SubjectOptions
23
24
hideOperator ?: boolean
24
- statements : Array < {
25
- subject : string
26
- verb : string
27
- object ?: unknown
28
- error ?: string
29
- } >
25
+ statements : StatementProp [ ]
30
26
subjectPlaceholder : string
31
27
}
32
28
33
- const Conditions : React . FC < Props > = ( {
29
+ const Conditions = ( {
34
30
canDelete,
35
31
statements,
36
32
options,
@@ -42,8 +38,8 @@ const Conditions: React.FC<Props> = ({
42
38
operator,
43
39
onChangeOperator,
44
40
onChangeStatements,
45
- } ) => {
46
- const objectIsEmpty = object => {
41
+ } : Props ) => {
42
+ const objectIsEmpty = ( object : unknown ) => {
47
43
if ( object === undefined ) return true
48
44
if ( object === null ) return true
49
45
if ( object === '' ) return true
@@ -73,15 +69,18 @@ const Conditions: React.FC<Props> = ({
73
69
onChangeStatements ( [ ...statements , emptyStatement ] )
74
70
}
75
71
76
- const handleRemoveStatement = index => {
72
+ const handleRemoveStatement = ( index : number ) => {
77
73
const updatedStatements = statements
78
74
. slice ( 0 , index )
79
75
. concat ( statements . slice ( index + 1 ) )
80
76
81
77
onChangeStatements ( updatedStatements )
82
78
}
83
79
84
- const handleUpdatestatement = ( newStatement , statementIndex ) => {
80
+ const handleUpdatestatement = (
81
+ newStatement : StatementProp ,
82
+ statementIndex : number
83
+ ) => {
85
84
const newStatements = statements . map ( ( statement , idx ) =>
86
85
idx === statementIndex ? newStatement : statement
87
86
)
@@ -95,8 +94,10 @@ const Conditions: React.FC<Props> = ({
95
94
< StrategySelector
96
95
isRtl = { isRtl }
97
96
operator = { operator }
98
- labels = { labels }
99
- onChangeOperator = { operator => onChangeOperator ( operator ) }
97
+ labels = { labels as Labels }
98
+ onChangeOperator = { ( newOperator : Operator ) =>
99
+ onChangeOperator ( newOperator )
100
+ }
100
101
/>
101
102
</ div >
102
103
) }
@@ -136,6 +137,17 @@ const Conditions: React.FC<Props> = ({
136
137
{ }
137
138
)
138
139
140
+ const handleKeyDown = ( {
141
+ key,
142
+ } : React . KeyboardEvent < HTMLDivElement > ) => {
143
+ const SPACE = ' '
144
+ const ENTER = 'Enter'
145
+
146
+ if ( key === SPACE || key === ENTER ) {
147
+ handleRemoveStatement ( statementIndex )
148
+ }
149
+ }
150
+
139
151
const statementContent = [
140
152
< div key = "1" className = "flex-grow-1" >
141
153
< Statement
@@ -152,6 +164,9 @@ const Conditions: React.FC<Props> = ({
152
164
canDelete &&
153
165
( ! isFullWidth ? (
154
166
< div
167
+ role = "button"
168
+ tabIndex = { 0 }
169
+ onKeyDown = { handleKeyDown }
155
170
key = "2"
156
171
className = "ma3 c-muted-2 pointer hover-c-danger"
157
172
onClick = { ( ) => handleRemoveStatement ( statementIndex ) }
@@ -167,7 +182,7 @@ const Conditions: React.FC<Props> = ({
167
182
icon = { < IconClose className = "c-on-action-primary" /> }
168
183
onClick = { ( ) => handleRemoveStatement ( statementIndex ) }
169
184
>
170
- { labels . delete ?? DEFAULT_LABELS . delete }
185
+ { labels ? .delete ?? DEFAULT_LABELS . delete }
171
186
</ ButtonWithIcon >
172
187
</ div >
173
188
) ) ,
@@ -192,8 +207,8 @@ const Conditions: React.FC<Props> = ({
192
207
< Separator
193
208
label = {
194
209
operator === 'all'
195
- ? labels . operatorAnd ?? DEFAULT_LABELS . operatorAnd
196
- : labels . operatorOr ?? DEFAULT_LABELS . operatorOr
210
+ ? labels ? .operatorAnd ?? DEFAULT_LABELS . operatorAnd
211
+ : labels ? .operatorOr ?? DEFAULT_LABELS . operatorOr
197
212
}
198
213
/>
199
214
) }
@@ -213,7 +228,7 @@ const Conditions: React.FC<Props> = ({
213
228
disabled = { ! canAddNewCondition ( ) }
214
229
onClick = { handleAddNewCondition }
215
230
>
216
- { labels . addNewCondition ?? DEFAULT_LABELS . addNewCondition }
231
+ { labels ? .addNewCondition ?? DEFAULT_LABELS . addNewCondition }
217
232
</ ButtonWithIcon >
218
233
</ div >
219
234
</ div >
0 commit comments