Skip to content

Commit 375c200

Browse files
iowillhoittweettypography
authored andcommitted
Generate unique IDs (#347)
* Replaces uniqueId with shortid on Input * Replaces uniqueId with shortid in DataTable * Replaces uniqueId with shortid on Checkbox * Replaces uniqueId with shortid on Menu Dropdown There is enough linting to be done in this file that it warrants it's own PR * adds shortid to card component * removes commented test * generate shortid on button * Update to the latest SLDS beta
1 parent 2b4d6a6 commit 375c200

File tree

10 files changed

+50
-46
lines changed

10 files changed

+50
-46
lines changed

components/button/index.jsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ import omit from 'lodash.omit';
1717

1818
import { BUTTON } from '../../utilities/constants';
1919

20+
// ### shortid
21+
// [npmjs.com/package/shortid](https://www.npmjs.com/package/shortid)
22+
// shortid is a short, non-sequential, url-friendly, unique id generator
23+
import shortid from 'shortid';
24+
2025
const displayName = BUTTON;
2126
const propTypes = {
2227
/**
@@ -65,6 +70,7 @@ const propTypes = {
6570
variant: React.PropTypes.oneOf(['base', 'neutral', 'brand', 'destructive', 'icon', 'inverse', 'icon-inverse'])
6671
};
6772
const defaultProps = {
73+
id: shortid.generate(),
6874
disabled: false,
6975
hint: false,
7076
iconSize: 'medium',

components/card/index.jsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ import Empty from './empty';
3232
// Removes the need for `PropTypes`.
3333
const { PropTypes } = React;
3434

35+
// ### shortid
36+
// [npmjs.com/package/shortid](https://www.npmjs.com/package/shortid)
37+
// shortid is a short, non-sequential, url-friendly, unique id generator
38+
import shortid from 'shortid';
39+
3540
import { CARD } from '../../utilities/constants';
3641

3742
// Allow for predicatable DOM queries with `querySelectorAll(cssClasses.base)`
@@ -84,11 +89,12 @@ const Card = React.createClass({
8489
/**
8590
* Set the HTML `id` of the card. This also sets the `id` of the filter and the header actions.
8691
*/
87-
id: PropTypes.string.isRequired
92+
id: PropTypes.string
8893
},
8994

9095
getDefaultProps () {
9196
return {
97+
id: shortid.generate(),
9298
heading: 'Related Items'
9399
};
94100
},

components/data-table/index.jsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1818
// ### React
1919
import React from 'react';
2020

21+
// ### shortid
22+
// [npmjs.com/package/shortid](https://www.npmjs.com/package/shortid)
23+
// shortid is a short, non-sequential, url-friendly, unique id generator
24+
import shortid from 'shortid';
25+
2126
// ### classNames
2227
import classNames from 'classnames';
2328

@@ -33,9 +38,6 @@ import isFunction from 'lodash.isfunction';
3338
// ### reject
3439
import reject from 'lodash.reject';
3540

36-
// ### uniqueId
37-
import uniqueId from 'lodash.uniqueid';
38-
3941
// This component's `checkProps` which issues warnings to developers about properties when in development mode (similar to React's built in development tools)
4042
import checkProps from './check-props';
4143

@@ -86,12 +88,15 @@ const DataTable = React.createClass({
8688
* Class names to be added to the table.
8789
*/
8890
className: PropTypes.oneOfType([PropTypes.array, PropTypes.object, PropTypes.string]),
91+
/**
92+
* A unique ID is needed in order to support keyboard navigation and ARIA support.
93+
*/
8994
id: PropTypes.string,
90-
/**
95+
/**
9196
* The collection of items to render in the table.
9297
*/
9398
items: PropTypes.array.isRequired,
94-
/**
99+
/**
95100
* This function fires when the selection of rows changes.
96101
*/
97102
onChange: PropTypes.func,
@@ -124,7 +129,7 @@ const DataTable = React.createClass({
124129
getDefaultProps () {
125130
return {
126131
bordered: false,
127-
id: uniqueId(`${DATA_TABLE}-`),
132+
id: shortid.generate(),
128133
selection: [],
129134
selectRows: false,
130135
stacked: false,

components/forms/checkbox/index.jsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ import React from 'react';
1919
// ### isFunction
2020
import isFunction from 'lodash.isfunction';
2121

22-
// ### uniqueId
23-
import uniqueId from 'lodash.uniqueid';
22+
// ### shortid
23+
// [npmjs.com/package/shortid](https://www.npmjs.com/package/shortid)
24+
// shortid is a short, non-sequential, url-friendly, unique id generator
25+
import shortid from 'shortid';
2426

2527
// ### Event Helpers
2628
import { KEYS, EventUtil } from '../../../utilities';
@@ -68,6 +70,9 @@ const Checkbox = React.createClass({
6870
* Message to display when the Checkbox is in an error state. When this is present, also visually highlights the component as in error.
6971
*/
7072
errorText: React.PropTypes.string,
73+
/**
74+
* A unique ID is needed in order to support keyboard navigation and ARIA support.
75+
*/
7176
id: PropTypes.string,
7277
/**
7378
* An optional label for the Checkbox.
@@ -85,7 +90,7 @@ const Checkbox = React.createClass({
8590

8691
getDefaultProps () {
8792
return {
88-
id: uniqueId(`${FORMS_CHECKBOX}-`)
93+
id: shortid.generate()
8994
};
9095
},
9196

components/forms/input/index.jsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1717
// React is an external dependency of the project.
1818
import React from 'react';
1919

20+
// ### shortid
21+
// [npmjs.com/package/shortid](https://www.npmjs.com/package/shortid)
22+
// shortid is a short, non-sequential, url-friendly, unique id generator
23+
import shortid from 'shortid';
24+
2025
// ### classNames
2126
// [github.com/JedWatson/classnames](https://github.com/JedWatson/classnames)
2227
// This project uses `classnames`, "a simple javascript utility for conditionally
@@ -99,7 +104,7 @@ const Input = React.createClass({
99104
/**
100105
* Every input must have a unique ID in order to support keyboard navigation and ARIA support.
101106
*/
102-
id: PropTypes.string.isRequired,
107+
id: PropTypes.string,
103108
/**
104109
* This label appears above the input.
105110
*/
@@ -160,6 +165,7 @@ const Input = React.createClass({
160165

161166
getDefaultProps () {
162167
return {
168+
id: shortid.generate(),
163169
iconPosition: 'left',
164170
type: 'text'
165171
};

components/forms/input/inline.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ const InlineEdit = React.createClass({
194194

195195
handleBlur () {
196196
if (!this.willSave) {
197-
this.willSave = setTimeout(this.saveEdits, 100);
197+
this.willSave = setTimeout(this.saveEdits, 200);
198198
}
199199
},
200200

components/menu-dropdown/index.jsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ import ListItemLabel from "../menu-list/list-item-label";
1818

1919
import { MENU_DROPDOWN } from '../../utilities/constants';
2020

21-
// ### uniqueId
22-
import uniqueId from 'lodash.uniqueid';
21+
// ### shortid
22+
// [npmjs.com/package/shortid](https://www.npmjs.com/package/shortid)
23+
// shortid is a short, non-sequential, url-friendly, unique id generator
24+
import shortid from 'shortid';
2325

2426
const displayName = MENU_DROPDOWN;
2527
const propTypes = {
@@ -43,6 +45,9 @@ const propTypes = {
4345
* Delay on menu closing.
4446
*/
4547
hoverCloseDelay: React.PropTypes.number,
48+
/**
49+
* A unique ID is needed in order to support keyboard navigation and ARIA support.
50+
*/
4651
id: React.PropTypes.string,
4752
label: React.PropTypes.string,
4853
/**
@@ -74,7 +79,7 @@ const defaultProps = {
7479
checkmark: false,
7580
disabled: false,
7681
hoverCloseDelay: 300,
77-
id: uniqueId(`${MENU_DROPDOWN}-`),
82+
id: shortid.generate(),
7883
openOn: "click",
7984
modal: true,
8085
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@
5858
"react-highlighter": "^0.3.2",
5959
"react-modal": "^0.6.1",
6060
"react-onclickoutside": "^4.5.0",
61+
"shortid": "^2.2.6",
6162
"tether": "1.1.0",
6263
"tether-drop": "1.2.2",
6364
"warning": "^2.1.0"
6465
},
6566
"devDependencies": {
6667
"@kadira/storybook": "^1.27.0",
67-
"@salesforce-ux/design-system": "git+ssh://[email protected]/salesforce-ux/design-system-internal.git#v2.1.0-beta.2-dist",
68+
"@salesforce-ux/design-system": "git+ssh://[email protected]/salesforce-ux/design-system-internal.git#v2.1.0-beta.3-dist",
6869
"@salesforce-ux/icons": "4.0.0",
6970
"async": "^2.0.0-rc.5",
7071
"babel": "^6.5.2",

stories/forms/input/index.jsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,18 @@ storiesOf(FORMS_INPUT, module)
1010
.addDecorator(getStory => <div className="slds-p-around--medium">{getStory()}</div>)
1111
.add('Standard Input', () => (
1212
<Input
13-
id="unique-id-1"
1413
label="Input Label"
1514
placeholder="Placeholder Text"
1615
/>
1716
))
1817
.add('Assistive Text, No Label', () => (
1918
<Input
20-
id="unique-id-1"
2119
assistiveText="Assistive Text"
2220
placeholder="Placeholder Text"
2321
/>
2422
))
2523
.add('Input with Clickable Icon', () => (
2624
<Input
27-
id="unique-id-2"
2825
label="Input Label"
2926
iconName="close"
3027
iconCategory="utility"
@@ -35,15 +32,13 @@ storiesOf(FORMS_INPUT, module)
3532
))
3633
.add('Read Only Input', () => (
3734
<Input
38-
id="unique-id-3"
3935
label="Input Label"
4036
readOnly
4137
value="Read Only Value"
4238
/>
4339
))
4440
.add('Required Input in Error State', () => (
4541
<Input
46-
id="unique-id-4"
4742
label="Input Label"
4843
required
4944
errorText="Error Message"

tests/input/input.test.jsx

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const {
1717

1818
describe('SLDS INPUT **************************************************', () => {
1919
const defaultProps = {
20-
id: 'unique-id-2',
2120
placeholder: 'Placeholder Text'
2221
};
2322

@@ -148,28 +147,4 @@ describe('SLDS INPUT **************************************************', () =>
148147
expect(error.textContent).to.equal('Error Message');
149148
});
150149
});
151-
152-
153-
// describe('Input with Clickable Icon', () => {
154-
// let component;
155-
// let input;
156-
// let label;
157-
// let icon;
158-
159-
// beforeEach(() => {
160-
// component = getInput({ label: 'Input Label' });
161-
// input = findRenderedDOMComponentWithTag(component, 'input');
162-
// inputWrapper = findRenderedDOMComponentWithClass(component, 'slds-form-element__control');
163-
// label = findRenderedDOMComponentWithClass(component, 'slds-form-element__label');
164-
// icon = findRenderedDOMComponentWithTag(component, 'svg');
165-
// });
166-
167-
// afterEach(() => {
168-
// removeInput();
169-
// });
170-
171-
// // input wrapper has class "slds-input-has-icon"
172-
// // icon has class "slds-input__icon"
173-
// // icon can be clicked (Simulate)
174-
// });
175150
});

0 commit comments

Comments
 (0)