Skip to content

Commit 4e38ccf

Browse files
committed
feat: add innerRef to Datepicker
1 parent 2055cee commit 4e38ccf

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

component-overview/examples/datepicker/Datepicker.jsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { useState } from 'react';
1+
import { useState, useRef } from 'react';
22
import Datepicker from '@sb1/ffe-datepicker-react';
33

44
() => {
55
const [date, setDate] = useState('01.01.2016');
6+
const innerRef = useRef(date); //Optional ref to the input element
67

78
return (
89
<Datepicker
@@ -13,6 +14,7 @@ import Datepicker from '@sb1/ffe-datepicker-react';
1314
minDate="01.01.2016"
1415
onChange={setDate}
1516
value={date}
17+
innerRef={innerRef}
1618
/>
1719
);
1820
}

packages/ffe-datepicker-react/src/datepicker/Datepicker.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Component } from 'react';
2-
import { bool, func, oneOfType, shape, string } from 'prop-types';
2+
import { bool, func, oneOfType, shape, string, PropTypes } from 'prop-types';
33
import classNames from 'classnames';
44
import { v4 as uuid } from 'uuid';
55
import Calendar from '../calendar';
@@ -292,6 +292,7 @@ export default class Datepicker extends Component {
292292
onChange,
293293
value,
294294
fullWidth,
295+
innerRef,
295296
} = this.props;
296297
const { minDate, maxDate } = this.state;
297298

@@ -349,6 +350,10 @@ export default class Datepicker extends Component {
349350
onChange={evt => onChange(evt.target.value)}
350351
onKeyDown={this.onInputKeydown}
351352
ref={c => {
353+
if (innerRef) {
354+
innerRef.current = c;
355+
}
356+
352357
this.dateInputRef = c;
353358
}}
354359
value={value}
@@ -399,6 +404,7 @@ Datepicker.defaultProps = {
399404
keepDisplayStateOnError: false,
400405
onValidationComplete: () => {},
401406
fullWidth: false,
407+
innerRef: undefined,
402408
};
403409

404410
Datepicker.propTypes = {
@@ -407,6 +413,9 @@ Datepicker.propTypes = {
407413
calendarAbove: bool,
408414
hideErrors: bool,
409415
onValidationComplete: func,
416+
innerRef:
417+
PropTypes.oneOfType([func, shape({ current: PropTypes.any })]) ||
418+
undefined,
410419
inputProps: shape({
411420
className: string,
412421
id: string,

0 commit comments

Comments
 (0)