This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[terra-signature] Example update (#4071)
- Loading branch information
1 parent
e7481a3
commit 7c269b0
Showing
7 changed files
with
220 additions
and
52 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
50 changes: 50 additions & 0 deletions
50
packages/terra-core-docs/src/terra-dev-site/doc/signature/example/ImageSignature.jsx
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,50 @@ | ||
import React from 'react'; | ||
import classNames from 'classnames/bind'; | ||
import Signature from 'terra-signature'; | ||
import PropTypes from 'prop-types'; | ||
import styles from './SignatureExample.module.scss'; | ||
|
||
const cx = classNames.bind(styles); | ||
|
||
const propTypes = { onClearSignature: PropTypes.func }; | ||
|
||
const ImageSignature = (props) => { | ||
const handleFileSelect = () => { | ||
const canvas = document.getElementById('upload'); | ||
if (canvas) { | ||
const ctx = canvas.getContext('2d'); | ||
ctx.clearRect(0, 0, 1000, 100); | ||
const imgFile = document.getElementById('file-select').files[0]; | ||
const img = new Image(); | ||
img.onload = () => { | ||
ctx.drawImage(img, 10, 0, 200, canvas.offsetHeight); | ||
}; | ||
img.src = URL.createObjectURL(imgFile); | ||
} | ||
}; | ||
|
||
const handleClear = () => { | ||
const canvas = document.getElementById('upload'); | ||
if (canvas) { | ||
const ctx = canvas.getContext('2d'); | ||
ctx.clearRect(0, 0, 1000, 100); | ||
document.getElementById('file-select').value = ''; | ||
props.onClearSignature(); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<div className={cx('signature-wrapper')}> | ||
<Signature id="upload" /> | ||
</div> | ||
<br /> | ||
<button type="button" onClick={handleClear}>Clear</button> | ||
<input className={cx('input-image')} id="file-select" type="file" accept="image/*" onChange={handleFileSelect} /> | ||
</div> | ||
); | ||
}; | ||
|
||
ImageSignature.propTypes = propTypes; | ||
|
||
export default ImageSignature; |
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
60 changes: 60 additions & 0 deletions
60
packages/terra-core-docs/src/terra-dev-site/doc/signature/example/TextSignature.jsx
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,60 @@ | ||
import React from 'react'; | ||
import classNames from 'classnames/bind'; | ||
import Signature from 'terra-signature'; | ||
import PropTypes from 'prop-types'; | ||
import styles from './SignatureExample.module.scss'; | ||
|
||
const cx = classNames.bind(styles); | ||
|
||
const propTypes = { onClearSignature: PropTypes.func, onAddSignature: PropTypes.func }; | ||
|
||
const TextSignature = (props) => { | ||
const [showTextArea, setshowTextArea] = React.useState(true); | ||
const textArea = React.useRef(); | ||
|
||
const handleClear = () => { | ||
if (showTextArea) { | ||
if (textArea && textArea.current) textArea.current.value = null; | ||
return; | ||
} | ||
const canvas = document.getElementById('text'); | ||
const ctx = canvas?.getContext('2d'); | ||
if (ctx) { | ||
ctx.font = '30px Arial'; | ||
ctx.clearRect(0, 0, 1000, 100); | ||
} | ||
if (textArea && textArea.current) textArea.current.value = null; | ||
setshowTextArea(true); | ||
props.onClearSignature(); | ||
}; | ||
|
||
const handleAdd = () => { | ||
if (textArea && textArea.current.value) { | ||
const canvas = document.getElementById('text'); | ||
const ctx = canvas?.getContext('2d'); | ||
if (ctx && canvas) { | ||
ctx.font = '30px Arial'; | ||
ctx.fillText(textArea.current.value, 100, 50); | ||
canvas.setAttribute('aria-label', textArea.current.value); | ||
setshowTextArea(false); | ||
props.onAddSignature(true); | ||
} | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<div className={cx('signature-wrapper')}> | ||
{(showTextArea) ? <textarea aria-label="Enter Name" ref={(node) => { textArea.current = node; }} className={cx('text-area-style')} /> : null} | ||
<Signature id="text" /> | ||
</div> | ||
<br /> | ||
<div className={cx('clear-button')}> | ||
<button type="button" onClick={handleClear}>Clear</button> | ||
<button className={cx('add-button')} type="button" onClick={handleAdd}>Add</button> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
TextSignature.propTypes = propTypes; | ||
export default TextSignature; |