Skip to content

Commit

Permalink
fix: fix getting value from radio input fp-126
Browse files Browse the repository at this point in the history
  • Loading branch information
what1s1ove committed Dec 18, 2023
1 parent 0e02950 commit c31dc62
Show file tree
Hide file tree
Showing 14 changed files with 262 additions and 195 deletions.
3 changes: 2 additions & 1 deletion src/libs/enums/control-element-type.enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ const ControlElementType = /** @type {const} */ ({
URL: 'url',
TEL: 'tel',
COLOR: 'color',
RADIO: 'radio',
HIDDEN: 'hidden',

EMAIL: 'email',

RADIO: 'radio',

NUMBER: 'number',
RANGE: 'range',

Expand Down
11 changes: 11 additions & 0 deletions src/libs/helpers/check-is-one-of/check-is-one-of.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @template {unknown} T
* @param {unknown} value
* @param {...T} validValues
* @returns {value is validValues[number]}
*/
const checkIsOnOf = (value, ...validValues) => {
return validValues.includes(/** @type {T} */ (value));
};

export { checkIsOnOf };
1 change: 1 addition & 0 deletions src/libs/helpers/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { checkIsOnOf } from './check-is-one-of/check-is-one-of.helper.js';
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getFileControlElementValue,
getMultiselectControlElementValue,
getNumericControlElementValue,
getRadioControlElementValue,
} from './helpers/helpers.js';

/** @typedef {import('../../libs/types/types.js').HTMLFormOperationalControlElement} HTMLFormOperationalControlElement */
Expand All @@ -36,7 +37,6 @@ const getFormControlPayload = (controlElement) => {
case ControlElementType.URL:
case ControlElementType.TEL:
case ControlElementType.COLOR:
case ControlElementType.RADIO:
case ControlElementType.HIDDEN:
case ControlElementType.TEXTAREA:
case ControlElementType.SELECT_ONE:
Expand All @@ -59,6 +59,13 @@ const getFormControlPayload = (controlElement) => {
)
);
}
case ControlElementType.RADIO: {
return /** @type {T} */ (
getRadioControlElementValue(
/** @type {HTMLInputElement} */ (controlElement),
)
);
}
case ControlElementType.NUMBER:
case ControlElementType.RANGE: {
return /** @type {T} */ (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* @param {HTMLInputElement
* | HTMLOutputElement
* | HTMLTextAreaElement
* | HTMLSelectElement} element
* | HTMLSelectElement
* | RadioNodeList} element
* @returns {string}
*/
const getControlElementValue = (element) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getControlElementValue } from '../get-control-element-value/get-control-element-value.helper.js';

const EMAIL_SEPARATOR = ',';
const EMAIL_SEPARATOR = /** @type {const} */ (',');

/**
* @param {HTMLInputElement} element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { VALUE_AS_ARRAY_IDENTIFIER } from '../../../../libs/constants/constants.
import { getFormControlElementsPayload } from '../get-form-control-elements-payload/get-form-control-elements-payload.js';

/** @typedef {import('../../../../libs/types/types.js').HTMLFormOperationalControlElement} HTMLFormOperationalControlElement */
/** @typedef {import('../../../../libs/types/types.js').HTMLFormControlElement} HTMLFormControlElement */

/**
* @template {Record<string, unknown>} T
Expand All @@ -16,10 +15,9 @@ const getFieldsetControlElementValue = (
getFormControlElementPayloadCallback,
element,
) => {
const elements = [.../** @type {HTMLFieldSetElement} */ (element).elements];
const fieldsetValue = getFormControlElementsPayload(
getFormControlElementPayloadCallback,
.../** @type {HTMLFormControlElement[]} */ (elements),
element.elements,
);

