Commit f2855693 by Torkel Ödegaard Committed by GitHub

ValueMappings: Fix issue with value mappings in override applying to all columns (#27718)

* ValueMappings: Fix issue with value mappings override creating default value mapping for all fields

* Fixed unit tests
parent f5c2b771
...@@ -17,6 +17,6 @@ export class ValueMappingsValueEditor extends React.PureComponent< ...@@ -17,6 +17,6 @@ export class ValueMappingsValueEditor extends React.PureComponent<
value = []; value = [];
} }
return <ValueMappingsEditor valueMappings={value} onChange={onChange} />; return <ValueMappingsEditor value={value} onChange={onChange} />;
} }
} }
...@@ -7,8 +7,8 @@ import { MappingType, RangeMap, SelectableValue, ValueMap, ValueMapping } from ' ...@@ -7,8 +7,8 @@ import { MappingType, RangeMap, SelectableValue, ValueMap, ValueMapping } from '
export interface Props { export interface Props {
valueMapping: ValueMapping; valueMapping: ValueMapping;
updateValueMapping: (valueMapping: ValueMapping) => void; onUpdate: (value: ValueMapping) => void;
removeValueMapping: () => void; onRemove: () => void;
} }
const MAPPING_OPTIONS: Array<SelectableValue<MappingType>> = [ const MAPPING_OPTIONS: Array<SelectableValue<MappingType>> = [
...@@ -16,27 +16,27 @@ const MAPPING_OPTIONS: Array<SelectableValue<MappingType>> = [ ...@@ -16,27 +16,27 @@ const MAPPING_OPTIONS: Array<SelectableValue<MappingType>> = [
{ value: MappingType.RangeToText, label: 'Range' }, { value: MappingType.RangeToText, label: 'Range' },
]; ];
export const MappingRow: React.FC<Props> = ({ valueMapping, updateValueMapping, removeValueMapping }) => { export const MappingRow: React.FC<Props> = ({ valueMapping, onUpdate, onRemove }) => {
const { type } = valueMapping; const { type } = valueMapping;
const onMappingValueChange = (value: string) => { const onMappingValueChange = (value: string) => {
updateValueMapping({ ...valueMapping, value: value }); onUpdate({ ...valueMapping, value: value });
}; };
const onMappingFromChange = (value: string) => { const onMappingFromChange = (value: string) => {
updateValueMapping({ ...valueMapping, from: value }); onUpdate({ ...valueMapping, from: value });
}; };
const onMappingToChange = (value: string) => { const onMappingToChange = (value: string) => {
updateValueMapping({ ...valueMapping, to: value }); onUpdate({ ...valueMapping, to: value });
}; };
const onMappingTextChange = (value: string) => { const onMappingTextChange = (value: string) => {
updateValueMapping({ ...valueMapping, text: value }); onUpdate({ ...valueMapping, text: value });
}; };
const onMappingTypeChange = (mappingType: MappingType) => { const onMappingTypeChange = (mappingType: MappingType) => {
updateValueMapping({ ...valueMapping, type: mappingType }); onUpdate({ ...valueMapping, type: mappingType });
}; };
const onKeyDown = (handler: (value: string) => void) => (e: React.KeyboardEvent<HTMLInputElement>) => { const onKeyDown = (handler: (value: string) => void) => (e: React.KeyboardEvent<HTMLInputElement>) => {
...@@ -103,7 +103,7 @@ export const MappingRow: React.FC<Props> = ({ valueMapping, updateValueMapping, ...@@ -103,7 +103,7 @@ export const MappingRow: React.FC<Props> = ({ valueMapping, updateValueMapping,
const label = ( const label = (
<HorizontalGroup justify="space-between" align="center"> <HorizontalGroup justify="space-between" align="center">
<Label>Mapping type</Label> <Label>Mapping type</Label>
<IconButton name="times" onClick={removeValueMapping} aria-label="ValueMappingsEditor remove button" /> <IconButton name="times" onClick={onRemove} aria-label="ValueMappingsEditor remove button" />
</HorizontalGroup> </HorizontalGroup>
); );
return ( return (
......
...@@ -8,5 +8,5 @@ export default { ...@@ -8,5 +8,5 @@ export default {
}; };
export const basic = () => { export const basic = () => {
return <ValueMappingsEditor valueMappings={[]} onChange={action('Mapping changed')} />; return <ValueMappingsEditor value={[]} onChange={action('Mapping changed')} />;
}; };
...@@ -11,7 +11,7 @@ const setup = (spy?: any, propOverrides?: object) => { ...@@ -11,7 +11,7 @@ const setup = (spy?: any, propOverrides?: object) => {
spy(mappings); spy(mappings);
} }
}, },
valueMappings: [ value: [
{ id: 1, type: MappingType.ValueToText, value: '20', text: 'Ok' }, { id: 1, type: MappingType.ValueToText, value: '20', text: 'Ok' },
{ id: 2, type: MappingType.RangeToText, from: '21', to: '30', text: 'Meh' }, { id: 2, type: MappingType.RangeToText, from: '21', to: '30', text: 'Meh' },
], ],
...@@ -64,11 +64,11 @@ describe('Next id to add', () => { ...@@ -64,11 +64,11 @@ describe('Next id to add', () => {
]); ]);
}); });
it('should default to 0', () => { it('should default to id 1', () => {
const onChangeSpy = jest.fn(); const onChangeSpy = jest.fn();
const wrapper = setup(onChangeSpy, { valueMappings: [] }); const wrapper = setup(onChangeSpy, { value: [] });
const add = wrapper.find('*[aria-label="ValueMappingsEditor add mapping button"]'); const add = wrapper.find('*[aria-label="ValueMappingsEditor add mapping button"]');
add.at(0).simulate('click'); add.at(0).simulate('click');
expect(onChangeSpy).toBeCalledWith([{ id: 0, type: MappingType.ValueToText, from: '', to: '', text: '' }]); expect(onChangeSpy).toBeCalledWith([{ id: 1, type: MappingType.ValueToText, from: '', to: '', text: '' }]);
}); });
}); });
...@@ -4,65 +4,48 @@ import { Button } from '../Button/Button'; ...@@ -4,65 +4,48 @@ import { Button } from '../Button/Button';
import { MappingRow } from './MappingRow'; import { MappingRow } from './MappingRow';
export interface Props { export interface Props {
valueMappings?: ValueMapping[]; value: ValueMapping[];
onChange: (valueMappings: ValueMapping[]) => void; onChange: (valueMappings: ValueMapping[]) => void;
} }
export const ValueMappingsEditor: React.FC<Props> = ({ valueMappings, onChange, children }) => { export const ValueMappingsEditor: React.FC<Props> = ({ value, onChange, children }) => {
const onAdd = () => { const onAdd = () => {
let update = valueMappings;
const defaultMapping = { const defaultMapping = {
type: MappingType.ValueToText, type: MappingType.ValueToText,
from: '', from: '',
to: '', to: '',
text: '', text: '',
}; };
const id = update && update.length > 0 ? Math.max(...update.map(v => v.id)) + 1 : 0;
if (update) { const id = Math.max(...value.map(v => v.id), 0) + 1;
update.push({
id, onChange([
...defaultMapping, ...value,
});
} else {
update = [
{ {
id, id,
...defaultMapping, ...defaultMapping,
}, },
]; ]);
}
onChange(update);
}; };
const onRemove = (index: number) => { const onRemove = (index: number) => {
const update = valueMappings; onChange(value.filter((_, i) => i !== index));
update!.splice(index, 1);
onChange(update!);
}; };
const onMappingChange = (index: number, value: ValueMapping) => { const onMappingChange = (update: ValueMapping) => {
const update = valueMappings; onChange(value.map(item => (item.id === update.id ? update : item)));
update![index] = value;
onChange(update!);
}; };
return ( return (
<> <>
{valueMappings && valueMappings.length > 0 && ( {value.map((valueMapping, index) => (
<>
{valueMappings.length > 0 &&
valueMappings.map((valueMapping, index) => (
<MappingRow <MappingRow
key={`${valueMapping.text}-${index}`} key={`${valueMapping.text}-${index}`}
valueMapping={valueMapping} valueMapping={valueMapping}
updateValueMapping={value => onMappingChange(index, value)} onUpdate={onMappingChange}
removeValueMapping={() => onRemove(index)} onRemove={() => onRemove(index)}
/> />
))} ))}
</>
)}
<Button <Button
size="sm" size="sm"
icon="plus" icon="plus"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment