Skip to content

Commit 23e029e

Browse files
authored
Merge pull request #379 from US-CBP/bugfix/form-investigations
Bugfix/form investigations
2 parents 58d7a52 + 08edd5f commit 23e029e

File tree

3 files changed

+24
-56
lines changed

3 files changed

+24
-56
lines changed

packages/web-components/src/components/cbp-file-input/cbp-file-input.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export class CbpFileInput {
209209
const Id = this.formField.getAttribute('id');
210210
Id ? this.fieldId = Id : this.formField.setAttribute('id', this.fieldId);
211211
const Name = this.formField.getAttribute('name');
212-
Name ? this.name = Name : this.formField.setAttribute('Name', this.name);
212+
Name ? this.name = Name : (this.name ? this.formField.setAttribute('Name', this.name) : null);
213213

214214
if (this.multiple) this.formField.setAttribute('multiple', '');
215215
if (this.accept) this.formField.setAttribute('accept', this.accept);

packages/web-components/src/components/cbp-form/cbp-form.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class CbpForm {
4646
// Add files from enhanced/multi-file inputs
4747
formData = this.addFiles(formData);
4848

49-
//console.log('resulting formData (array spread): ',[...formData]);
49+
console.log('cbp-form - Resulting formData (array spread): ', [...formData]);
5050

5151
// If the form submission is prevented, emit an event with the data instead
5252
if (this.preventSubmit) {
@@ -62,29 +62,26 @@ export class CbpForm {
6262
}
6363

6464
addFiles(formData) {
65-
/*
66-
Update FormData (or create a copy) with fields not supported natively:
67-
File input (multiple+enhanced, allowing manipulation of FileList)
68-
*/
65+
// Update formData with enhanced/multi-file input (allowing manipulation of FileList)
6966
Object.keys(this.files).forEach( key => {
70-
if (this.files?.[`${key}`]?.length > 0){
67+
if (this.files?.[key]?.length > 0){
7168
// delete the empty key if there are files specified
7269
formData.delete(key);
7370
// loop over the files and add each as a new entry using append (set overrides the same entry)
7471
this.files?.[key].forEach( file => {
7572
formData.append(key, file);
7673
});
7774
}
78-
formData[`${key}`] = this.files[`${key}`];
75+
formData[key] = this.files[key];
7976
});
8077
return formData;
8178
}
8279

8380
handleEnhancedFileInput(e) {
8481
// Keep tabs on enhanced/multi-file inputs' values
8582
const {name, value} = e.detail;
86-
this.files[name] = value;
87-
console.log('cbp-form - Tracking enhanced/multi-file input: ', this.files);
83+
// Only bother if the input has a name
84+
if(!!name) this.files[name] = value;
8885
}
8986

9087
componentWillLoad() {

packages/web-components/src/stories/tests/form-processing.stories.tsx

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,8 @@ function HTMLForm( {name, action, method, enctype, prefix, firstName, middleInit
268268
</cbp-slider>
269269
</cbp-form-field>
270270
271+
271272
<!-- File Inputs -->
272-
<label>
273-
Native File Input<br />
274-
<input type="file" name="nativefileinput" multiple/>
275-
</label><br /><br />
276-
277-
278273
<cbp-form-field label="Native File Input" description="Although styled as a design system input, this is a native file input in function.">
279274
<cbp-form-field-wrapper>
280275
<input type="file" name="nativefileinput" multiple>
@@ -322,10 +317,10 @@ const FormProcessingTemplate = ( args ) => {
322317

323318
// Set up event handlers for logging and setting errors on files via the `status` prop.
324319
setTimeout(() => {
325-
const formEl = document.querySelector(`form[name="${name}"]`) as HTMLFormElement;
320+
const formEl = document.querySelector(`form[name="${args.name}"]`) as HTMLFormElement;
326321
const submitButton = document.querySelector('button[type=submit]') as HTMLButtonElement;
327322

328-
//console.log('Event Listeners set: ', formEl, submitButton);
323+
console.log('Event Listeners set: ', formEl, submitButton);
329324

330325
submitButton?.addEventListener('click', e => {
331326
console.log('Submit button pressed', submitButton, e);
@@ -349,13 +344,15 @@ const FormProcessingTemplate = ( args ) => {
349344
*/
350345

351346
// List key/value pairs
347+
/*
352348
for(let [name, value] of formData) {
353349
console.log(`${name} =`, value);
354350
}
351+
*/
355352
// Spreading the formData as an array seems to give the same results as above.
356-
console.log('formData (array spread): ',[...formData]);
353+
console.log('Native form submit - formData (array spread): ',[...formData]);
357354
// This method makes the assumption that object keys are unique and only shows 1 value when they are not.
358-
console.log('formData as JS Object: ', Object.fromEntries(formData.entries()));
355+
//console.log('formData as JS Object: ', Object.fromEntries(formData.entries()));
359356

360357
/*
361358
formData.append("CustomField", "This is some extra data");
@@ -367,7 +364,7 @@ const FormProcessingTemplate = ( args ) => {
367364
*/
368365

369366
});
370-
}, 1000);
367+
}, 2000);
371368

372369
return `
373370
<h1>Native Form Tag</h1>
@@ -377,7 +374,9 @@ const FormProcessingTemplate = ( args ) => {
377374
};
378375

379376
export const FormProcessing = FormProcessingTemplate.bind({});
380-
377+
FormProcessing.args = {
378+
name: 'nativeForm'
379+
}
381380

382381

383382

@@ -386,7 +385,7 @@ const FormComponentProcessingTemplate = (args) => {
386385

387386
// Set up event handlers for logging and setting errors on files via the `status` prop.
388387
setTimeout(() => {
389-
const formEl = document.querySelector(`form[name="${name}"]`) as HTMLFormElement;
388+
const formEl = document.querySelector(`form[name="${args.name}"]`) as HTMLFormElement;
390389
const submitButton = document.querySelector('button[type=submit]') as HTMLButtonElement;
391390

392391
//console.log('Event Listeners set: ', formEl, submitButton);
@@ -397,39 +396,8 @@ const FormComponentProcessingTemplate = (args) => {
397396

398397
formEl?.addEventListener('submit', e => {
399398
console.log('Native Form submit', formEl, e);
400-
e.preventDefault();
401-
402399
let formData = new FormData(formEl);
403-
// Data can be added to the formData for submission:
404-
//formData.append("CustomField", "This is some extra data");
405-
406-
/*
407-
* Because formData is an iterable, logging it does not show its name/value pairs.
408-
* There are a number of ways to get at this, however, all of which require
409-
* adding "dom.iterable", to compilerOptions > lib in tsconfig.json to avoid the
410-
* following TypeScript/compile error:
411-
*
412-
* Type 'FormData' must have a '[Symbol.iterator]()' method that returns an iterator.
413-
*/
414-
415-
// List key/value pairs
416-
for(let [name, value] of formData) {
417-
console.log(`${name} =`, value);
418-
}
419-
// Spreading the formData as an array seems to give the same results as above.
420-
console.log('formData (array spread): ',[...formData]);
421-
// This method makes the assumption that object keys are unique and only shows 1 value when they are not.
422-
//console.log('formData as JS Object: ', Object.fromEntries(formData.entries()));
423-
424-
/*
425-
formData.append("CustomField", "This is some extra data");
426-
// formData can be manually submitted
427-
const response = await fetch("stash.php", {
428-
method: "POST",
429-
body: formData,
430-
});
431-
*/
432-
400+
console.log('Native form submit - formData (array spread): ',[...formData]);
433401
});
434402
}, 1000);
435403

@@ -445,4 +413,7 @@ const FormComponentProcessingTemplate = (args) => {
445413
`;
446414
};
447415

448-
export const FormComponentProcessing = FormComponentProcessingTemplate.bind({});
416+
export const FormComponentProcessing = FormComponentProcessingTemplate.bind({});
417+
FormComponentProcessing.args = {
418+
name: 'formComponent'
419+
}

0 commit comments

Comments
 (0)