README
antd components library
使用 React+typescript 的组件库
对于antd表单的二次封装,便于项目中方便使用,减少代码书写,
支持多种input、select、time方法,请参考antd
//启动本地环境
npm run stroybook
npm install antdpackaging --save
使用
// 加载样式, 如果项目中已经引入@import '~antd/dist/antd.css',则不需要引入样式,否则需要
import 'antdpackaging/dist/index.css'
// 引入组件
import { FormComponent } from 'antdpackaging'
// 代码示例
function App() {
const [ currentObj, setObj ] = useState({
name: '',
names: ['Jack-value', 'Lucy-value'],
Username: '',
AreaLabel: ['shanghai'],
Area: '',
time: '',
timeRange: [],
checkbox: ['beijing', 'shanghai'],
radio: '北京',
});
let QuickSearchTypeDic = [
{
'value': '',
'label': '所有'
},
{
'value': 'Jack-value',
'label': 'Jack'
},
{
'value': 'Lucy-value',
'label': 'Lucy'
},
{
'value': 'Tom-value',
'label': 'Tom'
}
];
let QuickSearch = [
{
key: 'beijing',
disabled: true,
value: '北京',
label: '北京'
},
{
key: 'shanghai',
value: '上海',
label: '上海'
}
];
let sourceList = [
[ // 多选
{
type: 'statusMultiple', md: 24, label: '多选', value: currentObj.names, name: 'names', query: true,
options: QuickSearchTypeDic,
},
],
[ // 单选
{
type: 'status', md: 24, label: '单选', value: currentObj.name, name: 'name', query: true,
options: QuickSearchTypeDic
},
],
[
{ type: 'input', addonBefore:"http://", must: true, hint: true, hintText: '友情提示', label: 'Username', value: currentObj.Username, name: 'Username' },
{
type: 'select', label: 'AreaLabel',
mode: 'multiple',
options: QuickSearch,
optionsObj: { label: 'key', value: 'key' },
value: currentObj.AreaLabel, name: 'AreaLabel'
},
{
type: 'select', label: 'Area',
showSearch: true,
onSearch: (e: string) => {
console.log(e)
},
options: QuickSearch,
value: currentObj.Area, name: 'Area'
}
],
[
{ type: 'time', label: 'time', value: currentObj.time, name: 'time',
disabledDate: (e)=>{
// 自定义方法
// return disabledStartDt(e, '', '',true)
}
},
{
type: 'timeRange', md: 16, label: 'timeRange', value: currentObj.timeRange, name: 'timeRange', showTime: true, dateFormat: 'YYYY-MM-DD HH:mm:ss'
}
],
[
{
type: 'checkbox', label: 'checkbox',
options: QuickSearch,
optionsObj: { label: 'value', value: 'key' },
value: currentObj.checkbox, name: 'checkbox'
},
{
type: 'radio', label: 'radio',
options: QuickSearch,
value: currentObj.radio, name: 'radio'
},
{
type: 'buttons',
name: <div style={{ marginLeft: '10px', textAlign: 'right' }}>
<Button type="primary" onClick={() => {
query()
action('callBcak')
}
}>
获取数据
</Button>
</div>
}
]
]
const query = (dt, item) => {
if (dt) {
// 点击后立即获取数据
if (item&&item.query) {
console.log('点击后立即获取数据',dt)
}
} else {
// 点击获取数据按钮后获取数据
console.log('点击查询按钮后获取数据',currentObj)
}
}
// useEffect(() => {
// if (currentItem && currentItem.query) {
// query()
// }
// }, [currentObj]);
const callBcak = (dt, item) => {
// setItem(item)
setObj(dt)
query(dt, item)
}
return (
<div className="App">
<FormComponent callBcak={(dt, item) => {
callBcak(dt, item)
}
} sourceList={sourceList} />
</div>
);
}
export default App;
FormComponent Component
| property | propType | required | default | description |
|---|---|---|---|---|
| sourceList | object[] | yes | - | 设置 数据源 |
| className | string | - | - | 设置 容器的className |
| style | CSSProperties | - | - | 设置 每行的style |
| callBcak | ((backData: any, item?: FormComponentItemProps) => void) | undefined | - | - | 设置 回调函数,参数一为当前表单的数据,参数二为当前行的传入数据 |
| size | "small" | "middle" | "large" | - | middle | 设置 每一行表单的大小 |
SourceListItem
| property | propType | required | default | description |
|---|---|---|---|---|
| type | string | - | - | 设置 表单类型 status、statusMultiple、input、time、timeRange、text、select、buttons、checkbox、radio |
| label | string | - | - | 设置 label名称 |
| name | any | - | - | 设置 返回主键值, 当type为text、buttons时返回传入值 |
| must | boolean | - | - | 设置 是否必填 |
| value | string | - | - | 设置 默认回显值格式statusMultiple: ['value1', 'value2'];timeRange: ['2021-02-12','2021-02-13'] |
| colClassName | string | - | - | 设置 每行的col className |
| formClassName | string | - | - | 设置 form的className |
| labelClassName | string | - | - | 设置 labelClassName的className |
| options | object[] | - | - | 设置 type为select、checkbox、radio、status、statusMultiple时的选项, 格式[{label: '中国', value: '中国'}],选择返回label值 checkbox、radio格式[{label: '中国', value: '中国', disabled: true}] 禁止某项 |
| mode | "multiple" | "tags" | - | - | 设置 Select 的模式为多选或标签 multiple | tags |
| optionsObj | { label: string; value: string; } | - | - | 设置 type为select、checkbox、radio、status、statusMultiple时的选项显示和返回字段,如果options的格式不是[{label: '中国', value:
'中国'}], 可以通过optionsObj实现,如[{label: '中国', value: '中国'}],设置optionsObj:{label:'label',value:'key'} 选择返回key值 |
| dateFormat | string | - | - | 设置 type为time、timeRange的时间格式 |
| placeholder | string | - | - | 设置 placeholder |
| query | boolean | - | - | 设置 值改变后是否立即查询,需在回调callBcak中判断 |
| hint | boolean | - | - | 设置 是否有hint |
| hintText | ReactNode | - | - | 设置 是否有hint内容 |
| md | number | - | - | 设置 没一列占的宽度,参考antd的col |
| colStyle | CSSProperties | - | - | 设置 每行的col 样式,如colStyle:{padding: 0} |
| labelStyle | CSSProperties | - | - | 设置 每行的label 样式,如labelStyle:{padding: 0} |
| styleWrapper | CSSProperties | - | - | 设置 type为status、statusMultiple的容器样式 |
| onChange | ((e: any, opt?: any) => void) | - | - |