Skip to content

Commit becb77f

Browse files
Add nanoid, remove deprecated shortId (#3170)
fixing test issues Co-authored-by: Sanal Panicker <[email protected]>
1 parent 067ba2c commit becb77f

9 files changed

+51
-48
lines changed

components/component-docs.json

+7-11
Original file line numberDiff line numberDiff line change
@@ -1403,7 +1403,11 @@
14031403
}
14041404
},
14051405
"required": false,
1406-
"description": "Event Callbacks\n* `onClickBack`: Called when the Back link is clicked.\n* `onClickHelp`: Called when the Help link is clicked.\n_Tested with Mocha testing._"
1406+
"description": "Event Callbacks\n* `onClickBack`: Called when the Back link is clicked.\n* `onClickHelp`: Called when the Help link is clicked.\n_Tested with Mocha testing._",
1407+
"defaultValue": {
1408+
"value": "{}",
1409+
"computed": false
1410+
}
14071411
},
14081412
"iconCategory": {
14091413
"type": {
@@ -9233,11 +9237,7 @@
92339237
}
92349238
},
92359239
"required": false,
9236-
"description": "**Assistive text for accessibility.**\n* `activeDescriptor`: The text that appears alongside a link that is currently active.",
9237-
"defaultValue": {
9238-
"value": "{\n\tactiveDescriptor: 'Current page:',\n}",
9239-
"computed": false
9240-
}
9240+
"description": "**Assistive text for accessibility.**\n* `activeDescriptor`: The text that appears alongside a link that is currently active."
92419241
},
92429242
"className": {
92439243
"type": {
@@ -9279,11 +9279,7 @@
92799279
"name": "string"
92809280
},
92819281
"required": false,
9282-
"description": "The `href` attribute of the link. Please pass in bookmarkable URLs from your routing library. Use `GlobalNavigationBarButton` if a \"real URL\" is not desired. If the `onClick` callback is specified this URL will still be prevented from changing the browser's location.",
9283-
"defaultValue": {
9284-
"value": "'#'",
9285-
"computed": false
9286-
}
9282+
"description": "The `href` attribute of the link. Please pass in bookmarkable URLs from your routing library. Use `GlobalNavigationBarButton` if a \"real URL\" is not desired. If the `onClick` callback is specified this URL will still be prevented from changing the browser's location."
92879283
},
92889284
"id": {
92899285
"type": {

components/date-picker/__tests__/__snapshots__/datepicker.dom-snapshot-test.jsx.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,7 @@ exports[`Datepicker
19231923
required={false}
19241924
role="textbox"
19251925
type="text"
1926-
value="2014"
1926+
value=""
19271927
/>
19281928
<span
19291929
className="slds-icon_container slds-input__icon slds-input__icon_right"
@@ -2783,7 +2783,7 @@ exports[`Datepicker Default DOM Snapshot 1`] = `
27832783
required={false}
27842784
role="textbox"
27852785
type="text"
2786-
value="2014"
2786+
value=""
27872787
/>
27882788
<span
27892789
className="slds-icon_container slds-input__icon slds-input__icon_right"
@@ -3486,7 +3486,7 @@ exports[`Datepicker Default HTML Snapshot 1`] = `
34863486
<div class=\\"slds-form-element__control\\">
34873487
<div class=\\"slds-combobox_container\\">
34883488
<div class=\\"slds-combobox slds-dropdown-trigger slds-dropdown-trigger_click ignore-react-onclickoutside slds-shrink-none\\" role=\\"combobox\\" aria-expanded=\\"false\\" aria-haspopup=\\"listbox\\">
3489-
<div class=\\"slds-combobox__form-element slds-input-has-icon slds-input-has-icon_right\\" role=\\"none\\"><input type=\\"text\\" autoComplete=\\"off\\" class=\\"slds-input slds-combobox__input\\" id=\\"sample-datepicker-year-picklist\\" placeholder=\\"Select an Option\\" readonly=\\"\\" role=\\"textbox\\" aria-autocomplete=\\"list\\" value=\\"2014\\" /><span class=\\"slds-icon_container slds-input__icon slds-input__icon_right\\"><svg aria-hidden=\\"true\\" class=\\"slds-icon slds-icon_x-small slds-icon-text-default\\">
3489+
<div class=\\"slds-combobox__form-element slds-input-has-icon slds-input-has-icon_right\\" role=\\"none\\"><input type=\\"text\\" autoComplete=\\"off\\" class=\\"slds-input slds-combobox__input\\" id=\\"sample-datepicker-year-picklist\\" placeholder=\\"Select an Option\\" readonly=\\"\\" role=\\"textbox\\" aria-autocomplete=\\"list\\" value=\\"\\" /><span class=\\"slds-icon_container slds-input__icon slds-input__icon_right\\"><svg aria-hidden=\\"true\\" class=\\"slds-icon slds-icon_x-small slds-icon-text-default\\">
34903490
<use href=\\"/assets/icons/utility-sprite/svg/symbols.svg#down\\"></use>
34913491
</svg></span></div>
34923492
</div>
@@ -3749,7 +3749,7 @@ exports[`ErrorPicker 1`] = `
37493749
required={false}
37503750
role="textbox"
37513751
type="text"
3752-
value="2014"
3752+
value=""
37533753
/>
37543754
<span
37553755
className="slds-icon_container slds-input__icon slds-input__icon_right"

package-lock.json

+13-21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"lodash.isfunction": "^3.0.9",
101101
"lodash.reject": "^4.6.0",
102102
"memoize-one": "^6.0.0",
103+
"nanoid": "^5.0.9",
103104
"popper.js": "^1.10.8",
104105
"prop-types": ">=15.7.2",
105106
"react-contenteditable": "^3.3.5",
@@ -108,7 +109,6 @@
108109
"react-onclickoutside": "^6.10.0",
109110
"react-required-if": "^1.0.3",
110111
"react-text-truncate": "^0.19.0",
111-
"shortid": "^2.2.15",
112112
"warning": "^4.0.3"
113113
},
114114
"peerDependencies": {

tests/__mocks__/nanoid-mock.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function getUniqueMockNanoId() {
2+
const randomString = Array.from({ length: 10 }, () => {
3+
const isUppercase = Math.random() < 0.5;
4+
const charCode = isUppercase
5+
? 65 + Math.floor(Math.random() * 26)
6+
: 97 + Math.floor(Math.random() * 26);
7+
return String.fromCharCode(charCode);
8+
}).join('');
9+
return `mockId_${randomString}`;
10+
}
11+
12+
module.exports = function generateId() {
13+
return getUniqueMockNanoId();
14+
};

tests/story-based-tests.accessibility-test.config.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"testEnvironment": "node",
55
"setupFilesAfterEnv": ["<rootDir>/tests/axe-puppeteer-matcher.js"],
66
"moduleNameMapper": {
7-
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
8-
"<rootDir>/tests/__mocks__/file-mock.js"
7+
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/tests/__mocks__/file-mock.js",
8+
".*/utilities/generate-id$": "<rootDir>/tests/__mocks__/nanoid-mock.js"
99
},
1010
"setupFiles": [
1111
"<rootDir>/.jest/register-context.js",

tests/story-based-tests.dom-snapshot-test.config.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"testRegex": "/(components|tests)/.*\\.dom-snapshot-test\\.jsx?$",
44
"testEnvironment": "node",
55
"moduleNameMapper": {
6-
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
7-
"<rootDir>/tests/__mocks__/file-mock.js"
6+
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/tests/__mocks__/file-mock.js",
7+
".*/utilities/generate-id$": "<rootDir>/tests/__mocks__/nanoid-mock.js"
88
},
99
"setupFiles": [
1010
"<rootDir>/.jest/register-context.js",
@@ -16,6 +16,7 @@
1616
"/.tmp-amd/",
1717
"/.tmp-commonjs/",
1818
"/.tmp-es/",
19+
"/.tmp-esm/",
1920
"/.tmp-npm/"
2021
],
2122
"transform": {

tests/story-based-tests.snapshot-test.config.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"testRegex": "/(components|tests)/.*\\.snapshot-test\\.jsx?$",
44
"testEnvironment": "node",
55
"moduleNameMapper": {
6-
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
7-
"<rootDir>/tests/__mocks__/file-mock.js"
6+
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/tests/__mocks__/file-mock.js",
7+
".*/utilities/generate-id$": "<rootDir>/tests/__mocks__/nanoid-mock.js"
88
},
99
"setupFiles": [
1010
"<rootDir>/.jest/register-context.js",
@@ -16,6 +16,7 @@
1616
"/.tmp-amd/",
1717
"/.tmp-commonjs/",
1818
"/.tmp-es/",
19+
"/.tmp-esm/",
1920
"/.tmp-npm/"
2021
],
2122
"transform": {

utilities/generate-id.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
1-
import shortid from 'shortid';
1+
import { nanoid } from 'nanoid';
22

33
/**
44
* Generate unique ID that is also a valid CSS identifier.
55
* @returns {string} The unique ID.
66
*/
77
function generateId() {
88
/**
9-
* `shortid.generate()` by itself will generates IDs that
9+
* `nanoid()` by itself will generates IDs that
1010
* are not always valid [1] CSS identifiers (e.g. will start
1111
* with a digit). So, when other parts of the codebase will
1212
* use those IDs as a selector, a `SyntaxError`² will be thrown
1313
* (i.e. `'#...' is not a valid selector`).
1414
*
1515
* The `slds-` prefix is used so to keep things simple.
16-
* `shortid.characters()` requires "a string of all 64 unique
16+
* `nanoid()` requires "a string of all 64 unique
1717
* characters"³, which means that in order to generate valid
1818
* CSS identifiers a-z, A-Z, and some "ISO 10646 characters
1919
* U+00A0 and higher"¹ will need to be included.
2020
*
2121
* [1] https://www.w3.org/TR/CSS21/syndata.html#value-def-identifier
2222
* [2] https://dom.spec.whatwg.org/#selectors
23-
* [3] https://github.com/dylang/shortid/blob/937ce2c8dd7001baec1785cb8dce6e6fe1bcf61f/README.md#shortidcharactersstring
2423
*/
2524

26-
return `slds-${shortid.generate()}`;
25+
return `slds-${nanoid()}`;
2726
}
2827

2928
export default generateId;

0 commit comments

Comments
 (0)