import * as React from 'react'; import { DefaultButton } from 'office-ui-fabric-react/lib/Button'; import { TextField } from 'office-ui-fabric-react/lib/TextField'; import cx = require("classnames") export interface RestorableTextFieldProps { className?: string label: string initialValue: string /** null => valid, otherwise error message */ validate?: (value: string) => string| null onChanged?: (value: string) => void } export interface RestorableTextFieldState { value: string } export class RestorableTextField extends React.Component { constructor(props: RestorableTextFieldProps) { super(props) this.state = { value: props.initialValue } } public render(): JSX.Element { return (
Restore
) } private validate = (value: string): string => { if(this.props.validate) { const rval = this.props.validate(value) return rval ? rval : "" } return "" } private onChanged = (value: string): void => { this.setState({ value }) if(this.props.onChanged) this.props.onChanged(value) } private restore = (): void => { this.setState({ value: this.props.initialValue }) } } /** Validate number with `isNan`. */ export class NumberTextField extends React.Component { constructor(props: RestorableTextFieldProps) { super(props) } public render(): JSX.Element { return ( ) } private validateNumber(value: string): string { return isNaN(Number(value)) ? `The value should be a number, actual is ${value}.` : '' } }