@justeattakeaway/pie-checkbox 0.3.1 → 0.5.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
@@ -30,11 +30,9 @@ This component can be easily integrated into various frontend frameworks and cus
30
30
  To install `pie-checkbox` in your application, run the following on your command line:
31
31
 
32
32
  ```bash
33
- # npm
34
- $ npm i @justeattakeaway/pie-checkbox
33
+ npm i @justeattakeaway/pie-checkbox
35
34
 
36
- # yarn
37
- $ yarn add @justeattakeaway/pie-checkbox
35
+ yarn add @justeattakeaway/pie-checkbox
38
36
  ```
39
37
 
40
38
  For full information on using PIE components as part of an application, check out the [Getting Started Guide](https://github.com/justeattakeaway/pie/wiki/Getting-started-with-PIE-Web-Components).
@@ -80,7 +78,8 @@ import { PieCheckbox } from '@justeattakeaway/pie-checkbox/dist/react';
80
78
  | `required` | `boolean` | `false` | If true, the checkbox is required to be checked before submitting the form. If it is not in checked state, the component validity state will be invalid. |
81
79
  | `label` | `string` | '' | Text associated with the checkbox. If there is no label to provide, make sure to pass label, labelledby or describedby to the aria property. |
82
80
  | `disabled` | `boolean` | `false` | Indicates whether or not the checkbox is disabled. |
83
- | `checked` | `boolean` | `false` | Indicates whether or not the checkbox is checked by default (when the page loads). |
81
+ | `checked` | `boolean` | `false` | Controls whether or not the checkbox is checked. |
82
+ | `defaultChecked` | `boolean` | `false` | Sets the default checked state for the checkbox. This does not directly set the initial checked state when the page loads, use `checked` for that. If the checkbox is inside a form which is reset, the `checked` state will be updated to match `defaultChecked`. |
84
83
  | `indeterminate` | `boolean` | `false` | Indicates whether the checkbox visually shows a horizontal line in the box instead of a check/tick. It has no impact on whether the checkbox's value is used in a form submission. That is decided by the checked state, regardless of the indeterminate state. |
85
84
  | `aria` | `object` | {} | accepts `label`, `labeledby` and `describedby` keys with string values. |
86
85
 
@@ -107,4 +106,4 @@ In your markup or JSX, you can then use these to set the properties for the `pie
107
106
 
108
107
  ## Contributing
109
108
 
110
- Check out our [contributing guide](https://github.com/justeattakeaway/pie/wiki/Contributing-Guide) for more information on [local development](https://github.com/justeattakeaway/pie/wiki/Contributing-Guide#local-development) and how to run specific [component tests](https://github.com/justeattakeaway/pie/wiki/Contributing-Guide#testing).
109
+ Check out our [contributing guide](https://github.com/justeattakeaway/pie/wiki/Contributing-Guide) for more information on [local development](https://github.com/justeattakeaway/pie/wiki/Contributing-Guide#local-development) and how to run specific [component tests](https://github.com/justeattakeaway/pie/wiki/Contributing-Guide#testing).
@@ -18,7 +18,7 @@
18
18
  "type": {
19
19
  "text": "DefaultProps"
20
20
  },
21
- "default": "{\n required: false,\n indeterminate: false,\n}"
21
+ "default": "{\n // a default value for the html <input type=\"checkbox\" /> value attribute.\n // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#value\n value: 'on',\n required: false,\n defaultChecked: false,\n indeterminate: false,\n checked: false,\n}"
22
22
  }
23
23
  ],
24
24
  "exports": [
@@ -40,12 +40,6 @@
40
40
  "kind": "class",
41
41
  "description": "",
42
42
  "name": "PieCheckbox",
43
- "slots": [
44
- {
45
- "description": "Default slot (checkbox label)",
46
- "name": ""
47
- }
48
- ],
49
43
  "members": [
50
44
  {
51
45
  "kind": "field",
@@ -59,9 +53,6 @@
59
53
  {
60
54
  "kind": "field",
61
55
  "name": "value",
62
- "type": {
63
- "text": "CheckboxProps['value']"
64
- },
65
56
  "privacy": "public",
66
57
  "attribute": "value"
67
58
  },
@@ -86,11 +77,16 @@
86
77
  {
87
78
  "kind": "field",
88
79
  "name": "checked",
89
- "type": {
90
- "text": "CheckboxProps['checked'] | undefined"
91
- },
92
80
  "privacy": "public",
93
- "attribute": "checked"
81
+ "attribute": "checked",
82
+ "reflects": true
83
+ },
84
+ {
85
+ "kind": "field",
86
+ "name": "defaultChecked",
87
+ "privacy": "public",
88
+ "attribute": "defaultChecked",
89
+ "reflects": true
94
90
  },
95
91
  {
96
92
  "kind": "field",
@@ -119,7 +115,8 @@
119
115
  "text": "CheckboxProps['indeterminate'] | undefined"
120
116
  },
121
117
  "privacy": "public",
122
- "attribute": "indeterminate"
118
+ "attribute": "indeterminate",
119
+ "reflects": true
123
120
  },
124
121
  {
125
122
  "kind": "field",
@@ -134,7 +131,7 @@
134
131
  "kind": "field",
135
132
  "name": "checkbox",
136
133
  "type": {
137
- "text": "HTMLInputElement | undefined"
134
+ "text": "HTMLInputElement"
138
135
  },
139
136
  "privacy": "private"
140
137
  },
@@ -149,27 +146,75 @@
149
146
  "readonly": true
150
147
  },
151
148
  {
152
- "kind": "field",
149
+ "kind": "method",
150
+ "name": "handleFormAssociation",
151
+ "privacy": "private",
152
+ "return": {
153
+ "type": {
154
+ "text": "void"
155
+ }
156
+ },
157
+ "description": "Ensures that the form value is in sync with the component."
158
+ },
159
+ {
160
+ "kind": "method",
161
+ "name": "formDisabledCallback",
162
+ "privacy": "public",
163
+ "return": {
164
+ "type": {
165
+ "text": "void"
166
+ }
167
+ },
168
+ "parameters": [
169
+ {
170
+ "name": "disabled",
171
+ "type": {
172
+ "text": "boolean"
173
+ },
174
+ "description": "The latest disabled state of the input."
175
+ }
176
+ ],
177
+ "description": "Called after the disabled state of the element changes,\neither because the disabled attribute of this element was added or removed;\nor because the disabled state changed on a <fieldset> that's an ancestor of this element."
178
+ },
179
+ {
180
+ "kind": "method",
153
181
  "name": "handleChange",
154
182
  "privacy": "private",
155
- "description": "The onChange function updates the checkbox state and emits an event for consumers.",
156
183
  "parameters": [
157
184
  {
158
- "description": "This should be the change event that was listened for on an input element with `type=\"checkbox\"`.",
159
185
  "name": "event",
160
186
  "type": {
161
187
  "text": "Event"
162
- }
188
+ },
189
+ "description": "This should be the change event that was listened for on an input element with `type=\"checkbox\"`."
190
+ }
191
+ ],
192
+ "description": "Captures the native change event and wraps it in a custom event."
193
+ },
194
+ {
195
+ "kind": "method",
196
+ "name": "formResetCallback",
197
+ "privacy": "public",
198
+ "return": {
199
+ "type": {
200
+ "text": "void"
163
201
  }
164
- ]
202
+ },
203
+ "description": "Called when the form that contains this component is reset.\nIf the current checked state is different to the default checked state,\nthe checked state is reset to the default checked state and a `change` event is emitted."
165
204
  }
