@justeattakeaway/pie-checkbox 0.4.0 → 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 // 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 indeterminate: false,\n checked: 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": [
@@ -81,6 +81,13 @@
81
81
  "attribute": "checked",
82
82
  "reflects": true
83
83
  },
84
+ {
85
+ "kind": "field",
86
+ "name": "defaultChecked",
87
+ "privacy": "public",
88
+ "attribute": "defaultChecked",
89
+ "reflects": true
90
+ },
84
91
  {
85
92
  "kind": "field",
86
93
  "name": "disabled",
@@ -124,7 +131,7 @@
124
131
  "kind": "field",
125
132
  "name": "checkbox",
126
133
  "type": {
127
- "text": "HTMLInputElement | undefined"
134
+ "text": "HTMLInputElement"
128
135
  },
129
136
  "privacy": "private"
130
137
  },
@@ -183,6 +190,17 @@
183
190
  }
184
191
  ],
185
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"
201
+ }
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."
186
204
  }
187
205
  ],
188
206
  "events": [
@@ -217,6 +235,10 @@
217
235
  "name": "checked",
218
236
  "fieldName": "checked"
219
237
  },
238
+ {
239
+ "name": "defaultChecked",
240
+ "fieldName": "defaultChecked"
241
+ },
220
242
  {
221
243
  "name": "disabled",
222
244
  "type": {
package/dist/index.d.ts CHANGED
@@ -31,7 +31,11 @@ export declare interface CheckboxProps {
31
31
  */
32
32
  disabled?: boolean;
33
33
  /**
34
- * Indicates whether or not the checkbox is checked.
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.
35
39
  */
36
40
  checked?: boolean;
37
41
  /**
@@ -49,7 +53,7 @@ export declare interface CheckboxProps {
49
53
  aria?: AriaProps;
50
54
  }
51
55
 
52
- export declare type DefaultProps = ComponentDefaultPropsGeneric<CheckboxProps, 'value' | 'required' | 'indeterminate' | 'checked'>;
56
+ export declare type DefaultProps = ComponentDefaultPropsGeneric<CheckboxProps, 'value' | 'required' | 'indeterminate' | 'checked' | 'defaultChecked'>;
53
57
 
54
58
  export declare const defaultProps: DefaultProps;
55
59
 
@@ -67,11 +71,12 @@ export declare class PieCheckbox extends PieCheckbox_base implements CheckboxPro
67
71
  label?: CheckboxProps['label'];
68
72
  name?: CheckboxProps['name'];
69
73
  checked: boolean;
74
+ defaultChecked: boolean;
70
75
  disabled?: CheckboxProps['disabled'];
71
76
  required?: CheckboxProps['required'];
72
77
  indeterminate?: CheckboxProps['indeterminate'];
73
78
  aria: CheckboxProps['aria'];
74
- private checkbox?;
79
+ private checkbox;
75
80
  /**
76
81
  * (Read-only) returns a ValidityState with the validity states that this element is in.
77
82
  * https://developer.mozilla.org/en-US/docs/Web/API/HTMLObjectElement/validity
@@ -95,6 +100,12 @@ export declare class PieCheckbox extends PieCheckbox_base implements CheckboxPro
95
100
  * @param {Event} event - This should be the change event that was listened for on an input element with `type="checkbox"`.
96
101
  */
97
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;
98
109
  render(): TemplateResult<1>;
99
110
  static styles: CSSResult;
100
111
  }
package/dist/index.js CHANGED
@@ -1,26 +1,27 @@
1
- import { LitElement as u, html as b, nothing as d, unsafeCSS as f } from "lit";
2
- import { FormControlMixin as y, RtlMixin as v, wrapNativeEvent as g, defineCustomElement as k } from "@justeattakeaway/pie-webc-core";
3
- import { live as x } from "lit/directives/live.js";
4
- import { property as a, query as $ } from "lit/decorators.js";
5
- import { ifDefined as C } from "lit/directives/if-defined.js";
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
6
  const F = `*,*:after,*:before{box-sizing:inherit}
7
- `, p = {
7
+ `, d = {
8
8
  // a default value for the html <input type="checkbox" /> value attribute.
9
9
  // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#value
10
10
  value: "on",
11
11
  required: !1,
12
+ defaultChecked: !1,
12
13
  indeterminate: !1,
13
14
  checked: !1
14
15
  };
15
- var _ = Object.defineProperty, q = Object.getOwnPropertyDescriptor, i = (h, e, n, s) => {
16
- for (var o = s > 1 ? void 0 : s ? q(e, n) : e, l = h.length - 1, c; l >= 0; l--)
17
- (c = h[l]) && (o = (s ? c(e, n, o) : c(o)) || o);
18
- return s && o && _(e, n, o), o;
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;
19
20
  };
20
- const O = "pie-checkbox";
21
- class t extends y(v(u)) {
21
+ const A = "pie-checkbox";
22
+ class t extends y(k(u)) {
22
23
  constructor() {
23
- super(...arguments), this.value = p.value, this.checked = p.checked, this.required = p.required, this.indeterminate = p.indeterminate;
24
+ super(...arguments), this.value = d.value, this.checked = d.checked, this.defaultChecked = d.defaultChecked, this.required = d.required, this.indeterminate = d.indeterminate;
24
25
  }
25
26
  /**
26
27
  * (Read-only) returns a ValidityState with the validity states that this element is in.
@@ -33,7 +34,7 @@ class t extends y(v(u)) {
33
34
  * Ensures that the form value is in sync with the component.
34
35
  */
35
36
  handleFormAssociation() {
36
- !!this._internals.form && !!this.name && this._internals.setFormValue(this.checked ? this.value : null);
37
+ !!this.form && !!this.name && this._internals.setFormValue(this.checked ? this.value : null);
37
38
  }
38
39
  /**
39
40
  * Called after the disabled state of the element changes,
@@ -57,33 +58,45 @@ class t extends y(v(u)) {
57
58
  handleChange(e) {
58
59
  const { checked: n } = e == null ? void 0 : e.currentTarget;
59
60
  this.checked = n;
60
- const s = g(e);
61
- this.dispatchEvent(s), this.handleFormAssociation();
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();
62
75
  }
63
76
  render() {
64
77
  const {
65
78
  checked: e,
66
79
  value: n,
67
- name: s,
80
+ name: a,
68
81
  label: o,
69
- disabled: l,
70
- required: c,
82
+ disabled: c,
83
+ required: l,
71
84
  indeterminate: m,
72
- aria: r
85
+ aria: i
73
86
  } = this;
74
- return b`
87
+ return f`
75
88
  <label data-test-id="checkbox-component">
76
89
  <input
77
90
  type="checkbox"
78
91
  .value=${n}
79
- ?checked=${x(e)}
80
- name=${C(s)}
81
- ?disabled=${l}
82
- ?required=${c}
83
- .indeterminate=${m}
84
- aria-label=${(r == null ? void 0 : r.label) || d}
85
- aria-labelledby=${o ? d : (r == null ? void 0 : r.labelledby) || d}
86
- aria-describedby= ${(r == null ? void 0 : r.describedby) || d}
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}
87
100
  @change=${this.handleChange}
88
101
  data-test-id="checkbox-input"
89
102
  />
@@ -92,36 +105,39 @@ class t extends y(v(u)) {
92
105
  }
93
106
  }
94
107
  t.shadowRootOptions = { ...u.shadowRootOptions, delegatesFocus: !0 };
95
- t.styles = f(F);
96
- i([
97
- a({ type: String })
108
+ t.styles = b(F);
109
+ r([
110
+ s({ type: String })
98
111
  ], t.prototype, "value", 2);
99
- i([
100
- a({ type: String })
112
+ r([
113
+ s({ type: String })
101
114
  ], t.prototype, "label", 2);
102
- i([
103
- a({ type: String })
115
+ r([
116
+ s({ type: String })
104
117
  ], t.prototype, "name", 2);
105
- i([
106
- a({ type: Boolean, reflect: !0 })
118
+ r([
119
+ s({ type: Boolean, reflect: !0 })
107
120
  ], t.prototype, "checked", 2);
108
- i([
109
- a({ type: Boolean, reflect: !0 })
121
+ r([
122
+ s({ type: Boolean, reflect: !0 })
123
+ ], t.prototype, "defaultChecked", 2);
124
+ r([
125
+ s({ type: Boolean, reflect: !0 })
110
126
  ], t.prototype, "disabled", 2);
111
- i([
112
- a({ type: Boolean, reflect: !0 })
127
+ r([
128
+ s({ type: Boolean, reflect: !0 })
113
129
  ], t.prototype, "required", 2);
114
- i([
115
- a({ type: Boolean, reflect: !0 })
130
+ r([
131
+ s({ type: Boolean, reflect: !0 })
116
132
  ], t.prototype, "indeterminate", 2);
117
- i([
118
- a({ type: Object })
133
+ r([
134
+ s({ type: Object })
119
135
  ], t.prototype, "aria", 2);
120
- i([
121
- $('input[type="checkbox"]')
136
+ r([
137
+ x('input[type="checkbox"]')
122
138
  ], t.prototype, "checkbox", 2);
123
- k(O, t);
139
+ C(A, t);
124
140
  export {
125
141
  t as PieCheckbox,
126
- p as defaultProps
142
+ d as defaultProps
127
143
  };
package/dist/react.d.ts CHANGED
@@ -32,7 +32,11 @@ export declare interface CheckboxProps {
32
32
  */
33
33
  disabled?: boolean;
34
34
  /**
35
- * Indicates whether or not the checkbox is checked.
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.
36
40
  */
37
41
  checked?: boolean;
38
42
  /**
@@ -50,7 +54,7 @@ export declare interface CheckboxProps {
50
54
  aria?: AriaProps;
51
55
  }
52
56
 
53
- export declare type DefaultProps = ComponentDefaultPropsGeneric<CheckboxProps, 'value' | 'required' | 'indeterminate' | 'checked'>;
57
+ export declare type DefaultProps = ComponentDefaultPropsGeneric<CheckboxProps, 'value' | 'required' | 'indeterminate' | 'checked' | 'defaultChecked'>;
54
58
 
55
59
  export declare const defaultProps: DefaultProps;
56
60
 
@@ -70,11 +74,12 @@ declare class PieCheckbox_2 extends PieCheckbox_base implements CheckboxProps {
70
74
  label?: CheckboxProps['label'];
71
75
  name?: CheckboxProps['name'];
72
76
  checked: boolean;
77
+ defaultChecked: boolean;
73
78
  disabled?: CheckboxProps['disabled'];
74
79
  required?: CheckboxProps['required'];
75
80
  indeterminate?: CheckboxProps['indeterminate'];
76
81
  aria: CheckboxProps['aria'];
77
- private checkbox?;
82
+ private checkbox;
78
83
  /**
79
84
  * (Read-only) returns a ValidityState with the validity states that this element is in.
80
85
  * https://developer.mozilla.org/en-US/docs/Web/API/HTMLObjectElement/validity
@@ -98,6 +103,12 @@ declare class PieCheckbox_2 extends PieCheckbox_base implements CheckboxProps {
98
103
  * @param {Event} event - This should be the change event that was listened for on an input element with `type="checkbox"`.
99
104
  */
100
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;
101
112
  render(): TemplateResult<1>;
102
113
  static styles: CSSResult;
103
114
  }
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.4.0",
4
+ "version": "0.5.0",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.js",
package/src/defs.ts CHANGED
@@ -27,7 +27,12 @@ export interface CheckboxProps {
27
27
  disabled?: boolean;
28
28
 
29
29
  /**
30
- * Indicates whether or not the checkbox is checked.
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,14 +53,14 @@ export interface CheckboxProps {
48
53
  aria?: AriaProps;
49
54
  }
50
55
 
51
- export type DefaultProps = ComponentDefaultPropsGeneric<CheckboxProps, 'value' | 'required' | 'indeterminate' | 'checked' >;
56
+ export type DefaultProps = ComponentDefaultPropsGeneric<CheckboxProps, 'value' | 'required' | 'indeterminate' | 'checked' | 'defaultChecked'>;
52
57
 
53
58
  export const defaultProps: DefaultProps = {
54
59
  // a default value for the html <input type="checkbox" /> value attribute.
55
60
  // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#value
56
61
  value: 'on',
57
62
  required: false,
63
+ defaultChecked: false,
58
64
  indeterminate: false,
59
65
  checked: false,
60
66
  };
61
-
package/src/index.ts CHANGED
@@ -38,6 +38,9 @@ export class PieCheckbox extends FormControlMixin(RtlMixin(LitElement)) implemen
38
38
  @property({ type: Boolean, reflect: true })
39
39
  public checked = defaultProps.checked;
40
40
 
41
+ @property({ type: Boolean, reflect: true })
42
+ public defaultChecked = defaultProps.defaultChecked;
43
+
41
44
  @property({ type: Boolean, reflect: true })
42
45
  public disabled?: CheckboxProps['disabled'];
43
46
 
@@ -51,7 +54,7 @@ export class PieCheckbox extends FormControlMixin(RtlMixin(LitElement)) implemen
51
54
  public aria: CheckboxProps['aria'];
52
55
 
53
56
  @query('input[type="checkbox"]')
54
- private checkbox?: HTMLInputElement;
57
+ private checkbox!: HTMLInputElement;
55
58
 
56
59
  /**
57
60
  * (Read-only) returns a ValidityState with the validity states that this element is in.
@@ -65,7 +68,7 @@ export class PieCheckbox extends FormControlMixin(RtlMixin(LitElement)) implemen
65
68
  * Ensures that the form value is in sync with the component.
66
69
  */
67
70
  private handleFormAssociation () : void {
68
- const isFormAssociated = !!this._internals.form && !!this.name;
71
+ const isFormAssociated = !!this.form && !!this.name;
69
72
  if (isFormAssociated) {
70
73
  this._internals.setFormValue(this.checked ? this.value : null);
71
74
  }
@@ -108,6 +111,24 @@ export class PieCheckbox extends FormControlMixin(RtlMixin(LitElement)) implemen
108
111
  this.handleFormAssociation();
109
112
  }
110
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
+ }
131
+
111
132
  render () {
112
133
  const {
113
134
  checked,
@@ -125,11 +146,11 @@ export class PieCheckbox extends FormControlMixin(RtlMixin(LitElement)) implemen
125
146
  <input
126
147
  type="checkbox"
127
148
  .value=${value}
128
- ?checked=${live(checked)}
149
+ .checked=${live(checked)}
129
150
  name=${ifDefined(name)}
130
151
  ?disabled=${disabled}
131
152
  ?required=${required}
132
- .indeterminate=${indeterminate}
153
+ .indeterminate=${!!indeterminate}
133
154
  aria-label=${aria?.label || nothing}
134
155
  aria-labelledby=${label ? nothing : aria?.labelledby || nothing}
135
156
  aria-describedby= ${aria?.describedby || nothing}