Skip to content

Commit

Permalink
TextArea refactor with react-aria
Browse files Browse the repository at this point in the history
  • Loading branch information
sneridagh committed Nov 13, 2022
1 parent 6c877ff commit 3a3bdd5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ styleguide.config
.vscode
packages/*
!packages/volto-slate
!packages/volto-quanta
.tx

# yarn 3
Expand Down
6 changes: 5 additions & 1 deletion packages/volto-quanta/src/components/Input/Input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ const Input = React.forwardRef((props, ref) => {
errorMessageProps={errorMessageProps}
className="text"
>
<input ref={inputRef} {...mergeProps(localInputProps, inputProps)} />
<input
ref={inputRef}
required={required}
{...mergeProps(localInputProps, inputProps)}
/>
</FormFieldWrapper>
);
});
Expand Down
59 changes: 41 additions & 18 deletions packages/volto-quanta/src/components/TextArea/TextArea.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,73 @@ import FormFieldWrapper from '../FormFieldWrapper/FormFieldWrapper';
import cx from 'classnames';
import { isNil } from 'lodash';
import { useForwardedRef } from '../../helpers';
import { useTextField } from 'react-aria';
import { mergeProps } from 'react-aria';

const TextArea = React.forwardRef((props, ref) => {
const {
disabled,
error,
id,
minLength,
maxLength,
onChange,
onClick,
placeholder,
readOnly,
required,
tabIndex,
title,
value,
} = props;

const TextAreaRef = useForwardedRef(ref);

const {
labelProps,
inputProps,
descriptionProps,
errorMessageProps,
} = useTextField(
{
...props,
inputElementType: 'textarea',
id: `field-${id}`,
label: title,
isDisabled: disabled,
isReadOnly: readOnly,
isRequired: required,
},
TextAreaRef,
);

const computeTabIndex = () => {
if (!isNil(tabIndex)) return tabIndex;
if (disabled) return -1;
};

const localTextAreaProps = {
className: cx('q input textarea', { error: error }),
placeholder: placeholder || ' ',
onChange: ({ target }) =>
readOnly
? undefined
: onChange(id, target.value === '' ? undefined : target.value),
onClick: () => onClick(),
tabIndex: computeTabIndex(),
value: value || '',
};

return (
<FormFieldWrapper {...props} className="text">
<FormFieldWrapper
{...props}
labelProps={labelProps}
descriptionProps={descriptionProps}
errorMessageProps={errorMessageProps}
className="text"
>
<textarea
aria-labelledby={`field-label-${id}`}
aria-describedby={`field-hint-${id}`}
className={cx('q input textarea', { error: error })}
id={`field-${id}`}
disabled={disabled}
placeholder={placeholder || ' '}
onChange={({ target }) =>
readOnly
? undefined
: onChange(id, target.value === '' ? undefined : target.value)
}
onClick={() => onClick()}
readOnly={readOnly}
ref={TextAreaRef}
required={required}
tabIndex={computeTabIndex()}
value={value || ''}
{...mergeProps(localTextAreaProps, inputProps)}
/>
</FormFieldWrapper>
);
Expand Down

0 comments on commit 3a3bdd5

Please sign in to comment.