-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add text input along with label, hint-text, error-text and form…
…-group components. (#212)
- Loading branch information
1 parent
5ac3adb
commit 4ce451f
Showing
16 changed files
with
582 additions
and
8 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
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,44 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { ErrorSize, ErrorText } from './error-text.js'; | ||
|
||
const meta = { | ||
title: 'Form/ErrorText', | ||
parameters: { | ||
docs: { | ||
description: { | ||
component: | ||
'Use hint text alongside a form input for help that’s relevant to the majority of users, like how their information will be used, or where to find it.', | ||
}, | ||
}, | ||
}, | ||
component: ErrorText, | ||
} satisfies Meta<typeof ErrorText>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default: Story = { | ||
argTypes: { | ||
size: { | ||
control: 'radio', | ||
options: Object.values(ErrorSize), | ||
table: { | ||
category: 'Appearance', | ||
type: { summary: 'Size of label' }, | ||
defaultValue: { summary: ErrorSize.md }, | ||
}, | ||
}, | ||
children: { | ||
control: 'text', | ||
table: { | ||
category: 'Content', | ||
type: { summary: 'React.ReactNode' }, | ||
defaultValue: { summary: 'Error' }, | ||
}, | ||
}, | ||
}, | ||
args: { | ||
children: 'Error', | ||
size: ErrorSize.md, | ||
}, | ||
}; |
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,30 @@ | ||
import React from 'react'; | ||
import { Text } from '../text/text.js'; | ||
|
||
export enum ErrorSize { | ||
sm = 'sm', | ||
md = 'md', | ||
lg = 'lg', | ||
} | ||
|
||
// Extend `React.HTMLAttributes<HTMLParagraphElement>` so that | ||
// the component can accept all the standard attributes and events that a `<p>` element can handle. | ||
export type ErrorTextProps = React.HTMLAttributes<HTMLParagraphElement> & { | ||
size?: ErrorSize; | ||
}; | ||
|
||
export const ErrorText: React.FC<ErrorTextProps> = ({ | ||
size = ErrorSize.md, | ||
...props | ||
}) => { | ||
return ( | ||
<Text | ||
as="p" | ||
size={size} | ||
className="gi-font-bold gi-leading-5 gi-text-red-600 gi-clear-both gi-block !gi-mb-[14px] !gi-mt-0" | ||
{...props} | ||
> | ||
{props.children} | ||
</Text> | ||
); | ||
}; |
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,23 @@ | ||
import React from 'react'; | ||
|
||
export type FormGroupProps = React.HTMLAttributes<HTMLDivElement> & { | ||
hasError?: boolean; | ||
children: React.ReactNode; | ||
}; | ||
|
||
export const FormGroup = React.forwardRef<HTMLDivElement, FormGroupProps>( | ||
({ hasError = false, children, ...props }, ref) => { | ||
return ( | ||
<div | ||
ref={ref} | ||
className={`gi-pt-2 gi-mb-4 ${hasError ? 'gi-px-4 gi-border-solid gi-border-l-xl gi-border-red-600' : ''}`} | ||
{...props} | ||
> | ||
{children} | ||
</div> | ||
); | ||
}, | ||
); | ||
|
||
// Set the displayName for debugging purposes | ||
FormGroup.displayName = 'FormGroup'; |
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,44 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { HintSize, HintText } from './hint-text.js'; | ||
|
||
const meta = { | ||
title: 'Form/HintText', | ||
parameters: { | ||
docs: { | ||
description: { | ||
component: | ||
'Use hint text alongside a form input for help that’s relevant to the majority of users, like how their information will be used, or where to find it.', | ||
}, | ||
}, | ||
}, | ||
component: HintText, | ||
} satisfies Meta<typeof HintText>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default: Story = { | ||
argTypes: { | ||
size: { | ||
control: 'radio', | ||
options: Object.values(HintSize), | ||
table: { | ||
category: 'Appearance', | ||
type: { summary: 'Size of label' }, | ||
defaultValue: { summary: HintSize.md }, | ||
}, | ||
}, | ||
children: { | ||
control: 'text', | ||
table: { | ||
category: 'Content', | ||
type: { summary: 'React.ReactNode' }, | ||
defaultValue: { summary: 'Hint' }, | ||
}, | ||
}, | ||
}, | ||
args: { | ||
children: 'Hint', | ||
size: HintSize.md, | ||
}, | ||
}; |
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,30 @@ | ||
import React from 'react'; | ||
import { Text } from '../text/text.js'; | ||
|
||
export enum HintSize { | ||
sm = 'sm', | ||
md = 'md', | ||
lg = 'lg', | ||
} | ||
|
||
// Extend `React.InputHTMLAttributes<HTMLInputElement>` so that | ||
// the component can accept all the standard attributes and events that an `<input>` element can handle. | ||
export type HintTextProps = React.HTMLAttributes<HTMLInputElement> & { | ||
size?: HintSize; | ||
}; | ||
|
||
// Use React.forwardRef to support refs properly | ||
export const HintText: React.FC<HintTextProps> = ({ size, ...props }, ref) => { | ||
return ( | ||
<Text | ||
size={size} | ||
className="gi-font-normal gi-leading-5 gi-text-gray-600 !gi-mb-[10px]" | ||
{...props} | ||
> | ||
{props.children} | ||
</Text> | ||
); | ||
}; | ||
|
||
// Set the displayName for debugging purposes | ||
HintText.displayName = 'HintText'; |
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,60 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { Label, LabelSize } from './label.js'; | ||
|
||
const meta = { | ||
title: 'Typography/Label', | ||
parameters: { | ||
docs: { | ||
description: { | ||
component: 'Label element to wrap label-text and a form input.', | ||
}, | ||
}, | ||
}, | ||
component: Label, | ||
} satisfies Meta<typeof Label>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default: Story = { | ||
argTypes: { | ||
ref: { | ||
control: false, | ||
table: { | ||
category: 'Ref', | ||
type: { summary: 'React.Ref<HTMLLabelElement>' }, | ||
defaultValue: { summary: '-' }, | ||
}, | ||
}, | ||
size: { | ||
control: 'radio', | ||
options: Object.values(LabelSize), | ||
table: { | ||
category: 'Appearance', | ||
type: { summary: 'Size of label' }, | ||
defaultValue: { summary: LabelSize.md }, | ||
}, | ||
}, | ||
htmlFor: { | ||
control: 'text', | ||
table: { | ||
category: 'Accessibility', | ||
type: { summary: 'string' }, | ||
defaultValue: { summary: '-' }, | ||
}, | ||
}, | ||
children: { | ||
control: 'text', | ||
table: { | ||
category: 'Content', | ||
type: { summary: 'React.ReactNode' }, | ||
defaultValue: { summary: 'Label' }, | ||
}, | ||
}, | ||
}, | ||
args: { | ||
htmlFor: 'input-id', | ||
size: LabelSize.md, | ||
children: 'Label', | ||
}, | ||
}; |
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,31 @@ | ||
import React from 'react'; | ||
|
||
export enum LabelSize { | ||
sm = 'sm', | ||
md = 'md', | ||
lg = 'lg', | ||
} | ||
|
||
// Extend `React.LabelHTMLAttributes<HTMLLabelElement>` for correct label attributes | ||
export type LabelProps = React.LabelHTMLAttributes<HTMLLabelElement> & { | ||
size?: LabelSize; | ||
}; | ||
|
||
// Use React.forwardRef to support refs properly | ||
export const Label = React.forwardRef<HTMLLabelElement, LabelProps>( | ||
({ size = LabelSize.md, htmlFor, ...props }, ref) => { | ||
return ( | ||
<label | ||
className={`gi-text-${size} gi-leading-5 gi-text-gray-950 gi-mb-2 gi-block`} | ||
ref={ref} | ||
htmlFor={htmlFor} | ||
{...props} | ||
> | ||
{props.children} | ||
</label> | ||
); | ||
}, | ||
); | ||
|
||
// Set the displayName for debugging purposes | ||
Label.displayName = 'Label'; |
Oops, something went wrong.