166
205
  ],
167
- "attributes": [
206
+ "events": [
168
207
  {
169
- "name": "value",
170
208
  "type": {
171
- "text": "CheckboxProps['value']"
209
+ "text": "CustomEvent"
172
210
  },
211
+ "description": "when checked state is changed.",
212
+ "name": "change"
213
+ }
214
+ ],
215
+ "attributes": [
216
+ {
217
+ "name": "value",
173
218
  "fieldName": "value"
174
219
  },
175
220
  {
@@ -188,11 +233,12 @@
188
233
  },
189
234
  {
190
235
  "name": "checked",
191
- "type": {
192
- "text": "CheckboxProps['checked'] | undefined"
193
- },
194
236
  "fieldName": "checked"
195
237
  },
238
+ {
239
+ "name": "defaultChecked",
240
+ "fieldName": "defaultChecked"
241
+ },
196
242
  {
197
243
  "name": "disabled",
198
244
  "type": {
@@ -223,6 +269,10 @@
223
269
  }
224
270
  ],
225
271
  "mixins": [
272
+ {
273
+ "name": "FormControlMixin",
274
+ "package": "@justeattakeaway/pie-webc-core"
275
+ },
226
276
  {
227
277
  "name": "RtlMixin",
228
278
  "package": "@justeattakeaway/pie-webc-core"
package/dist/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import { ComponentDefaultPropsGeneric } from '@justeattakeaway/pie-webc-core';
2
2
  import type { CSSResult } from 'lit';
3
+ import type { FormControlInterface } from '@justeattakeaway/pie-webc-core';
3
4
  import type { GenericConstructor } from '@justeattakeaway/pie-webc-core';
4
5
  import type { LitElement } from 'lit';
6
+ import type { PropertyValues } from 'lit';
5
7
  import type { RTLInterface } from '@justeattakeaway/pie-webc-core';
6
8
  import type { TemplateResult } from 'lit-html';
7
9
 
@@ -29,7 +31,11 @@ export declare interface CheckboxProps {
29
31
  */
30
32
  disabled?: boolean;
31
33
  /**
32
- * Same as the HTML checked attribute - indicates whether or not the checkbox is checked by default (when the page loads).
34
+ * The default checked state of the checkbox (not necessarily the same as the current checked state).
35
+ */
36
+ defaultChecked?: boolean;
37
+ /**
38
+ * The checked state of the checkbox.
33
39
  */
34
40
  checked?: boolean;
35
41
  /**
@@ -47,13 +53,13 @@ export declare interface CheckboxProps {
47
53
  aria?: AriaProps;
48
54
  }
49
55
 
50
- export declare type DefaultProps = ComponentDefaultPropsGeneric<CheckboxProps, 'required' | 'indeterminate'>;
56
+ export declare type DefaultProps = ComponentDefaultPropsGeneric<CheckboxProps, 'value' | 'required' | 'indeterminate' | 'checked' | 'defaultChecked'>;
51
57
 
52
58
  export declare const defaultProps: DefaultProps;
53
59
 
54
60
  /**
55
61
  * @tagname pie-checkbox
56
- * @slot - Default slot (checkbox label)
62
+ * @event {CustomEvent} change - when checked state is changed.
57
63
  */
58
64
  export declare class PieCheckbox extends PieCheckbox_base implements CheckboxProps {
59
65
  static shadowRootOptions: {
@@ -61,29 +67,49 @@ export declare class PieCheckbox extends PieCheckbox_base implements CheckboxPro
61
67
  mode: ShadowRootMode;
62
68
  slotAssignment?: SlotAssignmentMode | undefined;
63
69
  };
64
- value: CheckboxProps['value'];
70
+ value: string;
65
71
  label?: CheckboxProps['label'];
66
72
  name?: CheckboxProps['name'];
67
- checked?: CheckboxProps['checked'];
73
+ checked: boolean;
74
+ defaultChecked: boolean;
68
75
  disabled?: CheckboxProps['disabled'];
69
76
  required?: CheckboxProps['required'];
70
77
  indeterminate?: CheckboxProps['indeterminate'];
71
78
  aria: CheckboxProps['aria'];
72
- private checkbox?;
79
+ private checkbox;
73
80
  /**
74
81
  * (Read-only) returns a ValidityState with the validity states that this element is in.
75
82
  * https://developer.mozilla.org/en-US/docs/Web/API/HTMLObjectElement/validity
76
83
  */
77
84
  get validity(): ValidityState;
78
85
  /**
79
- * The onChange function updates the checkbox state and emits an event for consumers.
86
+ * Ensures that the form value is in sync with the component.
87
+ */
88
+ private handleFormAssociation;
89
+ /**
90
+ * Called after the disabled state of the element changes,
91
+ * either because the disabled attribute of this element was added or removed;
92
+ * or because the disabled state changed on a <fieldset> that's an ancestor of this element.
93
+ * @param disabled - The latest disabled state of the input.
94
+ */
95
+ formDisabledCallback(disabled: boolean): void;
96
+ protected firstUpdated(_changedProperties: PropertyValues<this>): void;
97
+ protected updated(_changedProperties: PropertyValues<this>): void;
98
+ /**
99
+ * Captures the native change event and wraps it in a custom event.
80
100
  * @param {Event} event - This should be the change event that was listened for on an input element with `type="checkbox"`.
81
101
  */
82
102
  private handleChange;
103
+ /**
104
+ * Called when the form that contains this component is reset.
105
+ * If the current checked state is different to the default checked state,
106
+ * the checked state is reset to the default checked state and a `change` event is emitted.
107
+ */
108
+ formResetCallback(): void;
83
109
  render(): TemplateResult<1>;
84
110
  static styles: CSSResult;
85
111
  }
86
112
 
87
- declare const PieCheckbox_base: GenericConstructor<RTLInterface> & typeof LitElement;
113
+ declare const PieCheckbox_base: GenericConstructor<FormControlInterface> & GenericConstructor<RTLInterface> & typeof LitElement;
88
114
 
89
115
  export { }
package/dist/index.js CHANGED
@@ -1,24 +1,27 @@
1
- import { LitElement as y, html as m, nothing as d, unsafeCSS as f } from "lit";
2
- import { RtlMixin as v, wrapNativeEvent as g, defineCustomElement as $ } from "@justeattakeaway/pie-webc-core";
3
- import { property as n, query as x } from "lit/decorators.js";
4
- import { ifDefined as h } from "lit/directives/if-defined.js";
5
- const q = `*,*:after,*:before{box-sizing:inherit}
6
- `, b = {
1
+ import { LitElement as u, html as f, nothing as h, unsafeCSS as b } from "lit";
2
+ import { FormControlMixin as y, RtlMixin as k, wrapNativeEvent as v, defineCustomElement as C } from "@justeattakeaway/pie-webc-core";
3
+ import { live as g } from "lit/directives/live.js";
4
+ import { property as s, query as x } from "lit/decorators.js";
5
+ import { ifDefined as $ } from "lit/directives/if-defined.js";
6
+ const F = `*,*:after,*:before{box-sizing:inherit}
7
+ `, d = {
8
+ // a default value for the html <input type="checkbox" /> value attribute.
9
+ // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#value
10
+ value: "on",
7
11
  required: !1,
8
- indeterminate: !1
12
+ defaultChecked: !1,
13
+ indeterminate: !1,
14
+ checked: !1
9
15
  };
10
- var C = Object.defineProperty, O = Object.getOwnPropertyDescriptor, o = (c, i, p, a) => {
11
- for (var t = a > 1 ? void 0 : a ? O(i, p) : i, l = c.length - 1, s; l >= 0; l--)
12
- (s = c[l]) && (t = (a ? s(i, p, t) : s(t)) || t);
13
- return a && t && C(i, p, t), t;
16
+ var E = Object.defineProperty, q = Object.getOwnPropertyDescriptor, r = (p, e, n, a) => {
17
+ for (var o = a > 1 ? void 0 : a ? q(e, n) : e, c = p.length - 1, l; c >= 0; c--)
18
+ (l = p[c]) && (o = (a ? l(e, n, o) : l(o)) || o);
19
+ return a && o && E(e, n, o), o;
14
20
  };
15
- const P = "pie-checkbox";
16
- class e extends v(y) {
21
+ const A = "pie-checkbox";
22
+ class t extends y(k(u)) {
17
23
  constructor() {
18
- super(...arguments), this.required = b.required, this.indeterminate = b.indeterminate, this.handleChange = (i) => {
19
- const p = g(i);
20
- this.dispatchEvent(p);
21
- };
24
+ super(...arguments), this.value = d.value, this.checked = d.checked, this.defaultChecked = d.defaultChecked, this.required = d.required, this.indeterminate = d.indeterminate;
22
25
  }
23
26
  /**
24
27
  * (Read-only) returns a ValidityState with the validity states that this element is in.
@@ -27,68 +30,114 @@ class e extends v(y) {
27
30
  get validity() {
28
31
  return this.checkbox.validity;
29
32
  }
33
+ /**
34
+ * Ensures that the form value is in sync with the component.
35
+ */
36
+ handleFormAssociation() {
37
+ !!this.form && !!this.name && this._internals.setFormValue(this.checked ? this.value : null);
38
+ }
39
+ /**
40
+ * Called after the disabled state of the element changes,
41
+ * either because the disabled attribute of this element was added or removed;
42
+ * or because the disabled state changed on a <fieldset> that's an ancestor of this element.
43
+ * @param disabled - The latest disabled state of the input.
44
+ */
45
+ formDisabledCallback(e) {
46
+ this.disabled = e;
47
+ }
48
+ firstUpdated(e) {
49
+ super.firstUpdated(e), this.handleFormAssociation();
50
+ }
51
+ updated(e) {
52
+ super.updated(e), this.handleFormAssociation();
53
+ }
54
+ /**
55
+ * Captures the native change event and wraps it in a custom event.
56
+ * @param {Event} event - This should be the change event that was listened for on an input element with `type="checkbox"`.
57
+ */
58
+ handleChange(e) {
59
+ const { checked: n } = e == null ? void 0 : e.currentTarget;
60
+ this.checked = n;
61
+ const a = v(e);
62
+ this.dispatchEvent(a), this.handleFormAssociation();
63
+ }
64
+ /**
65
+ * Called when the form that contains this component is reset.
66
+ * If the current checked state is different to the default checked state,
67
+ * the checked state is reset to the default checked state and a `change` event is emitted.
68
+ */
69
+ formResetCallback() {
70
+ if (this.checked === this.defaultChecked)
71
+ return;
72
+ this.checked = this.defaultChecked;
73
+ const e = new Event("change", { bubbles: !0, composed: !0 });
74
+ this.dispatchEvent(e), this.handleFormAssociation();
75
+ }
30
76
  render() {
31
77
  const {
32
- checked: i,
33
- value: p,
78
+ checked: e,
79
+ value: n,
34
80
  name: a,
35
- label: t,
36
- disabled: l,
37
- required: s,
38
- indeterminate: u,
39
- aria: r
81
+ label: o,
82
+ disabled: c,
83
+ required: l,
84
+ indeterminate: m,
85
+ aria: i
40
86
  } = this;
41
- return m`
42
- <label>
87
+ return f`
88
+ <label data-test-id="checkbox-component">
43
89
  <input
44
90
  type="checkbox"
45
- ?checked=${h(i)}
46
- .value=${h(p)}
47
- name=${h(a)}
48
- ?disabled=${l}
49
- ?required=${s}
50
- .indeterminate=${u}
51
- aria-label=${(r == null ? void 0 : r.label) || d}
52
- aria-labelledby=${t ? d : (r == null ? void 0 : r.labelledby) || d}
53
- aria-describedby= ${(r == null ? void 0 : r.describedby) || d}
91
+ .value=${n}
92
+ .checked=${g(e)}
93
+ name=${$(a)}
94
+ ?disabled=${c}
95
+ ?required=${l}
96
+ .indeterminate=${!!m}
97
+ aria-label=${(i == null ? void 0 : i.label) || h}
98
+ aria-labelledby=${o ? h : (i == null ? void 0 : i.labelledby) || h}
99
+ aria-describedby= ${(i == null ? void 0 : i.describedby) || h}
54
100
  @change=${this.handleChange}
55
- data-test-id="pie-checkbox"
101
+ data-test-id="checkbox-input"
56
102
  />
57
- ${t}
103
+ ${o}
58
104
  </label>`;
59
105
  }
60
106
  }
61
- e.shadowRootOptions = { ...y.shadowRootOptions, delegatesFocus: !0 };
62
- e.styles = f(q);
63
- o([
64
- n({ type: String })
65
- ], e.prototype, "value", 2);
66
- o([
67
- n({ type: String })
68
- ], e.prototype, "label", 2);
69
- o([
70
- n({ type: String })
71
- ], e.prototype, "name", 2);
72
- o([
73
- n({ type: Boolean })
74
- ], e.prototype, "checked", 2);
75
- o([
76
- n({ type: Boolean, reflect: !0 })
77
- ], e.prototype, "disabled", 2);
78
- o([
79
- n({ type: Boolean, reflect: !0 })
80
- ], e.prototype, "required", 2);
81
- o([
82
- n({ type: Boolean })
83
- ], e.prototype, "indeterminate", 2);
84
- o([
85
- n({ type: Object })
86
- ], e.prototype, "aria", 2);
87
- o([
88
- x("input")
89
- ], e.prototype, "checkbox", 2);
90
- $(P, e);
107
+ t.shadowRootOptions = { ...u.shadowRootOptions, delegatesFocus: !0 };
108
+ t.styles = b(F);
109
+ r([
110
+ s({ type: String })
111
+ ], t.prototype, "value", 2);
112
+ r([
113
+ s({ type: String })
114
+ ], t.prototype, "label", 2);
115
+ r([
116
+ s({ type: String })
117
+ ], t.prototype, "name", 2);
118
+ r([
119
+ s({ type: Boolean, reflect: !0 })
120
+ ], t.prototype, "checked", 2);
121
+ r([
122
+ s({ type: Boolean, reflect: !0 })
123
+ ], t.prototype, "defaultChecked", 2);
124
+ r([
125
+ s({ type: Boolean, reflect: !0 })
126
+ ], t.prototype, "disabled", 2);
127
+ r([
128
+ s({ type: Boolean, reflect: !0 })
129
+ ], t.prototype, "required", 2);
130
+ r([
131
+ s({ type: Boolean, reflect: !0 })
132
+ ], t.prototype, "indeterminate", 2);
133
+ r([
134
+ s({ type: Object })
135
+ ], t.prototype, "aria", 2);
136
+ r([
137
+ x('input[type="checkbox"]')
138
+ ], t.prototype, "checkbox", 2);
139
+ C(A, t);
91
140
  export {
92
- e as PieCheckbox,
93
- b as defaultProps
141
+ t as PieCheckbox,
142
+ d as defaultProps
94
143
  };
package/dist/react.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import { ComponentDefaultPropsGeneric } from '@justeattakeaway/pie-webc-core';
2
2
  import type { CSSResult } from 'lit';
3
+ import type { FormControlInterface } from '@justeattakeaway/pie-webc-core';
3
4
  import type { GenericConstructor } from '@justeattakeaway/pie-webc-core';
4
5
  import type { LitElement } from 'lit';
6
+ import type { PropertyValues } from 'lit';
5
7
  import * as React_2 from 'react';
6
8
  import type { RTLInterface } from '@justeattakeaway/pie-webc-core';
7
9
  import type { TemplateResult } from 'lit-html';
@@ -30,7 +32,11 @@ export declare interface CheckboxProps {
30
32
  */
31
33
  disabled?: boolean;
32
34
  /**
33
- * Same as the HTML checked attribute - indicates whether or not the checkbox is checked by default (when the page loads).
35
+ * The default checked state of the checkbox (not necessarily the same as the current checked state).
36
+ */
37
+ defaultChecked?: boolean;
38
+ /**
39
+ * The checked state of the checkbox.
34
40
  */
35
41
  checked?: boolean;
36
42
  /**
@@ -48,15 +54,15 @@ export declare interface CheckboxProps {
48
54
  aria?: AriaProps;
49
55
  }
50
56
 
51
- export declare type DefaultProps = ComponentDefaultPropsGeneric<CheckboxProps, 'required' | 'indeterminate'>;
57
+ export declare type DefaultProps = ComponentDefaultPropsGeneric<CheckboxProps, 'value' | 'required' | 'indeterminate' | 'checked' | 'defaultChecked'>;
52
58
 
53
59
  export declare const defaultProps: DefaultProps;
54
60
 
55
- export declare const PieCheckbox: React_2.ForwardRefExoticComponent<CheckboxProps & React_2.RefAttributes<PieCheckbox_2> & ReactBaseType>;
61
+ export declare const PieCheckbox: React_2.ForwardRefExoticComponent<CheckboxProps & React_2.RefAttributes<PieCheckbox_2> & PieCheckboxEvents & ReactBaseType>;
56
62
 
57
63
  /**
58
64
  * @tagname pie-checkbox
59
- * @slot - Default slot (checkbox label)
65
+ * @event {CustomEvent} change - when checked state is changed.
60
66
  */
61
67
  declare class PieCheckbox_2 extends PieCheckbox_base implements CheckboxProps {
62
68
  static shadowRootOptions: {
@@ -64,31 +70,55 @@ declare class PieCheckbox_2 extends PieCheckbox_base implements CheckboxProps {
64
70
  mode: ShadowRootMode;
65
71
  slotAssignment?: SlotAssignmentMode | undefined;
66
72
  };
67
- value: CheckboxProps['value'];
73
+ value: string;
68
74
  label?: CheckboxProps['label'];
69
75
  name?: CheckboxProps['name'];
70
- checked?: CheckboxProps['checked'];
76
+ checked: boolean;
77
+ defaultChecked: boolean;
71
78
  disabled?: CheckboxProps['disabled'];
72
79
  required?: CheckboxProps['required'];
73
80
  indeterminate?: CheckboxProps['indeterminate'];
74
81
  aria: CheckboxProps['aria'];
75
- private checkbox?;
82
+ private checkbox;
76
83
  /**
77
84
  * (Read-only) returns a ValidityState with the validity states that this element is in.
78
85
  * https://developer.mozilla.org/en-US/docs/Web/API/HTMLObjectElement/validity
79
86
  */
80
87
  get validity(): ValidityState;
81
88
  /**
82
- * The onChange function updates the checkbox state and emits an event for consumers.
89
+ * Ensures that the form value is in sync with the component.
90
+ */
91
+ private handleFormAssociation;
92
+ /**
93
+ * Called after the disabled state of the element changes,
94
+ * either because the disabled attribute of this element was added or removed;
95
+ * or because the disabled state changed on a <fieldset> that's an ancestor of this element.
96
+ * @param disabled - The latest disabled state of the input.
97
+ */
98
+ formDisabledCallback(disabled: boolean): void;
99
+ protected firstUpdated(_changedProperties: PropertyValues<this>): void;
100
+ protected updated(_changedProperties: PropertyValues<this>): void;
101
+ /**
102
+ * Captures the native change event and wraps it in a custom event.
83
103
  * @param {Event} event - This should be the change event that was listened for on an input element with `type="checkbox"`.
84
104
  */
85
105
  private handleChange;
106
+ /**
107
+ * Called when the form that contains this component is reset.
108
+ * If the current checked state is different to the default checked state,
109
+ * the checked state is reset to the default checked state and a `change` event is emitted.
110
+ */
111
+ formResetCallback(): void;
86
112
  render(): TemplateResult<1>;
87
113
  static styles: CSSResult;
88
114
  }
89
115
 
90
- declare const PieCheckbox_base: GenericConstructor<RTLInterface> & typeof LitElement;
116
+ declare const PieCheckbox_base: GenericConstructor<FormControlInterface> & GenericConstructor<RTLInterface> & typeof LitElement;
117
+
118
+ declare type PieCheckboxEvents = {
119
+ onChange?: (event: CustomEvent) => void;
120
+ };
91
121
 
92
- declare type ReactBaseType = React_2.HTMLAttributes<HTMLElement>;
122
+ declare type ReactBaseType = React_2.InputHTMLAttributes<HTMLInputElement>;
93
123
 
94
124
  export { }
package/dist/react.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import * as e from "react";
2
2
  import { createComponent as o } from "@lit/react";
3
3
  import { PieCheckbox as t } from "./index.js";
4
- import { defaultProps as h } from "./index.js";
4
+ import { defaultProps as b } from "./index.js";
5
5
  import "lit";
6
6
  import "@justeattakeaway/pie-webc-core";
7
+ import "lit/directives/live.js";
7
8
  import "lit/decorators.js";
8
9
  import "lit/directives/if-defined.js";
9
10
  const r = o({
@@ -11,9 +12,12 @@ const r = o({
11
12
  elementClass: t,
12
13
  react: e,
13
14
  tagName: "pie-checkbox",
14
- events: {}
15
- }), x = r;
15
+ events: {
16
+ onChange: "change"
17
+ // when checked state is changed.
18
+ }
19
+ }), h = r;
16
20
  export {
17
- x as PieCheckbox,
18
- h as defaultProps
21
+ h as PieCheckbox,
22
+ b as defaultProps
19
23
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@justeattakeaway/pie-checkbox",
3
3
  "description": "PIE Design System Checkbox built using Web Components",
4
- "version": "0.3.1",
4
+ "version": "0.5.0",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.js",
package/src/defs-react.ts CHANGED
@@ -1,8 +1,3 @@
1
1
  import React from 'react';
2
- /**
3
- * TODO: Verify if ReactBaseType can be set as a more specific React interface
4
- * Use the React IntrinsicElements interface to find how to map standard HTML elements to existing React Interfaces
5
- * Example: an HTML button maps to `React.ButtonHTMLAttributes<HTMLButtonElement>`
6
- * https://github.com/DefinitelyTyped/DefinitelyTyped/blob/0bb210867d16170c4a08d9ce5d132817651a0f80/types/react/index.d.ts#L2829
7
- */
8
- export type ReactBaseType = React.HTMLAttributes<HTMLElement>
2
+
3
+ export type ReactBaseType = React.InputHTMLAttributes<HTMLInputElement>
package/src/defs.ts CHANGED
@@ -27,7 +27,12 @@ export interface CheckboxProps {
27
27
  disabled?: boolean;
28
28
 
29
29
  /**
30
- * Same as the HTML checked attribute - indicates whether or not the checkbox is checked by default (when the page loads).
30
+ * The default checked state of the checkbox (not necessarily the same as the current checked state).
31
+ */
32
+ defaultChecked?: boolean;
33
+
34
+ /**
35
+ * The checked state of the checkbox.
31
36
  */
32
37
  checked?: boolean;
33
38
 
@@ -48,10 +53,14 @@ export interface CheckboxProps {
48
53
  aria?: AriaProps;
49
54
  }
50
55
 
51
- export type DefaultProps = ComponentDefaultPropsGeneric<CheckboxProps, 'required' | 'indeterminate'>;
56
+ export type DefaultProps = ComponentDefaultPropsGeneric<CheckboxProps, 'value' | 'required' | 'indeterminate' | 'checked' | 'defaultChecked'>;
52
57
 
53
58
  export const defaultProps: DefaultProps = {
59
+ // a default value for the html <input type="checkbox" /> value attribute.
60
+ // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#value
61
+ value: 'on',
54
62
  required: false,
63
+ defaultChecked: false,
55
64
  indeterminate: false,
65
+ checked: false,
56
66
  };
57
-
package/src/index.ts CHANGED
@@ -1,11 +1,13 @@
1
1
  import {
2
- LitElement, html, unsafeCSS, nothing,
2
+ LitElement, html, unsafeCSS, PropertyValues, nothing,
3
3
  } from 'lit';
4
4
  import {
5
5
  RtlMixin,
6
6
  defineCustomElement,
7
7
  wrapNativeEvent,
8
+ FormControlMixin,
8
9
  } from '@justeattakeaway/pie-webc-core';
10
+ import { live } from 'lit/directives/live.js';
9
11
  import { property, query } from 'lit/decorators.js';
10
12
  import { ifDefined } from 'lit/directives/if-defined.js';
11
13
 
@@ -19,13 +21,13 @@ const componentSelector = 'pie-checkbox';
19
21
 
20
22
  /**
21
23
  * @tagname pie-checkbox
22
- * @slot - Default slot (checkbox label)
24
+ * @event {CustomEvent} change - when checked state is changed.
23
25
  */
24
- export class PieCheckbox extends RtlMixin(LitElement) implements CheckboxProps {
26
+ export class PieCheckbox extends FormControlMixin(RtlMixin(LitElement)) implements CheckboxProps {
25
27
  static shadowRootOptions = { ...LitElement.shadowRootOptions, delegatesFocus: true };
26
28
 
27
29
  @property({ type: String })
28
- public value: CheckboxProps['value'];
30
+ public value = defaultProps.value;
29
31
 
30
32
  @property({ type: String })
31
33
  public label?: CheckboxProps['label'];
@@ -33,8 +35,11 @@ export class PieCheckbox extends RtlMixin(LitElement) implements CheckboxProps {
33
35
  @property({ type: String })
34
36
  public name?: CheckboxProps['name'];
35
37
 
36
- @property({ type: Boolean })
37
- public checked?: CheckboxProps['checked'];
38
+ @property({ type: Boolean, reflect: true })
39
+ public checked = defaultProps.checked;
40
+
41
+ @property({ type: Boolean, reflect: true })
42
+ public defaultChecked = defaultProps.defaultChecked;
38
43
 
39
44
  @property({ type: Boolean, reflect: true })
40
45
  public disabled?: CheckboxProps['disabled'];
@@ -42,14 +47,14 @@ export class PieCheckbox extends RtlMixin(LitElement) implements CheckboxProps {
42
47
  @property({ type: Boolean, reflect: true })
43
48
  public required?: CheckboxProps['required'] = defaultProps.required;
44
49
 
45
- @property({ type: Boolean })
50
+ @property({ type: Boolean, reflect: true })
46
51
  public indeterminate?: CheckboxProps['indeterminate'] = defaultProps.indeterminate;
47
52
 
48
53
  @property({ type: Object })
49
54
  public aria: CheckboxProps['aria'];
50
55
 
51
- @query('input')
52
- private checkbox?: HTMLInputElement;
56
+ @query('input[type="checkbox"]')
57
+ private checkbox!: HTMLInputElement;
53
58
 
54
59
  /**
55
60
  * (Read-only) returns a ValidityState with the validity states that this element is in.
@@ -60,16 +65,69 @@ export class PieCheckbox extends RtlMixin(LitElement) implements CheckboxProps {
60
65
  }
61
66
 
62
67
  /**
63
- * The onChange function updates the checkbox state and emits an event for consumers.
68
+ * Ensures that the form value is in sync with the component.
69
+ */
70
+ private handleFormAssociation () : void {
71
+ const isFormAssociated = !!this.form && !!this.name;
72
+ if (isFormAssociated) {
73
+ this._internals.setFormValue(this.checked ? this.value : null);
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Called after the disabled state of the element changes,
79
+ * either because the disabled attribute of this element was added or removed;
80
+ * or because the disabled state changed on a <fieldset> that's an ancestor of this element.
81
+ * @param disabled - The latest disabled state of the input.
82
+ */
83
+ public formDisabledCallback (disabled: boolean): void {
84
+ this.disabled = disabled;
85
+ }
86
+
87
+ protected firstUpdated (_changedProperties: PropertyValues<this>): void {
88
+ super.firstUpdated(_changedProperties);
89
+
90
+ this.handleFormAssociation();
91
+ }
92
+
93
+ protected updated (_changedProperties: PropertyValues<this>): void {
94
+ super.updated(_changedProperties);
95
+
96
+ this.handleFormAssociation();
97
+ }
98
+
99
+ /**
100
+ * Captures the native change event and wraps it in a custom event.
64
101
  * @param {Event} event - This should be the change event that was listened for on an input element with `type="checkbox"`.
65
102
  */
66
- private handleChange = (event: Event) => {
103
+ private handleChange (event: Event) {
104
+ const { checked } = event?.currentTarget as HTMLInputElement;
105
+ this.checked = checked;
67
106
  // This is because some events set `composed` to `false`.
68
107
  // Reference: https://javascript.info/shadow-dom-events#event-composed
69
108
  const customChangeEvent = wrapNativeEvent(event);
70
-
71
109
  this.dispatchEvent(customChangeEvent);
72
- };
110
+
111
+ this.handleFormAssociation();
112
+ }
113
+
114
+ /**
115
+ * Called when the form that contains this component is reset.
116
+ * If the current checked state is different to the default checked state,
117
+ * the checked state is reset to the default checked state and a `change` event is emitted.
118
+ */
119
+ public formResetCallback () : void {
120
+ if (this.checked === this.defaultChecked) {
121
+ return;
122
+ }
123
+
124
+ this.checked = this.defaultChecked;
125
+
126
+ const changeEvent = new Event('change', { bubbles: true, composed: true });
127
+ this.dispatchEvent(changeEvent);
128
+
129
+ this.handleFormAssociation();
130
+ }
73
131
 
74
132
  render () {
75
133
  const {
@@ -84,20 +142,20 @@ export class PieCheckbox extends RtlMixin(LitElement) implements CheckboxProps {
84
142
  } = this;
85
143
 
86
144
  return html`
87
- <label>
145
+ <label data-test-id="checkbox-component">
88
146
  <input
89
147
  type="checkbox"
90
- ?checked=${ifDefined(checked)}
91
- .value=${ifDefined(value)}
148
+ .value=${value}
149
+ .checked=${live(checked)}
92
150
  name=${ifDefined(name)}
93
151
  ?disabled=${disabled}
94
152
  ?required=${required}
95
- .indeterminate=${indeterminate}
153
+ .indeterminate=${!!indeterminate}
96
154
  aria-label=${aria?.label || nothing}
97
155
  aria-labelledby=${label ? nothing : aria?.labelledby || nothing}
98
156
  aria-describedby= ${aria?.describedby || nothing}
99
157
  @change=${this.handleChange}
100
- data-test-id="pie-checkbox"
158
+ data-test-id="checkbox-input"
101
159
  />
102
160
  ${label}
103
161
  </label>`;
package/src/react.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as React from 'react';
2
- import { createComponent } from '@lit/react';
2
+ import { createComponent, EventName } from '@lit/react';
3
3
  import { PieCheckbox as PieCheckboxLit } from './index';
4
4
  import { CheckboxProps } from './defs';
5
5
 
@@ -10,10 +10,16 @@ const PieCheckboxReact = createComponent({
10
10
  elementClass: PieCheckboxLit,
11
11
  react: React,
12
12
  tagName: 'pie-checkbox',
13
- events: {},
13
+ events: {
14
+ onChange: 'change' as EventName<CustomEvent>, // when checked state is changed.
15
+ },
14
16
  });
15
17
 
16
- type ReactBaseType = React.HTMLAttributes<HTMLElement>
18
+ type ReactBaseType = React.InputHTMLAttributes<HTMLInputElement>
19
+
20
+ type PieCheckboxEvents = {
21
+ onChange?: (event: CustomEvent) => void;
22
+ };
17
23
 
18
24
  export const PieCheckbox = PieCheckboxReact as React.ForwardRefExoticComponent<React.PropsWithoutRef<CheckboxProps>
19
- & React.RefAttributes<PieCheckboxLit> & ReactBaseType>;
25
+ & React.RefAttributes<PieCheckboxLit> & PieCheckboxEvents & ReactBaseType>;