提交 30fb6785 authored 作者: 肖伟鸣's avatar 肖伟鸣

1

上级 de407d97
import React, { Fragment, Component, useEffect, } from 'react'; import React, { Fragment, Component, useEffect } from 'react';
import { DatePicker, Picker, TextareaItem, InputItem, List, Switch, Modal, Button, Checkbox, } from 'antd-mobile'; import {
DatePicker,
Picker,
TextareaItem,
InputItem,
List,
Switch,
Modal,
Button,
Checkbox,
} from 'antd-mobile';
import moment from 'moment'; import moment from 'moment';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import FieldList from '@/H5Public/baseComponents/FieldList'; import FieldList from '@/H5Public/baseComponents/FieldList';
import TextareaItemMultiRows from '../TextareaItemMultiRows' import TextareaItemMultiRows from '../TextareaItemMultiRows';
import { useState } from 'react'; import { useState } from 'react';
const DiyInput = props => {
const DiyInput = (props) => {
let { config, formValue, changeValue, otherProps } = props; let { config, formValue, changeValue, otherProps } = props;
if (config.readOnly) { if (config.readOnly) {
config.placeholder = ''; config.placeholder = '';
...@@ -22,7 +31,7 @@ const DiyInput = (props) => { ...@@ -22,7 +31,7 @@ const DiyInput = (props) => {
editable={!config.readOnly} editable={!config.readOnly}
value={formValue[config.key]} value={formValue[config.key]}
placeholder={config.placeholder} placeholder={config.placeholder}
onChange={(e) => { onChange={e => {
changeValue(e, config.key); changeValue(e, config.key);
}} }}
{...config.otherProps} {...config.otherProps}
...@@ -32,8 +41,8 @@ const DiyInput = (props) => { ...@@ -32,8 +41,8 @@ const DiyInput = (props) => {
); );
}; };
const DiyTextarea = (props) => { const DiyTextarea = props => {
let { config, formValue, changeValue, noLengthLimit, rows, } = props; let { config, formValue, changeValue, noLengthLimit, rows } = props;
if (config.readOnly) { if (config.readOnly) {
config.placeholder = ''; config.placeholder = '';
} else { } else {
...@@ -48,24 +57,22 @@ const DiyTextarea = (props) => { ...@@ -48,24 +57,22 @@ const DiyTextarea = (props) => {
placeholder={config.placeholder} placeholder={config.placeholder}
editable={!config.readOnly} editable={!config.readOnly}
value={formValue[config.key]} value={formValue[config.key]}
onChange={(e) => { onChange={e => {
changeValue(e, config.key); changeValue(e, config.key);
}} }}
{...config.otherProps} {...config.otherProps}
></TextareaItem>
>
</TextareaItem>
); );
}; };
const DiyPicker = (props) => { const DiyPicker = props => {
let { config, formValue, changeValue } = props; let { config, formValue, changeValue } = props;
if (config.readOnly) { if (config.readOnly) {
config.placeholder = ' '; config.placeholder = ' ';
} else { } else {
config.placeholder = config.placeholder || '点击输入'; config.placeholder = config.placeholder || '点击输入';
} }
let opt = config.options.map((x) => { let opt = config.options.map(x => {
return { return {
label: x.name, label: x.name,
value: x.key, value: x.key,
...@@ -79,19 +86,17 @@ const DiyPicker = (props) => { ...@@ -79,19 +86,17 @@ const DiyPicker = (props) => {
key={config.key} key={config.key}
value={[formValue[config.key]]} value={[formValue[config.key]]}
disabled={config.readOnly} disabled={config.readOnly}
onChange={(val) => { onChange={val => {
changeValue(val[0], config.key); changeValue(val[0], config.key);
}} }}
{...config.otherProps} {...config.otherProps}
> >
<List.Item arrow={config.readOnly ? '' : 'horizontal'}> <List.Item arrow={config.readOnly ? '' : 'horizontal'}>{giveRequiredName(config)}</List.Item>
{giveRequiredName(config)}
</List.Item>
</Picker> </Picker>
); );
}; };
const DiySwitch = (props) => { const DiySwitch = props => {
let { config, formValue, changeValue } = props; let { config, formValue, changeValue } = props;
if (config.readOnly) { if (config.readOnly) {
config.placeholder = ' '; config.placeholder = ' ';
...@@ -105,7 +110,7 @@ const DiySwitch = (props) => { ...@@ -105,7 +110,7 @@ const DiySwitch = (props) => {
<Switch <Switch
checked={formValue[config.key]} checked={formValue[config.key]}
disabled={config.readOnly} disabled={config.readOnly}
onChange={(val) => { onChange={val => {
changeValue(val, config.key); changeValue(val, config.key);
}} }}
{...config.otherProps} {...config.otherProps}
...@@ -117,10 +122,10 @@ const DiySwitch = (props) => { ...@@ -117,10 +122,10 @@ const DiySwitch = (props) => {
); );
}; };
const DiyDatePicker = (props) => { const DiyDatePicker = props => {
let { config, formValue, changeValue } = props; let { config, formValue, changeValue } = props;
let value = formValue[config.key] ? new Date(formValue[config.key].replace(/-/g, '/')) : null; let value = formValue[config.key] ? new Date(formValue[config.key].replace(/-/g, '/')) : null;
const change = (date) => { const change = date => {
let v = moment(date.valueOf()).format(config.formatter || 'YYYY-MM-DD'); let v = moment(date.valueOf()).format(config.formatter || 'YYYY-MM-DD');
changeValue(v, config.key); changeValue(v, config.key);
}; };
...@@ -136,26 +141,19 @@ const DiyDatePicker = (props) => { ...@@ -136,26 +141,19 @@ const DiyDatePicker = (props) => {
mode={config.mode || 'date'} mode={config.mode || 'date'}
key={config.key} key={config.key}
disabled={config.readOnly} disabled={config.readOnly}
onChange={(date) => change(date)} onChange={date => change(date)}
{...config.otherProps} {...config.otherProps}
> >
<List.Item <List.Item arrow="horizontal">{giveRequiredName(config)}</List.Item>
arrow="horizontal"
>
{giveRequiredName(config)}
</List.Item>
</DatePicker> </DatePicker>
); );
}; };
const DiyCheckBox = ({ config, formValue, changeValue }) => {
const DiyCheckBox = ({config, formValue, changeValue}) => {
const [modalVisible, toggleModal] = useState(false); const [modalVisible, toggleModal] = useState(false);
const [selectedKeys, setSelectedKeys] = useState([]); const [selectedKeys, setSelectedKeys] = useState([]);
useEffect(() => { useEffect(() => {
setSelectedKeys(formValue[config.key] || []) setSelectedKeys(formValue[config.key] || []);
}, [formValue, config.key]); }, [formValue, config.key]);
function onChange(item) { function onChange(item) {
...@@ -174,7 +172,10 @@ const DiyCheckBox = ({config, formValue, changeValue}) => { ...@@ -174,7 +172,10 @@ const DiyCheckBox = ({config, formValue, changeValue}) => {
<List.Item <List.Item
arrow="horizontal" arrow="horizontal"
onClick={() => !config.readOnly && toggleModal(true)} onClick={() => !config.readOnly && toggleModal(true)}
extra={config.options.filter(i => formValue[config.key] && formValue[config.key].includes(i.key)).map(i => i.name).join(',')} extra={config.options
.filter(i => formValue[config.key] && formValue[config.key].includes(i.key))
.map(i => i.name)
.join(',')}
> >
{giveRequiredName(config)} {giveRequiredName(config)}
</List.Item> </List.Item>
...@@ -185,23 +186,29 @@ const DiyCheckBox = ({config, formValue, changeValue}) => { ...@@ -185,23 +186,29 @@ const DiyCheckBox = ({config, formValue, changeValue}) => {
animationType="slide-up" animationType="slide-up"
> >
<List renderHeader={() => <div>{config.name}</div>}> <List renderHeader={() => <div>{config.name}</div>}>
<div style={{maxHeight: '50vh', overflow: 'scroll'}}> <div style={{ maxHeight: '50vh', overflow: 'scroll' }}>
{config.options.map(i => ( {config.options.map(i => (
<Checkbox.CheckboxItem checked={selectedKeys.includes(i.key)} key={i.key} onChange={() => onChange(i)}> <Checkbox.CheckboxItem
checked={selectedKeys.includes(i.key)}
key={i.key}
onChange={() => onChange(i)}
>
{i.name} {i.name}
</Checkbox.CheckboxItem> </Checkbox.CheckboxItem>
))} ))}
</div> </div>
<List.Item> <List.Item>
<Button type="primary" onClick={submit}>确定</Button> <Button type="primary" onClick={submit}>
确定
</Button>
</List.Item> </List.Item>
</List> </List>
</Modal> </Modal>
</> </>
) );
} };
const giveRequiredName = (config) => { const giveRequiredName = config => {
if (config.required) { if (config.required) {
return ( return (
<Fragment> <Fragment>
...@@ -219,16 +226,14 @@ const giveRequiredName = (config) => { ...@@ -219,16 +226,14 @@ const giveRequiredName = (config) => {
} }
}; };
class FormArray extends Component { class FormArray extends Component {
changeValue = (value, key) => { changeValue = (value, key) => {
this.props.changeValue(value, key); this.props.changeValue(value, key);
}; };
detailDom = () => { detailDom = () => {
const { config, formValues, readOnly } = this.props; const { config, formValues, readOnly } = this.props;
return config.map((x) => { return config.map(x => {
x.readOnly = readOnly || x.readOnly; x.readOnly = readOnly || x.readOnly;
switch (x.type) { switch (x.type) {
case 'InputItem': case 'InputItem':
...@@ -238,7 +243,6 @@ class FormArray extends Component { ...@@ -238,7 +243,6 @@ class FormArray extends Component {
config={x} config={x}
formValue={formValues} formValue={formValues}
changeValue={this.changeValue} changeValue={this.changeValue}
/> />
); );
case 'TextareaItem': case 'TextareaItem':
...@@ -258,7 +262,7 @@ class FormArray extends Component { ...@@ -258,7 +262,7 @@ class FormArray extends Component {
value={formValues[x.key]} value={formValues[x.key]}
onChange={val => this.changeValue(val, x.key)} onChange={val => this.changeValue(val, x.key)}
/> />
) );
case 'Picker': case 'Picker':
return ( return (
<DiyPicker <DiyPicker
...@@ -297,24 +301,18 @@ class FormArray extends Component { ...@@ -297,24 +301,18 @@ class FormArray extends Component {
); );
default: default:
return null; return null;
} }
}); });
}; };
render() { render() {
return ( return <List>{this.detailDom()}</List>;
<List>
{this.detailDom()}
</List>
);
} }
} }
FormArray.propTypes = { FormArray.propTypes = {
formValues: PropTypes.object.isRequired, //数据 formValues: PropTypes.object.isRequired, //数据
config: PropTypes.array.isRequired, // 配置项 config: PropTypes.array.isRequired, // 配置项
changeValue: PropTypes.func.isRequired, // 改值的方法 changeValue: PropTypes.func.isRequired, // 改值的方法
readOnly: PropTypes.bool, // 是否 只读 readOnly: PropTypes.bool, // 是否 只读
}; };
...@@ -332,4 +330,3 @@ FormArray.defaultProps = { ...@@ -332,4 +330,3 @@ FormArray.defaultProps = {
}; };
export default FormArray; export default FormArray;
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论