|
- // material-ui
- import {
- Button,
- Grid, TextField,
- Typography
- } from '@mui/material';
- import MainCard from "components/MainCard";
- import { useForm } from "react-hook-form";
- import * as React from "react";
- import * as DateUtils from "utils/DateUtils";
- import { ThemeProvider } from "@emotion/react";
- // import { useNavigate } from "react-router-dom";
- import { PNSPS_BUTTON_THEME } from "../../../themes/buttonConst";
- import { FormattedMessage, useIntl } from "react-intl";
- import { makeStyles } from '@mui/styles';
- // ==============================|| DASHBOARD - DEFAULT ||============================== //
- const useStyles = makeStyles(() => ({
- root: {
- position: "relative"
- },
- display: {
- position: "absolute",
- top: 2,
- left: 12,
- bottom: 2,
- background: "white",
- pointerEvents: "none",
- right: 50,
- display: "flex",
- alignItems: "center"
- },
- }));
-
- const SearchPublicNoticeForm = ({ applySearch, searchCriteria }) => {
- // const navigate = useNavigate()
-
- const [minDate, setMinDate] = React.useState(searchCriteria.dateFrom);
- const [maxDate, setMaxDate] = React.useState(searchCriteria.dateTo);
-
- const [fromDateValue, setFromDateValue] = React.useState("dd / mm / yyyy");
- const [toDateValue, setToDateValue] = React.useState("dd / mm / yyyy");
-
- const intl = useIntl();
-
- React.useEffect(() => {
- // console.log(minDate)
- setFromDateValue(minDate);
- }, [minDate]);
-
- React.useEffect(() => {
- setToDateValue(maxDate);
- }, [maxDate]);
-
- function FormDateInputComponent({inputRef, ...props }) {
- const classes = useStyles();
- return (
- <>
- <div className={classes.display}>
- {DateUtils.dateStr(fromDateValue)=="Invalid Date"?
- fromDateValue
- :
- DateUtils.dateStr(fromDateValue)}
- </div>
- <input
- // className={classes.input}
- ref={inputRef}
- {...props}
- // onChange={handleChange}
- value={fromDateValue}
- max= {maxDate}
- />
- </>
- );
- }
-
- function ToDateInputComponent({inputRef, ...props }) {
- const classes = useStyles();
- return (
- <>
- <div className={classes.display}>
- {DateUtils.dateStr(toDateValue)=="Invalid Date"?
- toDateValue
- :
- DateUtils.dateStr(toDateValue)}
- </div>
- <input
- // className={classes.input}
- ref={inputRef}
- {...props}
- // onChange={handleChange}
- value={toDateValue}
- min = {minDate}
- />
- </>
- );
- }
-
- const marginBottom = 2.5;
- const { reset, register, handleSubmit } = useForm()
- const onSubmit = (data) => {
- let sentDateFrom = "";
- let sentDateTo = "";
- if( fromDateValue!="dd / mm / yyyy"&&toDateValue!="dd / mm / yyyy"){
- sentDateFrom = DateUtils.dateValue(fromDateValue)!="Invalid Date"?DateUtils.dateValue(fromDateValue):""
- sentDateTo = DateUtils.dateValue(toDateValue)!="Invalid Date"?DateUtils.dateValue(toDateValue):""
- }
- const temp = {
- key: data.key,
- dateFrom: sentDateFrom,
- dateTo: sentDateTo,
- };
- applySearch(temp);
- };
-
-
- function resetForm() {
- setMinDate(DateUtils.dateValue(new Date().setDate(new Date().getDate()-14)))
- setMaxDate(DateUtils.dateValue(new Date()))
- reset();
- }
-
-
- return (
- <MainCard xs={12} md={12} lg={12}
- border={false}
- content={false}
- sx={{ backgroundColor: '#fff' }}
- >
-
- <form onSubmit={handleSubmit(onSubmit)}>
- <Grid container sx={{ backgroundColor: '#ffffff', ml: 2, mt: 1, mb: marginBottom }} width="98%">
- {/*row 1*/}
- <Grid item justifyContent="space-between" alignItems="center" sx={{ mt: 1, ml: 3, mb: marginBottom }}>
- <Typography variant="pnspsFormHeader" >
- <FormattedMessage id="search"></FormattedMessage>
- </Typography>
- </Grid>
- {/*row 2*/}
- <Grid container display="flex" alignItems={"center"}>
- <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom }}>
- <TextField
- fullWidth
- {...register("key")}
- id='key'
- label={intl.formatMessage({ id: 'keyword' }) + ":"}
- defaultValue={searchCriteria.key}
- InputLabelProps={{
- shrink: true
- }}
- />
- </Grid>
-
- <Grid item xs={9} s={6} md={5} lg={3} sx={{ ml: 3, mr: 3, mb: marginBottom }}>
- <Grid container>
- <Grid item xs={5.25} s={5.25} md={5.25} lg={5.5}>
- <TextField
- fullWidth
- {...register("dateFrom")}
- id="dateFrom"
- type="date"
- label={intl.formatMessage({ id: 'dateFrom' }) + ":"}
- defaultValue={searchCriteria.dateFrom}
- InputProps={{
- inputComponent: FormDateInputComponent,
- }}
- onChange={(newValue) => {
- if(newValue.target.value!=""){
- setMinDate(newValue.target.value);
- }
- }}
- InputLabelProps={{
- shrink: true
- }}
- sx={{ "& .MuiInputBase-input": {display:"block", textIndent: "-9999px"} }}
- />
- </Grid>
-
- <Grid item xs={1.5} s={1.5} md={1.5} lg={1} sx={{ mt: 0.8, display: 'flex', justifyContent: "center", alignItems: 'flex-start' }}>
- <FormattedMessage id="to"></FormattedMessage>
- </Grid>
-
- <Grid item xs={5.25} s={5.25} md={5.25} lg={5.5}>
- <TextField
- fullWidth
- InputLabelProps={{
- shrink: true
- }}
- {...register("dateTo")}
- InputProps={{
- inputComponent: ToDateInputComponent,
- }}
- onChange={(newValue) => {
- if(newValue.target.value!=""){
- setMaxDate(newValue.target.value);
- }
- }}
- id="dateTo"
- type="date"
- //label={"Submit Date(To)"}
- defaultValue={searchCriteria.dateTo}
- sx={{ "& .MuiInputBase-input": {display:"block", textIndent: "-9999px"} }}
- />
- </Grid>
- </Grid>
- </Grid>
-
- </Grid>
-
- {/*last row*/}
- <Grid container maxWidth justifyContent="flex-end">
- <ThemeProvider theme={PNSPS_BUTTON_THEME}>
- {/* <Grid item sx={{ ml: 3 }}>
- <Button
- variant="contained"
- color="green"
- onClick={() => {
- navigate('/setting/announcement/details/' + 0);
- }}
- >
- <FormattedMessage id="create"></FormattedMessage>
- </Button>
- </Grid> */}
- <Grid item sx={{ ml: 3 }}>
- <Button
- variant="contained"
- onClick={resetForm}
- >
- <FormattedMessage id="reset"></FormattedMessage>
- </Button>
- </Grid>
-
- <Grid item sx={{ ml: 3 }}>
- <Button
- variant="contained"
- type="submit"
- >
- <FormattedMessage id="submit"></FormattedMessage>
- </Button>
- </Grid>
- </ThemeProvider>
- </Grid>
- </Grid>
- </form>
- </MainCard>
- );
- };
-
- export default SearchPublicNoticeForm;
|