├── .gitignore ├── .npmignore ├── note.md ├── package.json ├── LICENSE ├── SecurityText.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.[aod] 2 | *.DS_Store 3 | .DS_Store 4 | *Thumbs.db 5 | *.iml 6 | .gradle 7 | .idea 8 | node_modules 9 | npm-debug.log 10 | /android/build 11 | /ios/**/*xcuserdata* 12 | /ios/**/*xcshareddata* -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | *.[aod] 2 | *.DS_Store 3 | .DS_Store 4 | *Thumbs.db 5 | *.iml 6 | .gradle 7 | .idea 8 | node_modules 9 | npm-debug.log 10 | /android/build 11 | /ios/**/*xcuserdata* 12 | /ios/**/*xcshareddata* -------------------------------------------------------------------------------- /note.md: -------------------------------------------------------------------------------- 1 | 2 | * 要注意须支持的方式: 3 | 4 | 1. 13898761923 -> 138****1923 5 | 2. 58.89 -> **** 6 | 3. 30901290 -> **** 7 | 4. 张三 -> **** 8 | 9 | * 允许指定保密字符的起始位置与结束位置(位置下标允许传负值, 如超出范围时自动用极限值代替) 10 | * 允许指定保密后字符串的显示长度(无视原字符串的长度) 11 | * 允许切换保密和非保密状态 -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@react-native-component/react-native-smart-security-text", 3 | "version": "1.0.1", 4 | "description": "A smart security text for React Native app", 5 | "main": "SecurityText.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/react-native-component/react-native-smart-security-text.git" 12 | }, 13 | "keywords": [ 14 | "react-native", 15 | "smart", 16 | "security", 17 | "text", 18 | "component" 19 | ], 20 | "author": "HISAME SHIZUMARU", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/react-native-component/react-native-smart-security-text/issues" 24 | }, 25 | "homepage": "https://github.com/react-native-component/react-native-smart-security-text#readme" 26 | } 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /SecurityText.js: -------------------------------------------------------------------------------- 1 | /* 2 | * A smart security text for react-native apps 3 | * https://github.com/react-native-component/react-native-smart-security-text/ 4 | * Released under the MIT license 5 | * Copyright (c) 2016 react-native-component 6 | */ 7 | 8 | import React, { 9 | PropTypes, 10 | Component, 11 | } from 'react' 12 | import { 13 | Text, 14 | } from 'react-native' 15 | 16 | export default class SecurityText extends Component { 17 | 18 | static propTypes = { 19 | ...Text.propTypes, 20 | securityOptions: PropTypes.shape({ 21 | isSecurity: PropTypes.bool, 22 | startIndex: PropTypes.number, 23 | endIndex: PropTypes.number, 24 | length: PropTypes.number, 25 | securityChar: PropTypes.string, 26 | }), 27 | 28 | 29 | } 30 | 31 | render() { 32 | return ( 33 | 35 | {this._renderContent(this.props.securityOptions)} 36 | 37 | ) 38 | } 39 | 40 | _renderContent({isSecurity = false, startIndex = 0, endIndex, length, securityChar = '*'} = {}) { 41 | if(isSecurity) { 42 | let texts = [] 43 | React.Children.forEach(this.props.children, (child) => { 44 | if(!React.isValidElement(child)) { 45 | if(length > 0) { 46 | child = this._padEnd(length, securityChar) 47 | } 48 | else { 49 | let textLength = child.length 50 | if(Math.abs(startIndex) > textLength - 1) { 51 | startIndex = textLength - 1 52 | } 53 | if(Math.abs(endIndex) > textLength) { 54 | endIndex = textLength 55 | } 56 | child = child.slice(0, startIndex) + this._padEnd(endIndex - startIndex, securityChar) + child.slice(endIndex) 57 | } 58 | texts.push(child) 59 | } 60 | }) 61 | return texts 62 | } 63 | else { 64 | return this.props.children 65 | } 66 | } 67 | 68 | _padEnd(length, string) { 69 | let text = '' 70 | for(let i = 0; i < length; i++) { 71 | text += string 72 | } 73 | return text 74 | } 75 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-native-smart-security-text 2 | 3 | [![npm](https://img.shields.io/npm/v/react-native-smart-security-text.svg)](https://www.npmjs.com/package/react-native-smart-security-text) 4 | [![npm](https://img.shields.io/npm/dm/react-native-smart-security-text.svg)](https://www.npmjs.com/package/react-native-smart-security-text) 5 | [![npm](https://img.shields.io/npm/dt/react-native-smart-security-text.svg)](https://www.npmjs.com/package/react-native-smart-security-text) 6 | [![npm](https://img.shields.io/npm/l/react-native-smart-security-text.svg)](https://github.com/react-native-component/react-native-smart-security-text/blob/master/LICENSE) 7 | 8 | A smart security text for React Native apps, written in JS for cross-platform support. 9 | It works on iOS and Android. 10 | 11 | This component is compatible with React Native 0.25 and newer. 12 | 13 | ## Preview 14 | 15 | ![react-native-smart-security-text-preview-ios][1] 16 | ![react-native-smart-security-text-preview-android][2] 17 | 18 | ## Installation 19 | 20 | ``` 21 | npm install react-native-smart-security-text --save 22 | ``` 23 | 24 | ## Full Demo 25 | 26 | see [ReactNativeComponentDemos][0] 27 | 28 | ## Usage 29 | 30 | Install the package from npm with `npm install react-native-smart-security-text --save`. 31 | Then, require it from your app's JavaScript files with `import SecurityText from 'react-native-smart-security-text'`. 32 | 33 | ```js 34 | import React, { 35 | Component, 36 | } from 'react' 37 | import { 38 | View, 39 | Text, 40 | Image, 41 | } from 'react-native' 42 | 43 | import SecurityText from 'react-native-smart-security-text' 44 | import Button from 'react-native-smart-button' 45 | 46 | import image_eye_open from '../images/eye_open.png' 47 | import image_eye_close from '../images/eye_close.png' 48 | 49 | export default class SecurityTextDemo extends Component { 50 | 51 | // 构造 52 | constructor(props) { 53 | super(props) 54 | // 初始状态 55 | this.state = { 56 | isSecurity: false, 57 | } 58 | } 59 | 60 | render() { 61 | return ( 62 | 63 | 69 | 76 | 15912390987 77 | 78 | 85 | 15912390987 86 | 87 | 94 | 15912390987 95 | 96 | 103 | 15912390987 104 | 105 | 112 | 15912390987 113 | 114 | 120 | username 121 | 122 | 128 | address 129 | 130 | 131 | ) 132 | } 133 | 134 | _onSecurityChange = () => { 135 | let isSecurity = !this.state.isSecurity 136 | this.setState({ 137 | isSecurity, 138 | }) 139 | } 140 | } 141 | ``` 142 | 143 | ## Props 144 | 145 | Prop | Type | Optional | Default | Description 146 | ---------------------------- | ------ | -------- | --------- | ----------- 147 | ...Text.propTypes | | Yes | | see [react-native documents][3] 148 | securityOptions | shape | Yes | {} | 149 | securityOptions.isSecurity | bool | Yes | false | when the value is true, origin text will be replaced with security text 150 | securityOptions.startIndex | number | Yes | 0 | determines the startIndex of security text 151 | securityOptions.endIndex | number | Yes | | determines the endIndex of security text 152 | securityOptions.length | number | Yes | | determines the length of security text 153 | securityOptions.securityChar | string | Yes | '*' | determines the securityChar of security text 154 | 155 | [0]: https://github.com/cyqresig/ReactNativeComponentDemos 156 | [1]: http://cyqresig.github.io/img/react-native-smart-security-text-preview-ios-v1.0.0.gif 157 | [2]: http://cyqresig.github.io/img/react-native-smart-security-text-preview-android-v1.0.0.gif 158 | [3]: https://facebook.github.io/react-native/docs/text.html 159 | --------------------------------------------------------------------------------