2020-06-04 14:01:31 +02:00
|
|
|
import React, {useState} from 'react';
|
|
|
|
import { SubmitInput } from './SubmitInput';
|
|
|
|
|
|
|
|
type EditableValueProps = {
|
|
|
|
value: string;
|
|
|
|
setValue: (key: string, value: string | number) => Promise<void>;
|
|
|
|
field: string;
|
|
|
|
type: 'text' | 'number';
|
2020-08-27 17:21:34 +02:00
|
|
|
options?: {
|
|
|
|
min?: number;
|
|
|
|
max?: number;
|
|
|
|
disabled?: boolean;
|
|
|
|
};
|
2020-06-04 14:01:31 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:21:34 +02:00
|
|
|
const EditableValue: React.FC<EditableValueProps> = ({ setValue, field, value: currentValue, type, options }) => {
|
2020-06-04 14:01:31 +02:00
|
|
|
const [active, setActive] = useState<boolean>(false);
|
|
|
|
|
|
|
|
const submit = (value: string | number) => {
|
|
|
|
if (value === currentValue) {
|
|
|
|
setActive(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setValue(field, value).then(() => {
|
|
|
|
setActive(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (active) {
|
|
|
|
return <SubmitInput
|
|
|
|
name={field}
|
|
|
|
autoFocus={true}
|
|
|
|
onSubmitValue={(value) => submit(type === 'number' ? parseInt(value) : value)}
|
|
|
|
onClick={event => event.stopPropagation()}
|
|
|
|
initialValue={currentValue}
|
|
|
|
type={type}
|
|
|
|
focus={true}
|
2020-08-27 17:21:34 +02:00
|
|
|
min={options?.min}
|
|
|
|
max={options?.max}
|
2020-06-04 14:01:31 +02:00
|
|
|
/>;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onClick(ev) {
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
|
|
|
setActive(true);
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:21:34 +02:00
|
|
|
if (options?.disabled) {
|
|
|
|
return <span>{currentValue}</span>;
|
|
|
|
}
|
|
|
|
|
2020-11-24 11:45:06 +01:00
|
|
|
return <a className="action-rename" onClick={onClick}>{currentValue} <span className="icon icon-rename icon-visible"></span></a>;
|
2020-06-04 14:01:31 +02:00
|
|
|
};
|
|
|
|
|
2020-08-27 17:21:34 +02:00
|
|
|
export default EditableValue;
|