onBlur and focusOnRender props were added

This commit is contained in:
Vladimir Khvan 2023-07-05 14:13:12 +05:00
parent 736529b0c4
commit f7bed93bb7
2 changed files with 28 additions and 11 deletions

View File

@ -20,9 +20,11 @@ import TimePicker from "@docspace/components/time-picker";
#### Properties #### Properties
| Props | Type | Required | Values | Default | Description | | Props | Type | Required | Values | Default | Description |
| -------------- | :------------: | :------: | :-----------: | :-----------------------: | ------------------------------------------------------------ | | --------------- | :------: | :------: | :----: | :-----: | ------------------------------------------------ |
| `className` | `string` | - | - | '' | Allows to set classname | | `className` | `string` | - | - | '' | Allows to set classname |
| `date` | `object` | - | - | - | Inital date | | `date` | `object` | - | - | - | Inital date |
| `setDate` | `func` | - | - | - | State setter function | | `setDate` | `func` | - | - | - | State setter function |
| `onChange` | `func` | - | - | - | Allow you to handle changing events of component | | `onChange` | `func` | - | - | - | Allow you to handle changing events of component |
| `hasError` | `bool` | - | - | false | Indicates error | | `hasError` | `bool` | - | - | false | Indicates error |
| `onBlur` | `func` | - | - | - | Triggers function on blur |
| `focusOnRender` | `bool` | - | - | false | Focus input on render |

View File

@ -1,4 +1,4 @@
import React, { useRef, useState } from "react"; import React, { useRef, useState, useEffect } from "react";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import moment from "moment"; import moment from "moment";
import styled, { css } from "styled-components"; import styled, { css } from "styled-components";
@ -51,10 +51,11 @@ const TimePicker = ({
className, className,
hasError, hasError,
tabIndex, tabIndex,
onBlur,
focusOnRender,
}) => { }) => {
const hoursInputRef = useRef(null); const hoursInputRef = useRef(null);
const minutesInputRef = useRef(null); const minutesInputRef = useRef(null);
const timeInputRef = useRef(null);
const [isInputFocused, setIsInputFocused] = useState(false); const [isInputFocused, setIsInputFocused] = useState(false);
@ -62,6 +63,17 @@ const TimePicker = ({
const [minutes, setMinutes] = useState(moment(date, "HH:mm").format("mm")); const [minutes, setMinutes] = useState(moment(date, "HH:mm").format("mm"));
const mountRef = useRef(false);
useEffect(() => {
onBlur && mountRef.current && !isInputFocused && onBlur();
}, [isInputFocused]);
useEffect(() => {
focusOnRender && hoursInputRef.current.select();
mountRef.current = true;
}, []);
const changeHours = (time) => { const changeHours = (time) => {
setHours(time); setHours(time);
setDate( setDate(
@ -155,7 +167,6 @@ const TimePicker = ({
return ( return (
<TimeInput <TimeInput
ref={timeInputRef}
onClick={focusHoursInput} onClick={focusHoursInput}
className={className} className={className}
hasError={hasError} hasError={hasError}
@ -183,7 +194,6 @@ const TimePicker = ({
</TimeInput> </TimeInput>
); );
}; };
TimePicker.propTypes = { TimePicker.propTypes = {
/** Inital date */ /** Inital date */
date: PropTypes.object, date: PropTypes.object,
@ -197,12 +207,17 @@ TimePicker.propTypes = {
hasError: PropTypes.bool, hasError: PropTypes.bool,
/** Tab index allows to make element focusable */ /** Tab index allows to make element focusable */
hasError: PropTypes.bool, hasError: PropTypes.bool,
/** Triggers function on blur */
onBlur: PropTypes.func,
/** Focus input on render */
focusOnRender: PropTypes.bool,
}; };
TimePicker.defaultProps = { TimePicker.defaultProps = {
onChange: () => {}, onChange: () => {},
className: "", className: "",
hasError: false, hasError: false,
focusOnRender: false,
}; };
export default TimePicker; export default TimePicker;