Skip to content

Commit

Permalink
DataField#toInput updates
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeAbby committed Nov 6, 2024
1 parent f2bb3c1 commit e28f5c2
Show file tree
Hide file tree
Showing 7 changed files with 358 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ export default class HTMLColorPickerElement extends AbstractFormInputElement<str
/**
* Create a HTMLColorPickerElement using provided configuration data.
*/
static create(config: FormInputConfig): HTMLColorPickerElement;
static create(config: FormInputConfig<unknown>): HTMLColorPickerElement;
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ declare class HTMLDocumentTagsElement extends AbstractFormInputElement<Record<st
}

declare namespace HTMLDocumentTagsElement {
interface DocumentTagsInputConfig extends FormInputConfig {
interface DocumentTagsInputConfig extends FormInputConfig<unknown> {
/**
* A specific document type in CONST.ALL_DOCUMENT_TYPES
*/
Expand Down
97 changes: 59 additions & 38 deletions src/foundry/client-esm/applications/forms/fields.d.mts
Original file line number Diff line number Diff line change
@@ -1,58 +1,68 @@
import type { InexactPartial } from "../../../../types/utils.d.mts";
import type { NullishProps } from "../../../../types/utils.d.mts";

export type CustomFormGroup = (
field: foundry.data.fields.DataField,
groupConfig: FormGroupConfig,
inputConfig: FormInputConfig,
inputConfig: FormInputConfig<unknown>,
) => HTMLDivElement;

export type CustomFormInput = (
field: foundry.data.fields.DataField,
config: FormInputConfig,
config: FormInputConfig<unknown>,
) => HTMLElement | HTMLCollection;

export interface FormGroupConfig {
/**
* A text label to apply to the form group
*/
label: string;
interface _FormGroupConfig {
/**
* An optional units string which is appended to the label
*/
units?: string;
/**
* An HTML element or collection of elements which provide the inputs
* for the group
*/
input: HTMLElement | HTMLCollection;
units: string;

/**
* Hint text displayed as part of the form group
*/
hint?: string;
hint: string | null | undefined;

/**
* Some parent CSS id within which field names are unique. If provided,
* this root ID is used to automatically assign "id" attributes to input
* elements and "for" attributes to corresponding labels
*/
rootId?: string;
rootId: string | null | undefined;

/**
* An array of CSS classes applied to the form group element
*/
classes?: string[];
classes: string[] | null | undefined;

/**
* Is the "stacked" class applied to the form group
*/
stacked?: boolean;
stacked: boolean | null | undefined;

/**
* Should labels or other elements within this form group be
* automatically localized?
*/
localize?: boolean;
localize: boolean | null | undefined;

/**
* A custom form group widget function which replaces the default
* group HTML generation
*/
widget?: CustomFormGroup;
widget: CustomFormGroup | null | undefined;
}

export interface FormGroupConfig extends NullishProps<_FormGroupConfig> {
/**
* A text label to apply to the form group
*/
label: string;

/**
* An HTML element or collection of elements which provide the inputs
* for the group
*/
input: HTMLElement | HTMLCollection;
}

interface _FormInputConfig<FormInputValue = unknown> {
Expand Down Expand Up @@ -104,11 +114,16 @@ interface _FormInputConfig<FormInputValue = unknown> {
input: CustomFormInput;
}

export interface FormInputConfig<FormInputValue = unknown> extends InexactPartial<_FormInputConfig<FormInputValue>> {
export interface FormInputConfig<FormInputValue> extends NullishProps<_FormInputConfig<FormInputValue>> {
/**
* The name of the form element
*
* @remarks
*
* An explicit `undefined` is not allowed here because of the line
* `{name: this.fieldPath, ...config};` in `DataField#toInput`.
*/
name: string;
name?: string;
}

/**
Expand All @@ -135,12 +150,12 @@ export interface EditorInputConfig extends FormInputConfig<string> {
*/
export function createEditorInput(config: EditorInputConfig): HTMLDivElement;

interface MultiSelectInputConfig extends FormInputConfig<string[]>, Omit<SelectInputConfig, "blank"> {}

/**
* Create a `<multi-select>` element for a StringField.
*/
export function createMultiSelectInput(
config: FormInputConfig<string[]> & Omit<SelectInputConfig, "blank">,
): HTMLSelectElement;
export function createMultiSelectInput(config: MultiSelectInputConfig): HTMLSelectElement;

export interface NumberInputConfig extends FormInputConfig<number> {
min?: number;
Expand All @@ -163,44 +178,50 @@ export interface FormSelectOption {
rule?: boolean;
}

// Compatible with multiple different types of FormInputConfig so this does *not* extend that interface
export interface SelectInputConfig {
options: FormSelectOption[];

interface _SelectInputConfig {
/**
* An option to control the order and display of optgroup elements. The order of
* strings defines the displayed order of optgroup elements.
* A blank string may be used to define the position of ungrouped options.
* If not defined, the order of groups corresponds to the order of options.
*/
groups?: string[];
groups: string[];

blank?: string;
blank: string;

/**
* An alternative value key of the object passed to the options array
*/
valueAttr?: string;
valueAttr: string;

/**
* An alternative label key of the object passed to the options array
*/
labelAttr?: string;
labelAttr: string;

/**
* Localize value labels
*
* @defaultValue `false`
*/
localize?: boolean;
localize: boolean;

/**
* Sort options alphabetically by label within groups
*
* @defaultValue `false`
*/
sort?: boolean;
sort: boolean;

/**
* Customize the type of select that is created
*/
type?: "single" | "multi" | "checkboxes";
type: "single" | "multi" | "checkboxes";
}

// Compatible with multiple different types of FormInputConfig so this does *not* extend that interface
export interface SelectInputConfig extends NullishProps<_SelectInputConfig> {
options: FormSelectOption[];
}

export interface TextAreaInputConfig extends FormInputConfig<string> {
Expand Down Expand Up @@ -231,7 +252,7 @@ export function createTextInput(config: FormInputConfig<string>): HTMLInputEleme
* const optgroups = foundry.applications.fields.prepareSelectOptionGroups({options, groups, blank: true, sort: true});
* ```
*/
export function prepareSelectOptionGroups(config: FormInputConfig & SelectInputConfig): {
export function prepareSelectOptionGroups(config: FormInputConfig<unknown> & SelectInputConfig): {
group: string;
options: FormSelectOption[];
}[];
Expand All @@ -241,4 +262,4 @@ export function prepareSelectOptionGroups(config: FormInputConfig & SelectInputC
* @param input - The element being configured
* @param config - Configuration for the element
*/
export function setInputAttributes(input: HTMLElement, config: FormInputConfig<any>): void;
export function setInputAttributes(input: HTMLElement, config: FormInputConfig<unknown>): void;
Loading

0 comments on commit e28f5c2

Please sign in to comment.