@skyux/forms 15.0.0-alpha.7 → 15.0.0-alpha.9

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.
@@ -8,6 +8,9 @@ import { SkyIconHarness } from '@skyux/icon/testing';
8
8
 
9
9
  /**
10
10
  * Harness for interacting with a character counter indicator component in tests.
11
+ *
12
+ * @deprecated Use `SkyInputBoxHarness` with an input box that sets the
13
+ * `characterLimit` input instead.
11
14
  */
12
15
  class SkyCharacterCounterIndicatorHarness extends SkyComponentHarness {
13
16
  /**
@@ -122,6 +125,75 @@ class SkyFormErrorsHarness extends SkyComponentHarness {
122
125
  }
123
126
  }
124
127
 
128
+ /**
129
+ * @internal
130
+ */
131
+ async function getCharacterLimitRatio(labelEl) {
132
+ const text = (await labelEl.text()).trim();
133
+ const parts = text.split('/');
134
+ const countText = parts[0]?.trim();
135
+ const limitText = parts[1]?.trim();
136
+ const count = Number(countText);
137
+ const limit = Number(limitText);
138
+ // The label mirrors whatever `characterLimit` coerced to, which allows
139
+ // decimals and negatives, so don't restrict this to digits.
140
+ if (parts.length !== 2 ||
141
+ !countText ||
142
+ !limitText ||
143
+ isNaN(count) ||
144
+ isNaN(limit)) {
145
+ throw new Error(`Expected the character limit label to read "count/limit" but found "${text}".`);
146
+ }
147
+ return { count, limit };
148
+ }
149
+ /**
150
+ * @internal
151
+ */
152
+ async function isOverCharacterLimit(labelEl) {
153
+ return await labelEl.hasClass('sky-error-label');
154
+ }
155
+
156
+ /**
157
+ * Setting `characterLimit` on `sky-input-box` used to render a
158
+ * `sky-character-counter-indicator`, but now renders a
159
+ * `sky-input-box-character-limit`. Since `SkyInputBoxHarness.getCharacterCounter()`
160
+ * is public API, it must still return a `SkyCharacterCounterIndicatorHarness`,
161
+ * so this subclass preserves that type while reading the new markup. Delete it
162
+ * when the deprecated character counter component is removed.
163
+ *
164
+ * @internal
165
+ */
166
+ class SkyInputBoxCharacterCounterIndicatorCompatHarness extends SkyCharacterCounterIndicatorHarness {
167
+ static { this.hostSelector = 'sky-input-box-character-limit'; }
168
+ #getLabel = this.locatorFor('.sky-input-box-character-limit-label');
169
+ async getCharacterCount() {
170
+ return (await getCharacterLimitRatio(await this.#getLabel())).count;
171
+ }
172
+ async getCharacterCountLimit() {
173
+ return (await getCharacterLimitRatio(await this.#getLabel())).limit;
174
+ }
175
+ async isOverLimit() {
176
+ return await isOverCharacterLimit(await this.#getLabel());
177
+ }
178
+ }
179
+
180
+ /**
181
+ * @internal
182
+ */
183
+ class SkyInputBoxCharacterLimitHarness extends SkyComponentHarness {
184
+ static { this.hostSelector = 'sky-input-box-character-limit'; }
185
+ #getLabel = this.locatorFor('.sky-input-box-character-limit-label');
186
+ async getCharacterCount() {
187
+ return (await getCharacterLimitRatio(await this.#getLabel())).count;
188
+ }
189
+ async getCharacterLimit() {
190
+ return (await getCharacterLimitRatio(await this.#getLabel())).limit;
191
+ }
192
+ async isOverCharacterLimit() {
193
+ return await isOverCharacterLimit(await this.#getLabel());
194
+ }
195
+ }
196
+
125
197
  /**
126
198
  * Harness for interacting with an input box component in tests.
127
199
  */
@@ -130,6 +202,7 @@ class SkyInputBoxHarness extends SkyQueryableComponentHarness {
130
202
  * @internal
131
203
  */
132
204
  static { this.hostSelector = 'sky-input-box'; }
205
+ #getCharacterLimit = this.locatorForOptional(SkyInputBoxCharacterLimitHarness);
133
206
  #getHintText = this.locatorForOptional('.sky-input-box-hint-text');
134
207
  #getLabel = this.locatorForOptional('.sky-control-label');
135
208
  #getWrapper = this.locatorFor('.sky-input-box');
@@ -152,16 +225,46 @@ class SkyInputBoxHarness extends SkyQueryableComponentHarness {
152
225
  /**
153
226
  * Gets the character counter indicator for the input box or throws an error if
154
227
  * a character limit is not specified.
228
+ *
229
+ * @deprecated Use `getCharacterCount()`, `getCharacterLimit()`, and
230
+ * `isOverCharacterLimit()` instead. Those methods require the `characterLimit`
231
+ * input, so an input box that projects a `sky-character-counter-indicator` component
232
+ * must migrate to that input first.
155
233
  */
156
234
  async getCharacterCounter() {
157
- const characterCounter = await this.locatorForOptional(new HarnessPredicate(SkyCharacterCounterIndicatorHarness, {
235
+ const characterCounter = (await this.locatorForOptional(new HarnessPredicate(SkyCharacterCounterIndicatorHarness, {
158
236
  ancestor: '.sky-input-box-label-wrapper',
159
- }))();
237
+ }))()) ??
238
+ (await this.locatorForOptional(SkyInputBoxCharacterCounterIndicatorCompatHarness)());
160
239
  if (!characterCounter) {
161
240
  throw new Error('The input box does not have a character limit specified.');
162
241
  }
163
242
  return characterCounter;
164
243
  }
244
+ /**
245
+ * Gets the current character count, or throws an error if the input box does
246
+ * not set the `characterLimit` input.
247
+ */
248
+ async getCharacterCount() {
249
+ const harness = await this.#getCharacterLimitOrThrow();
250
+ return await harness.getCharacterCount();
251
+ }
252
+ /**
253
+ * Gets the character limit, or throws an error if the input box does not set
254
+ * the `characterLimit` input.
255
+ */
256
+ async getCharacterLimit() {
257
+ const harness = await this.#getCharacterLimitOrThrow();
258
+ return await harness.getCharacterLimit();
259
+ }
260
+ /**
261
+ * Whether the character count has exceeded the character limit. Throws an
262
+ * error if the input box does not set the `characterLimit` input.
263
+ */
264
+ async isOverCharacterLimit() {
265
+ const harness = await this.#getCharacterLimitOrThrow();
266
+ return await harness.isOverCharacterLimit();
267
+ }
165
268
  /**
166
269
  * Gets a list of status indicator harnesses for errors not automatically
167
270
  * handled by input box.
@@ -297,6 +400,13 @@ class SkyInputBoxHarness extends SkyQueryableComponentHarness {
297
400
  const host = await this.host();
298
401
  return await host.hasClass('sky-form-field-stacked');
299
402
  }
403
+ async #getCharacterLimitOrThrow() {
404
+ const characterLimit = await this.#getCharacterLimit();
405
+ if (!characterLimit) {
406
+ throw new Error('The input box does not have a character limit specified.');
407
+ }
408
+ return characterLimit;
409
+ }
300
410
  async #getElementTextOrDefault(el) {
301
411
  return (await el?.text())?.trim() ?? '';
302
412
  }