-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make feature text modal an own component
- Loading branch information
1 parent
0bac307
commit 9ce529d
Showing
2 changed files
with
109 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import * as React from 'react'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { Modal, ModalProps } from 'antd'; | ||
import TextArea from 'antd/lib/input/TextArea'; | ||
|
||
import Feature from 'ol/Feature'; | ||
import Geometry from 'ol/geom/Geometry'; | ||
|
||
import StringUtil from '@terrestris/base-util/dist/StringUtil/StringUtil'; | ||
|
||
type OwnProps = { | ||
feature: Feature<Geometry>|null; | ||
onOk: () => void; | ||
onCancel: () => void; | ||
/** | ||
* Maximal length of feature label. | ||
* If exceeded label will be divided into multiple lines. Optional. | ||
*/ | ||
maxLabelLineLength?: number; | ||
} | ||
|
||
export type FeatureLabelModalProps = OwnProps & Omit<ModalProps, 'closable'|'visible'|'onOk'|'onCancel'>; | ||
|
||
export const FeatureLabelModal: React.FC<FeatureLabelModalProps> = ({ | ||
feature, | ||
onOk, | ||
onCancel, | ||
maxLabelLineLength, | ||
...passThroughProps | ||
}) => { | ||
const [label, setLabel] = useState<string>(null); | ||
const [showPrompt, setShowPrompt] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
if (feature) { | ||
setLabel(feature.get('label') ?? ''); | ||
setShowPrompt(true); | ||
} else { | ||
setShowPrompt(false); | ||
} | ||
}, [feature]) | ||
|
||
const onOkInternal = () => { | ||
feature.set('label', maxLabelLineLength !== undefined ? | ||
StringUtil.stringDivider(label, maxLabelLineLength, '\n') : | ||
label | ||
); | ||
onOk(); | ||
}; | ||
|
||
return (showPrompt && <Modal | ||
visible={showPrompt} | ||
closable={false} | ||
onOk={onOkInternal} | ||
onCancel={onCancel} | ||
{...passThroughProps} | ||
> | ||
<TextArea | ||
value={label} | ||
onChange={e => setLabel(e.target.value)} | ||
autoSize | ||
/> | ||
</Modal>); | ||
} |