Skip to content
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

feat(Select): add disabled flag for component #2678

Merged
merged 4 commits into from
Aug 9, 2019
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
56 changes: 44 additions & 12 deletions packages/patternfly-4/react-core/src/components/Select/Select.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ propComponents: ['Select', 'SelectOption', 'SelectGroup']
typescript: true
---

import { Select, SelectOption, SelectVariant, SelectGroup } from '@patternfly/react-core';
import { Select, SelectOption, SelectVariant, SelectGroup, Checkbox } from '@patternfly/react-core';

## Single select input

```js
import React from 'react';
import { Select, SelectOption, SelectVariant } from '@patternfly/react-core';
import { Select, SelectOption, SelectVariant, Checkbox } from '@patternfly/react-core';

class SingleSelectInput extends React.Component {
constructor(props) {
Expand All @@ -28,7 +28,8 @@ class SingleSelectInput extends React.Component {

this.state = {
isExpanded: false,
selected: null
selected: null,
isDisabled: false
};

this.onToggle = isExpanded => {
Expand All @@ -54,10 +55,16 @@ class SingleSelectInput extends React.Component {
isExpanded: false
});
};

this.toggleDisabled = (checked) => {
this.setState({
isDisabled: checked
})
}
}

render() {
const { isExpanded, selected } = this.state;
const { isExpanded, selected, isDisabled } = this.state;
const titleId = 'title-id';
return (
<div>
Expand All @@ -72,6 +79,7 @@ class SingleSelectInput extends React.Component {
selections={selected}
isExpanded={isExpanded}
ariaLabelledBy={titleId}
isDisabled={isDisabled}
>
{this.options.map((option, index) => (
<SelectOption
Expand All @@ -82,6 +90,14 @@ class SingleSelectInput extends React.Component {
/>
))}
</Select>
<Checkbox
label="isDisabled"
isChecked={this.state.isDisabled}
onChange={this.toggleDisabled}
aria-label="disabled checkbox"
id="toggle-disabled"
name="toggle-disabled"
/>
</div>
);
}
Expand Down Expand Up @@ -258,17 +274,18 @@ class TypeaheadSelectInput extends React.Component {
constructor(props) {
super(props);
this.options = [
{ value: 'Alabama', disabled: false },
{ value: 'Florida', disabled: false },
{ value: 'New Jersey', disabled: false },
{ value: 'New Mexico', disabled: false },
{ value: 'New York', disabled: false },
{ value: 'North Carolina', disabled: false }
{ value: 'Alabama' },
{ value: 'Florida' },
{ value: 'New Jersey' },
{ value: 'New Mexico' },
{ value: 'New York' },
{ value: 'North Carolina' }
];

this.state = {
isExpanded: false,
selected: null
selected: null,
isDisabled: false
};

this.onToggle = isExpanded => {
Expand All @@ -294,10 +311,16 @@ class TypeaheadSelectInput extends React.Component {
isExpanded: false
});
};

this.toggleDisabled = (checked) => {
this.setState({
isDisabled: checked
})
}
}

render() {
const { isExpanded, selected } = this.state;
const { isExpanded, selected, isDisabled } = this.state;
const titleId = 'typeahead-select-id';
return (
<div>
Expand All @@ -314,11 +337,20 @@ class TypeaheadSelectInput extends React.Component {
isExpanded={isExpanded}
ariaLabelledBy={titleId}
placeholderText="Select a state"
isDisabled={isDisabled}
>
{this.options.map((option, index) => (
<SelectOption isDisabled={option.disabled} key={index} value={option.value} />
))}
</Select>
<Checkbox
label="isDisabled"
isChecked={this.state.isDisabled}
onChange={this.toggleDisabled}
aria-label="toggle disabled checkbox"
id="toggle-disabled-typeahead"
name="toggle-disabled-typeahead"
/>
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ describe('select', () => {
expect(view).toMatchSnapshot();
});

test('renders disabled successfully', () => {
const view = mount(
<Select variant={SelectVariant.single} onSelect={jest.fn()} onToggle={jest.fn()} isDisabled>
{selectOptions}
</Select>
);
expect(view).toMatchSnapshot();
});

test('renders expanded successfully', () => {
const view = mount(
<Select variant={SelectVariant.single} onSelect={jest.fn()} onToggle={jest.fn()} isExpanded>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface SelectProps
isGrouped?: boolean;
/** Display the toggle with no border or background */
isPlain?: boolean;
/** Flag to inficate if select is disabled */
isDisabled?: boolean;
/** Title text of Select */
placeholderText?: string | React.ReactNode;
/** Selected item */
Expand Down Expand Up @@ -79,6 +81,7 @@ export class Select extends React.Component<SelectProps, SelectState> {
isExpanded: false,
isGrouped: false,
isPlain: false,
isDisabled: false,
'aria-label': '',
ariaLabelledBy: '',
ariaLabelTypeAhead: '',
Expand Down Expand Up @@ -276,6 +279,7 @@ export class Select extends React.Component<SelectProps, SelectState> {
isExpanded,
isGrouped,
isPlain,
isDisabled,
selections,
ariaLabelledBy,
ariaLabelTypeAhead,
Expand Down Expand Up @@ -309,7 +313,6 @@ export class Select extends React.Component<SelectProps, SelectState> {
</ChipGroup>
);
}

return (
<div
className={css(styles.select, isExpanded && styles.modifiers.expanded, className)}
Expand All @@ -329,6 +332,7 @@ export class Select extends React.Component<SelectProps, SelectState> {
variant={variant}
ariaLabelToggle={ariaLabelToggle}
handleTypeaheadKeys={this.handleTypeaheadKeys}
isDisabled={isDisabled}
>
{variant === SelectVariant.single && (
<div className={css(styles.selectToggleWrapper)}>
Expand Down Expand Up @@ -367,6 +371,7 @@ export class Select extends React.Component<SelectProps, SelectState> {
onChange={this.onChange}
onFocus={this.handleFocus}
autoComplete="off"
disabled={isDisabled}
/>
</div>
{selections && (
Expand All @@ -378,6 +383,7 @@ export class Select extends React.Component<SelectProps, SelectState> {
}}
aria-label={ariaLabelClear}
type="button"
disabled={isDisabled}
>
<TimesCircleIcon aria-hidden />
</button>
Expand All @@ -399,6 +405,7 @@ export class Select extends React.Component<SelectProps, SelectState> {
onChange={this.onChange}
onFocus={this.handleFocus}
autoComplete="off"
disabled={isDisabled}
/>
</div>
{selections && (Array.isArray(selections) && selections.length > 0) && (
Expand All @@ -410,6 +417,7 @@ export class Select extends React.Component<SelectProps, SelectState> {
}}
aria-label={ariaLabelClear}
type="button"
disabled={isDisabled}
>
<TimesCircleIcon aria-hidden />
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export interface SelectToggleProps extends React.HTMLProps<HTMLElement> {
isActive?: boolean;
/** Display the toggle with no border or background */
isPlain?: boolean;
/** Flag indicating if select is disabled */
isDisabled?: boolean;
/** Type of the toggle button, defaults to 'button' */
type?: 'reset' | 'button' | 'submit' | undefined;
/** Id of label for the Select aria-labelledby */
Expand All @@ -52,6 +54,7 @@ export class SelectToggle extends React.Component<SelectToggleProps> {
isHovered: false,
isActive: false,
isPlain: false,
isDisabled: false,
variant: false,
ariaLabelledBy: '',
ariaLabelToggle: '',
Expand Down Expand Up @@ -154,6 +157,7 @@ export class SelectToggle extends React.Component<SelectToggleProps> {
isActive,
isHovered,
isPlain,
isDisabled,
variant,
onToggle,
onEnter,
Expand Down Expand Up @@ -190,6 +194,7 @@ export class SelectToggle extends React.Component<SelectToggleProps> {
styles.selectToggle,
isFocused && styles.modifiers.focus,
isHovered && styles.modifiers.hover,
isDisabled && styles.modifiers.disabled,
isActive && styles.modifiers.active,
isPlain && styles.modifiers.plain,
className
Expand All @@ -201,6 +206,7 @@ export class SelectToggle extends React.Component<SelectToggleProps> {
}
}}
onKeyDown={this.onKeyDown}
disabled={isDisabled}
>
{children}
<CaretDownIcon className={css(styles.selectToggleArrow)} />
Expand All @@ -215,12 +221,15 @@ export class SelectToggle extends React.Component<SelectToggleProps> {
isFocused && styles.modifiers.focus,
isHovered && styles.modifiers.hover,
isActive && styles.modifiers.active,
isDisabled && styles.modifiers.disabled,
isPlain && styles.modifiers.plain,
isTypeahead && styles.modifiers.typeahead,
className
)}
onClick={_event => {
onToggle(true);
if(!isDisabled) {
onToggle(true);
}
}}
onKeyDown={this.onKeyDown}
>
Expand All @@ -236,6 +245,7 @@ export class SelectToggle extends React.Component<SelectToggleProps> {
onClose();
}
}}
disabled={isDisabled}
>
<CaretDownIcon className={css(styles.selectToggleArrow)} />
</button>
Expand Down
Loading