* Removed console log * Used current scope as default parent scope for fields * Finished editable fields on people show page * Added stories * Console log * Lint
67 lines
1.7 KiB
TypeScript
67 lines
1.7 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
|
|
import { InplaceInputPhoneDisplayMode } from '@/ui/display/component/InplaceInputPhoneDisplayMode';
|
|
import { EditableField } from '@/ui/editable-field/components/EditableField';
|
|
import { FieldContext } from '@/ui/editable-field/states/FieldContext';
|
|
import { InplaceInputText } from '@/ui/inplace-input/components/InplaceInputText';
|
|
import { RecoilScope } from '@/ui/recoil-scope/components/RecoilScope';
|
|
|
|
type OwnProps = {
|
|
icon?: React.ReactNode;
|
|
placeholder?: string;
|
|
value: string | null | undefined;
|
|
onSubmit?: (newValue: string) => void;
|
|
};
|
|
|
|
export function PhoneEditableField({
|
|
icon,
|
|
placeholder,
|
|
value,
|
|
onSubmit,
|
|
}: OwnProps) {
|
|
const [internalValue, setInternalValue] = useState(value);
|
|
|
|
useEffect(() => {
|
|
setInternalValue(value);
|
|
}, [value]);
|
|
|
|
async function handleChange(newValue: string) {
|
|
setInternalValue(newValue);
|
|
}
|
|
|
|
async function handleSubmit() {
|
|
if (!internalValue) return;
|
|
|
|
onSubmit?.(internalValue);
|
|
}
|
|
|
|
async function handleCancel() {
|
|
setInternalValue(value);
|
|
}
|
|
|
|
return (
|
|
<RecoilScope SpecificContext={FieldContext}>
|
|
<EditableField
|
|
onSubmit={handleSubmit}
|
|
onCancel={handleCancel}
|
|
iconLabel={icon}
|
|
editModeContent={
|
|
<InplaceInputText
|
|
placeholder={placeholder ?? ''}
|
|
autoFocus
|
|
value={internalValue ?? ''}
|
|
onChange={(newValue: string) => {
|
|
handleChange(newValue);
|
|
}}
|
|
/>
|
|
}
|
|
displayModeContent={
|
|
<InplaceInputPhoneDisplayMode value={internalValue ?? ''} />
|
|
}
|
|
isDisplayModeContentEmpty={!(internalValue !== '')}
|
|
useEditButton
|
|
/>
|
|
</RecoilScope>
|
|
);
|
|
}
|