Skip to content

Commit

Permalink
fix: input checkbox value should always be included even when null (#158
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ghiscoding authored Nov 10, 2023
1 parent 63fe04b commit 4d33cff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/src/utils/domUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function createDomStructure(item: HtmlStruct, appendToElm?: HTMLElement,
delete item.props.innerHTML;
}

const elm = createDomElement(item.tagName, objectRemoveEmptyProps(item.props), appendToElm);
const elm = createDomElement(item.tagName, objectRemoveEmptyProps(item.props, ['class', 'title', 'style']), appendToElm);
let parent: HTMLElement | null | undefined = parentElm;
if (!parent) {
parent = elm;
Expand Down
14 changes: 13 additions & 1 deletion lib/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,20 @@ export function isDefined(val: any) {
return val !== undefined && val !== null && val !== '';
}

export function objectRemoveEmptyProps(obj: any) {
/**
* Remove all empty props from an object,
* we can optionally provide a fixed list of props to consider for removal (anything else will be excluded)
* @param {*} obj
* @param {Array<String>} [clearProps] - optional list of props to consider for removal (anything else will be excluded)
* @returns cleaned object
*/
export function objectRemoveEmptyProps(obj: any, clearProps?: string[]) {
if (typeof obj === 'object') {
if (clearProps) {
return Object.fromEntries(
Object.entries(obj).filter(([name, val]) => (!isDefined(val) && !clearProps.includes(name)) || isDefined(val))
);
}
return Object.fromEntries(Object.entries(obj).filter(([_, v]) => isDefined(v)));
}
return obj;
Expand Down

0 comments on commit 4d33cff

Please sign in to comment.