const hasArrayValue = element.name.endsWith(VALUE_AS_ARRAY_IDENTIFIER);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ControlElementType } from '../../../../libs/enums/enums.js';
import { checkIsOnOf } from '../../../../libs/helpers/helpers.js';
import { checkHasValueAsArray } from '../check-has-value-as-array/check-has-value-as-array.helper.js';
import { checkIsReferToAnotherElement } from '../check-is-refer-to-another-element/check-is-refer-to-another-element.helper.js';
import { getControlElementValue } from '../get-control-element-value/get-control-element-value.helper.js';
import { getOperationalControlElements } from '../get-operational-control-elements/get-operational-control-elements.helper.js';
import { normalizeValueAsArrayControlElementName } from '../normalize-value-as-array-control-element-name/normalize-value-as-array-control-element-name.helper.js';

Expand All @@ -11,13 +14,17 @@ import { normalizeValueAsArrayControlElementName } from '../normalize-value-as-a
* @param {<T extends unknown>(
* element: HTMLFormOperationalControlElement,
* ) => T} getFormControlElementPayloadCallback
* @param {...HTMLFormControlElement} controlElements
* @param {HTMLFormControlsCollection} elements
* @returns {T}
*/
const getFormControlElementsPayload = (
getFormControlElementPayloadCallback,
...controlElements
elements,
) => {
const controlElements = /** @type {HTMLFormControlElement[]} */ ([
...elements,
]);

const operationalControlElements =
getOperationalControlElements(controlElements);

Expand All @@ -33,6 +40,34 @@ const getFormControlElementsPayload = (
continue;
}

const isRadioNodeList = checkIsOnOf(
operationalControlElement.type,
ControlElementType.RADIO,
);

if (isRadioNodeList) {
const hasValue = Boolean(
elementsValues[operationalControlElement.name],
);

if (!hasValue) {
const key = /** @type {keyof T} */ (
operationalControlElement.name
);
const value = /** @type {T[keyof T]} */ (
getControlElementValue(
/** @type {RadioNodeList} */ (
elements.namedItem(operationalControlElement.name)
),
)
);

elementsValues[key] = value;
}

continue;
}

const hasValueAsArray = checkHasValueAsArray(operationalControlElement);

if (hasValueAsArray) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
/** @typedef {import('../../../../libs/types/types.js').HTMLFormControlElement} HTMLFormControlElement */
/** @typedef {import('../../../../libs/types/types.js').HTMLFormOperationalControlElement} HTMLFormOperationalControlElement */

const OPERATIONAL_CONTROL_ELEMENT_CHECKERS = [
const OPERATIONAL_CONTROL_ELEMENT_CHECKERS = /** @type {const} */ ([
checkHasControlElementName,
checkIsAllowedControlElement,
checkIsAllowedControlElementType,
];
]);

/**
* @param {HTMLFormControlElement[]} elements
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const UNCHECKED_RADIO_CONTROL_ELEMENT_VALUE = /** @type {const} */ ('');

/**
* @param {HTMLInputElement} element
* @returns {string}
*/
const getRadioControlElementValue = (element) => {
return element.checked
? element.value
: UNCHECKED_RADIO_CONTROL_ELEMENT_VALUE;
};

export { getRadioControlElementValue };
1 change: 1 addition & 0 deletions src/packages/get-form-control-payload/helpers/helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 4 additions & 7 deletions src/packages/get-form-payload/get-form-payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ import {
getFormControlPayload,
} from '../get-form-control-payload/get-form-control-payload.js';

/** @typedef {import('../../libs/types/types.js').HTMLFormControlElement} HTMLFormControlElement */

/**
* @template {Record<string, unknown>} T
* @param {HTMLFormElement} formElement
* @returns {T}
*/
const getFormPayload = (formElement) => {
const elements = /** @type {HTMLFormControlElement[]} */ ([
...formElement.elements,
]);

return getFormControlElementsPayload(getFormControlPayload, ...elements);
return getFormControlElementsPayload(
getFormControlPayload,
formElement.elements,
);
};

export { getFormPayload };
Loading

0 comments on commit c31dc62

Please sign in to comment.