-
Notifications
You must be signed in to change notification settings - Fork 318
ReactInputSelection的使用
司徒正美 edited this page Sep 19, 2017
·
2 revisions
ReactInputSelection的出现是React从技术上拉开距离的标志,很少有库能搞定Range与seletion。简言而之,就是输入区的光标与拖动时圈定的选择区。这个非常难实现。由于React每次都是全量更新,可能导致焦点消失,从而导致光标与选区都没有了,在做表格验证或富文本编辑器时,就需要用到而更新时保存光标与选区,在更新后还原。
由于这功能的代码量比较多,就不纳入核心库了,大家按需加载吧。代码基本上从React核心库内扒下来的,只做了少量修改。
用法如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script type='text/javascript' src="./dist/React.js"></script>
<script type='text/javascript' src="./lib/ReactInputSelection.js"></script>
<!--<script src="./test/react-lite.js"></script>-->
<script type='text/javascript' src="./lib/babel.js"></script>
<script type='text/babel'>
class App extends React.Component {
constructor(props){
super(props)
this.state = {
value: 111
}
}
change(e){
console.log(e.target.innerHTML)
this.setState({
value: e.target.innerHTML
})
}
render(){
return (
<div contentEditable={true} onInput={ this.change.bind(this) } >{this.state.value}
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('example'))
</script>
</head>
<body>
<div>开发者工具</div>
<div id='example'></div>
</body>
</html>
``html
<script type='text/javascript' src="./dist/React.js"></script> <script type='text/javascript' src="./test/react.development.js"></script> <script type='text/javascript' src="./test/react-dom.development.js"></script><script type='text/javascript' src="./lib/babel.js"></script>
<pre>应该等于
</pre>
<h1 id='root' class="root">
</h1>
<script type='text/babel'>
var container = document.getElementById("example")
var div = container
var PropTypes = React.PropTypes
if(!window.ReactDOM){
window.ReactDOM = React
}
var expect = function(a) {
return {
toBe: function(b) {
console.log(a,"vs", b, a === b)
},
toEqual(b){
console.log(a,"vs", b, a +""=== b+"")
},
toThrow(b){
try{
a()
}catch(e){
console.log(e,"vs", b, e.message +""=== b+"")
}
}
}
}
const Hello = ({ name }) => <h1>Hello {name}!</h1>;
class ShowMyError extends React.Component {
constructor(props) {
super(props);
this.state = { error: false };
}
componentDidCatch(error, info) {
console.log(error, info)
this.setState({ error, info });
}
render() {
if (this.state.error) {
return (
<div>
<h1>
Error AGAIN: {this.state.error.toString()}
</h1>
{this.state.info &&
this.state.info.componentStack.split("\n").map(i => {
return (
<div key={i}>
{i}
</div>
);
})}
</div>
);
}
return this.props.children;
}
}
class Broken extends React.Component {
constructor(props) {
super(props);
this.state = { throw: false, count: 0 };
}
render() {
if (this.state.throw) {
throw new Error("YOLO");
}
return (
<div>
<button
onClick={e => {
this.setState({ throw: true });
}}
>
button will render error.
</button>
<button onClick={e => {
this.setState(({ count }) => ({
count: count + 1
}));
}}>button will not throw</button>
<div>
{"All good here. Count: "}{this.state.count}
</div>
</div>
);
}
}
class App extends React.Component {
render() {
const styles = {
fontFamily: "sans-serif",
textAlign: "center"
};
return (
<div style={styles}>
<Hello name="ShowMyError" />
<h2>
Start clicking to see some {"\u2728"}magic{"\u2728"}
</h2>
<ShowMyError>
<Broken />
</ShowMyError>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));