Skip to content

Commit edd53c9

Browse files
committed
first commit
1 parent 8379dd5 commit edd53c9

File tree

5 files changed

+129
-15
lines changed

5 files changed

+129
-15
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ dist
2020
npm-debug.log*
2121
yarn-debug.log*
2222
yarn-error.log*
23+
24+
.idea

example/src/App.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
import React from 'react'
1+
import React, { useState, useEffect } from 'react'
22

3-
import { ExampleComponent } from 'react-scroll-log-text'
3+
import ScrollText from 'react-scroll-log-text'
44
import 'react-scroll-log-text/dist/index.css'
55

66
const App = () => {
7-
return <ExampleComponent text="Create React Library Example 😄" />
7+
const [data, setData ] = useState(['aaaa','bbbbb', 'ccc', 'dddd']); // 默认数据
8+
9+
useEffect(() =>{
10+
setInterval(()=> {
11+
const arr = [Math.random()];
12+
setData(arr)
13+
}, 3000);
14+
}, [])
15+
16+
return <ScrollText data={data} length={4} liStyle={{lineHeight: '40px'}} childrenFc={(v) => <div>={v}=</div>}/>
817
}
918

1019
export default App

package.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44
"description": "This is a React log text scrolling component.",
55
"author": "rfw",
66
"license": "MIT",
7-
"keywords": ["react", "components", "scroll", "log", "text"],
7+
"keywords": [
8+
"react",
9+
"components",
10+
"scroll",
11+
"log",
12+
"text"
13+
],
814
"repository": "rfw/react-scroll-log-text",
15+
"homepage": "https://github.com/rfw/react-scroll-log-text",
916
"main": "dist/index.js",
1017
"module": "dist/index.modern.js",
1118
"source": "src/index.js",
@@ -28,7 +35,6 @@
2835
"react": "^16.0.0"
2936
},
3037
"devDependencies": {
31-
"microbundle-crl": "^0.13.10",
3238
"babel-eslint": "^10.0.3",
3339
"cross-env": "^7.0.2",
3440
"eslint": "^6.8.0",
@@ -42,13 +48,16 @@
4248
"eslint-plugin-react": "^7.17.0",
4349
"eslint-plugin-standard": "^4.0.1",
4450
"gh-pages": "^2.2.0",
51+
"microbundle-crl": "^0.13.10",
4552
"npm-run-all": "^4.1.5",
4653
"prettier": "^2.0.4",
54+
"prop-types": "^15.7.2",
4755
"react": "^16.13.1",
4856
"react-dom": "^16.13.1",
4957
"react-scripts": "^3.4.1"
5058
},
5159
"files": [
5260
"dist"
53-
]
61+
],
62+
"dependencies": {}
5463
}

src/index.js

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,92 @@
11
import React from 'react'
22
import styles from './styles.module.css'
3+
import PropTypes from 'prop-types'
34

4-
export const ExampleComponent = ({ text }) => {
5-
return <div className={styles.test}>Example Component: {text}</div>
5+
export default class ScrollText extends React.Component {
6+
constructor(props) {
7+
super(props)
8+
this.state = {
9+
data: props.data,
10+
marginTop: '0',
11+
animate: false
12+
}
13+
}
14+
15+
scrollUp = () => {
16+
const { animateSpeed } = this.props
17+
const time = animateSpeed ? animateSpeed * 1000 + 1 : 1000
18+
const li = this.textRef.getElementsByTagName('li')
19+
if (li.length === 0) return
20+
const height = li[0].scrollHeight
21+
this.setState({
22+
animate: true,
23+
marginTop: '-' + height + 'px'
24+
})
25+
setTimeout(() => {
26+
this.state.data.shift()
27+
this.setState({
28+
animate: false,
29+
marginTop: '0'
30+
})
31+
}, time)
32+
}
33+
34+
startScrollUp = () => {
35+
if (Array.isArray(this.state.data) && this.state.data.length === 0) return
36+
this.scrollUp()
37+
}
38+
39+
componentDidUpdate(prevProps) {
40+
const { length, data } = this.props
41+
const dataList = this.state.data
42+
const newData = data
43+
if (
44+
newData.length !== 0 &&
45+
JSON.stringify(dataList.slice(-1)) !== JSON.stringify(newData)
46+
) {
47+
this.setState(
48+
{
49+
data: [...dataList, ...newData]
50+
},
51+
() => {
52+
if (dataList.length >= length) {
53+
this.startScrollUp()
54+
}
55+
}
56+
)
57+
}
58+
}
59+
60+
render() {
61+
const { data, marginTop, animate } = this.state
62+
const { animateSpeed, liStyle, childrenFc } = this.props
63+
64+
return (
65+
<ul
66+
ref={(e) => {
67+
this.textRef = e
68+
}}
69+
id={styles.scrollList}
70+
style={
71+
animate
72+
? { transition: ` margin ${animateSpeed || 1}s`, marginTop }
73+
: { marginTop }
74+
}
75+
>
76+
{data.map((v, i) => (
77+
<li key={i} style={liStyle}>
78+
{childrenFc ? childrenFc(v) : v}
79+
</li>
80+
))}
81+
</ul>
82+
)
83+
}
84+
}
85+
86+
ScrollText.propTypes = {
87+
animateSpeed: PropTypes.number, // 速度,单位:秒
88+
length: PropTypes.number, // 大于多少条开始滚动
89+
liStyle: PropTypes.object, // 像素带单位 eg: 40px
90+
data: PropTypes.array.isRequired, // 数据
91+
childrenFc: PropTypes.func // 自定义渲染item
692
}

src/styles.module.css

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
/* add css module styles here (optional) */
2-
3-
.test {
4-
margin: 2em;
5-
padding: 0.5em;
6-
border: 2px solid #000;
7-
font-size: 2em;
8-
text-align: center;
2+
#scrollList {
3+
margin: 0;
4+
padding: 0;
5+
list-style-type: none;
6+
list-style-position: outside;
97
}
8+
#scrollList li {
9+
list-style: none;
10+
font-size: 12px;
11+
white-space: nowrap;
12+
overflow: hidden;
13+
text-overflow: ellipsis;
14+
line-height: 30px;
15+
margin: 0;
16+
padding: 0;
17+
}

0 commit comments

Comments
 (0)