mon-entreprise/source/components/AttachDictionary.js

44 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

import marked from 'Engine/marked'
2018-01-08 15:07:26 +00:00
import { path } from 'ramda'
import React, { Component } from 'react'
2017-10-13 13:47:16 +00:00
import './Dictionary.css'
import Overlay from './Overlay'
// On ajoute à la section la possibilité d'ouvrir un panneau d'explication des termes.
// Il suffit à la section d'appeler une fonction fournie en lui donnant du JSX
export let AttachDictionary = dictionary => Decorated =>
class withDictionary extends Component {
state = {
term: null,
explanation: null
}
2018-11-13 11:23:21 +00:00
onClick = e => {
let term = e.target.dataset['termDefinition'],
explanation = path([term, 'description'], dictionary)
if (!term) return null
this.setState({ explanation, term })
}
renderExplanationMarkdown(explanation, term) {
return marked(`### Mécanisme : ${term}\n\n${explanation}`)
}
render() {
let { explanation, term } = this.state
return (
2018-11-13 11:23:21 +00:00
<div onClick={this.onClick}>
<Decorated {...this.props} />
{explanation && (
<Overlay
onClose={() => this.setState({ term: null, explanation: null })}>
<div
id="dictionaryPanel"
dangerouslySetInnerHTML={{
__html: this.renderExplanationMarkdown(explanation, term)
}}
/>
</Overlay>
)}
</div>
)
}
}