Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(datepicker): add innerRef to Datepicker #1775

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion component-overview/examples/datepicker/Datepicker.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useState } from 'react';
import { useState, useRef } from 'react';
import Datepicker from '@sb1/ffe-datepicker-react';

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

return (
<Datepicker
Expand All @@ -13,6 +14,7 @@ import Datepicker from '@sb1/ffe-datepicker-react';
minDate="01.01.2016"
onChange={setDate}
value={date}
innerRef={innerRef}
/>
);
}
11 changes: 10 additions & 1 deletion packages/ffe-datepicker-react/src/datepicker/Datepicker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Component } from 'react';
import { bool, func, oneOfType, shape, string } from 'prop-types';
import { bool, func, oneOfType, shape, string, PropTypes } from 'prop-types';
import classNames from 'classnames';
import { v4 as uuid } from 'uuid';
import Calendar from '../calendar';
Expand Down Expand Up @@ -292,6 +292,7 @@ export default class Datepicker extends Component {
onChange,
value,
fullWidth,
innerRef,
} = this.props;
const { minDate, maxDate } = this.state;

Expand Down Expand Up @@ -349,6 +350,10 @@ export default class Datepicker extends Component {
onChange={evt => onChange(evt.target.value)}
onKeyDown={this.onInputKeydown}
ref={c => {
if (innerRef) {
innerRef.current = c;
}

this.dateInputRef = c;
}}
value={value}
Expand Down Expand Up @@ -399,6 +404,7 @@ Datepicker.defaultProps = {
keepDisplayStateOnError: false,
onValidationComplete: () => {},
fullWidth: false,
innerRef: undefined,
};

Datepicker.propTypes = {
Expand All @@ -407,6 +413,9 @@ Datepicker.propTypes = {
calendarAbove: bool,
hideErrors: bool,
onValidationComplete: func,
innerRef:
PropTypes.oneOfType([func, shape({ current: PropTypes.any })]) ||
undefined,
inputProps: shape({
className: string,
id: string,
Expand Down
Loading