primeng-tri-state-checkbox
Version:
A TypeScript utility library for tri-state checkbox functionality in Angular applications
129 lines (128 loc) • 3.82 kB
JavaScript
// src/tri-state.ts
function cycleTriState(currentValue) {
if (currentValue === null) return true;
if (currentValue === true) return false;
return null;
}
function turnToTriState(input) {
if (input && typeof input === "object" && "setValue" in input && "value" in input) {
const formControl = input;
formControl.setValue(cycleTriState(formControl.value));
return void 0;
}
return cycleTriState(input);
}
function getPrimeNGTriStateProps(value) {
return {
value,
indeterminate: value === null,
binary: true
};
}
function getTriStateDisplay(value) {
return {
checked: value === true,
indeterminate: value === null
};
}
function triStateToString(value) {
if (value === null) return "null";
return value ? "true" : "false";
}
function stringToTriState(value) {
if (value === "null" || value === "") return null;
if (value === "true") return true;
if (value === "false") return false;
return null;
}
function getTriStateTailwindClasses(value, customClasses) {
const defaultClasses = {
null: "text-yellow-600 bg-yellow-50 border-yellow-300",
true: "text-green-600 bg-green-50 border-green-300",
false: "text-red-600 bg-red-50 border-red-300"
};
const classes = { ...defaultClasses, ...customClasses };
if (value === null) return classes.null || "";
if (value === true) return classes.true || "";
return classes.false || "";
}
function getTriStateIcon(value, iconSet = "pi") {
if (iconSet === "pi") {
if (value === null) return "pi pi-minus";
if (value === true) return "pi pi-check";
return "pi pi-times";
} else {
if (value === null) return "fa fa-minus";
if (value === true) return "fa fa-check";
return "fa fa-times";
}
}
// src/primeng-utils.ts
function handlePrimeNGTriStateChange(event, formControl) {
const newValue = event.checked;
if (formControl.value === null && newValue === true) {
formControl.setValue(true);
} else if (formControl.value === true && newValue === false) {
formControl.setValue(false);
} else if (formControl.value === false && newValue === false) {
formControl.setValue(null);
} else {
formControl.setValue(cycleTriState(formControl.value));
}
}
function getPrimeNGTriStateConfig(value, inputId, name) {
return {
value,
inputId,
name,
indeterminate: value === null,
binary: true
};
}
function getTriStateLabel(value, labels) {
const defaultLabels = {
null: "Unknown",
true: "Yes",
false: "No"
};
const finalLabels = { ...defaultLabels, ...labels };
if (value === null) return finalLabels.null || "";
if (value === true) return finalLabels.true || "";
return finalLabels.false || "";
}
function generatePrimeNGTriStateTemplate(fieldName, config = {}) {
const inputId = config.inputId || fieldName;
const name = config.name || fieldName;
const label = config.label || `Has ${fieldName}?`;
const containerClasses = config.containerClasses || "flex items-center gap-2";
const labelClasses = config.labelClasses || "whitespace-nowrap text-sm";
return `
<div class="${containerClasses}">
<p-checkbox
[value]="form.controls.${fieldName}.value"
inputId="${inputId}"
name="${name}"
[indeterminate]="form.controls.${fieldName}.value === null"
[binary]="true"
(onChange)="turnToTriState(form.controls.${fieldName})" />
<label class="${labelClasses}" for="${inputId}">
${label}
</label>
</div>`.trim();
}
export {
cycleTriState,
turnToTriState as default,
generatePrimeNGTriStateTemplate,
getPrimeNGTriStateConfig,
getPrimeNGTriStateProps,
getTriStateDisplay,
getTriStateIcon,
getTriStateLabel,
getTriStateTailwindClasses,
handlePrimeNGTriStateChange,
stringToTriState,
triStateToString,
turnToTriState
};
//# sourceMappingURL=index.mjs.map