Skip to content
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
122 changes: 99 additions & 23 deletions packages/patternfly-4/react-core/src/components/Select/Select.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,19 +312,21 @@ import { Select, SelectOption, SelectVariant } from '@patternfly/react-core';
class TypeaheadSelectInput extends React.Component {
constructor(props) {
super(props);
this.options = [
{ value: 'Alabama' },
{ value: 'Florida' },
{ value: 'New Jersey' },
{ value: 'New Mexico' },
{ value: 'New York' },
{ value: 'North Carolina' }
];

this.state = {
options: [
{ value: 'Alabama' },
{ value: 'Florida' },
{ value: 'New Jersey' },
{ value: 'New Mexico' },
{ value: 'New York' },
{ value: 'North Carolina' }
],
isExpanded: false,
selected: null,
isDisabled: false
isDisabled: false,
isCreatable: false,
hasOnCreateOption: false
};

this.onToggle = isExpanded => {
Expand All @@ -344,22 +346,40 @@ class TypeaheadSelectInput extends React.Component {
}
};

this.onCreateOption = (newValue) => {
this.setState({
options: [...this.state.options, {value: newValue}]
});
}

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

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

this.toggleCreatable = (checked) => {
this.setState({
isCreatable: checked
})
}

this.toggleCreateNew = (checked) => {
this.setState({
hasOnCreateOption: checked
})
}
}

render() {
const { isExpanded, selected, isDisabled } = this.state;
const { isExpanded, selected, isDisabled, isCreatable, hasOnCreateOption, options } = this.state;
const titleId = 'typeahead-select-id';
return (
<div>
Expand All @@ -377,8 +397,10 @@ class TypeaheadSelectInput extends React.Component {
ariaLabelledBy={titleId}
placeholderText="Select a state"
isDisabled={isDisabled}
isCreatable={isCreatable}
onCreateOption={hasOnCreateOption && this.onCreateOption || undefined}
>
{this.options.map((option, index) => (
{options.map((option, index) => (
<SelectOption isDisabled={option.disabled} key={index} value={option.value} />
))}
</Select>
Expand All @@ -390,6 +412,22 @@ class TypeaheadSelectInput extends React.Component {
id="toggle-disabled-typeahead"
name="toggle-disabled-typeahead"
/>
<Checkbox
label="isCreatable"
isChecked={this.state.isCreatable}
onChange={this.toggleCreatable}
aria-label="toggle creatable checkbox"
id="toggle-creatable-typeahead"
name="toggle-creatable-typeahead"
/>
<Checkbox
label="onCreateOption"
isChecked={this.state.hasOnCreateOption}
onChange={this.toggleCreateNew}
aria-label="toggle new checkbox"
id="toggle-new-typeahead"
name="toggle-new-typeahead"
/>
</div>
);
}
Expand Down Expand Up @@ -496,18 +534,26 @@ import { Select, SelectOption, SelectVariant } from '@patternfly/react-core';
class MultiTypeaheadSelectInput 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 = {
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 }
],
isExpanded: false,
selected: []
selected: [],
isCreatable: false,
hasOnCreateOption: false
};

this.onCreateOption = (newValue) => {
this.setState({
options: [...this.state.options, {value: newValue}]
});
};

this.onToggle = isExpanded => {
Expand Down Expand Up @@ -537,10 +583,22 @@ class MultiTypeaheadSelectInput extends React.Component {
isExpanded: false
});
};

this.toggleCreatable = (checked) => {
this.setState({
isCreatable: checked
})
}

this.toggleCreateNew = (checked) => {
this.setState({
hasOnCreateOption: checked
})
}
}

render() {
const { isExpanded, selected } = this.state;
const { isExpanded, selected, isCreatable, hasOnCreateOption } = this.state;
const titleId = 'multi-typeahead-select-id';

return (
Expand All @@ -558,11 +616,29 @@ class MultiTypeaheadSelectInput extends React.Component {
isExpanded={isExpanded}
ariaLabelledBy={titleId}
placeholderText="Select a state"
isCreatable={isCreatable}
onCreateOption={hasOnCreateOption && this.onCreateOption || undefined}
>
{this.options.map((option, index) => (
{this.state.options.map((option, index) => (
<SelectOption isDisabled={option.disabled} key={index} value={option.value} />
))}
</Select>
<Checkbox
label="isCreatable"
isChecked={this.state.isCreatable}
onChange={this.toggleCreatable}
aria-label="toggle creatable checkbox"
id="toggle-creatable-typeahead-multi"
name="toggle-creatable-typeahead-multi"
/>
<Checkbox
label="onCreateOption"
isChecked={this.state.hasOnCreateOption}
onChange={this.toggleCreateNew}
aria-label="toggle new checkbox"
id="toggle-new-typeahead-multi"
name="toggle-new-typeahead-multi"
/>
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,24 @@ describe('typeahead select', () => {
view.update();
expect(view).toMatchSnapshot();
});

test('test creatable option', () => {
const mockEvent = { target: { value: 'test' } } as React.ChangeEvent<HTMLInputElement>;
const view = mount(
<Select
variant={SelectVariant.typeahead}
onToggle={jest.fn()}
isExpanded
isCreatable
>
{selectOptions}
</Select>
);
const inst = view.instance() as Select;
inst.onChange(mockEvent);
view.update();
expect(view).toMatchSnapshot();
});
});

describe('typeahead multi select', () => {
Expand Down
Loading