组件化思路:以selection为例子,使用prop-types实现组件化控制,重用
需求
书接上文,UI 积累之select section
这里又来两个需求了。
当我点击选择了option后,我应该看到的是我选择的option的内容
多例重用,即同样是个selection,我只是需要改点东西,其他不变,比如selection里面的字内容,font-size, font-family, selection的width, height等。。。如何只开发一个组件就满足这个“无理要求”呢?
第一只老虎--显示option内容
我们的dom是这样的:
<Selectsection>
<select>
<option>Please select one option...</option>
<option>...</option>
<option>...</option>
</select>
<span>Please select one option...</span>
<div></div>
</Selectsection>
具体实现,通过react的state倒是能很简单实现。
给select绑定个onChange事件,触发onSelect方法,当选择select的option的时候,把选到的值传给真正显示的span
问题来了:1)怎么拿。 2)怎么给
1)怎么拿:
点击的时候,通过event.target.value拿到选中的option的值
2)怎么给:
在组件渲染的时候,constructor里的state定义一个值存放选中的值,叫showValue,当选择时,在onSelect方法里,拿到event.target.value后,set给这个值,同时dom中的span进行this.state.showValue值绑定。
Talk is cheap, show me the code
完整代码如下:
class Selection extends Component {
constructor(props){
super(props)
this.state = {
showValue: "Please select one option..."
}
}
onSelect(e){
this.setState({showValue: e.target.value});
}
render() {
return (
<Selectsection>
<select onChange={this.onSelect.bind(this)}>
<option>Please select one option...</option>
<option>...</option>
<option>...</option>
</select>
<span>{this.state.showValue}</span>
<div></div>
</Selectsection>
);
}
}
实例图:
第二只老虎--组件化
看上面的代码可以知道,引入这个selection的方式是这样的
render(
<Selection />
)
但是你这个selection啊,我同一个页面要引入N个,其中一个高度要是40px,另一个宽度要长点,500px,还有一个长宽都不用变,你给我变下这个selection的default的字啊,不叫Please select one option..., 叫什么Please kindly select one option...,还有一个,你给我保持原样不变哈,谢谢了。
WTF, 怎么做呢?
需求啊,莫得办法
为了开发方便,我自己设计,要是能组件化,几个属性在引入的时候可以选择性定义就好了,比如:
render(
<div>
<Selection />
<Selection width="500px" />
<Selection height="40px" />
<Selection wordings="Please kindly select one option..."/ />
</div>
)
能这么实现就完美了。
怎么做呢,这就要引入一个包,叫prop-types了,定义属于这个组件的变量,然后将其运用到组件的每个dom的css上。
接下来以上述为例子。
定义属于这个组件的类型:
Selection.propTypes = {
height: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string,
]),
width: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string,
]),
words: PropTypes.string
}
Selection.defaultProps = {
height: '30px',
width: '300px',
words: 'Please select one option...'
}
然后就是通过react的this.props引入啦
Talk is cheap, show me the code
index.js
class App extends Component {
render() {
return (
<div>
<Selection />
<Selection height="40px" />
<Selection width="500px" />
<Selection words="Please kindly select one option..." />
</div>
);
}
}
Selection.js
class Selection extends Component {
constructor(props){
super(props)
this.state = {
showValue: this.props.words
}
}
onSelect(e){
this.setState({showValue: e.target.value});
console.log(e.target.value)
}
render() {
const { width, height } = this.props
const style = {
width: width,
height: height
}
const suitableHeight = (parseInt(height.substring(0, height.length-2)) - 30) / 2 + 6;
const spanStyle = {
width: width,
height: height,
paddingTop:suitableHeight
}
const arrowStyle = {
top:suitableHeight
}
return (
<Selectsection style={style}>
<select onChange={this.onSelect.bind(this)} value={this.state.showValue} style={style}>
<option>{this.props.words}</option>
<option>...</option>
<option>...</option>
</select>
<span style={spanStyle}>{this.state.showValue}</span>
<div style={arrowStyle}></div>
</Selectsection>
);
}
}
Selection.propTypes = {
height: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string,
]),
width: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string,
]),
words: PropTypes.string
}
Selection.defaultProps = {
height: '30px',
width: '300px',
words: 'Please select one option...'
}
效果图:
hm。。。应该差不多了,这里代码里就忽略了
自定义属性时候纯数字和字符串的判断
当height是比30小的时候的判断处理
有兴趣自己加
ok,完美收工
组件化思路:以selection为例子,使用prop-types实现组件化控制,重用的更多相关文章
- iOS组件化思路 <转>
随着应用需求逐步迭代,应用的代码体积将会越来越大,为了更好的管理应用工程,我们开始借助CocoaPods版本管理工具对原有应用工程进行拆分.但是仅仅完成代码拆分还不足以解决业务之间的代码耦合,为了更好 ...
- iOS组件化思路-大神博客研读和思考
一.大神博客研读 随着应用需求逐步迭代,应用的代码体积将会越来越大,为了更好的管理应用工程,我们开始借助CocoaPods版本管理工具对原有应用工程进行拆分.但是仅仅完成代码拆分还不足以解决业务之间的 ...
- Android基于代理的插件化思路分析
前言 正常的App开发流程基本上是这样的:开发功能-->测试--->上线,上线后发现有大bug,紧急修复---->发新版本---->用户更新----->bug修复.从发现 ...
- 修饰模式(Decorator结构化)C#简单的例子
修饰模式(Decorator结构化)C#简单的例子 播放器的基本功能是移动.执行等.BaseAbility 新增功能:1.伤害技能harmAbility:2.阻碍技能BaulkAbility:3.辅助 ...
- vue父组件异步传递prop到子组件echarts画图问题踩坑总结
效果图: 大致思路:考虑到5张图都是折线图,所以准备用一个子组件承接echarts画图,然后父组件通过prop传递不同数据来展示不同的图 踩坑问题: 1.引入line子组件,画了5个元素,但是只显示一 ...
- DRF - 序列化组件(GET/PUT/DELETE接口设计)、视图优化组件
一.序列化组件 基于上篇随笔的表结构 , 通过序列化组件的ModelSerializer设计如下三个接口 : GET 127.0.0.1:8000/books/{id} # 获取一条数据,返回值:{} ...
- WeCenter 社交化问答社区程序 | WeCenter 是一款知识型的社交化问答社区程序,专注于社区内容的整理、归类、检索和再发行
WeCenter 社交化问答社区程序 | WeCenter 是一款知识型的社交化问答社区程序,专注于社区内容的整理.归类.检索和再发行 为什么选择 WeCenter 程序? 让您的社区更智能地运作,强 ...
- Blazor组件自做二 : 使用JS隔离制作手写签名组件
Blazor组件自做二 : 使用JS隔离制作手写签名组件 本文相关参考链接 JavaScript 模块中的 JavaScript 隔离 Viewer.js工程 Blazor组件自做一 : 使用JS隔离 ...
- Vue组件化应用构建 官网例子 Unknown custom element: <todo-item>
[博客园cnblogs笔者m-yb原创,转载请加本文博客链接,笔者github: https://github.com/mayangbo666,公众号aandb7,QQ群927113708] htt ...
随机推荐
- npm 切换成淘宝镜像
npm install nrm -g nrm use taobao
- LeetCode No.94,95,96
No.94 InorderTraversal 二叉树的中序遍历 题目 给定一个二叉树,返回它的中序 遍历. 示例 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶:递 ...
- 《C程序设计语言》练习1-10
#include<stdio.h> main() { int c; c=getchar(); while (c !=EOF) { if (c=='\t') { c='\\'; putcha ...
- 被这个C程序折腾死了
The C programming language 的第13页,1.5.3 行计数的那里,那个统计换行符个数的程序我好像无法运行,无论输入什么,按多少下enter,什么都出不来. #include& ...
- [LC] 53. Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- jenkins-自定义工作空间目录
- differences between evolution
- 源码中TODO、FIXME和XXX的含义
前言: 今天在阅读Qt Creator的源代码时,发现一些注释中有FIXME英文单词,用英文词典居然查不到其意义! 实际上,在阅读一些开源代码时,我们常会碰到诸如:TODO.FIXME和XXX的单词 ...
- 前端之css引入方式/长度及颜色单位/常用样式
1.css三种引入方式 <!DOCTYPE html><html><head> <meta charset="UTF-8"> < ...
- Mybatis与Spring整合(纯注解)
java1.5版本之后开始支持注解,spring*2开始提供注解配置方式,到spring**4后spring推荐使用注解配置 IOC注解(主要作用就是在spring容器中声明一个Bean,同xml中的 ...