Skip to content

Commit

Permalink
Fix constraint validation in components (#6299)
Browse files Browse the repository at this point in the history
* allow passing anchor target to validate() method of form-associated

* fix validation in TextField

* fix validation in Button

* fix validation in Combobox

* fix validation in NumberField

* fix validation in Search

* fix validation in TextArea

* Change files

* update readme

* inheirt docs for components

* Add API docs for validate() API

* update API report

* remove references to private interface

Co-authored-by: nicholasrice <[email protected]>
  • Loading branch information
nicholasrice and nicholasrice authored Aug 25, 2022
1 parent 8ed6b1a commit 7ca925b
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Fix constraint validation in focus-delegated components",
"packageName": "@microsoft/fast-foundation",
"email": "[email protected]",
"dependentChangeType": "prerelease"
}
9 changes: 7 additions & 2 deletions packages/web-components/fast-foundation/docs/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ export class FASTButton extends FormAssociatedButton {
type: "submit" | "reset" | "button";
// (undocumented)
protected typeChanged(previous: "submit" | "reset" | "button" | void, next: "submit" | "reset" | "button"): void;
validate(): void;
}

// @internal
Expand Down Expand Up @@ -884,6 +885,7 @@ export class FASTCombobox extends FormAssociatedCombobox {
setPositioning(): void;
// @internal
slottedOptionsChanged(prev: Element[] | undefined, next: Element[]): void;
validate(): void;
get value(): string;
set value(next: string);
}
Expand Down Expand Up @@ -1353,6 +1355,7 @@ export class FASTNumberField extends FormAssociatedNumberField {
step: number;
stepDown(): void;
stepUp(): void;
validate(): void;
get valueAsNumber(): number;
set valueAsNumber(next: number);
// @internal
Expand Down Expand Up @@ -1621,6 +1624,7 @@ export class FASTSearch extends FormAssociatedSearch {
spellcheck: boolean;
// (undocumented)
protected spellcheckChanged(): void;
validate(): void;
}

// @internal
Expand Down Expand Up @@ -1886,6 +1890,7 @@ export class FASTTextArea extends FormAssociatedTextArea {
spellcheck: boolean;
// (undocumented)
protected spellcheckChanged(): void;
validate(): void;
}

// @internal
Expand Down Expand Up @@ -1937,6 +1942,7 @@ export class FASTTextField extends FormAssociatedTextField {
// (undocumented)
protected spellcheckChanged(): void;
type: TextFieldType;
validate(): void;
}

// @internal
Expand Down Expand Up @@ -2185,8 +2191,7 @@ export interface FormAssociated extends Omit<ElementInternals_2, "labels"> {
requiredChanged(prev: boolean, next: boolean): void;
// (undocumented)
stopPropagation(e: Event): void;
// (undocumented)
validate(): void;
validate(anchor?: HTMLElement): void;
// (undocumented)
value: string;
// (undocumented)
Expand Down
17 changes: 9 additions & 8 deletions packages/web-components/fast-foundation/src/button/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,15 @@ This component is built with the expectation that focus is delegated to the butt

#### Methods

| Name | Privacy | Description | Parameters | Return | Inherited From |
| ----------------------- | --------- | ----------- | ------------------------------------------------------------------------------------------ | ------ | -------------- |
| `formactionChanged` | protected | | | `void` | |
| `formenctypeChanged` | protected | | | `void` | |
| `formmethodChanged` | protected | | | `void` | |
| `formnovalidateChanged` | protected | | | `void` | |
| `formtargetChanged` | protected | | | `void` | |
| `typeChanged` | protected | | `previous: "submit" or "reset" or "button" or void, next: "submit" or "reset" or "button"` | `void` | |
| Name | Privacy | Description | Parameters | Return | Inherited From |
| ----------------------- | --------- | ------------------------------------------------- | ------------------------------------------------------------------------------------------ | ------ | -------------- |
| `formactionChanged` | protected | | | `void` | |
| `formenctypeChanged` | protected | | | `void` | |
| `formmethodChanged` | protected | | | `void` | |
| `formnovalidateChanged` | protected | | | `void` | |
| `formtargetChanged` | protected | | | `void` | |
| `typeChanged` | protected | | `previous: "submit" or "reset" or "button" or void, next: "submit" or "reset" or "button"` | `void` | |
| `validate` | public | {@inheritDoc (FormAssociated:interface).validate} | | `void` | |

#### Attributes

Expand Down
5 changes: 5 additions & 0 deletions packages/web-components/fast-foundation/src/button/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ export class FASTButton extends FormAssociatedButton {
@observable
public defaultSlottedContent: HTMLElement[];

/** {@inheritDoc (FormAssociated:interface).validate} */
public validate(): void {
super.validate(this.control);
}

/**
* @internal
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ See [listbox-option](/docs/components/listbox-option) for more information.

| Name | Privacy | Description | Parameters | Return | Inherited From |
| -------------------- | --------- | -------------------------------------------------------------------------- | ---------------------------------------------------------------------- | ------ | -------------- |
| `validate` | public | {@inheritDoc (FormAssociated:interface).validate} | | `void` | |
| `positionChanged` | protected | | `prev: SelectPosition or undefined, next: SelectPosition or undefined` | `void` | |
| `filterOptions` | public | Filter available options by text value. | | `void` | |
| `setPositioning` | public | Calculate and apply listbox positioning based on available viewport space. | `force` | `void` | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ export class FASTCombobox extends FormAssociatedCombobox {
this.updateValue();
}

/** {@inheritDoc (FormAssociated:interface).validate} */
public validate(): void {
super.validate(this.control);
}

private get isAutocompleteInline(): boolean {
return (
this.autocomplete === ComboboxAutocomplete.inline || this.isAutocompleteBoth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,14 @@ export interface FormAssociated extends Omit<ElementInternals, "labels"> {
nameChanged?(previous: string, next: string): void;
requiredChanged(prev: boolean, next: boolean): void;
stopPropagation(e: Event): void;
validate(): void;

/**
* Sets the validity of the custom element. By default this uses the proxy element to determine
* validity, but this can be extended or replaced in implementation.
*
* @param anchor - The anchor element to provide to ElementInternals.setValidity for surfacing the browser's constraint validation UI
*/
validate(anchor?: HTMLElement): void;
valueChanged(previous: string, next: string): void;
}

Expand Down Expand Up @@ -625,13 +632,14 @@ export function FormAssociated<T extends ConstructableFormAssociated>(BaseCtor:
this.shadowRoot?.removeChild(this.proxySlot as HTMLSlotElement);
}

/**
* Sets the validity of the custom element. By default this uses the proxy element to determine
* validity, but this can be extended or replaced in implementation.
*/
public validate(): void {
/** {@inheritDoc (FormAssociated:interface).validate} */
public validate(anchor?: HTMLElement): void {
if (this.proxy instanceof HTMLElement) {
this.setValidity(this.proxy.validity, this.proxy.validationMessage);
this.setValidity(
this.proxy.validity,
this.proxy.validationMessage,
anchor
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,12 @@ This component is built with the expectation that focus is delegated to the inpu

#### Methods

| Name | Privacy | Description | Parameters | Return | Inherited From |
| ---------- | ------- | ----------------------------------------- | ---------- | ------ | -------------- |
| `stepUp` | public | Increments the value using the step value | | `void` | |
| `stepDown` | public | Decrements the value using the step value | | `void` | |
| `select` | public | Selects all the text in the number field | | `void` | |
| Name | Privacy | Description | Parameters | Return | Inherited From |
| ---------- | ------- | ------------------------------------------------- | ---------- | ------ | -------------- |
| `validate` | public | {@inheritDoc (FormAssociated:interface).validate} | | `void` | |
| `stepUp` | public | Increments the value using the step value | | `void` | |
| `stepDown` | public | Decrements the value using the step value | | `void` | |
| `select` | public | Selects all the text in the number field | | `void` | |

#### Events

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ export class FASTNumberField extends FormAssociatedNumberField {
this.isUserInput = false;
}

/** {@inheritDoc (FormAssociated:interface).validate} */
public validate(): void {
super.validate(this.control);
}

/**
* Sets the internal value to a valid number between the min and max properties
* @param value - user input
Expand Down
25 changes: 13 additions & 12 deletions packages/web-components/fast-foundation/src/search/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,19 @@ This component is built with the expectation that focus is delegated to the inpu

#### Methods

| Name | Privacy | Description | Parameters | Return | Inherited From |
| -------------------- | --------- | --------------------------------------- | ---------- | ------ | -------------- |
| `readOnlyChanged` | protected | | | `void` | |
| `autofocusChanged` | protected | | | `void` | |
| `placeholderChanged` | protected | | | `void` | |
| `listChanged` | protected | | | `void` | |
| `maxlengthChanged` | protected | | | `void` | |
| `minlengthChanged` | protected | | | `void` | |
| `patternChanged` | protected | | | `void` | |
| `sizeChanged` | protected | | | `void` | |
| `spellcheckChanged` | protected | | | `void` | |
| `handleClearInput` | public | Handles the control's clear value event | | `void` | |
| Name | Privacy | Description | Parameters | Return | Inherited From |
| -------------------- | --------- | ------------------------------------------------- | ---------- | ------ | -------------- |
| `readOnlyChanged` | protected | | | `void` | |
| `autofocusChanged` | protected | | | `void` | |
| `placeholderChanged` | protected | | | `void` | |
| `listChanged` | protected | | | `void` | |
| `maxlengthChanged` | protected | | | `void` | |
| `minlengthChanged` | protected | | | `void` | |
| `patternChanged` | protected | | | `void` | |
| `sizeChanged` | protected | | | `void` | |
| `spellcheckChanged` | protected | | | `void` | |
| `validate` | public | {@inheritDoc (FormAssociated:interface).validate} | | `void` | |
| `handleClearInput` | public | Handles the control's clear value event | | `void` | |

#### Attributes

Expand Down
5 changes: 5 additions & 0 deletions packages/web-components/fast-foundation/src/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ export class FASTSearch extends FormAssociatedSearch {
}
}

/** {@inheritDoc (FormAssociated:interface).validate} */
public validate(): void {
super.validate(this.control);
}

/**
* Handles the internal control's `input` event
* @internal
Expand Down
19 changes: 10 additions & 9 deletions packages/web-components/fast-foundation/src/text-area/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,16 @@ This component is built with the expectation that focus is delegated to the inpu

#### Methods

| Name | Privacy | Description | Parameters | Return | Inherited From |
| ------------------- | --------- | ------------------------------------- | ---------- | ------ | -------------- |
| `readOnlyChanged` | protected | | | `void` | |
| `autofocusChanged` | protected | | | `void` | |
| `listChanged` | protected | | | `void` | |
| `maxlengthChanged` | protected | | | `void` | |
| `minlengthChanged` | protected | | | `void` | |
| `spellcheckChanged` | protected | | | `void` | |
| `select` | public | Selects all the text in the text area | | `void` | |
| Name | Privacy | Description | Parameters | Return | Inherited From |
| ------------------- | --------- | ------------------------------------------------- | ---------- | ------ | -------------- |
| `readOnlyChanged` | protected | | | `void` | |
| `autofocusChanged` | protected | | | `void` | |
| `listChanged` | protected | | | `void` | |
| `maxlengthChanged` | protected | | | `void` | |
| `minlengthChanged` | protected | | | `void` | |
| `spellcheckChanged` | protected | | | `void` | |
| `select` | public | Selects all the text in the text area | | `void` | |
| `validate` | public | {@inheritDoc (FormAssociated:interface).validate} | | `void` | |

#### Events

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ export class FASTTextArea extends FormAssociatedTextArea {
public handleChange(): void {
this.$emit("change");
}

/** {@inheritDoc (FormAssociated:interface).validate} */
public validate(): void {
super.validate(this.control);
}
}

/**
Expand Down
Loading

0 comments on commit 7ca925b

Please sign in to comment.