Skip to content

Commit

Permalink
TextArea complete
Browse files Browse the repository at this point in the history
  • Loading branch information
sneridagh committed Jul 3, 2021
1 parent c20df21 commit b52187b
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/components/FormFieldWrapper/FormFieldWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';

const FieldWrapper = (props) => {
const { children, description, disabled, error, fieldSet, id, title } = props;
console.log(error);

return (
<div className="q field">
{children}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Input/Input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ const Input = React.forwardRef((props, ref) => {
minLength={minLength || null}
maxLength={maxLength || null}
placeholder={placeholder || ' '}
onClick={() => onClick()}
onChange={({ target }) =>
readOnly
? undefined
: onChange(id, target.value === '' ? undefined : target.value)
}
onClick={() => onClick()}
readOnly={readOnly}
ref={inputRef}
required={required}
Expand Down
41 changes: 37 additions & 4 deletions src/components/TextArea/TextArea.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,31 @@ import React from 'react';
import PropTypes from 'prop-types';
import FormFieldWrapper from '../FormFieldWrapper/FormFieldWrapper';
import cx from 'classnames';
import { isNil } from 'lodash';
import { useForwardedRef } from '../../helpers';

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

const TextAreaRef = useForwardedRef(ref);

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

return (
<FormFieldWrapper {...props} className="text">
Expand All @@ -13,12 +35,23 @@ const TextArea = (props) => {
aria-describedby={`field-hint-${id}`}
className={cx('q input textarea', { error: error })}
id={`field-${id}`}
disabled={disabled}
placeholder={placeholder || ' '}
// {...props}
onChange={({ target }) =>
readOnly
? undefined
: onChange(id, target.value === '' ? undefined : target.value)
}
onClick={() => onClick()}
readOnly={readOnly}
ref={TextAreaRef}
required={required}
tabIndex={computeTabIndex()}
value={value || ''}
/>
</FormFieldWrapper>
);
};
});

TextArea.propTypes = {
id: PropTypes.string.isRequired,
Expand Down
81 changes: 81 additions & 0 deletions src/components/TextArea/TextArea.stories.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React from 'react';
import TextAreaComponent from './TextArea';

const TextArea = (args) => {
const [value, setValue] = React.useState(args.value ?? '');
const onChange = (block, value) => {
args.onChange({ value });
setValue(value);
};

return <TextAreaComponent {...args} onChange={onChange} value={value} />;
};

export const Default = TextArea.bind({});
Default.args = {
id: 'field-empty',
title: 'field 1 title',
description: 'Optional help text',
placeholder: 'Type something…',
};

export const Required = TextArea.bind({});
Required.args = {
id: 'field-empty',
title: 'field 1 title',
description: 'Optional help text',
placeholder: 'Type something…',
required: true,
};

export const Filled = TextArea.bind({});
Filled.args = {
id: 'field-filled',
title: 'Filled field title',
description: 'Optional help text',
value: 'Filled with value A',
placeholder: 'Type something…',
required: true,
};

export const Errored = TextArea.bind({});
Errored.args = {
id: 'field-errored',
title: 'Errored field title',
description: 'Optional help text',
placeholder: 'Type something…',
value: 'Filled with value A',
error: ['This is the error'],
required: true,
};

export const Disabled = TextArea.bind({});
Disabled.args = {
id: 'field-disabled',
title: 'Disabled field title',
description: 'Optional help text',
placeholder: 'Type something…',
disabled: true,
};

export default {
title: 'TextArea',
component: TextAreaComponent,
decorators: [
(Story) => (
<div style={{ width: '400px' }}>
<Story />
</div>
),
],
argTypes: {
// controlled value prop
value: {
control: {
disable: true,
},
},
},
// excludeStories: ['searchResults'],
// subcomponents: { ArgsTable },
};

0 comments on commit b52187b

Please sign in to comment.