@justeattakeaway/pie-button 0.31.0 → 0.32.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -20,7 +20,8 @@
20
20
  - [HTML example](#html)
21
21
  - [Vue example (using Nuxt 3)](#vue-templates-using-nuxt-3)
22
22
  - [React example (using Next 13)](#react-templates-using-next-13)
23
- 8. [Testing](#testing)
23
+ 6. [Forms usage](#forms-usage)
24
+ 7. [Testing](#testing)
24
25
  - [Browser Tests](#browser-tests)
25
26
  - [Visual Tests](#visual-tests)
26
27
 
@@ -96,15 +97,23 @@ yarn dev --filter=pie-storybook
96
97
 
97
98
  ## Props
98
99
 
99
- | Property | Type | Default | Description |
100
- |-------------|-----------|-----------|-------------------------------------------------------------------------------------------------------------------|
101
- | size | `String` | `medium` | Size of the button, one of `sizes` – `xsmall`, `small-expressive`, `small-productive`, `medium`, `large` |
102
- | type | `String` | `submit` | Type of the button, one of `types` – `submit`, `button`, `reset`, `menu` |
103
- | variant | `String` | `primary` | Variant of the button, one of `variants` – `primary`, `secondary`, `outline`, `ghost`, `destructive`, `destructive-ghost`, `inverse`, `ghost-inverse` or `outline-inverse` |
104
- | disabled | `Boolean` | `false` | If `true`, disables the button. |
105
- | isFullWidth | `Boolean` | `false` | If `true`, sets the button width to 100% of its container. |
106
- | isLoading | `Boolean` | `false` | If `true`, displays a loading indicator inside the button. |
107
- | iconPlacement | `String` | `leading` | Icon placements of the icon slot, if provided, one of `iconPlacements` - `leading`, `trailing` |
100
+ | Property | Type | Default | Description |
101
+ |----------------|-----------|------------|----------------------------------------------------------------------------------------------------------------------|
102
+ | size | `String` | `medium` | Size of the button, one of `sizes` – `xsmall`, `small-expressive`, `small-productive`, `medium`, `large` |
103
+ | type | `String` | `submit` | Type of the button, one of `types` – `submit`, `button`, `reset`. [Read further on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-type)|
104
+ | variant | `String` | `primary` | Variant of the button, one of `variants` – `primary`, `secondary`, `outline`, `ghost`, `destructive`, `destructive-ghost`, `inverse`, `ghost-inverse`, `outline-inverse` |
105
+ | disabled | `Boolean` | `false` | If `true`, disables the button. |
106
+ | isFullWidth | `Boolean` | `false` | If `true`, sets the button width to 100% of its container. |
107
+ | isLoading | `Boolean` | `false` | If `true`, displays a loading indicator inside the button. |
108
+ | iconPlacement | `String` | `leading` | Icon placements of the icon slot, if provided, one of `iconPlacements` - `leading`, `trailing` |
109
+ | name | `String` | `undefined`| The name of the button, to be submitted with form data. [Read further on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-name) |
110
+ | value | `String` | `undefined`| The value of the button, to be submitted with form data. [Read further on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-value) |
111
+ | formaction | `String` | `undefined`| Designates an alternative URL for form data submission. [Read further on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formaction) |
112
+ | formenctype | `String` | `undefined`| Specifies the form data encoding type. [Read further on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formenctype) |
113
+ | formmethod | `String` | `undefined`| Sets the HTTP method for form data. [Read further on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formmethod) |
114
+ | formnovalidate | `Boolean` | `undefined` | Ensures the form is submitted without native HTML validation. [Read further on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formnovalidate) |
115
+ | formtarget | `String` | `undefined`| Dictates where to display the response after form submission. [Read further on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formtarget) |
116
+
108
117
 
109
118
  In your markup or JSX, you can then use these to set the properties for the `pie-button` component:
110
119
 
@@ -168,6 +177,48 @@ For example, to add a click handler in various templates:
168
177
 
169
178
  ```
170
179
 
180
+ ## Forms usage
181
+
182
+ The `pie-button` web component is designed to integrate with standard HTML forms just like a native HTML button. When positioned inside a form, the component will automatically associate itself, enabling it to directly interact with the form context.
183
+
184
+ ### Button Attributes
185
+
186
+ The `pie-button` provides a set of attributes to customize its behavior within forms:
187
+
188
+ - `type`: Determines the button's function. Set to `submit` for form submissions or `reset` to clear form fields.
189
+ - `formaction`: Designates an alternative URL for form data submission when this specific button is clicked.
190
+ - `formenctype`: Specifies the form data encoding type during submission via this button.
191
+ - `formmethod`: Sets the HTTP method (e.g., GET or POST) for form data when initiated by this button.
192
+ - `formnovalidate`: If present, ensures the form is submitted without validation checks.
193
+ - `formtarget`: Dictates where to display the response after form submission.
194
+
195
+ Please see the [MDN docs]((https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)) for more information on these attributes.
196
+
197
+ ### Integration Example
198
+
199
+ ```html
200
+ <form action="/default-endpoint" method="post">
201
+ <input type="text" name="username" required>
202
+ <pie-button
203
+ type="submit"
204
+ formaction="/alternate-endpoint"
205
+ formenctype="multipart/form-data"
206
+ formmethod="post"
207
+ formnovalidate
208
+ formtarget="_blank">
209
+ Submit
210
+ </pie-button>
211
+ </form>
212
+ ```
213
+
214
+ In this example:
215
+
216
+ - The pie-button, when clicked, will send the form data to /alternate-endpoint instead of the form's default /default-endpoint.
217
+ - It uses the multipart/form-data encoding type for form submission.
218
+ - The form will submit using the POST method.
219
+ - No validation will be performed during submission, thanks to formnovalidate.
220
+ - The form's submission response will be opened in a new browser tab/window because of the formtarget="_blank" attribute.
221
+
171
222
  ## Testing
172
223
 
173
224
  ### Browser tests
package/dist/index.d.ts CHANGED
@@ -8,7 +8,7 @@ export declare interface ButtonProps {
8
8
  */
9
9
  size: typeof sizes[number];
10
10
  /**
11
- * What type attribute should be applied to the button. For example submit, button or menu.
11
+ * What type attribute should be applied to the button. For example submit, button.
12
12
  */
13
13
  type: typeof types[number];
14
14
  /**
@@ -31,8 +31,52 @@ export declare interface ButtonProps {
31
31
  * When true, displays a loading indicator inside the button.
32
32
  */
33
33
  isLoading: boolean;
34
+ /**
35
+ * The name of the button, submitted as a pair with the button's value as part of the form data, when that button is used to submit the form.
36
+ * [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
37
+ */
38
+ name?: string;
39
+ /**
40
+ * Defines the value associated with the button's name when it's submitted with the form data. This value is passed to the server in params when the form is submitted using this button.
41
+ * [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
42
+ */
43
+ value?: string;
44
+ /**
45
+ * The URL that processes the information submitted by the button. Overrides the action attribute of the button's form owner. Does nothing if there is no form owner.
46
+ * [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
47
+ */
48
+ formaction?: string;
49
+ /**
50
+ * If the button is a submit button (it's inside/associated with a <form> and doesn't have type="button"), specifies how to encode the form data that is submitted.
51
+ * If this attribute is specified, it overrides the enctype attribute of the button's form owner.
52
+ * [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
53
+ */
54
+ formenctype?: typeof formEncodingtypes[number];
55
+ /**
56
+ * If the button is a submit button (it's inside/associated with a <form> and doesn't have type="button"), this attribute specifies the HTTP method used to submit the form.
57
+ * If specified, this attribute overrides the method attribute of the button's form owner.
58
+ * [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
59
+ */
60
+ formmethod?: typeof formMethodTypes[number];
61
+ /**
62
+ * If the button is a submit button, this Boolean attribute specifies that the form is not to be validated when it is submitted.
63
+ * If this attribute is specified, it overrides the novalidate attribute of the button's form owner.
64
+ * [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
65
+ */
66
+ formnovalidate?: boolean;
67
+ /**
68
+ * If the button is a submit button, this attribute is an author-defined name or standardized, underscore-prefixed keyword indicating where to display the response from submitting the form.
69
+ * [MDN reference](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes)
70
+ */
71
+ formtarget?: typeof formTargetTypes[number];
34
72
  }
35
73
 
74
+ export declare const formEncodingtypes: readonly ["application/x-www-form-urlencoded", "multipart/form-data", "text/plain"];
75
+
76
+ export declare const formMethodTypes: readonly ["post", "get", "dialog"];
77
+
78
+ export declare const formTargetTypes: readonly ["_self", "_blank", "_parent", "_top"];
79
+
36
80
  export declare const iconPlacements: readonly ["leading", "trailing"];
37
81
 
38
82
  /**
@@ -40,6 +84,10 @@ export declare const iconPlacements: readonly ["leading", "trailing"];
40
84
  * @slot - Default slot
41
85
  */
42
86
  export declare class PieButton extends LitElement implements ButtonProps {
87
+ static formAssociated: boolean;
88
+ private readonly _internals;
89
+ get form(): HTMLFormElement | null;
90
+ constructor();
43
91
  size: ButtonProps['size'];
44
92
  type: ButtonProps['type'];
45
93
  variant: ButtonProps['variant'];
@@ -47,6 +95,23 @@ export declare class PieButton extends LitElement implements ButtonProps {
47
95
  disabled: boolean;
48
96
  isLoading: boolean;
49
97
  isFullWidth: boolean;
98
+ name?: string;
99
+ value?: string;
100
+ formaction: ButtonProps['formaction'];
101
+ formenctype: ButtonProps['formenctype'];
102
+ formmethod: ButtonProps['formmethod'];
103
+ formnovalidate: ButtonProps['formnovalidate'];
104
+ formtarget: ButtonProps['formtarget'];
105
+ /**
106
+ * This method creates an invisible button of the same type as pie-button. It is then clicked, and immediately removed from the DOM.
107
+ * This is done so that we trigger native form actions, such as submit and reset in the browser. The performance impact of adding and removing a single button to the DOM
108
+ * should be neglible, however this should be monitored.
109
+ * This is the only viable way of guaranteeing native button behaviour when using a web component in place of an actual HTML button.
110
+ *
111
+ * TODO: if we need to repeat this logic elsewhere, then we should consider moving this code to a shared class or mixin.
112
+ */
113
+ private _simulateNativeButtonClick;
114
+ private _handleClick;
50
115
  render(): TemplateResult<1>;
51
116
  focus(): void;
52
117
  static styles: CSSResult;
@@ -54,7 +119,7 @@ export declare class PieButton extends LitElement implements ButtonProps {
54
119
 
55
120
  export declare const sizes: readonly ["xsmall", "small-productive", "small-expressive", "medium", "large"];
56
121
 
57
- export declare const types: readonly ["submit", "button", "reset", "menu"];
122
+ export declare const types: readonly ["submit", "button", "reset"];
58
123
 
59
124
  export declare type Variant = typeof variants[number];
60
125