You can use functions like below in SPFx form customizer to save list form:
In render method:
document.getElementById('save').addEventListener('click', this._onSave.bind(this));
Save function:
private _onSave = async (): Promise<void> => {
// disable all input elements while we're saving the item
this.domElement.querySelectorAll('input').forEach(el => el.setAttribute('disabled', 'disabled'));
// reset previous error message if any
this.domElement.querySelector(`.${styles.error}`).innerHTML = '';
let request: Promise<SPHttpClientResponse>;
const title: string = (document.getElementById('title') as HTMLInputElement).value;
switch (this.displayMode) {
case FormDisplayMode.New:
request = this._createItem(title);
break;
case FormDisplayMode.Edit:
request = this._updateItem(title);
}
const res: SPHttpClientResponse = await request;
if (res.ok) {
// You MUST call this.formSaved() after you save the form.
this.formSaved();
}
else {
const error: { error: { message: string } } = await res.json();
this.domElement.querySelector(`.${styles.error}`).innerHTML = `An error has occurred while saving the item. Please try again. Error: ${error.error.message}`;
this.domElement.querySelectorAll('input').forEach(el => el.removeAttribute('disabled'));
}
}
private _createItem(title: string): Promise<SPHttpClientResponse> {
return this.context.spHttpClient
.post(this.context.pageContext.web.absoluteUrl + `/_api/web/lists/getByTitle('${this.context.list.title}')/items`, SPHttpClient.configurations.v1, {
headers: {
'content-type': 'application/json;odata.metadata=none'
},
body: JSON.stringify({
Title: title
})
});
}
private _updateItem(title: string): Promise<SPHttpClientResponse> {
return this.context.spHttpClient
.post(this.context.pageContext.web.absoluteUrl + `/_api/web/lists/getByTitle('${this.context.list.title}')/items(${this.context.itemId})`, SPHttpClient.configurations.v1, {
headers: {
'content-type': 'application/json;odata.metadata=none',
'if-match': this._etag,
'x-http-method': 'MERGE'
},
body: JSON.stringify({
Title: title
})
});
}
Follow this Microsoft official documentation for detailed steps: Build your first Form Customizer extension
Also, check this video for detailed walkthrough about - How to create custom list forms with SharePoint Framework