import * as css from './index.scss' import { WeElement, tag, h, extractClass, classNames } from 'omi' import { MDCTextField } from '@material/textfield/index' import '../icon' //import { MDCRipple } from '@material/ripple/index' //@ts-ignore import '../theme.ts' interface Props { type: 'date' | 'time' | 'color' | 'datetime-local', fullWidth: boolean, textarea: boolean, outlined: boolean, noLabel: boolean, showHelper: boolean, helperText: string, iconRight: boolean, //counter: number[], counter: boolean, path: string, paths: object, //Multi-line Text Field (Textarea) with Character Counter (textarea+counter) label: string, required: boolean, pattern: string, //RegExp string such as [A-z]{3} minLength: number, maxLength: number, min: number, max: number, step: number, rows: number, cols: number, value: string, disabled: boolean, //also add style class useNativeValidation: boolean, valid: boolean, helperTextContent: string, //ripple: MDCRipple, leadingIconAriaLabel: string, trailingIconAriaLabel: string, leadingIconContent: string, trailingIconContent: string } interface Data { } function extract(from, props):any { const to = {} props.forEach(prop => { if (from[prop] !== undefined) { to[prop] = from[prop] } }) return to } @tag('m-text-field') export default class TextField extends WeElement{ static css = css static defaultProps = { showHelper: true, type: 'text' } static propTypes = { fullWidth: Boolean, textarea: Boolean, outlined: Boolean, noLabel: Boolean, showHelper: Boolean, helperText: String, iconRight: Boolean, counter: Boolean, path: String, paths: Object, //Multi-line Text Field (Textarea) with Character Counter (textarea+counter) label: String, required: Boolean, pattern: String, //RegExp string such as [A-z]{3} minLength: Number, maxLength: Number, min: Number, max: Number, step: Number, rows: Number, cols: Number, value: String, disabled: Boolean, //also add style class useNativeValidation: Boolean, valid: Boolean, helperTextContent: String, //ripple: MDCRipple, leadingIconAriaLabel: String, trailingIconAriaLabel: String, leadingIconContent: String, trailingIconContent: String, leftIcon: String, rightIcon: String, iconEvent: Boolean } mdc: MDCTextField root: HTMLElement installed() { this.mdc = new MDCTextField(this.root) } focus = () => { this.mdc.focus() } layout = () => { this.mdc.layout() } uninstall() { this.mdc.destroy() } refIt = (e) => { this.root = e } iconClick = () => { this.mdc.focus() this.fire('iconclick') } render(props) { const cls = extractClass(props, 'mdc-text-field', { 'mdc-text-field--outlined': props.outlined, 'mdc-text-field--fullwidth': props.fullWidth, 'mdc-text-field--textarea': props.textarea, 'mdc-text-field--disabled': props.disabled, 'mdc-text-field--with-leading-icon': props.leftIcon || ((props.path || props.paths) && !props.iconRight), 'mdc-text-field--with-trailing-icon': props.rightIcon ||((props.path || props.paths) && props.iconRight) }) const inputProps = extract(props, ['disabled', 'required', 'pattern', 'value', 'minLength', 'maxLength', 'min', 'max', 'step']) if(props.fullWidth && !props.outlined){ inputProps.placeholder = props.label props.label = null //直接修改 props 需要注意同步 this['__omiattr_'],不然下次和this['__omiattr_']的值进行 diff 结果相同导致不更新 this['__omiattr_'] && (this['__omiattr_'].label = null) } const vd = [
{(props.path || props.paths) && !props.iconRight && } {props.leftIcon && {props.leftIcon}} {props.counter && props.textarea &&
} { props.textarea ? : } { props.outlined ?
{props.label === undefined || !props.noLabel && }
: (props.label === undefined || !props.noLabel && ) } {(props.path || props.paths) && props.iconRight && } {props.rightIcon && {props.rightIcon}} {!props.outlined &&
}
] if (props.helperText || (props.counter && !props.textarea)) { vd.push(
{props.helperText &&
{props.helperText}
} {props.counter && !props.textarea &&
}
) } return vd } }