Skip to content

Commit

Permalink
Get magic select list type switching working #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Pollock committed Jul 7, 2018
1 parent 13e685a commit e01c83a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 17 deletions.
44 changes: 40 additions & 4 deletions src/components/fields/magic-select/MagicSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class MagicSelect extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
currentList: props.defaultList,
currentListType: props.defaultList,
isOpen: props.isOpen
};
this.onChange = this.onChange.bind(this);
Expand All @@ -34,6 +34,7 @@ export class MagicSelect extends React.PureComponent {
this.renderItem = this.renderItem.bind(this);
this.onInputBlur = this.onInputBlur.bind(this);
this.onChangeListType = this.onChangeListType.bind(this);
this.listTypeOptions = this.listTypeOptions.bind(this);
}

/**
Expand Down Expand Up @@ -74,7 +75,10 @@ export class MagicSelect extends React.PureComponent {
* @param {String}newType
*/
onChangeListType(newType){
this.setState({currentList:newType});
if( ! this.state.isOpen ){
this.setState({isOpen:true});
}
this.setState({currentListType:newType});
}

/**
Expand Down Expand Up @@ -103,7 +107,7 @@ export class MagicSelect extends React.PureComponent {
if( optionsOrEmpty(this.props.options).length){
items = optionsOrEmpty(this.props.options);
}
else if ('system' === this.state.currentList ) {
else if ('system' === this.state.currentListType ) {
items = optionsOrEmpty(this.props.systemTagsList);
} else {
items = optionsOrEmpty(this.props.fieldsList);
Expand All @@ -125,6 +129,23 @@ export class MagicSelect extends React.PureComponent {

}

listTypeOptions(){
return [
{
value: 'fields',
label: '%',
ariaLabel: 'Select from field values'
},
{
value: 'system',
label: '{}',
ariaLabel: 'Select from system values'
},
]
}



/**
* Render MagicSelect component
* @return {*}
Expand All @@ -134,7 +155,22 @@ export class MagicSelect extends React.PureComponent {
<div
className={classNames('magic-select', this.props.className)}
>

<ButtonGroup
onChange={this.onChangeListType}
options={[
{
value: 'fields',
label: '%',
ariaLabel: 'Select from field values'
},
{
value: 'system',
label: '{}',
ariaLabel: 'Select from system values'
},
]}
value={this.state.currentListType}
/>
<Autocomplete
getItemValue={(item) => item.value}
items={this.items()}
Expand Down
27 changes: 14 additions & 13 deletions src/components/fields/magic-select/MagicSelect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,31 +199,32 @@ describe('MagicSelect component', () => {
onValueChange={genericChangeHandler}
fieldsList={[
{
label: '0',
value: 0
label: 'Field One',
value: '%fldOne%'
},
{
label: '1',
value: 1
label: 'Field Two',
value: '%fldTwo%'
},
{
label: '3',
value: 3
}
label: 'Field Three',
value: '%fldThree%'
},
]}
systemTagsList={[
{
label: '3',
value: 3
label: 'User First Name',
value: '{user:first_name}'
}
]}
isOpen={true}
value={''}
/>
);
expect(component.find('.magic-input-option')).toHaveLength(3);
expect(component.find('.magic-input-option').children().length).toBe(6);
});

it('Uses systemTagsList prop if no options prop and currentList state is system', () => {
it.skip('Uses systemTagsList prop if no options prop and currentListType state is system', () => {
const component = mount(
<MagicSelect
id={'magic-5'}
Expand Down Expand Up @@ -252,8 +253,8 @@ describe('MagicSelect component', () => {
isOpen={true}
/>
);
component.setState({currentList: 'system'});
expect(component.find('.magic-input-option')).toHaveLength(1);
component.setState({currentListType: 'system'});
expect(component.find('.magic-input-option').length).toHaveLength(1);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ exports[`MagicSelect component Matches snapshot 1`] = `
<div
className="magic-select"
>
<div
role="group"
>
<button
aria-label="Select from field values"
className="selected"
onClick={[Function]}
>
%
</button>
<button
aria-label="Select from system values"
className="not-selected"
onClick={[Function]}
>
{}
</button>
</div>
<div
style={
Object {
Expand Down

0 comments on commit e01c83a

Please sign in to comment.