@saasbase-io/core-elements 1.7.0 → 1.9.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.
@@ -0,0 +1,281 @@
1
+ import { LitElement } from '../../../../../node_modules/lit';
2
+ import { PasswordFieldContent, PasswordFieldProps, PasswordFieldRule, PasswordFieldVariant } from './field.types';
3
+ /**
4
+ * A self-validating password field for auth widget forms.
5
+ * @element sb-password-field
6
+ *
7
+ * @part password-field (rtg-field)
8
+ * @part password-field-content (rtg-field-content): label + description wrapper
9
+ * @part password-field-label (rtg-field-label)
10
+ * @part password-field-mark (span): decorative marker with text or `"*"` in label
11
+ * @part password-field-description (rtg-field-description)
12
+ * @part password-field-input (rtg-input/rtg-password-input)
13
+ * @part password-field-error (rtg-field-error)
14
+ *
15
+ * @fires sb-password-field:validate
16
+ * { id, parentId, name, value, valid, type, message }
17
+ * - id: inputId
18
+ * - parentId: rootId
19
+ * - type: "required" | "format" | "success" | "minlength" | "maxlength"
20
+ */
21
+ export declare class SbPasswordField extends LitElement implements PasswordFieldProps {
22
+ private static readonly _ROOT;
23
+ private static readonly _CONTENT;
24
+ private static readonly _LABEL;
25
+ private static readonly _MARK;
26
+ private static readonly _DESCRIPTION;
27
+ private static readonly _INPUT;
28
+ private static readonly _ERROR;
29
+ private static readonly _PATTERNS;
30
+ /**
31
+ * The stable CSS part names exposed for external targeting.
32
+ */
33
+ static get parts(): {
34
+ root: string;
35
+ content: string;
36
+ label: string;
37
+ mark: string;
38
+ description: string;
39
+ input: string;
40
+ error: string;
41
+ };
42
+ /**
43
+ * The name of the custom event fired on validate.
44
+ */
45
+ static get validateEventName(): string;
46
+ /**
47
+ * A unique value used as a substring in the generated part/child IDs, when
48
+ * the `child-id` prop is not provided.
49
+ */
50
+ seed: string;
51
+ /**
52
+ * The `id` value given to the root child/part and used as the base of other
53
+ * child/part IDs.
54
+ */
55
+ childId?: string;
56
+ /**
57
+ * The `id` value given to the input child/part to override the auto-generated
58
+ * input ID, derived from the `rootId`.
59
+ */
60
+ fieldId?: string;
61
+ /**
62
+ * The `name` prop of the input part, used by the parent form.
63
+ */
64
+ name: string;
65
+ /**
66
+ * The variant of the password field input, where `"toggle"` renders a
67
+ * hide/show button addon in the input element.
68
+ */
69
+ variant: PasswordFieldVariant;
70
+ /**
71
+ * Whether the label and description are grouped together above the input, or
72
+ * split up with the input in-between them.
73
+ */
74
+ content: PasswordFieldContent;
75
+ /**
76
+ * The text content of the field label.
77
+ */
78
+ label: string;
79
+ /**
80
+ * The text used as the input placeholder.
81
+ */
82
+ placeholder?: string;
83
+ /**
84
+ * The text content of the field description.
85
+ */
86
+ description?: string;
87
+ /**
88
+ * The text rendered in the field label as a custom mark.
89
+ */
90
+ mark?: string;
91
+ /**
92
+ * When provided with `required`, a caret mark is rendered in the field label.
93
+ */
94
+ caret?: boolean;
95
+ /**
96
+ * Whether the field is in a disabled state.
97
+ */
98
+ disabled?: boolean;
99
+ /**
100
+ * When provided, the parent form cannot be submitted without this field.
101
+ */
102
+ required?: boolean;
103
+ /**
104
+ * The minimum number of characters required for a valid password.
105
+ */
106
+ minlength?: number;
107
+ /**
108
+ * The maximum number of characters accepted for a valid password.
109
+ */
110
+ maxlength?: number;
111
+ /**
112
+ * The format rules for the password, detailing the `name` or `pattern` for
113
+ * each rule, as well as an optional `error` message pairing.
114
+ */
115
+ rules: PasswordFieldRule[];
116
+ /**
117
+ * Error message shown when the field is required but empty upon submission.
118
+ */
119
+ requiredError: string;
120
+ /**
121
+ * Custom error message shown when the password is shorter than minlength.
122
+ */
123
+ minlengthError?: string;
124
+ /**
125
+ * Custom error message shown when the password is longer than maxlength.
126
+ */
127
+ maxlengthError?: string;
128
+ /**
129
+ * Fallback error message shown when the field is invalid upon submission and
130
+ * the unmet rules have not specified an error message.
131
+ */
132
+ formatError: string;
133
+ /**
134
+ * Indicates that the field is in an invalid state.
135
+ */
136
+ private _invalid;
137
+ /**
138
+ * The error message(s) rendered in field error when in an invalid state.
139
+ */
140
+ private _errors;
141
+ /**
142
+ * The current value of the password input.
143
+ */
144
+ private _value;
145
+ /**
146
+ * Reference to the nearest `form` ancestor.
147
+ */
148
+ private _form;
149
+ /**
150
+ * Overrides default behavior to render into light DOM.
151
+ */
152
+ protected createRenderRoot(): this;
153
+ /**
154
+ * The `id` of the root element. Equal to `childId` when provided, otherwise
155
+ * derived from the component's part name and `seed`.
156
+ */
157
+ get rootId(): string;
158
+ /**
159
+ * The `id` of the field content element, derived from `rootId` with the
160
+ * content part name appended.
161
+ */
162
+ get contentId(): string;
163
+ /**
164
+ * The `id` of the field label element, derived from `rootId` with the label
165
+ * part name appended.
166
+ */
167
+ get labelId(): string;
168
+ /**
169
+ * The `id` of the field description element, derived from `rootId` with the
170
+ * description part name appended.
171
+ */
172
+ get descriptionId(): string;
173
+ /**
174
+ * The `id` of the mark element, derived from `rootId` with the mark part name
175
+ * appended.
176
+ */
177
+ get markId(): string;
178
+ /**
179
+ * The `id` of the input element, equal to `fieldId` when provided, otherwise
180
+ * derived from `rootId` with the input part name appended.
181
+ */
182
+ get inputId(): string;
183
+ /**
184
+ * The `id` of the field error element, derived from `rootId` with the error
185
+ * part name appended.
186
+ */
187
+ get errorId(): string;
188
+ /**
189
+ * Returns sanitized `minlength` and `maxlength` values, filtering out
190
+ * non-positive numbers and discarding `maxlength` if it is less than
191
+ * `minlength` to prevent conflicting length constraints.
192
+ */
193
+ private get _lengths();
194
+ /**
195
+ * Adds the input, form submit, and form reset event listeners.
196
+ */
197
+ connectedCallback(): void;
198
+ /**
199
+ * Removes the input, form submit, and form reset event listeners.
200
+ */
201
+ disconnectedCallback(): void;
202
+ /**
203
+ * The public validate method that calls the private `_validate()` method for
204
+ * manual, external validation.
205
+ */
206
+ validate(): boolean;
207
+ /**
208
+ * Defines and dispatches the validate event.
209
+ */
210
+ private _dispatchValidate;
211
+ /**
212
+ * Handles the required condition validation, accepting empty non-required
213
+ * fields and non-empty required fields, otherwise dispatching and displaying
214
+ * the required error.
215
+ */
216
+ private _validateRequired;
217
+ /**
218
+ * Handles the minlength condition validation, determining if a valid
219
+ * `minlength` prop was provided, dispatching and displaying the minlength
220
+ * error if the value is shorter than accepted.
221
+ */
222
+ private _validateMinlength;
223
+ /**
224
+ * Handles the maxlength condition validation, determining if a valid
225
+ * `maxlength` prop was provided, dispatching and displaying the maxlength
226
+ * error if the value is longer than accepted.
227
+ */
228
+ private _validateMaxlength;
229
+ /**
230
+ * Handles the format condition validation, testing against each of the
231
+ * `rules` patterns, and dispatching and displaying the relevant or fallback
232
+ * format error(s) for an invalid value.
233
+ */
234
+ private _validateFormat;
235
+ /**
236
+ * Resets the `_invalid` and `_errors` private props and dispatches the
237
+ * validate event, indicating a successfully validated and valid password.
238
+ */
239
+ private _doValidateSuccess;
240
+ /**
241
+ * Handles validation and dispatches the success event if the required,
242
+ * length, and format conditions are met.
243
+ */
244
+ private _validate;
245
+ /**
246
+ * Handles the input event, updating the private `_value` prop.
247
+ */
248
+ private _handleInput;
249
+ /**
250
+ * Handles the form submit event, running the `_validate()` method and
251
+ * preventing the default submission behavior if invalid.
252
+ */
253
+ private _handleFormSubmit;
254
+ /**
255
+ * Handles the form reset event, resetting the input element's `value` as well
256
+ * as the password field's private props: `_value`, `_invalid`, and `_errors`.
257
+ */
258
+ private _handleFormReset;
259
+ /**
260
+ * Conditionally returns the mark template for the field label. When `caret`
261
+ * and `required` are provided, the mark becomes an asterisk. When `mark` is
262
+ * provided, its value is rendered inside the element.
263
+ * If both a caret and custom text mark have their conditions met, the custom
264
+ * mark will be prioritised and rendered instead of the asterisk.
265
+ */
266
+ private _renderMark;
267
+ /**
268
+ * Returns the field label template with the label text and optional mark.
269
+ */
270
+ private _renderLabel;
271
+ /**
272
+ * Returns the field description template if `description` is defined.
273
+ */
274
+ private _renderDescription;
275
+ /**
276
+ * Returns the field error template with the `_errors` messages if the
277
+ * `_invalid` state is `true`.
278
+ */
279
+ private _renderError;
280
+ render(): import('../../../../../node_modules/lit-html').TemplateResult<1>;
281
+ }
@@ -0,0 +1,283 @@
1
+ import { LitElement } from '../../../../../node_modules/lit';
2
+ import { UsernameFieldContent, UsernameFieldProps } from './field.types';
3
+ /**
4
+ * A self-validating username field for auth widget forms.
5
+ * @element sb-username-field
6
+ *
7
+ * @part username-field (rtg-field)
8
+ * @part username-field-content (rtg-field-content): label + description wrapper
9
+ * @part username-field-label (rtg-field-label)
10
+ * @part username-field-mark (span): decorative marker with text or `"*"` in label
11
+ * @part username-field-description (rtg-field-description)
12
+ * @part username-field-input (rtg-input)
13
+ * @part username-field-error (rtg-field-error)
14
+ *
15
+ * @fires sb-username-field:validate
16
+ * { id, parentId, name, value, valid, type, message }
17
+ * - id: inputId
18
+ * - parentId: rootId
19
+ * - type: "required" | "format" | "success" | "minlength" | "maxlength"
20
+ */
21
+ export declare class SbUsernameField extends LitElement implements UsernameFieldProps {
22
+ private static readonly _ROOT;
23
+ private static readonly _CONTENT;
24
+ private static readonly _LABEL;
25
+ private static readonly _MARK;
26
+ private static readonly _DESCRIPTION;
27
+ private static readonly _INPUT;
28
+ private static readonly _ERROR;
29
+ private static readonly _PATTERN;
30
+ /**
31
+ * The stable CSS part names exposed for external targeting.
32
+ */
33
+ static get parts(): {
34
+ root: string;
35
+ content: string;
36
+ label: string;
37
+ mark: string;
38
+ description: string;
39
+ input: string;
40
+ error: string;
41
+ };
42
+ /**
43
+ * The name of the custom event fired on validate.
44
+ */
45
+ static get validateEventName(): string;
46
+ /**
47
+ * A unique value used as a substring in the generated part/child IDs, when
48
+ * the `child-id` prop is not provided.
49
+ */
50
+ seed: string;
51
+ /**
52
+ * The `id` value given to the root child/part and used as the base of other
53
+ * child/part IDs.
54
+ */
55
+ childId?: string;
56
+ /**
57
+ * The `id` value given to the input child/part to override the auto-generated
58
+ * input ID, derived from the `rootId`.
59
+ */
60
+ fieldId?: string;
61
+ /**
62
+ * The `name` prop of the input part, used by the parent form.
63
+ */
64
+ name: string;
65
+ /**
66
+ * Whether the label and description are grouped together above the input, or
67
+ * split up with the input in-between them.
68
+ */
69
+ content: UsernameFieldContent;
70
+ /**
71
+ * The text content of the field label.
72
+ */
73
+ label: string;
74
+ /**
75
+ * The text used as the input placeholder.
76
+ */
77
+ placeholder?: string;
78
+ /**
79
+ * The text content of the field description.
80
+ */
81
+ description?: string;
82
+ /**
83
+ * The text rendered in the field label as a custom mark.
84
+ */
85
+ mark?: string;
86
+ /**
87
+ * When provided with `required`, a caret mark is rendered in the field label.
88
+ */
89
+ caret?: boolean;
90
+ /**
91
+ * Whether the field is in a disabled state.
92
+ */
93
+ disabled?: boolean;
94
+ /**
95
+ * When provided, the parent form cannot be submitted without this field.
96
+ */
97
+ required?: boolean;
98
+ /**
99
+ * The minimum number of characters required for a valid username.
100
+ */
101
+ minlength?: number;
102
+ /**
103
+ * The maximum number of characters accepted for a valid username.
104
+ */
105
+ maxlength?: number;
106
+ /**
107
+ * The regex pattern for a valid username, used in place of `_PATTERN`.
108
+ */
109
+ pattern?: string;
110
+ /**
111
+ * Error message shown when the field is required but empty upon submission.
112
+ */
113
+ requiredError: string;
114
+ /**
115
+ * Custom error message shown when the username is shorter than minlength.
116
+ */
117
+ minlengthError?: string;
118
+ /**
119
+ * Custom error message shown when the username is longer than maxlength.
120
+ */
121
+ maxlengthError?: string;
122
+ /**
123
+ * Error message shown when the field is invalid upon submission.
124
+ */
125
+ formatError: string;
126
+ /**
127
+ * Indicates that the field is in an invalid state.
128
+ */
129
+ private _invalid;
130
+ /**
131
+ * The text rendered in field error when in an invalid state.
132
+ */
133
+ private _error;
134
+ /**
135
+ * The current value of the username input.
136
+ */
137
+ private _value;
138
+ /**
139
+ * Reference to the nearest `form` ancestor.
140
+ */
141
+ private _form;
142
+ /**
143
+ * Overrides default behavior to render into light DOM.
144
+ */
145
+ protected createRenderRoot(): this;
146
+ /**
147
+ * The `id` of the root element. Equal to `childId` when provided, otherwise
148
+ * derived from the component's part name and `seed`.
149
+ */
150
+ get rootId(): string;
151
+ /**
152
+ * The `id` of the field content element, derived from `rootId` with the
153
+ * content part name appended.
154
+ */
155
+ get contentId(): string;
156
+ /**
157
+ * The `id` of the field label element, derived from `rootId` with the label
158
+ * part name appended.
159
+ */
160
+ get labelId(): string;
161
+ /**
162
+ * The `id` of the field description element, derived from `rootId` with the
163
+ * description part name appended.
164
+ */
165
+ get descriptionId(): string;
166
+ /**
167
+ * The `id` of the mark element, derived from `rootId` with the mark part name
168
+ * appended.
169
+ */
170
+ get markId(): string;
171
+ /**
172
+ * The `id` of the input element, equal to `fieldId` when provided, otherwise
173
+ * derived from `rootId` with the input part name appended.
174
+ */
175
+ get inputId(): string;
176
+ /**
177
+ * The `id` of the field error element, derived from `rootId` with the error
178
+ * part name appended.
179
+ */
180
+ get errorId(): string;
181
+ /**
182
+ * Returns a the normalized value of `_value` by trimming whitespace and
183
+ * converting letters to lowercase.
184
+ */
185
+ private get _normalizedValue();
186
+ /**
187
+ * Returns sanitized `minlength` and `maxlength` values, filtering out
188
+ * non-positive numbers and discarding `maxlength` if it is less than
189
+ * `minlength` to prevent conflicting length constraints.
190
+ */
191
+ private get _lengths();
192
+ /**
193
+ * Returns the resolved regex pattern, using a `RegExp` built from the
194
+ * `pattern` prop when provided, otherwise falling back to `_PATTERN`.
195
+ */
196
+ private get _pattern();
197
+ /**
198
+ * Adds the input, form submit, and form reset event listeners.
199
+ */
200
+ connectedCallback(): void;
201
+ /**
202
+ * Removes the input, form submit, and form reset event listeners.
203
+ */
204
+ disconnectedCallback(): void;
205
+ /**
206
+ * The public validate method that calls the private `_validate()` method for
207
+ * manual, external validation.
208
+ */
209
+ validate(): boolean;
210
+ /**
211
+ * Defines and dispatches the validate event.
212
+ */
213
+ private _dispatchValidate;
214
+ /**
215
+ * Handles the required condition validation, accepting empty non-required
216
+ * fields and non-empty required fields, otherwise dispatching and displaying
217
+ * the required error.
218
+ */
219
+ private _validateRequired;
220
+ /**
221
+ * Handles the minlength condition validation, determining if a valid
222
+ * `minlength` prop was provided, dispatching and displaying the minlength
223
+ * error if the value is shorter than accepted.
224
+ */
225
+ private _validateMinlength;
226
+ /**
227
+ * Handles the maxlength condition validation, determining if a valid
228
+ * `maxlength` prop was provided, dispatching and displaying the maxlength
229
+ * error if the value is longer than accepted.
230
+ */
231
+ private _validateMaxlength;
232
+ /**
233
+ * Handles the format condition validation, testing against `_pattern`, and
234
+ * dispatching and displaying the format error for an invalid value.
235
+ */
236
+ private _validateFormat;
237
+ /**
238
+ * Resets the `_invalid` and `_error` private props and dispatches the
239
+ * validate event, indicating a successfully validated and valid username.
240
+ */
241
+ private _doValidateSuccess;
242
+ /**
243
+ * Handles validation and dispatches the success event if the required,
244
+ * length, and format conditions are met.
245
+ */
246
+ private _validate;
247
+ /**
248
+ * Handles the input event, updating the private `_value` prop.
249
+ */
250
+ private _handleInput;
251
+ /**
252
+ * Handles the form submit event, running the `_validate()` method and
253
+ * preventing the default submission behavior if invalid.
254
+ */
255
+ private _handleFormSubmit;
256
+ /**
257
+ * Handles the form reset event, resetting the input element's `value` as well
258
+ * as the username field's private props: `_value`, `_invalid`, and `_error`.
259
+ */
260
+ private _handleFormReset;
261
+ /**
262
+ * Conditionally returns the mark template for the field label. When `caret`
263
+ * and `required` are provided, the mark becomes an asterisk. When `mark` is
264
+ * provided, its value is rendered inside the element.
265
+ * If both a caret and custom text mark have their conditions met, the custom
266
+ * mark will be prioritised and rendered instead of the asterisk.
267
+ */
268
+ private _renderMark;
269
+ /**
270
+ * Returns the field label template with the label text and optional mark.
271
+ */
272
+ private _renderLabel;
273
+ /**
274
+ * Returns the field description template if `description` is defined.
275
+ */
276
+ private _renderDescription;
277
+ /**
278
+ * Returns the field error template with the `_error` message if the
279
+ * `_invalid` state is `true`.
280
+ */
281
+ private _renderError;
282
+ render(): import('../../../../../node_modules/lit-html').TemplateResult<1>;
283
+ }
@@ -0,0 +1,15 @@
1
+ import { Meta, StoryObj } from '@storybook/web-components-vite';
2
+ declare const meta: Meta;
3
+ export default meta;
4
+ type Story = StoryObj;
5
+ export declare const Basic: Story;
6
+ export declare const Label: Story;
7
+ export declare const Placeholder: Story;
8
+ export declare const Description: Story;
9
+ export declare const Variant: Story;
10
+ export declare const Content: Story;
11
+ export declare const Disabled: Story;
12
+ export declare const Caret: Story;
13
+ export declare const Mark: Story;
14
+ export declare const Validate: Story;
15
+ export declare const Form: Story;
@@ -0,0 +1,14 @@
1
+ import { Meta, StoryObj } from '@storybook/web-components-vite';
2
+ declare const meta: Meta;
3
+ export default meta;
4
+ type Story = StoryObj;
5
+ export declare const Basic: Story;
6
+ export declare const Label: Story;
7
+ export declare const Placeholder: Story;
8
+ export declare const Description: Story;
9
+ export declare const Content: Story;
10
+ export declare const Disabled: Story;
11
+ export declare const Caret: Story;
12
+ export declare const Mark: Story;
13
+ export declare const Validate: Story;
14
+ export declare const Form: Story;
@@ -1,3 +1,5 @@
1
+ type BaseFieldValidateType = "success" | "required" | "format";
2
+ type BaseFieldValidateTypeWithLength = BaseFieldValidateType | "minlength" | "maxlength";
1
3
  type BaseFieldContent = "split" | "group";
