generated from finsweet/developer-starter
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created new
setFormFieldValue
method that replaces the `selectInput…
…Element` one.
- Loading branch information
1 parent
165e7dc
commit 095d015
Showing
4 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { simulateEvent } from '.'; | ||
|
||
import type { FormField } from '..'; | ||
|
||
/** | ||
* Sets a value to a FormField element and emits `click`, `input` and `change` Events. | ||
* | ||
* @param element The FormField to update. | ||
* @param value `boolean` for Checkboxes and Radios, `string` for the rest. | ||
*/ | ||
export const setFormFieldValue = (element: FormField, value: string | boolean): void => { | ||
const { type } = element; | ||
|
||
if (typeof value === 'boolean') { | ||
if ( | ||
!(element instanceof HTMLInputElement) || | ||
(type !== 'radio' && type !== 'checkbox') || | ||
(type === 'checkbox' && value === false) || | ||
value === element.checked | ||
) | ||
return; | ||
|
||
element.checked = value; | ||
} else { | ||
if (type === 'radio' || type === 'checkbox' || element.value === value) return; | ||
|
||
element.value = value; | ||
} | ||
|
||
// Emit DOM events | ||
simulateEvent(element, ['click', 'input', 'change']); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters