Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix getting value from radio input fp-126 #130

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 /** @type {unknown[]} */ (validValues).includes(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,7 +10,8 @@ import {
getFileControlElementValue,
getMultiselectControlElementValue,
getNumericControlElementValue,
} from './helpers/helpers.js';
getRadioControlElementValue,
} from './libs/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 Expand Up @@ -120,5 +127,5 @@ const getFormControlPayload = (controlElement) => {
});
};

export { getFormControlElementsPayload } from './helpers/helpers.js';
export { getFormControlElementsPayload } from './libs/helpers/helpers.js';
export { getFormControlPayload };
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
VALUE_AS_ARRAY_CUSTOM_CONTROL_ELEMENT_TYPES,
VALUE_AS_ARRAY_IDENTIFIER,
} from '../../../../libs/constants/constants.js';
} from '../../../../../libs/constants/constants.js';

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

/**
* @param {HTMLFormOperationalControlElement} element
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @typedef {import('../../../../libs/types/types.js').HTMLFormOperationalControlElement} HTMLFormOperationalControlElement */
/** @typedef {import('../../../../../libs/types/types.js').HTMLFormOperationalControlElement} HTMLFormOperationalControlElement */

/**
* @param {HTMLFormOperationalControlElement} currentElement
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { VALUE_AS_ARRAY_IDENTIFIER } from '../../../../libs/constants/constants.js';
import { VALUE_AS_ARRAY_IDENTIFIER } from '../../../../../libs/constants/constants.js';

/**
* @param {HTMLInputElement} element
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
@@ -1,8 +1,7 @@
import { VALUE_AS_ARRAY_IDENTIFIER } from '../../../../libs/constants/constants.js';
import { VALUE_AS_ARRAY_IDENTIFIER } from '../../../../../libs/constants/constants.js';
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 */
/** @typedef {import('../../../../../libs/types/types.js').HTMLFormOperationalControlElement} HTMLFormOperationalControlElement */

/**
* @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,23 +1,30 @@
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';

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

/**
* @template {Record<string, unknown>} T
* @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 @@ -4,14 +4,14 @@ import {
checkIsAllowedControlElementType,
} from './helpers/helpers.js';

/** @typedef {import('../../../../libs/types/types.js').HTMLFormControlElement} HTMLFormControlElement */
/** @typedef {import('../../../../libs/types/types.js').HTMLFormOperationalControlElement} HTMLFormOperationalControlElement */
/** @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
@@ -1,4 +1,4 @@
/** @typedef {import('../../../../../../libs/types/types.js').HTMLFormControlElement} HTMLFormControlElement */
/** @typedef {import('../../../../../../../libs/types/types.js').HTMLFormControlElement} HTMLFormControlElement */

/**
* @param {HTMLFormControlElement} element
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BANNED_CONTROL_ELEMENT_TYPES } from '../../../../../../libs/constants/constants.js';
import { BANNED_CONTROL_ELEMENT_TYPES } from '../../../../../../../libs/constants/constants.js';

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

/**
* @param {HTMLFormControlElement} element
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { bannedElementNameToElementInstance } from '../../../../../../libs/maps/maps.js';
import { bannedElementNameToElementInstance } from '../../../../../../../libs/maps/maps.js';

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

/**
* @param {HTMLFormControlElement} element
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 };

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

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { VALUE_AS_ARRAY_IDENTIFIER } from '../../../../libs/constants/constants.js';
import { VALUE_AS_ARRAY_IDENTIFIER } from '../../../../../libs/constants/constants.js';

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

/**
* @param {HTMLFormOperationalControlElement} element
Expand Down
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
Loading