2
4
  type BaseFieldProps = {
3
5
  seed: string;
@@ -15,14 +17,44 @@ type BaseFieldProps = {
15
17
  requiredError: string;
16
18
  formatError: string;
17
19
  };
18
- export type EmailFieldValidateType = "success" | "required" | "format";
20
+ type BaseFieldPropsWithLength = BaseFieldProps & {
21
+ minlength?: number;
22
+ maxlength?: number;
23
+ minlengthError?: string;
24
+ maxlengthError?: string;
25
+ };
26
+ export type EmailFieldValidateType = BaseFieldValidateType;
19
27
  export type EmailFieldContent = BaseFieldContent;
20
28
  export type EmailFieldProps = Omit<BaseFieldProps, "content"> & {
21
29
  content: EmailFieldContent;
22
30
  };
23
- export type PhoneFieldValidateType = "success" | "required" | "format";
31
+ export type PhoneFieldValidateType = BaseFieldValidateType;
24
32
  export type PhoneFieldContent = BaseFieldContent;
25
33
  export type PhoneFieldProps = Omit<BaseFieldProps, "content"> & {
26
34
  content: PhoneFieldContent;
27
35
  };
36
+ export type UsernameFieldValidateType = BaseFieldValidateTypeWithLength;
37
+ export type UsernameFieldContent = BaseFieldContent;
38
+ export type UsernameFieldProps = Omit<BaseFieldPropsWithLength, "content"> & {
39
+ content: UsernameFieldContent;
40
+ pattern?: string;
41
+ };
42
+ export type PasswordFieldValidateType = BaseFieldValidateTypeWithLength;
43
+ export type PasswordFieldVariant = "toggle" | "simple";
44
+ export type PasswordFieldContent = BaseFieldContent;
45
+ export type PasswordFieldRuleName = "uppercase" | "lowercase" | "number" | "special";
46
+ export type PasswordFieldRule = {
47
+ name: PasswordFieldRuleName;
48
+ pattern?: undefined;
49
+ error?: string;
50
+ } | {
51
+ name?: undefined;
52
+ pattern: string;
53
+ error?: string;
54
+ };
55
+ export type PasswordFieldProps = Omit<BaseFieldPropsWithLength, "content"> & {
56
+ variant: PasswordFieldVariant;
57
+ content: PasswordFieldContent;
58
+ rules: PasswordFieldRule[];
59
+ };
28
60
  export {};
@@ -1,9 +1,13 @@
1
1
  import { SbEmailField } from './_email-field';
2
+ import { SbPasswordField } from './_password-field';
2
3
  import { SbPhoneField } from './_phone-field';
3
- export { SbEmailField, SbPhoneField };
4
+ import { SbUsernameField } from './_username-field';
5
+ export { SbEmailField, SbPasswordField, SbPhoneField, SbUsernameField };
4
6
  declare global {
5
7
  interface HTMLElementTagNameMap {
6
8
  "sb-email-field": SbEmailField;
7
9
  "sb-phone-field": SbPhoneField;
10
+ "sb-username-field": SbUsernameField;
11
+ "sb-password-field": SbPasswordField;
8
12
  }
9
13
  }