primeng-tri-state-checkbox
Version:
A TypeScript utility library for tri-state checkbox functionality in Angular applications
136 lines (133 loc) • 4.91 kB
TypeScript
/**
* Utility function to cycle through tri-state values: null -> true -> false -> null
* @param currentValue - The current boolean or null value
* @returns The next value in the tri-state cycle
*/
declare function cycleTriState(currentValue: boolean | null): boolean | null;
/**
* Type definition for Angular FormControl with tri-state boolean values
*/
interface TriStateFormControl {
value: boolean | null;
setValue(value: boolean | null): void;
}
/**
* Universal tri-state function that can cycle values or update FormControls
* @param input - Either a FormControl to update or a boolean/null value to cycle
* @returns If input is a value, returns the next tri-state value. If input is FormControl, updates it and returns void.
*
* @example
* ```typescript
* onTriStateChange(control: any): void {
* turnToTriState(control);
* }
* ```
*
* ```html
* <p-checkbox
* [value]="form.controls.isActive.value"
* inputId="isActive"
* name="isActive"
* [indeterminate]="form.controls.isActive.value === null"
* [binary]="true"
* (onChange)="onTriStateChange(form.controls.isActive)" />
* ```
*/
declare function turnToTriState<T extends TriStateFormControl | boolean | null>(input: T): T extends TriStateFormControl ? void : boolean | null;
/**
* Returns the display state for PrimeNG tri-state checkbox
* @param value - The current tri-state value
* @returns Object with value and indeterminate properties for PrimeNG p-checkbox
*/
declare function getPrimeNGTriStateProps(value: boolean | null): {
value: boolean | null;
indeterminate: boolean;
binary: boolean;
};
/**
* Returns the display state for tri-state checkbox
* @param value - The current tri-state value
* @returns Object with checked and indeterminate properties
*/
declare function getTriStateDisplay(value: boolean | null): {
checked: boolean;
indeterminate: boolean;
};
/**
* Converts a tri-state value to a string representation
* @param value - The tri-state value
* @returns String representation of the state
*/
declare function triStateToString(value: boolean | null): 'true' | 'false' | 'null';
/**
* Parses a string to tri-state value
* @param value - String representation of tri-state
* @returns The tri-state boolean value
*/
declare function stringToTriState(value: string): boolean | null;
/**
* Gets Tailwind CSS classes for tri-state visual feedback
* @param value - The current tri-state value
* @param customClasses - Optional custom classes for each state
* @returns CSS classes string
*/
declare function getTriStateTailwindClasses(value: boolean | null, customClasses?: {
null?: string;
true?: string;
false?: string;
}): string;
/**
* Returns an icon class name based on tri-state value (for use with PrimeNG icons)
* @param value - The current tri-state value
* @param iconSet - Icon set to use ('pi' for PrimeIcons, 'fa' for FontAwesome)
* @returns Icon class string
*/
declare function getTriStateIcon(value: boolean | null, iconSet?: 'pi' | 'fa'): string;
/**
* PrimeNG-specific tri-state checkbox utilities
*/
/**
* Event handler for PrimeNG p-checkbox onChange event
* @param event - The PrimeNG checkbox change event
* @param formControl - The FormControl to update
*/
declare function handlePrimeNGTriStateChange(event: any, formControl: TriStateFormControl): void;
/**
* Returns PrimeNG template configuration for tri-state checkbox
* @param value - Current tri-state value
* @param inputId - HTML input ID
* @param name - Input name attribute
* @returns Configuration object for PrimeNG p-checkbox
*/
declare function getPrimeNGTriStateConfig(value: boolean | null, inputId: string, name: string): {
value: boolean | null;
inputId: string;
name: string;
indeterminate: boolean;
binary: boolean;
};
/**
* Returns the label text based on tri-state value
* @param value - Current tri-state value
* @param labels - Custom labels for each state
* @returns Label text
*/
declare function getTriStateLabel(value: boolean | null, labels?: {
null?: string;
true?: string;
false?: string;
}): string;
/**
* Generates a complete PrimeNG checkbox template string
* @param fieldName - Form control field name
* @param config - Configuration options
* @returns HTML template string for PrimeNG checkbox
*/
declare function generatePrimeNGTriStateTemplate(fieldName: string, config?: {
label?: string;
inputId?: string;
name?: string;
containerClasses?: string;
labelClasses?: string;
}): string;
export { type TriStateFormControl, cycleTriState, turnToTriState as default, generatePrimeNGTriStateTemplate, getPrimeNGTriStateConfig, getPrimeNGTriStateProps, getTriStateDisplay, getTriStateIcon, getTriStateLabel, getTriStateTailwindClasses, handlePrimeNGTriStateChange, stringToTriState, triStateToString, turnToTriState };