Skip to content

Commit

Permalink
fix(types): better declaration files
Browse files Browse the repository at this point in the history
This is the first round of fixes/improvements for type declarations of Fomantic. Some properties and methods were missing, some were too restrictive.
  • Loading branch information
prudho authored Oct 5, 2023
1 parent 3564140 commit a03991c
Show file tree
Hide file tree
Showing 16 changed files with 573 additions and 79 deletions.
10 changes: 8 additions & 2 deletions types/fomantic-ui-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ declare namespace FomanticUI {
*/
on: string;

/**
* Object containing all templates endpoints
* @default {}
*/
api: {[key: string]: string};

/**
* Can be set to 'local' to cache successful returned AJAX responses when using a JSON API.
* This helps avoid server roundtrips when API endpoints will return the same results when accessed repeatedly.
Expand All @@ -137,7 +143,7 @@ declare namespace FomanticUI {
* UI state will be applied to this element, defaults to triggering element.
* @default false
*/
stateContext: false | JQuery;
stateContext: false | string | JQuery<any>;

/**
* Whether to encode parameters with 'encodeURIComponent' before adding into url string.
Expand Down Expand Up @@ -259,7 +265,7 @@ declare namespace FomanticUI {
* Method for transmitting request to server.
* @default 'get'
*/
method: 'get' | 'post' | 'put' | 'delete' | 'head' | 'options' | 'patch';
method: Uppercase<'get' | 'post' | 'put' | 'delete' | 'head' | 'options' | 'patch'> | Lowercase<'get' | 'post' | 'put' | 'delete' | 'head' | 'options' | 'patch'>;

/**
* Expected data type of response.
Expand Down
101 changes: 93 additions & 8 deletions types/fomantic-ui-calendar.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ declare namespace FomanticUI {
* Pass false to updateInput to disable updating the input.
* Pass false to fireChange to disable the onBeforeChange and onChange callbacks for this change
*/
(behavior: 'set date', date: string, updateInput: boolean, fireChange: boolean): JQuery;
(behavior: 'set date', date: Date | string | null, updateInput?: boolean, fireChange?: boolean): JQuery;

/**
* Get the current selection mode (year, month, day, hour, minute)
Expand Down Expand Up @@ -82,12 +82,12 @@ declare namespace FomanticUI {
/**
* Set the minimal selectable date
*/
(behavior: 'set minDate', date: Date | string): JQuery;
(behavior: 'set minDate', date: Date | string | null): JQuery;

/**
* Set the maximal selectable date
*/
(behavior: 'set maxDate', date: Date | string): JQuery;
(behavior: 'set maxDate', date: Date | string | null): JQuery;

(behavior: 'destroy'): JQuery;

Expand Down Expand Up @@ -214,7 +214,7 @@ declare namespace FomanticUI {
*
* @default null
*/
initialDate: null | Date;
initialDate: Date | string | null;

/**
* Display mode to start in, can be 'year', 'month', 'day', 'hour', 'minute' (false = 'day').
Expand Down Expand Up @@ -319,12 +319,14 @@ declare namespace FomanticUI {
*
* @default false
*/
selectAdjacentDays: 5 | 10 | 15 | 20 | 30;
selectAdjacentDays: boolean;

popupOptions: Calendar.PopupSettings;

text: Calendar.TextSettings;

formatter: Calendar.FormatterSettings;

// endregion

// region Callbacks
Expand All @@ -333,12 +335,12 @@ declare namespace FomanticUI {
* Is called before a calendar date changes. 'return false;' will cancel the change.
* @since 2.8.0
*/
onBeforeChange(this: JQuery): void;
onBeforeChange(this: JQuery, date?: Date, text?: string, mode?: string): void;

/**
* Is called after a calendar date has changed.
*/
onChange(this: JQuery): void;
onChange(this: JQuery, date?: Date): void;

/**
* Is called before a calendar is shown. 'return false;' will prevent the calendar to be shown.
Expand All @@ -364,7 +366,7 @@ declare namespace FomanticUI {
* Is called when a cell of the calendar is selected providing its value and current mode.
* 'return false;' will prevent the selection.
*/
onSelect(this: JQuery, date: Date, mode: string): void;
onSelect(this: JQuery, date?: Date, mode?: string): void;

// endregion

Expand Down Expand Up @@ -436,6 +438,7 @@ declare namespace FomanticUI {
namespace Calendar {
type PopupSettings = Partial<Pick<Settings.Popup, keyof Settings.Popup>>;
type TextSettings = Partial<Pick<Settings.Texts, keyof Settings.Texts>>;
type FormatterSettings = Partial<Pick<Settings.Formatters, keyof Settings.Formatters>>;
type SelectorSettings = Partial<Pick<Settings.Selectors, keyof Settings.Selectors>>;
type ClassNameSettings = Partial<Pick<Settings.ClassNames, keyof Settings.ClassNames>>;
type RegExpSettings = Partial<Pick<Settings.RegExps, keyof Settings.RegExps>>;
Expand Down Expand Up @@ -471,6 +474,16 @@ declare namespace FomanticUI {
*/
days: string[];

/**
* @default ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
*/
dayNamesShort: string[];

/**
* @default ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
*/
dayNames: string[];

/**
* @default ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
*/
Expand Down Expand Up @@ -507,6 +520,78 @@ declare namespace FomanticUI {
weekNo: string;
}

interface Formatters {
/**
*
*/
yearHeader(date: Date, settings?: CalendarSettings): string;

/**
* @default 'YYYY'
*/
monthHeader: string;

/**
* @default 'MMMM YYYY'
*/
dayHeader: string;

/**
* @default 'MMMM D, YYYY'
*/
hourHeader: string;

/**
* @default 'MMMM D, YYYY'
*/
minuteHeader: string;

/**
* @default 'MMMM D, YYYY'
*/
dayColumnHeader(day: number, settings: CalendarSettings): string;

/**
* @default 'MMMM D, YYYY h:mm A'
*/
datetime: string;

/**
* @default 'MMMM D, YYYY'
*/
date: string;

/**
* @default 'h:mm A'
*/
time: string;

/**
* @default 'h:mm A'
*/
cellTime: string;

/**
* @default 'MMMM YYYY'
*/
month: string;

/**
* @default 'YYYY'
*/
year: string;

/**
*
*/
today(settings: CalendarSettings): string;

/**
*
*/
cell(cell: string, date: Date, cellOptions: any): any
}

interface Selectors {
/**
* @default '.ui.popup'
Expand Down
18 changes: 14 additions & 4 deletions types/fomantic-ui-checkbox.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ declare namespace FomanticUI {
*/
(behavior: 'enable'): JQuery;

/**
* Disable interaction with a checkbox.
*/
(behavior: 'disable'): JQuery;

/**
* Set a checkbox state to checked without callbacks.
*/
Expand Down Expand Up @@ -82,6 +87,11 @@ declare namespace FomanticUI {
*/
(behavior: 'is unchecked'): boolean;

/**
* Returns whether element is not determinate.
*/
(behavior: 'is indeterminate'): boolean;

/**
* Returns whether element is able to be changed.
*/
Expand Down Expand Up @@ -175,22 +185,22 @@ declare namespace FomanticUI {
/**
* Callback before a checkbox is checked. Can cancel change by returning 'false'.
*/
beforeChecked(this: JQuery): void | false;
beforeChecked(this: JQuery): void | Promise<void> | boolean;

/**
* Callback before a checkbox is set to indeterminate. Can cancel change by returning 'false'.
*/
beforeIndeterminate(this: JQuery): void | false;
beforeIndeterminate(this: JQuery): void | Promise<void> | false;

/**
* Callback before a checkbox is set to determinate. Can cancel change by returning 'false'.
*/
beforeDeterminate(this: JQuery): void | false;
beforeDeterminate(this: JQuery): void | Promise<void> | false;

/**
* Callback before a checkbox is unchecked. Can cancel change by returning 'false'.
*/
beforeUnchecked(this: JQuery): void | false;
beforeUnchecked(this: JQuery): void | Promise<void> | false;

/**
* Callback after a checkbox is enabled.
Expand Down
29 changes: 12 additions & 17 deletions types/fomantic-ui-dropdown.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare namespace FomanticUI {
* Recreates dropdown menu from passed values.
* values should be an object with the following structure: { values: [ {value, text, name} ] }.
*/
(behavior: 'setup menu', values: object): void;
(behavior: 'setup menu', values: object): JQuery;

/**
* Changes dropdown to use new values.
Expand All @@ -17,7 +17,7 @@ declare namespace FomanticUI {
/**
* Refreshes all cached selectors and data
*/
(behavior: 'refresh'): void;
(behavior: 'refresh'): JQuery;

/**
* Toggles current visibility of dropdown
Expand All @@ -29,20 +29,20 @@ declare namespace FomanticUI {
* If a function is provided to callback, it's called after the dropdown-menu is shown.
* Set preventFocus to true if you don't want the dropdown field to focus after the menu is shown
*/
(behavior: 'show', callback: Function, preventFocus: boolean): void;
(behavior: 'show', callback?: Function, preventFocus?: boolean): void;

/**
* Hides dropdown.
* If a function is provided to callback, it's called after the dropdown-menu is hidden.
* Set preventBlur to true if you don't want the dropdown field to blur after the menu is hidden
*/
(behavior: 'hide', callback:Function, preventBlur: boolean): void;
(behavior: 'hide', callback?: Function, preventBlur?: boolean): void;

/**
* Clears dropdown of selection.
* Set preventChangeTrigger to true to omit the change event (default: false).
*/
(behavior: 'clear', preventChangeTrigger: boolean): void;
(behavior: 'clear', preventChangeTrigger?: boolean): JQuery;

/**
* Hides all other dropdowns that is not current dropdown
Expand All @@ -53,7 +53,7 @@ declare namespace FomanticUI {
* Restores dropdown text and value to its value on page load.
* Set preventChangeTrigger to true to omit the change event (default: false).
*/
(behavior: 'restore defaults', preventChangeTrigger: boolean): void;
(behavior: 'restore defaults', preventChangeTrigger?: boolean): void;

/**
* Restores dropdown text to its value on page load
Expand All @@ -79,33 +79,28 @@ declare namespace FomanticUI {
* Sets value as selected.
* Set preventChangeTrigger to true to omit the change event (default: false).
*/
(behavior: 'set selected', value: string, preventChangeTrigger: boolean): void;
(behavior: 'set selected', value: string | string[], preventChangeTrigger?: boolean, keepSearchTerm?: boolean): JQuery;

/**
* Remove value from selected
*/
(behavior: 'remove selected', value: string): void;

/**
* Adds a group of values as selected
*/
(behavior: 'set selected', values: string[]): void;

/**
* Sets selected values to exactly specified values, removing current selection
*/
(behavior: 'set exactly', values: string[]): void;
(behavior: 'set exactly', values: string[]): JQuery;

/**
* Sets dropdown text to a value
*/
(behavior: 'text', text: string): void;
(behavior: 'set text', text: string): JQuery;

/**
* Sets dropdown input to value (does not update display state).
* Set preventChangeTrigger to true to omit the change event (default: false).
*/
(behavior: 'set value', value: string, preventChangeTrigger: boolean): void;
(behavior: 'set value', value: string, preventChangeTrigger?: boolean): JQuery;

/**
* Returns current dropdown text
Expand Down Expand Up @@ -265,7 +260,7 @@ declare namespace FomanticUI {
* @see {@link https://fomantic-ui.com/behaviors/api.html#/settings}
* @default false
*/
apiSettings: false | APISettings | JQueryAjaxSettings;
apiSettings: false | Partial<Pick<APISettings, keyof APISettings>> | Partial<Pick<JQueryAjaxSettings, keyof JQueryAjaxSettings>>;

/**
* Whether dropdown should select new option when using keyboard shortcuts.
Expand Down Expand Up @@ -506,7 +501,7 @@ declare namespace FomanticUI {
* Is called after a dropdown value changes.
* Receives the name and value of selection and the active menu element.
*/
onChange(value: string, text: string, $choice: JQuery): void;
onChange(value?: string, text?: string, $choice?: JQuery): void;

/**
* Is called after a dropdown selection is added using a multiple select dropdown, only receives the added value.
Expand Down
Loading

0 comments on commit a03991c

Please sign in to comment.