Skip to content

Instantly share code, notes, and snippets.

@tijhaart
Created March 6, 2017 17:08
Show Gist options
  • Select an option

  • Save tijhaart/0c78ef4f29c879bfae2ad07d47a61b56 to your computer and use it in GitHub Desktop.

Select an option

Save tijhaart/0c78ef4f29c879bfae2ad07d47a61b56 to your computer and use it in GitHub Desktop.
esnextbin sketch
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ESNextbin Sketch</title>
<!-- put additional styles and scripts here -->
</head>
<body>
<!-- put markup and other contents here -->
<app />
</body>
</html>
console.clear();
import { get } from 'lodash';
const history = [];
console.log(history);
const renderOption = (option) => `<option${option.selected ? ' selected' : ''} value="${option.key}">${option.label}</option>`;
function selectItemsForSliceTypeByIndex(state) {
const { selectedIndex, selectedSliceType } = state;
const query = [
'entities',
'indices',
selectedIndex,
'sliceTypes',
selectedSliceType,
];
console.debug('query', query);
return get(state, query, []);
};
function mapToOptionElement(selectedSliceTypeValue) {
return (value) => {
//console.debug(selectedSliceTypeValue, value);
return {
selected: selectedSliceTypeValue === value,
key: value.toLowerCase(),
label: value
};
};
}
function onStateChange(state) {
const { selectedSliceTypeValue } = state;
const options = selectItemsForSliceTypeByIndex(state).map(mapToOptionElement(selectedSliceTypeValue));
console.debug('options', options);
console.log(`<select>\n\t${options.map(renderOption).join('\n\t')}\n</select>`);
};
let state = {
selectedIdentifier: 'Foo',
selectedIndex: 'i',
selectedSliceType: 'elementParameters',
selectedSliceTypeValue: 'PreviouslySelected',
entities: {
indices: {
i: {
sliceTypes: {
subsets: ['i', 'ai'],
elementParameters: ['SelectedItem', 'PreviouslySelected'],
fixedElements: ['item1', 'item2', 'item3']
}
},
j: {
sliceTypes: {
subsets: ['t'],
elementParameters: ['SelectedPeriod', 'AnotherPeriod'],
fixedElements: ['2016', '2015', '2014']
}
},
}
},
};
setState({
selectedIndex: 'i',
});
function setState(newState) {
history.push({...state, ...newState });
onStateChange(history[history.length - 1]);
};
{
"name": "esnextbin-sketch",
"version": "0.0.0",
"dependencies": {
"lodash": "4.17.4",
"babel-runtime": "6.22.0"
}
}
'use strict';
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _lodash = require('lodash');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
console.clear();
var history = [];
console.log(history);
var renderOption = function renderOption(option) {
return '<option' + (option.selected ? ' selected' : '') + ' value="' + option.key + '">' + option.label + '</option>';
};
function selectItemsForSliceTypeByIndex(state) {
var selectedIndex = state.selectedIndex;
var selectedSliceType = state.selectedSliceType;
var query = ['entities', 'indices', selectedIndex, 'sliceTypes', selectedSliceType];
console.debug('query', query);
return (0, _lodash.get)(state, query, []);
};
function mapToOptionElement(selectedSliceTypeValue) {
return function (value) {
//console.debug(selectedSliceTypeValue, value);
return {
selected: selectedSliceTypeValue === value,
key: value.toLowerCase(),
label: value
};
};
}
function onStateChange(state) {
var selectedSliceTypeValue = state.selectedSliceTypeValue;
var options = selectItemsForSliceTypeByIndex(state).map(mapToOptionElement(selectedSliceTypeValue));
console.debug('options', options);
console.log('<select>\n\t' + options.map(renderOption).join('\n\t') + '\n</select>');
};
var state = {
selectedIdentifier: 'Foo',
selectedIndex: 'i',
selectedSliceType: 'elementParameters',
selectedSliceTypeValue: 'PreviouslySelected',
entities: {
indices: {
i: {
sliceTypes: {
subsets: ['i', 'ai'],
elementParameters: ['SelectedItem', 'PreviouslySelected'],
fixedElements: ['item1', 'item2', 'item3']
}
},
j: {
sliceTypes: {
subsets: ['t'],
elementParameters: ['SelectedPeriod', 'AnotherPeriod'],
fixedElements: ['2016', '2015', '2014']
}
}
}
}
};
setState({
selectedIndex: 'i'
});
function setState(newState) {
history.push((0, _extends3.default)({}, state, newState));
onStateChange(history[history.length - 1]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment