/**
* Created by Administrator on 2017/8/8.
*/
///
import * as React from "react"
import {addType, addTypeWithWrapper, getComponentProps} from "../field";
import {FieldArray, WrappedFieldArrayProps} from "redux-form"
import { AutoComplete, Radio ,Checkbox, InputNumber, Tooltip, Upload, Button, Icon} from 'antd';
const RadioGroup = Radio.Group;
import {Input,Select,DatePicker} from "antd";
const {TextArea} =Input;
const {RangePicker} = DatePicker;
const Option = Select.Option;
import {WidgetProps} from "../field";
import {Options} from "../form";
import {RuntimeAsyncOptions} from "../form";
import {renderFields} from "../render-fields";
import { setButton } from "../buttons";
const PropTypes = require('prop-types')
const RCSelect = require("rc-select").default
import * as moment from "moment"
import { ResolveMaybePromise } from '../resolve-maybe-promise';
import { isArray } from 'util';
RCSelect.propTypes['value'] = PropTypes.any
Option.propTypes['value'] = PropTypes.any
Select.propTypes['value'] = PropTypes.any
const emptyArray:any[] = []
// const convertValueToString = Comp=>(props)=>{
// let onChange=!props.onChange?undefined:(value)=>{
// props.onChange()
// }
// return
// }
const errorStyle={color:"red"};
function TextInput(props:WidgetProps){
const componentProps = getComponentProps(props.fieldSchema)
return
}
function SelectInput (props:WidgetProps){
const {fieldSchema,input,meta} = props
const componentProps:any = getComponentProps(props.fieldSchema)
return
{(options)=>{
if(options == undefined)
options = emptyArray
const value = fieldSchema.multiple||componentProps.mode==="multiple"?(isArray(input.value)?input.value:[]):input.value
return
}}
{meta.error}
}
function CheckboxInput (props:WidgetProps){
const componentProps = getComponentProps(props.fieldSchema)
return
props.input.onChange((e.target as HTMLInputElement).checked)}
checked={Boolean(props.input.value)}
{...componentProps}
/>
}
function DateTimeInput(props:WidgetProps){
const value=props.input.value?moment(props.input.value):undefined;
const componentProps = getComponentProps(props.fieldSchema)
return
props.input.onChange(dateString)}
{...componentProps}
/>
{props.meta.error}
}
function DateInput(props:WidgetProps){
let value= null;
if(props.input.value){
if(!(props.input.value instanceof moment))
value= moment(props.input.value);
}
const componentProps = getComponentProps(props.fieldSchema)
return
{props.input.onChange(dateString)}}
{...componentProps}
/>
{props.meta.error}
}
function DateTimeRangeInput (props:WidgetProps){
let value =props.input.value
const componentProps = getComponentProps(props.fieldSchema)
return
{
props.input.onChange(dataStrings);
}}
{...componentProps}
/>
{props.meta.error}
}
function NumberInput(props:WidgetProps){
let required={
required:props.required
};
const componentProps = getComponentProps(props.fieldSchema)
return
{if(isNaN(parseFloat(value as any))){
props.input.onChange(0)
}else{
props.input.onChange(parseFloat(value as any) )
}
}}
{...componentProps}
/>
{props.meta.error}
}
const AutoCompleteSelect = function(props:WidgetProps){
const {meta,input,fieldSchema} = props;
const componentProps = getComponentProps(props.fieldSchema)
return
{options=>{
return ({value:itm.value,text:itm.name})):emptyArray}
style={{ width:"100%" }}
value={input.value}
onSelect={(value)=>input.onChange(value)}
{...componentProps}
/>
}}
{meta.error}
}
class FileInput extends React.Component{
onChange=(info:any)=>{
this.props.input.onChange((info.fileList as any[]).map(file=>{
if(file.response && file.response.url){
file.url = file.response.url;
}
return {
...file
}
}).filter(file=>{
if(file.response && file.response.url){
return file.status==="done";
}
return true;
}))
};
customRequest=(customRequestParams:any)=>{
const {onSuccess,onError,onProgress,file,filename} = customRequestParams
if(!this.props.fieldSchema.onFileChange){
setTimeout(()=>{
onProgress({percent:100});
onSuccess(filename,null);
},1)
}else{
this.props.fieldSchema.onFileChange(file).then(previewUrl=>{
onProgress({percent:100});
onSuccess(previewUrl,null);
},(err)=>onError(err))
}
};
render(){
const componentProps = getComponentProps(this.props.fieldSchema)
return
}
}
function SelectRadio (props:WidgetProps){
const componentProps = getComponentProps(props.fieldSchema)
return
{options=>props.input.onChange(v)}
{...componentProps}
>
{
options?options.map((option) => (
{option.name}
)):null
}
}
{props.meta.error}
}
function DateRangeInput (props:WidgetProps){
const dateFormat = props.fieldSchema.dateFormat || 'YYYY-MM-DD';
const value=props.input.value
const from =value?value[0]:undefined;
const to =value?value[1]:undefined;
const componentProps = getComponentProps(props.fieldSchema)
return
{props.input.onChange(dateStrings)}}
{...componentProps}
/>
}
function TextareaInput (props:WidgetProps){
const componentProps = getComponentProps(props.fieldSchema)
return
}
class AutoCompleteAsync extends React.Component{
pendingUpdate:any;
fetchingQuery:any;
$isMounted:boolean;
componentDidMount(){
this.$isMounted=true;
}
componentWillUnmount(){
this.$isMounted=false;
}
componentWillReceiveProps(this:AutoCompleteAsync,nextProps:typeof this['props']){
if(nextProps.input.value!==this.props.input.value)
this.setState({
searchText:this.findName(nextProps.input.value)
})
}
findName(value:any){
const entry = (this.state.dataSource as Options).find(x=>x.value === value);
return entry?entry.name:value;
}
onUpdateInput=(name:string)=>{
const throttle = this.props.fieldSchema.throttle || 400
this.setState({
searchText:name
});
if(this.pendingUpdate)
clearTimeout(this.pendingUpdate);
this.pendingUpdate = setTimeout(()=>{
this.fetchingQuery = name;
const result = (this.props.fieldSchema.options as RuntimeAsyncOptions)(name,this.props);
if(result instanceof Promise)
result.then(options=>{
if(this.fetchingQuery === name && this.$isMounted)
this.setState({
dataSource:options.map(itm=>({text:itm.name,value:itm.value}))
})
});
else this.setState({
dataSource:result.map(itm=>({text:itm.name,value:itm.value}))
})
},throttle);
};
onSelected=(params:any)=>{
this.props.input.onChange(params.value)
};
state={
searchText:"",
dataSource:emptyArray
};
render(){
const {meta,input,fieldSchema} = this.props;
return
input.onChange(value)}
disabled={this.props.disabled}
onSearch={this.onUpdateInput}
filterOption
/>
{meta.error}
}
}
class AutoCompleteText extends React.Component{
onUpdateInput=(name:string)=>{
const entry = (this.props.fieldSchema.options as Options).find(x=>x.name===name);
return this.props.input.onChange(entry?entry.value:name);
};
render(){
const {input,meta,fieldSchema} = this.props;
return
({text:itm.name,value:itm.value}))}
onSearch={this.onUpdateInput}
onSelect={(value)=>input.onChange(value)}
filterOption
/>
{meta.error}
}
}
class ArrayFieldRenderer extends React.Component>,any>{
render(){
const props = this.props;
return
{
props.fields.map((_, i) => {
let children = props.fieldSchema.children;
return
props.fields.remove(i)}/>
{
renderFields(props.meta.form,children,props.keyPath+"["+i+"]")
}
})
}
props.fields.push(props.fieldSchema.defaultValue||{})}/>
}
}
addType('text',TextInput);
addType('select',SelectInput);
addType('radio',SelectRadio)
addType('checkbox',CheckboxInput);
addType('date',DateInput);
addType('autocomplete-text',AutoCompleteText);
addType('datetime',DateTimeInput);
addType('datetimeRange',DateTimeRangeInput);
addType('number',NumberInput);
addType('autocomplete',AutoCompleteSelect);
addType("file",FileInput);
addType("dateRange",DateRangeInput);
addType("textarea",TextareaInput);
addType("password",TextInput);
addType("email",TextInput);
addType('text',TextInput);
addTypeWithWrapper("array",(props)=>{
return
});
addType("autocomplete-async",AutoCompleteAsync);
setButton(function(props:any){
switch (props.type) {
case 'submit':
return ;
case "button":
return
default:
return null;
}
});