-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components): add textarea (#53)
- Loading branch information
1 parent
d88fd24
commit 7a3bbba
Showing
8 changed files
with
346 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { Meta, StoryFn, StoryObj } from '@storybook/react'; | ||
import { useState } from 'react'; | ||
import { InformationCircle } from '@interlay/icons'; | ||
|
||
import { Flex, Span } from '..'; | ||
|
||
import { TextArea, TextAreaProps } from '.'; | ||
|
||
export default { | ||
title: 'Forms/TextArea', | ||
component: TextArea, | ||
parameters: { | ||
layout: 'centered' | ||
}, | ||
args: { | ||
label: 'Address' | ||
} | ||
} as Meta<typeof TextArea>; | ||
|
||
export const Default: StoryObj<TextAreaProps> = {}; | ||
|
||
export const Controlled: StoryFn<TextAreaProps> = (args) => { | ||
const [state, setState] = useState<string>(); | ||
|
||
return <TextArea {...args} value={state} onChange={(e) => setState(e.target.value)} />; | ||
}; | ||
|
||
export const DefaultValue: StoryObj<TextAreaProps> = { | ||
args: { | ||
defaultValue: 'Sesame Street' | ||
} | ||
}; | ||
|
||
export const WithDescription: StoryObj<TextAreaProps> = { | ||
args: { | ||
description: 'Please enter your street address' | ||
} | ||
}; | ||
|
||
export const WithErrorMessage: StoryObj<TextAreaProps> = { | ||
args: { | ||
errorMessage: 'Please enter your street address' | ||
} | ||
}; | ||
|
||
export const WithMultipleErrorMessage: StoryObj<TextAreaProps> = { | ||
args: { | ||
errorMessage: ['Please enter your street address', 'Please enter your street address'] | ||
} | ||
}; | ||
|
||
export const SideLabel: StoryObj<TextAreaProps> = { | ||
args: { | ||
labelPosition: 'side' | ||
} | ||
}; | ||
|
||
export const MaxWidth: StoryObj<TextAreaProps> = { | ||
args: { | ||
maxWidth: 'spacing28' | ||
} | ||
}; | ||
|
||
export const MinHeight: StoryObj<TextAreaProps> = { | ||
args: { | ||
minHeight: 'spacing28' | ||
} | ||
}; | ||
|
||
export const Adornments: StoryFn<TextAreaProps> = (args) => ( | ||
<Flex direction='column'> | ||
<TextArea {...args} label='Start Adornment' startAdornment={<InformationCircle />} /> | ||
<TextArea {...args} endAdornment={<InformationCircle />} label='End Adornment' /> | ||
<TextArea | ||
{...args} | ||
bottomAdornment={ | ||
<Span color='tertiary' size='xs'> | ||
$0.00 | ||
</Span> | ||
} | ||
label='Bottom Adornment' | ||
/> | ||
</Flex> | ||
); | ||
|
||
export const Sizes: StoryFn<TextAreaProps> = (args) => ( | ||
<Flex direction='column'> | ||
<TextArea {...args} label='Small' size='small' /> | ||
<TextArea {...args} label='Medium' /> | ||
<TextArea {...args} label='Large' size='large' /> | ||
</Flex> | ||
); | ||
|
||
export const Disabled: StoryObj<TextAreaProps> = { | ||
args: { | ||
isDisabled: true | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useDOMRef } from '@interlay/hooks'; | ||
import { AriaTextFieldOptions, useTextField } from '@react-aria/textfield'; | ||
import { mergeProps } from '@react-aria/utils'; | ||
import { forwardRef } from 'react'; | ||
|
||
import { BaseInput, BaseInputProps } from '../Input'; | ||
|
||
type Props = { | ||
value?: string; | ||
defaultValue?: string; | ||
onValueChange?: (value: string) => void; | ||
onChange?: (e: React.ChangeEvent<HTMLTextAreaElement>) => void; | ||
}; | ||
|
||
type InheritAttrs = Omit< | ||
BaseInputProps, | ||
keyof Props | 'errorMessageProps' | 'descriptionProps' | 'inputProps' | 'elementType' | ||
>; | ||
|
||
type AriaAttrs = Omit<AriaTextFieldOptions<'textarea'>, (keyof Props & InheritAttrs) | 'onChange'>; | ||
|
||
type TextAreaProps = Props & InheritAttrs & AriaAttrs; | ||
|
||
const elementType = 'textarea'; | ||
|
||
const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>( | ||
({ isInvalid, onValueChange, onChange, ...props }, ref): JSX.Element => { | ||
const inputRef = useDOMRef(ref); | ||
// We are specifing `validationState` so that when there are errors, `aria-invalid` is set to `true` | ||
const { inputProps, descriptionProps, errorMessageProps, labelProps } = useTextField( | ||
{ | ||
...props, | ||
isInvalid: isInvalid || !!props.errorMessage, | ||
inputElementType: elementType, | ||
onChange: onValueChange | ||
}, | ||
inputRef | ||
); | ||
|
||
return ( | ||
<BaseInput | ||
ref={inputRef as any} | ||
descriptionProps={descriptionProps} | ||
elementType={elementType} | ||
errorMessageProps={errorMessageProps} | ||
inputProps={mergeProps(inputProps, { onChange })} | ||
labelProps={labelProps} | ||
{...props} | ||
/> | ||
); | ||
} | ||
); | ||
|
||
TextArea.displayName = 'TextArea'; | ||
|
||
export { TextArea }; | ||
export type { TextAreaProps }; |
Oops, something went wrong.