Skip to content

Commit

Permalink
feat(Select): add demo, ts demo, cypress test. add disabled to typeah…
Browse files Browse the repository at this point in the history
…ead form sub-elements
  • Loading branch information
kmcfaul committed Aug 9, 2019
1 parent cd20989 commit fcfb875
Show file tree
Hide file tree
Showing 8 changed files with 640 additions and 116 deletions.
160 changes: 160 additions & 0 deletions packages/patternfly-4/react-core/src/components/Select/Select.md
Expand Up @@ -88,6 +88,88 @@ class SingleSelectInput extends React.Component {
}
```

## Disabled single select input

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

class DisabledSingleSelectInput extends React.Component {
constructor(props) {
super(props);
this.options = [
{ value: 'Choose...', disabled: false, isPlaceholder: true },
{ value: 'Mr', disabled: false },
{ value: 'Miss', disabled: false },
{ value: 'Mrs', disabled: false },
{ value: 'Ms', disabled: false },
{ value: 'Dr', disabled: false },
{ value: 'Other', disabled: false }
];

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

this.onToggle = isExpanded => {
this.setState({
isExpanded
});
};

this.onSelect = (event, selection, isPlaceholder) => {
if (isPlaceholder) this.clearSelection();
else {
this.setState({
selected: selection,
isExpanded: false
});
console.log('selected:', selection);
}
};

this.clearSelection = () => {
this.setState({
selected: null,
isExpanded: false
});
};
}

render() {
const { isExpanded, selected } = this.state;
const titleId = 'title-id';
return (
<div>
<span id={titleId} hidden>
Title
</span>
<Select
variant={SelectVariant.single}
aria-label="Select Input"
onToggle={this.onToggle}
onSelect={this.onSelect}
selections={selected}
isExpanded={isExpanded}
ariaLabelledBy={titleId}
isDisabled
>
{this.options.map((option, index) => (
<SelectOption
isDisabled={option.disabled}
key={index}
value={option.value}
isPlaceholder={option.isPlaceholder}
/>
))}
</Select>
</div>
);
}
}
```

## Checkbox select input

```js
Expand Down Expand Up @@ -325,6 +407,84 @@ class TypeaheadSelectInput extends React.Component {
}
```

## Disabled typeahead select input

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

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 }
];

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

this.onToggle = isExpanded => {
this.setState({
isExpanded
});
};

this.onSelect = (event, selection, isPlaceholder) => {
if (isPlaceholder) this.clearSelection();
else {
this.setState({
selected: selection,
isExpanded: false
});
console.log('selected:', selection);
}
};

this.clearSelection = () => {
this.setState({
selected: null,
isExpanded: false
});
};
}

render() {
const { isExpanded, selected } = this.state;
const titleId = 'typeahead-select-id';
return (
<div>
<span id={titleId} hidden>
Select a state
</span>
<Select
variant={SelectVariant.typeahead}
aria-label="Select a state"
onToggle={this.onToggle}
onSelect={this.onSelect}
onClear={this.clearSelection}
selections={selected}
isExpanded={isExpanded}
ariaLabelledBy={titleId}
placeholderText="Select a state"
isDisabled
>
{this.options.map((option, index) => (
<SelectOption isDisabled={option.disabled} key={index} value={option.value} />
))}
</Select>
</div>
);
}
}
```

## Typeahead select input - custom filtering

```js
Expand Down
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
Expand Up @@ -371,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 @@ -382,6 +383,7 @@ export class Select extends React.Component<SelectProps, SelectState> {
}}
aria-label={ariaLabelClear}
type="button"
disabled={isDisabled}
>
<TimesCircleIcon aria-hidden />
</button>
Expand All @@ -403,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 @@ -414,6 +417,7 @@ export class Select extends React.Component<SelectProps, SelectState> {
}}
aria-label={ariaLabelClear}
type="button"
disabled={isDisabled}
>
<TimesCircleIcon aria-hidden />
</button>
Expand Down
Expand Up @@ -227,7 +227,9 @@ export class SelectToggle extends React.Component<SelectToggleProps> {
className
)}
onClick={_event => {
onToggle(true);
if(!isDisabled) {
onToggle(true);
}
}}
onKeyDown={this.onKeyDown}
>
Expand Down

0 comments on commit fcfb875

Please sign in to comment.