Can input text

This commit is contained in:
Pierre HUBERT 2021-05-10 18:02:09 +02:00
parent 4b9977b93f
commit d02ab79f50
2 changed files with 116 additions and 2 deletions

View File

@ -1,12 +1,15 @@
import React from 'react'; import React from 'react';
import './App.css'; import './App.css';
import { Button, createMuiTheme, ThemeProvider } from '@material-ui/core'; import { Button, createMuiTheme, ThemeProvider } from '@material-ui/core';
import { ApplicationDialogsProvider, matAlert, matConfirm } from './ui/widgets/DialogsProvider'; import { ApplicationDialogsProvider, input, matAlert, matConfirm } from './ui/widgets/DialogsProvider';
import { lightBlue, pink } from '@material-ui/core/colors';
function App() { function App() {
const darkTheme = createMuiTheme({ const darkTheme = createMuiTheme({
palette: { palette: {
type: 'dark', type: 'dark',
primary: lightBlue,
secondary: pink,
}, },
}); });
@ -20,6 +23,13 @@ function App() {
<Button onClick={()=>matAlert("heloo!!")}>alert</Button> <Button onClick={()=>matAlert("heloo!!")}>alert</Button>
<Button onClick={()=>matConfirm("heloo!!").then((r) => matAlert("response: " + r))}>confirm</Button> <Button onClick={()=>matConfirm("heloo!!").then((r) => matAlert("response: " + r))}>confirm</Button>
<Button onClick={()=>input({
label: "Your best joke",
message: "Please share your best joke:",
initialValue: "initial",
minLength: 2,
maxLength: 10,
}).then((r) => matAlert("you wrote: " + r))}>input</Button>
</div> </div>
</ThemeProvider> </ThemeProvider>
); );

View File

@ -4,11 +4,20 @@
* @author Pierre Hubert * @author Pierre Hubert
*/ */
import { Dialog, DialogTitle, DialogContent, DialogContentText, DialogActions, Button } from "@material-ui/core"; import { Dialog, DialogTitle, DialogContent, DialogContentText, DialogActions, Button, TextField } from "@material-ui/core";
import React from "react"; import React from "react";
let cache : ApplicationDialogsProvider; let cache : ApplicationDialogsProvider;
export interface TextInputOptions {
title ?: string,
message ?: string,
initialValue ?: string,
minLength ?: number,
maxLength ?: number,
label : string,
}
interface AppDiagProvState { interface AppDiagProvState {
// Alert dialog // Alert dialog
@ -20,11 +29,19 @@ interface AppDiagProvState {
showConfirm: boolean, showConfirm: boolean,
resolveConfirm ?: (confirmed: boolean) => void, resolveConfirm ?: (confirmed: boolean) => void,
// Text input dialog
showInputDialog: boolean,
inputValue: string,
inputOptions: TextInputOptions,
resolveInput ?: (text: string) => void,
} }
export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvState> { export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvState> {
private acceptConfirm: () => void; private acceptConfirm: () => void;
private rejectConfirm: () => void; private rejectConfirm: () => void;
private cancelInput: () => void;
private confirmInput: () => void;
constructor(props: any) { constructor(props: any) {
super(props); super(props);
@ -35,15 +52,28 @@ export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvS
alertMessage: "", alertMessage: "",
showAlert: false, showAlert: false,
// Confirm dialog
showConfirm: false, showConfirm: false,
confirmMessage: "", confirmMessage: "",
resolveConfirm: undefined, resolveConfirm: undefined,
// Text input dialog
showInputDialog: false,
inputValue: "",
inputOptions: {
label: ""
},
resolveInput: undefined,
}; };
this.handleCloseAlert = this.handleCloseAlert.bind(this); this.handleCloseAlert = this.handleCloseAlert.bind(this);
this.acceptConfirm = this.handleCloseConfirm.bind(this, true); this.acceptConfirm = this.handleCloseConfirm.bind(this, true);
this.rejectConfirm = this.handleCloseConfirm.bind(this, false); this.rejectConfirm = this.handleCloseConfirm.bind(this, false);
this.handleInputValueChanged = this.handleInputValueChanged.bind(this);
this.cancelInput = this.handleCloseInput.bind(this, true);
this.confirmInput = this.handleCloseInput.bind(this, false);
} }
showAlert(message: string) { showAlert(message: string) {
@ -75,6 +105,43 @@ export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvS
this.state.resolveConfirm && this.state.resolveConfirm(accept); this.state.resolveConfirm && this.state.resolveConfirm(accept);
} }
showInput(options: TextInputOptions) : Promise<string> {
return new Promise((res, _rej) => {
this.setState({
showInputDialog: true,
inputOptions: options,
resolveInput: res,
inputValue: options.initialValue || ""
});
});
}
handleInputValueChanged(e: React.ChangeEvent<HTMLInputElement>){
this.setState({inputValue: e.target.value});
}
handleCloseInput(cancel: boolean) {
this.setState({
showInputDialog: false
});
if (!cancel)
this.state.resolveInput && this.state.resolveInput(this.state.inputValue);
}
get isInputValid() : boolean {
const options = this.state.inputOptions;
const value = this.state.inputValue;
if (options.minLength && value.length < options.minLength)
return false;
if (options.maxLength && value.length > options.maxLength)
return false;
return true;
}
render() { render() {
cache = this; cache = this;
@ -125,6 +192,39 @@ export class ApplicationDialogsProvider extends React.Component<{}, AppDiagProvS
</Button> </Button>
</DialogActions> </DialogActions>
</Dialog> </Dialog>
{/* Text input dialog */}
<Dialog
open={this.state.showInputDialog}
onClose={this.rejectConfirm}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
<DialogTitle id="alert-dialog-title">
{this.state.inputOptions.title || this.state.inputOptions.label}
</DialogTitle>
<DialogContent>
{this.state.inputOptions.message ? <DialogContentText id="alert-dialog-description">
{this.state.inputOptions.message} <br /><br />
</DialogContentText> : <span></span> }
<TextField
label={this.state.inputOptions.label}
variant="outlined"
value={this.state.inputValue}
onChange={this.handleInputValueChanged}
/>
</DialogContent>
<DialogActions>
<Button onClick={this.cancelInput} color="default">
Cancel
</Button>
<Button onClick={this.confirmInput} color="default" disabled={!this.isInputValid}>
OK
</Button>
</DialogActions>
</Dialog>
</div>) </div>)
} }
} }
@ -136,3 +236,7 @@ export function matAlert(msg: string) {
export function matConfirm(msg: string) : Promise<boolean> { export function matConfirm(msg: string) : Promise<boolean> {
return cache.showConfirm(msg); return cache.showConfirm(msg);
} }
export function input(options: TextInputOptions) : Promise<string> {
return cache.showInput(options);
}