mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-25 00:07:47 +01:00
fix: add common component input-list-field
This commit is contained in:
parent
2beef592a5
commit
57bdf63043
49
frontend/src/component/common/input-list-field.jsx
Normal file
49
frontend/src/component/common/input-list-field.jsx
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import PropTypes from 'prop-types';
|
||||||
|
import { Textfield } from 'react-mdl';
|
||||||
|
|
||||||
|
function InputListField({ label, values = [], error, name, updateValues, placeholder = '', onBlur = () => {} }) {
|
||||||
|
const handleChange = evt => {
|
||||||
|
const values = evt.target.value.split(/,\s?/);
|
||||||
|
const trimmedValues = values.map(v => v.trim());
|
||||||
|
updateValues(trimmedValues);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleKeyDown = evt => {
|
||||||
|
if (evt.key === 'Backspace') {
|
||||||
|
const currentValue = evt.target.value;
|
||||||
|
if (currentValue.endsWith(', ')) {
|
||||||
|
evt.preventDefault();
|
||||||
|
const value = currentValue.slice(0, -2);
|
||||||
|
updateValues(value.split(/,\s*/));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Textfield
|
||||||
|
name={name}
|
||||||
|
floatingLabel
|
||||||
|
error={error}
|
||||||
|
placeholder={placeholder}
|
||||||
|
value={values ? values.join(', ') : ''}
|
||||||
|
onKeyDown={handleKeyDown}
|
||||||
|
onChange={handleChange}
|
||||||
|
onBlur={onBlur}
|
||||||
|
label={label}
|
||||||
|
style={{ width: '100%' }}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
InputListField.propTypes = {
|
||||||
|
label: PropTypes.string.isRequired,
|
||||||
|
values: PropTypes.array,
|
||||||
|
error: PropTypes.string,
|
||||||
|
placeholder: PropTypes.string,
|
||||||
|
name: PropTypes.string.isRequired,
|
||||||
|
updateValues: PropTypes.func.isRequired,
|
||||||
|
onBlur: PropTypes.func,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default InputListField;
|
@ -1,7 +1,7 @@
|
|||||||
import React, { Component } from 'react';
|
import React, { Component } from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { Textfield } from 'react-mdl';
|
import { Textfield } from 'react-mdl';
|
||||||
import Select from './select';
|
import Select from '../../common/select';
|
||||||
|
|
||||||
import StrategyInputPercentage from './strategy-input-percentage';
|
import StrategyInputPercentage from './strategy-input-percentage';
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ import {
|
|||||||
Tooltip,
|
Tooltip,
|
||||||
Icon,
|
Icon,
|
||||||
} from 'react-mdl';
|
} from 'react-mdl';
|
||||||
import MySelect from '../form/select';
|
import MySelect from '../../common/select';
|
||||||
import { trim } from '../form/util';
|
import { trim } from '../form/util';
|
||||||
import styles from './variant.scss';
|
import styles from './variant.scss';
|
||||||
import OverrideConfig from './override-config';
|
import OverrideConfig from './override-config';
|
||||||
@ -38,9 +38,7 @@ function AddVariant({ showDialog, closeDialog, save, validateName, editVariant,
|
|||||||
setPayload(editVariant.payload);
|
setPayload(editVariant.payload);
|
||||||
}
|
}
|
||||||
if (editVariant.overrides) {
|
if (editVariant.overrides) {
|
||||||
setOverrides(
|
setOverrides(editVariant.overrides);
|
||||||
editVariant.overrides.map(o => ({ contextName: o.contextName, values: o.values.join(', ') }))
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
setOverrides([]);
|
setOverrides([]);
|
||||||
}
|
}
|
||||||
@ -80,10 +78,7 @@ function AddVariant({ showDialog, closeDialog, save, validateName, editVariant,
|
|||||||
overrides: overrides
|
overrides: overrides
|
||||||
.map(o => ({
|
.map(o => ({
|
||||||
contextName: o.contextName,
|
contextName: o.contextName,
|
||||||
values: o.values
|
values: o.values,
|
||||||
.split(',')
|
|
||||||
.map(v => v.trim())
|
|
||||||
.filter(v => v),
|
|
||||||
}))
|
}))
|
||||||
.filter(o => o.values && o.values.length > 0),
|
.filter(o => o.values && o.values.length > 0),
|
||||||
};
|
};
|
||||||
@ -110,7 +105,7 @@ function AddVariant({ showDialog, closeDialog, save, validateName, editVariant,
|
|||||||
closeDialog();
|
closeDialog();
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateOverrideOption = index => e => {
|
const updateOverrideType = index => e => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
setOverrides(
|
setOverrides(
|
||||||
overrides.map((o, i) => {
|
overrides.map((o, i) => {
|
||||||
@ -122,14 +117,25 @@ function AddVariant({ showDialog, closeDialog, save, validateName, editVariant,
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const removeOverrideOption = index => e => {
|
const updateOverrideValues = (index, values) => {
|
||||||
|
setOverrides(
|
||||||
|
overrides.map((o, i) => {
|
||||||
|
if (i === index) {
|
||||||
|
o.values = values;
|
||||||
|
}
|
||||||
|
return o;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const removeOverride = index => e => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
setOverrides(overrides.filter((o, i) => i !== index));
|
setOverrides(overrides.filter((o, i) => i !== index));
|
||||||
};
|
};
|
||||||
|
|
||||||
const onAddOverride = e => {
|
const onAddOverride = e => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
setOverrides([...overrides, ...[{ contextName: 'userId', values: '' }]]);
|
setOverrides([...overrides, ...[{ contextName: 'userId', values: [] }]]);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -210,8 +216,10 @@ function AddVariant({ showDialog, closeDialog, save, validateName, editVariant,
|
|||||||
|
|
||||||
<OverrideConfig
|
<OverrideConfig
|
||||||
overrides={overrides}
|
overrides={overrides}
|
||||||
removeOverrideOption={removeOverrideOption}
|
removeOverride={removeOverride}
|
||||||
updateOverrideOption={updateOverrideOption}
|
updateOverrideType={updateOverrideType}
|
||||||
|
updateOverrideValues={updateOverrideValues}
|
||||||
|
updateValues={updateOverrideValues}
|
||||||
/>
|
/>
|
||||||
<a href="#add-override" onClick={onAddOverride}>
|
<a href="#add-override" onClick={onAddOverride}>
|
||||||
<small>Add override</small>
|
<small>Add override</small>
|
||||||
|
@ -1,14 +1,19 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { Textfield, Grid, Cell, IconButton } from 'react-mdl';
|
import { Grid, Cell, IconButton } from 'react-mdl';
|
||||||
import MySelect from '../form/select';
|
import MySelect from '../../common/select';
|
||||||
|
import InputListField from '../../common/input-list-field';
|
||||||
|
|
||||||
const overrideOptions = [
|
const overrideOptions = [
|
||||||
{ key: 'userId', label: 'userId' },
|
{ key: 'userId', label: 'userId' },
|
||||||
{ key: 'appName', label: 'appName' },
|
{ key: 'appName', label: 'appName' },
|
||||||
];
|
];
|
||||||
|
|
||||||
function OverrideConfig({ overrides, updateOverrideOption, removeOverrideOption }) {
|
function OverrideConfig({ overrides, updateOverrideType, updateOverrideValues, removeOverride }) {
|
||||||
|
const updateValues = i => values => {
|
||||||
|
updateOverrideValues(i, values);
|
||||||
|
};
|
||||||
|
|
||||||
return overrides.map((o, i) => (
|
return overrides.map((o, i) => (
|
||||||
<Grid noSpacing key={`override=${i}`}>
|
<Grid noSpacing key={`override=${i}`}>
|
||||||
<Cell col={3}>
|
<Cell col={3}>
|
||||||
@ -17,22 +22,21 @@ function OverrideConfig({ overrides, updateOverrideOption, removeOverrideOption
|
|||||||
label="Context Field"
|
label="Context Field"
|
||||||
value={o.contextName}
|
value={o.contextName}
|
||||||
options={overrideOptions}
|
options={overrideOptions}
|
||||||
onChange={updateOverrideOption(i)}
|
onChange={updateOverrideType(i)}
|
||||||
/>
|
/>
|
||||||
</Cell>
|
</Cell>
|
||||||
<Cell col={8}>
|
<Cell col={8}>
|
||||||
<Textfield
|
<InputListField
|
||||||
floatingLabel
|
label="Values (v1, v2, ...)"
|
||||||
label="Values"
|
|
||||||
name="values"
|
name="values"
|
||||||
placeholder="val1, val2, ..."
|
placeholder=""
|
||||||
style={{ width: '100%' }}
|
style={{ width: '100%' }}
|
||||||
value={o.values}
|
values={o.values}
|
||||||
onChange={updateOverrideOption(i)}
|
updateValues={updateValues(i)}
|
||||||
/>
|
/>
|
||||||
</Cell>
|
</Cell>
|
||||||
<Cell col={1} style={{ textAlign: 'right' }}>
|
<Cell col={1} style={{ textAlign: 'right' }}>
|
||||||
<IconButton name="delete" onClick={removeOverrideOption(i)} />
|
<IconButton name="delete" onClick={removeOverride(i)} />
|
||||||
</Cell>
|
</Cell>
|
||||||
</Grid>
|
</Grid>
|
||||||
));
|
));
|
||||||
@ -40,8 +44,9 @@ function OverrideConfig({ overrides, updateOverrideOption, removeOverrideOption
|
|||||||
|
|
||||||
OverrideConfig.propTypes = {
|
OverrideConfig.propTypes = {
|
||||||
overrides: PropTypes.array.isRequired,
|
overrides: PropTypes.array.isRequired,
|
||||||
updateOverrideOption: PropTypes.func.isRequired,
|
updateOverrideType: PropTypes.func.isRequired,
|
||||||
removeOverrideOption: PropTypes.func.isRequired,
|
updateOverrideValues: PropTypes.func.isRequired,
|
||||||
|
removeOverride: PropTypes.func.isRequired,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default OverrideConfig;
|
export default OverrideConfig;
|
||||||
|
Loading…
Reference in New Issue
Block a user