Skip to content

Add CSP Nonce option to Select component #3260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/pages/props/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export default function Api() {
selectOption: OptionType => void,
selectProps: any,
setValue: (ValueType, ActionTypes) => void,
emotion: any,
}

// passed as the second argument to \`onChange\`
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
"url": "https://github.com/JedWatson/react-select.git"
},
"dependencies": {
"@emotion/memoize": "^0.7.1",
"classnames": "^2.2.5",
"emotion": "^9.1.2",
"create-emotion": "^10.0.4",
"memoize-one": "^4.0.0",
"prop-types": "^15.6.0",
"raf": "^3.4.0",
Expand Down
19 changes: 15 additions & 4 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import React, { Component, type ElementRef, type Node } from 'react';

import memoizeOne from 'memoize-one';
import memoize from '@emotion/memoize';
import createEmotion from 'create-emotion';
import { MenuPlacer } from './components/Menu';
import isEqual from './internal/react-fast-compare';

Expand Down Expand Up @@ -242,6 +244,8 @@ export type Props = {
tabSelectsValue: boolean,
/* The value of the select; reflected by the selected option */
value: ValueType,
/* A CSP Nonce which will be used in injected style sheets */
nonce?: string
};

export const defaultProps = {
Expand Down Expand Up @@ -305,6 +309,8 @@ type ElRef = ElementRef<*>;

let instanceId = 1;

const getEmotion = memoize(nonce => createEmotion(nonce ? { nonce } : {}));

export default class Select extends Component<Props, State> {
static defaultProps = defaultProps;
state = {
Expand Down Expand Up @@ -369,6 +375,8 @@ export default class Select extends Component<Props, State> {
const selectValue = cleanValue(value);
const menuOptions = this.buildMenuOptions(props, selectValue);

this.emotion = getEmotion(props.nonce);

this.state.menuOptions = menuOptions;
this.state.selectValue = selectValue;
}
Expand Down Expand Up @@ -721,6 +729,7 @@ export default class Select extends Component<Props, State> {
setValue,
selectProps: props,
theme: this.getTheme(),
emotion: this.emotion
};
}

Expand Down Expand Up @@ -1370,6 +1379,7 @@ export default class Select extends Component<Props, State> {
disabled={isDisabled}
tabIndex={tabIndex}
value=""
emotion={this.emotion}
/>
);
}
Expand Down Expand Up @@ -1402,6 +1412,7 @@ export default class Select extends Component<Props, State> {
theme={theme}
type="text"
value={inputValue}
emotion={this.emotion}
{...ariaAttributes}
/>
);
Expand All @@ -1427,8 +1438,8 @@ export default class Select extends Component<Props, State> {

if (!this.hasValue() || !controlShouldRenderValue) {
return inputValue ? null : (
<Placeholder
{...commonProps}
<Placeholder
{...commonProps}
key="placeholder"
isDisabled={isDisabled}
isFocused={isFocused}
Expand Down Expand Up @@ -1679,7 +1690,7 @@ export default class Select extends Component<Props, State> {
onTopArrive={onMenuScrollToTop}
onBottomArrive={onMenuScrollToBottom}
>
<ScrollBlock isEnabled={menuShouldBlockScroll}>
<ScrollBlock emotion={this.emotion} isEnabled={menuShouldBlockScroll}>
<MenuList
{...commonProps}
innerRef={this.getMenuListRef}
Expand Down Expand Up @@ -1750,7 +1761,7 @@ export default class Select extends Component<Props, State> {
renderLiveRegion() {
if (!this.state.isFocused) return null;
return (
<A11yText aria-live="assertive">
<A11yText emotion={this.emotion} aria-live="assertive">
<p id="aria-selection-event">&nbsp;{this.state.ariaLiveSelection}</p>
<p id="aria-context">&nbsp;{this.constructAriaLiveMessage()}</p>
</A11yText>
Expand Down
3,283 changes: 3,282 additions & 1 deletion src/__tests__/__snapshots__/Async.test.js.snap

Large diffs are not rendered by default.

3,283 changes: 3,282 additions & 1 deletion src/__tests__/__snapshots__/AsyncCreatable.test.js.snap

Large diffs are not rendered by default.

Loading