feat: multiple UX improvements to labels (#7358)

Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
Co-authored-by: Nithin David Thomas <1277421+nithindavid@users.noreply.github.com>
This commit is contained in:
Shivam Mishra
2023-06-25 18:49:49 +05:30
committed by GitHub
parent 4f8ce7b597
commit 996325f35b
16 changed files with 485 additions and 20 deletions

View File

@@ -106,6 +106,11 @@ export const hasPressedCommandPlusKKey = e => {
return e.metaKey && e.keyCode === 75;
};
/**
* Returns a string representation of the hotkey pattern based on the provided event object.
* @param {KeyboardEvent} e - The keyboard event object.
* @returns {string} - The hotkey pattern string.
*/
export const buildHotKeys = e => {
const key = e.key.toLowerCase();
if (['shift', 'meta', 'alt', 'control'].includes(key)) {
@@ -127,3 +132,30 @@ export const buildHotKeys = e => {
hotKeyPattern += key;
return hotKeyPattern;
};
/**
* Determines whether the active element is typeable.
*
* @param {KeyboardEvent} e - The keyboard event object.
* @returns {boolean} `true` if the active element is typeable, `false` otherwise.
*
* @example
* document.addEventListener('keydown', e => {
* if (isActiveElementTypeable(e)) {
* handleTypeableElement(e);
* }
* });
*/
export const isActiveElementTypeable = e => {
/** @type {HTMLElement | null} */
// @ts-ignore
const activeElement = e.target || document.activeElement;
return !!(
activeElement?.tagName === 'INPUT' ||
activeElement?.tagName === 'NINJA-KEYS' ||
activeElement?.tagName === 'TEXTAREA' ||
activeElement?.contentEditable === 'true' ||
activeElement?.className?.includes('ProseMirror')
);
};

View File

@@ -0,0 +1,22 @@
export const labelSanitizePattern = /[^a-zA-Z0-9_-]/g;
export const spacesPattern = /\s+/g;
/**
* Sanitizes a label by removing unwanted characters and replacing spaces with hyphens.
*
* @param {string | undefined | null} label - The label to sanitize.
* @returns {string} The sanitized label.
*
* @example
* const label = 'My Label 123';
* const sanitizedLabel = sanitizeLabel(label); // 'my-label-123'
*/
export const sanitizeLabel = (label = '') => {
if (!label) return '';
return label
.trim()
.toLowerCase()
.replace(spacesPattern, '-')
.replace(labelSanitizePattern, '');
};

View File

@@ -4,6 +4,7 @@ import {
hasPressedShift,
hasPressedCommand,
buildHotKeys,
isActiveElementTypeable,
} from '../KeyboardHelpers';
describe('#KeyboardHelpers', () => {
@@ -39,3 +40,37 @@ describe('#KeyboardHelpers', () => {
});
});
});
describe('isActiveElementTypeable', () => {
it('should return true if the active element is an input element', () => {
const event = { target: document.createElement('input') };
const result = isActiveElementTypeable(event);
expect(result).toBe(true);
});
it('should return true if the active element is a textarea element', () => {
const event = { target: document.createElement('textarea') };
const result = isActiveElementTypeable(event);
expect(result).toBe(true);
});
it('should return true if the active element is a contentEditable element', () => {
const element = document.createElement('div');
element.contentEditable = 'true';
const event = { target: element };
const result = isActiveElementTypeable(event);
expect(result).toBe(true);
});
it('should return false if the active element is not typeable', () => {
const event = { target: document.createElement('div') };
const result = isActiveElementTypeable(event);
expect(result).toBe(false);
});
it('should return false if the active element is null', () => {
const event = { target: null };
const result = isActiveElementTypeable(event);
expect(result).toBe(false);
});
});

View File

@@ -0,0 +1,44 @@
import { sanitizeLabel } from '../sanitizeData';
describe('sanitizeLabel', () => {
it('should return an empty string when given an empty string', () => {
const label = '';
const sanitizedLabel = sanitizeLabel(label);
expect(sanitizedLabel).toEqual('');
});
it('should remove leading and trailing whitespace', () => {
const label = ' My Label ';
const sanitizedLabel = sanitizeLabel(label);
expect(sanitizedLabel).toEqual('my-label');
});
it('should convert all characters to lowercase', () => {
const label = 'My Label';
const sanitizedLabel = sanitizeLabel(label);
expect(sanitizedLabel).toEqual('my-label');
});
it('should replace spaces with hyphens', () => {
const label = 'My Label 123';
const sanitizedLabel = sanitizeLabel(label);
expect(sanitizedLabel).toEqual('my-label-123');
});
it('should remove any characters that are not alphanumeric, underscore, or hyphen', () => {
const label = 'My_Label!123';
const sanitizedLabel = sanitizeLabel(label);
expect(sanitizedLabel).toEqual('my_label123');
});
it('should handle null and undefined input', () => {
const nullLabel = null;
const undefinedLabel = undefined;
// @ts-ignore - intentionally passing null and undefined to test
const sanitizedNullLabel = sanitizeLabel(nullLabel);
const sanitizedUndefinedLabel = sanitizeLabel(undefinedLabel);
expect(sanitizedNullLabel).toEqual('');
expect(sanitizedUndefinedLabel).toEqual('');
});
});