0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-17 23:44:39 -05:00

Added autofocus option to input fields

no refs

Allows input components to define if they want to autofocus on component load, useful for autofocus on form pages in Portal
This commit is contained in:
Rish 2021-03-10 15:05:49 +05:30
parent d381382f4e
commit 74f8d4eb86
2 changed files with 10 additions and 1 deletions

View file

@ -1,4 +1,4 @@
import React from 'react'; import React, { useEffect, useRef } from 'react';
import {isCookiesDisabled} from '../../utils/helpers'; import {isCookiesDisabled} from '../../utils/helpers';
export const InputFieldStyles = ` export const InputFieldStyles = `
@ -92,8 +92,10 @@ function InputField({
onKeyDown = () => {}, onKeyDown = () => {},
tabindex, tabindex,
maxlength, maxlength,
autoFocus,
errorMessage errorMessage
}) { }) {
const fieldNode = useRef(null);
id = id || `input-${name}`; id = id || `input-${name}`;
const labelClasses = hideLabel ? 'gh-portal-input-label hidden' : 'gh-portal-input-label'; const labelClasses = hideLabel ? 'gh-portal-input-label hidden' : 'gh-portal-input-label';
const inputClasses = errorMessage ? 'gh-portal-input error' : 'gh-portal-input'; const inputClasses = errorMessage ? 'gh-portal-input error' : 'gh-portal-input';
@ -116,6 +118,11 @@ function InputField({
default: default:
break; break;
} }
useEffect(() => {
if (autoFocus) {
fieldNode.current.focus();
}
}, [autoFocus]);
return ( return (
<section className='gh-portal-input-section'> <section className='gh-portal-input-section'>
<div className='gh-portal-input-labelcontainer'> <div className='gh-portal-input-labelcontainer'>
@ -123,6 +130,7 @@ function InputField({
<InputError message={errorMessage} name={name} /> <InputError message={errorMessage} name={name} />
</div> </div>
<input <input
ref={fieldNode}
id={id} id={id}
className={inputClasses} className={inputClasses}
type={type} type={type}

View file

@ -19,6 +19,7 @@ const FormInput = ({field, onChange, onBlur = () => { }, onKeyDown = () => {}})
onBlur={e => onBlur(e, field)} onBlur={e => onBlur(e, field)}
tabIndex={field.tabindex} tabIndex={field.tabindex}
errorMessage={field.errorMessage} errorMessage={field.errorMessage}
autoFocus={field.autoFocus}
/> />
</> </>
); );