|
|
|
@ -3,19 +3,33 @@ import { ModalDialog, ModalDialogProps, ValidationResult } from '@remix-ui/modal |
|
|
|
|
import { ModalTypes } from '../../types' |
|
|
|
|
|
|
|
|
|
interface ModalWrapperProps extends ModalDialogProps { |
|
|
|
|
modalType?: ModalTypes |
|
|
|
|
defaultValue?: string |
|
|
|
|
modalType?: ModalTypes |
|
|
|
|
defaultValue?: string |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const ModalWrapper = (props: ModalWrapperProps) => { |
|
|
|
|
const [state, setState] = useState<ModalDialogProps>() |
|
|
|
|
const ref = useRef() |
|
|
|
|
const formRef = useRef() |
|
|
|
|
const data = useRef() |
|
|
|
|
|
|
|
|
|
const getFormData = () => { |
|
|
|
|
if (formRef.current) { |
|
|
|
|
const formData = new FormData(formRef.current) |
|
|
|
|
const data = {} |
|
|
|
|
for (const pair of formData.entries()) { |
|
|
|
|
data[pair[0]] = pair[1] |
|
|
|
|
} |
|
|
|
|
return data |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const onFinishPrompt = async () => { |
|
|
|
|
if (ref.current === undefined) { |
|
|
|
|
if (ref.current === undefined && formRef.current === undefined) { |
|
|
|
|
onOkFn() |
|
|
|
|
} else { |
|
|
|
|
} else if (formRef.current) { |
|
|
|
|
(props.okFn) ? props.okFn(getFormData()) : props.resolve(getFormData()) |
|
|
|
|
} else if(ref.current) { |
|
|
|
|
// @ts-ignore: Object is possibly 'null'.
|
|
|
|
|
(props.okFn) ? props.okFn(ref.current.value) : props.resolve(ref.current.value) |
|
|
|
|
} |
|
|
|
@ -43,9 +57,29 @@ const ModalWrapper = (props: ModalWrapperProps) => { |
|
|
|
|
<> |
|
|
|
|
{props.message} |
|
|
|
|
<input onChange={onInputChanged} type={props.modalType === ModalTypes.password ? 'password' : 'text'} defaultValue={defaultValue} data-id="modalDialogCustomPromp" ref={ref} className="form-control" /> |
|
|
|
|
{!validation.valid && <span className='text-warning'>{validation.message}</span>} |
|
|
|
|
</> |
|
|
|
|
) |
|
|
|
|
{validation && !validation.valid && <span className='text-warning'>{validation.message}</span>} |
|
|
|
|
</> |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const onFormChanged = () => { |
|
|
|
|
if (props.validationFn) { |
|
|
|
|
const validation = props.validationFn(getFormData()) |
|
|
|
|
setState(prevState => { |
|
|
|
|
return { ...prevState, message: createForm(validation), validation } |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const createForm = (validation: ValidationResult) => { |
|
|
|
|
return ( |
|
|
|
|
<> |
|
|
|
|
<form onChange={onFormChanged} ref={formRef}> |
|
|
|
|
{props.message} |
|
|
|
|
</form> |
|
|
|
|
{validation && !validation.valid && <span className='text-warning'>{validation.message}</span>} |
|
|
|
|
</> |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
@ -61,6 +95,14 @@ const ModalWrapper = (props: ModalWrapperProps) => { |
|
|
|
|
message: createModalMessage(props.defaultValue, { valid: true }) |
|
|
|
|
}) |
|
|
|
|
break |
|
|
|
|
case ModalTypes.form: |
|
|
|
|
setState({ |
|
|
|
|
...props, |
|
|
|
|
okFn: onFinishPrompt, |
|
|
|
|
cancelFn: onCancelFn, |
|
|
|
|
message: createForm({ valid: true }) |
|
|
|
|
}) |
|
|
|
|
break |
|
|
|
|
default: |
|
|
|
|
setState({ |
|
|
|
|
...props, |
|
|
|
|