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

Corrige le bug d'autoFocus dans les modales #1190

Merged
merged 2 commits into from
Jan 21, 2025
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
37 changes: 24 additions & 13 deletions front/src/components/Field.jsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
import clsx from 'clsx'
import React, { forwardRef } from 'react'
import React, { forwardRef, useEffect, useRef } from 'react'
import styles from './field.module.scss'

export default forwardRef(function Field(
{ hasError, className, prefix, children, label, id, type, ...otherProps },
{
hasError,
className,
prefix,
children,
label,
id,
type,
autoFocus = false,
...otherProps
},
forwardedRef
) {
const classNames = [
const inputRef = forwardedRef ?? useRef()
const classNames = clsx(
styles.field,
prefix && styles.withPrefix,
'control-field',
]

if (className) {
classNames.push(className)
}
if (hasError) {
classNames.push(styles.error)
}
className && className,
hasError && styles.error
)

const computedStyles = { '--chars-count': prefix?.length }
useEffect(() => {
if (autoFocus) {
inputRef.current.focus()
}
}, [])

return (
<div className={clsx(classNames)}>
<div className={classNames}>
{label && <label htmlFor={id}>{label}</label>}
<div
className={clsx('control', otherProps.icon && 'has-icons-left')}
Expand All @@ -36,7 +47,7 @@ export default forwardRef(function Field(
{...otherProps}
className="input"
type={type || 'text'}
ref={forwardedRef}
ref={inputRef}
/>
{otherProps.icon && (
<span className="icon is-small is-left">
Expand Down
1 change: 1 addition & 0 deletions front/src/components/TagCreate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export default function TagCreate() {
<section className={styles.create}>
<form onSubmit={handleCreateTag}>
<Field
autoFocus={true}
label={t('tag.createForm.nameField')}
type="text"
{...nameBindings}
Expand Down
1 change: 1 addition & 0 deletions front/src/components/workspace/CreateWorkspace.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default function CreateWorkspace() {
<section>
<form onSubmit={handleSubmit}>
<Field
autoFocus={true}
ref={nameInputRef}
{...nameBindings}
label={t('workspace.createForm.nameField')}
Expand Down
Loading