@saasbase-io/core-elements 1.7.0 → 1.8.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,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"
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,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,4 @@
1
+ type BaseFieldValidateType = "success" | "required" | "format";
1
2
  type BaseFieldContent = "split" | "group";
2
3
  type BaseFieldProps = {
3
4
  seed: string;
@@ -15,14 +16,24 @@ type BaseFieldProps = {
15
16
  requiredError: string;
16
17
  formatError: string;
17
18
  };
18
- export type EmailFieldValidateType = "success" | "required" | "format";
19
+ export type EmailFieldValidateType = BaseFieldValidateType;
19
20
  export type EmailFieldContent = BaseFieldContent;
20
21
  export type EmailFieldProps = Omit<BaseFieldProps, "content"> & {
21
22
  content: EmailFieldContent;
22
23
  };
23
- export type PhoneFieldValidateType = "success" | "required" | "format";
24
+ export type PhoneFieldValidateType = BaseFieldValidateType;
24
25
  export type PhoneFieldContent = BaseFieldContent;
25
26
  export type PhoneFieldProps = Omit<BaseFieldProps, "content"> & {
26
27
  content: PhoneFieldContent;
27
28
  };
29
+ export type UsernameFieldValidateType = BaseFieldValidateType | "minlength" | "maxlength";
30
+ export type UsernameFieldContent = BaseFieldContent;
31
+ export type UsernameFieldProps = Omit<BaseFieldProps, "content"> & {
32
+ content: UsernameFieldContent;
33
+ minlength?: number;
34
+ maxlength?: number;
35
+ pattern?: string;
36
+ minlengthError?: string;
37
+ maxlengthError?: string;
38
+ };
28
39
  export {};
@@ -1,9 +1,11 @@
1
1
  import { SbEmailField } from './_email-field';
2
2
  import { SbPhoneField } from './_phone-field';
3
- export { SbEmailField, SbPhoneField };
3
+ import { SbUsernameField } from './_username-field';
4
+ export { SbEmailField, SbPhoneField, SbUsernameField };
4
5
  declare global {
5
6
  interface HTMLElementTagNameMap {
6
7
  "sb-email-field": SbEmailField;
7
8
  "sb-phone-field": SbPhoneField;
9
+ "sb-username-field": SbUsernameField;
8
10
  }
9
11
  }