Skip to content

Commit 9c75c5a

Browse files
committed
feat: add renderValue prop
1 parent 578498b commit 9c75c5a

12 files changed

+240
-152
lines changed

dist/index.es.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.es.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

+7-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/src/App.js

+41
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,47 @@ export default function App() {
996996
</div>
997997
</div>
998998
{/* ============ End of Example ============ */}
999+
<div className="tile is-parent is-vertical is-10" id="render-value">
1000+
<div className="subtitle">
1001+
<a href="#edit-on-enter">Customizing Rendered Value</a>
1002+
</div>
1003+
<p className="content">
1004+
You may want to customize the renderd value. You may want to parse
1005+
it or apply some regular expressions before rendering.
1006+
<br />
1007+
You can pass <code>renderValue</code> function prop for this
1008+
purpose. EdiText will call that function before rendering the value.
1009+
</p>
1010+
<div className="columns">
1011+
<div className="column is-half">
1012+
<SyntaxHighlighter language="javascript" showLineNumbers={true}>
1013+
{examples.example25}
1014+
</SyntaxHighlighter>
1015+
</div>
1016+
<div className="column">
1017+
<div className="subtitle">Output</div>
1018+
<EdiText
1019+
value="this the website of the matrix: www.whatisthematrix.com"
1020+
onSave={handleSave}
1021+
submitOnEnter
1022+
editing={true}
1023+
renderValue={(value) => {
1024+
const regexp = /(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]/gi
1025+
const valueWithParsedURLS = value.replace(
1026+
regexp,
1027+
"<a target='_blank' href='http://$&'>$&</a>"
1028+
)
1029+
return (
1030+
<span
1031+
dangerouslySetInnerHTML={{ __html: valueWithParsedURLS }}
1032+
/>
1033+
)
1034+
}}
1035+
/>
1036+
</div>
1037+
</div>
1038+
</div>
1039+
{/* ============ End of Example ============ */}
9991040
</div>
10001041
<footer className="footer" style={{ padding: 20 }}>
10011042
<div className="content has-text-centered">

example/src/_examples.js

+18
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,21 @@ export const example24 = `<input
413413
tabIndex={8}
414414
/>
415415
`
416+
export const example25 = `<EdiText
417+
value="Unfortunately, no one can be told what The Matrix is."
418+
onSave={handleSave}
419+
submitOnEnter
420+
renderValue={(value) => {
421+
const regexp = /(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]/gi
422+
const valueWithParsedURLS = value.replace(
423+
regexp,
424+
"<a target='_blank' href='http://$&'>$&</a>"
425+
)
426+
return (
427+
<span
428+
dangerouslySetInnerHTML={{ __html: valueWithParsedURLS }}
429+
/>
430+
)
431+
}}
432+
/>
433+
`

example/src/index.css

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
.my-react-header{
2-
font-size: 1.2em;
3-
border: 1.5px solid #6CDAF7;
4-
padding: 5px;
5-
border-radius: 5px;
6-
background-color: #1F2229;
7-
color: #6CDAF7;
1+
.my-react-header {
2+
font-size: 1.2em;
3+
border: 1.5px solid #6cdaf7;
4+
padding: 5px;
5+
border-radius: 5px;
6+
background-color: #1f2229;
7+
color: #6cdaf7;
88
}
99
a {
10-
color: #1F2229;
10+
color: crimson;
1111
}
1212
a:hover {
1313
color: palevioletred;

index.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ declare module 'react-editext' {
193193
* NOTE: This requires the element to be in focus.
194194
*/
195195
startEditingOnEnter?: boolean
196+
/**
197+
* Custom render method for the content in the view mode.
198+
* Use this prop to customize the displayed value in view mode.
199+
* The return value from this function will be rendered in view mode.
200+
* You can return string or JSX. Both are allowed.
201+
*/
202+
renderValue?: (value: any) => any
196203
}
197204

198205
export default class EdiText extends React.Component<EdiTextProps, any> {

src/index.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ export default class EdiText extends Component {
272272
hideIcons,
273273
buttonsAlign,
274274
editOnViewClick,
275-
showButtonsOnHover
275+
showButtonsOnHover,
276+
renderValue
276277
} = this.props
277278
// calculate edit button classes
278279
const editButtonDefaultClasses = classnames(
@@ -293,6 +294,10 @@ export default class EdiText extends Component {
293294
const viewClickHandler = editOnViewClick
294295
? this.handleActivateEditMode
295296
: undefined
297+
const value =
298+
typeof renderValue === 'function'
299+
? renderValue(this.state.value)
300+
: this.state.value
296301
return (
297302
<div
298303
className={viewContainerClass}
@@ -310,7 +315,7 @@ export default class EdiText extends Component {
310315
onClick={viewClickHandler}
311316
editext="view"
312317
>
313-
{this.state.value}
318+
{value}
314319
</div>
315320
)}
316321
<div className={buttonsContainerClass}>
@@ -335,7 +340,7 @@ export default class EdiText extends Component {
335340
onClick={viewClickHandler}
336341
editext={dataAttributes.viewContainer}
337342
>
338-
{this.state.value}
343+
{value}
339344
</div>
340345
)}
341346
</div>
@@ -428,5 +433,6 @@ EdiText.propTypes = {
428433
// from creating duplicate code for both `inputProps` and `viewProps`
429434
tabIndex: PropTypes.any,
430435
startEditingOnFocus: PropTypes.bool,
431-
startEditingOnEnter: PropTypes.bool
436+
startEditingOnEnter: PropTypes.bool,
437+
renderValue: PropTypes.func
432438
}

0 commit comments

Comments
 (0)