add getForm() method; closes #1180

pull/1216/head
Cory LaViska 2023-02-28 12:10:14 -05:00
rodzic 8a1efac9b8
commit 218e78e947
11 zmienionych plików z 57 dodań i 11 usunięć

Wyświetl plik

@ -10,6 +10,7 @@ New versions of Shoelace are released as-needed and generally occur when a criti
- Added TypeScript types to all custom events [#1183](https://github.com/shoelace-style/shoelace/pull/1183) - Added TypeScript types to all custom events [#1183](https://github.com/shoelace-style/shoelace/pull/1183)
- Added the `svg` part to `<sl-icon>` - Added the `svg` part to `<sl-icon>`
- Added the `getForm()` method to all form controls [#1180](https://github.com/shoelace-style/shoelace/issues/1180)
- Fixed a bug in `<sl-select>` that caused the display label to render incorrectly in Chrome after form validation [#1197](https://github.com/shoelace-style/shoelace/discussions/1197) - Fixed a bug in `<sl-select>` that caused the display label to render incorrectly in Chrome after form validation [#1197](https://github.com/shoelace-style/shoelace/discussions/1197)
- Fixed a bug in `<sl-input>` that prevented users from applying their own value for `autocapitalize`, `autocomplete`, and `autocorrect` when using `type="password` [#1205](https://github.com/shoelace-style/shoelace/issues/1205) - Fixed a bug in `<sl-input>` that prevented users from applying their own value for `autocapitalize`, `autocomplete`, and `autocorrect` when using `type="password` [#1205](https://github.com/shoelace-style/shoelace/issues/1205)
- Fixed a bug in `<sl-tab-group>` that prevented scroll controls from showing when dynamically adding tabs [#1208](https://github.com/shoelace-style/shoelace/issues/1208) - Fixed a bug in `<sl-tab-group>` that prevented scroll controls from showing when dynamically adding tabs [#1208](https://github.com/shoelace-style/shoelace/issues/1208)

Wyświetl plik

@ -257,6 +257,11 @@ export default class SlButton extends ShoelaceElement implements ShoelaceFormCon
return true; return true;
} }
/** Gets the associated form, if one exists. */
getForm(): HTMLFormElement | null {
return this.formControlController.getForm();
}
/** Checks for validity and shows the browser's validation message if the control is invalid. */ /** Checks for validity and shows the browser's validation message if the control is invalid. */
reportValidity() { reportValidity() {
if (this.isButton()) { if (this.isButton()) {

Wyświetl plik

@ -158,6 +158,11 @@ export default class SlCheckbox extends ShoelaceElement implements ShoelaceFormC
return this.input.checkValidity(); return this.input.checkValidity();
} }
/** Gets the associated form, if one exists. */
getForm(): HTMLFormElement | null {
return this.formControlController.getForm();
}
/** Checks for validity and shows the browser's validation message if the control is invalid. */ /** Checks for validity and shows the browser's validation message if the control is invalid. */
reportValidity() { reportValidity() {
return this.input.reportValidity(); return this.input.reportValidity();

Wyświetl plik

@ -764,6 +764,11 @@ export default class SlColorPicker extends ShoelaceElement implements ShoelaceFo
return this.input.checkValidity(); return this.input.checkValidity();
} }
/** Gets the associated form, if one exists. */
getForm(): HTMLFormElement | null {
return this.formControlController.getForm();
}
/** Checks for validity and shows the browser's validation message if the control is invalid. */ /** Checks for validity and shows the browser's validation message if the control is invalid. */
reportValidity() { reportValidity() {
if (!this.inline && !this.validity.valid) { if (!this.inline && !this.validity.valid) {

Wyświetl plik

@ -377,6 +377,11 @@ export default class SlInput extends ShoelaceElement implements ShoelaceFormCont
return this.input.checkValidity(); return this.input.checkValidity();
} }
/** Gets the associated form, if one exists. */
getForm(): HTMLFormElement | null {
return this.formControlController.getForm();
}
/** Checks for validity and shows the browser's validation message if the control is invalid. */ /** Checks for validity and shows the browser's validation message if the control is invalid. */
reportValidity() { reportValidity() {
return this.input.reportValidity(); return this.input.reportValidity();

Wyświetl plik

@ -252,12 +252,9 @@ export default class SlRadioGroup extends ShoelaceElement implements ShoelaceFor
return true; return true;
} }
/** Sets a custom validation message. Pass an empty string to restore validity. */ /** Gets the associated form, if one exists. */
setCustomValidity(message = '') { getForm(): HTMLFormElement | null {
this.customValidityMessage = message; return this.formControlController.getForm();
this.errorMessage = message;
this.validationInput.setCustomValidity(message);
this.formControlController.updateValidity();
} }
/** Checks for validity and shows the browser's validation message if the control is invalid. */ /** Checks for validity and shows the browser's validation message if the control is invalid. */
@ -279,6 +276,14 @@ export default class SlRadioGroup extends ShoelaceElement implements ShoelaceFor
return isValid; return isValid;
} }
/** Sets a custom validation message. Pass an empty string to restore validity. */
setCustomValidity(message = '') {
this.customValidityMessage = message;
this.errorMessage = message;
this.validationInput.setCustomValidity(message);
this.formControlController.updateValidity();
}
render() { render() {
const hasLabelSlot = this.hasSlotController.test('label'); const hasLabelSlot = this.hasSlotController.test('label');
const hasHelpTextSlot = this.hasSlotController.test('help-text'); const hasHelpTextSlot = this.hasSlotController.test('help-text');

Wyświetl plik

@ -254,6 +254,11 @@ export default class SlRange extends ShoelaceElement implements ShoelaceFormCont
return this.input.checkValidity(); return this.input.checkValidity();
} }
/** Gets the associated form, if one exists. */
getForm(): HTMLFormElement | null {
return this.formControlController.getForm();
}
/** Checks for validity and shows the browser's validation message if the control is invalid. */ /** Checks for validity and shows the browser's validation message if the control is invalid. */
reportValidity() { reportValidity() {
return this.input.reportValidity(); return this.input.reportValidity();

Wyświetl plik

@ -639,6 +639,11 @@ export default class SlSelect extends ShoelaceElement implements ShoelaceFormCon
return this.valueInput.checkValidity(); return this.valueInput.checkValidity();
} }
/** Gets the associated form, if one exists. */
getForm(): HTMLFormElement | null {
return this.formControlController.getForm();
}
/** Checks for validity and shows the browser's validation message if the control is invalid. */ /** Checks for validity and shows the browser's validation message if the control is invalid. */
reportValidity() { reportValidity() {
return this.valueInput.reportValidity(); return this.valueInput.reportValidity();

Wyświetl plik

@ -163,6 +163,11 @@ export default class SlSwitch extends ShoelaceElement implements ShoelaceFormCon
return this.input.checkValidity(); return this.input.checkValidity();
} }
/** Gets the associated form, if one exists. */
getForm(): HTMLFormElement | null {
return this.formControlController.getForm();
}
/** Checks for validity and shows the browser's validation message if the control is invalid. */ /** Checks for validity and shows the browser's validation message if the control is invalid. */
reportValidity() { reportValidity() {
return this.input.reportValidity(); return this.input.reportValidity();

Wyświetl plik

@ -281,6 +281,11 @@ export default class SlTextarea extends ShoelaceElement implements ShoelaceFormC
return this.input.checkValidity(); return this.input.checkValidity();
} }
/** Gets the associated form, if one exists. */
getForm(): HTMLFormElement | null {
return this.formControlController.getForm();
}
/** Checks for validity and shows the browser's validation message if the control is invalid. */ /** Checks for validity and shows the browser's validation message if the control is invalid. */
reportValidity() { reportValidity() {
return this.input.reportValidity(); return this.input.reportValidity();

Wyświetl plik

@ -95,7 +95,7 @@ export default class ShoelaceElement extends LitElement {
} }
export interface ShoelaceFormControl extends ShoelaceElement { export interface ShoelaceFormControl extends ShoelaceElement {
// Standard form attributes // Form attributes
name: string; name: string;
value: unknown; value: unknown;
disabled?: boolean; disabled?: boolean;
@ -103,7 +103,7 @@ export interface ShoelaceFormControl extends ShoelaceElement {
defaultChecked?: boolean; defaultChecked?: boolean;
form?: string; form?: string;
// Standard validation attributes // Constraint validation attributes
pattern?: string; pattern?: string;
min?: number | Date; min?: number | Date;
max?: number | Date; max?: number | Date;
@ -112,13 +112,13 @@ export interface ShoelaceFormControl extends ShoelaceElement {
minlength?: number; minlength?: number;
maxlength?: number; maxlength?: number;
// Validation properties // Form validation properties
readonly validity: ValidityState; readonly validity: ValidityState;
readonly validationMessage: string; readonly validationMessage: string;
// Validation methods // Form validation methods
checkValidity: () => boolean; checkValidity: () => boolean;
/** Checks for validity and shows the browser's validation message if the control is invalid. */ getForm: () => HTMLFormElement | null;
reportValidity: () => boolean; reportValidity: () => boolean;
setCustomValidity: (message: string) => void; setCustomValidity: (message: string) => void;
} }