@@ -113,23 +113,37 @@ function get_default_relation_label(
113
113
return [ ] ;
114
114
}
115
115
116
+ /**
117
+ * Remove any records from the array that are already linked to the current record.
118
+ * @param multiple - do we allow multiple relations?
119
+ * @param value - current value of the field
120
+ * @param all_records - array of records that are available to link to
121
+ * @returns a reduced array of records for linking
122
+ */
116
123
function excludes_related_record (
117
124
multiple : boolean ,
118
125
value : any ,
119
126
all_records : RecordReference [ ]
120
127
) {
121
128
const relations : string [ ] = multiple ? [ ] : [ value ?. record_id ] ;
122
129
const records : RecordReference [ ] = [ ] ;
130
+
131
+ // if we allow multiple links and we have a current value then extract
132
+ // the record_ids from the current value and add them to the relations array
123
133
if ( multiple && value )
124
134
value . map ( ( record : RecordReference ) =>
125
135
record !== null ? relations . push ( record . record_id ) : record
126
136
) ;
127
137
138
+ // filter the all_records array to remove any records that are already linked
139
+ // if we don't allow multiple values OR we have no current value
140
+ // then all records would be included
128
141
all_records . map ( ( record : RecordReference ) =>
129
142
relations . includes ( record . record_id ) ? record : records . push ( record )
130
143
) ;
131
144
return records ;
132
145
}
146
+
133
147
type DisplayChildProps = {
134
148
handleUnlink : Function ;
135
149
handleReset : Function ;
@@ -239,12 +253,18 @@ export function RelatedRecordSelector(props: FieldProps & Props) {
239
253
let mounted = true ;
240
254
( async ( ) => {
241
255
if ( project_id !== undefined && mounted && props . isconflict !== true ) {
256
+ // need to enable the field for multiple=false since it defaults to disabled
257
+ // enable if there is no existing value or if the existing value doesn't have
258
+ // a record id (why would it not have a record ID?)
242
259
if (
243
260
! multiple &&
244
261
props . form . values [ field_name ] &&
245
262
props . form . values [ field_name ] [ 'record_id' ] === undefined
246
263
)
247
264
setIs_enabled ( true ) ;
265
+ // or just no existing value
266
+ if ( ! multiple && ! props . form . values [ field_name ] ) setIs_enabled ( true ) ;
267
+
248
268
if ( ! multiple ) {
249
269
if (
250
270
props . form . values [ field_name ] &&
@@ -607,8 +627,7 @@ export function RelatedRecordSelector(props: FieldProps & Props) {
607
627
</ Typography >
608
628
{ is_enabled && (
609
629
< Typography variant = "caption" >
610
- It's a single related, to enable Add record or Link, remove
611
- link firstly
630
+ Remove existing link to enable Add record or Link
612
631
</ Typography >
613
632
) }
614
633
</ Grid >
0 commit comments