-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add field-label component (#267)
closes #4
Co-Authored-By: Andre Luiz Rabello <andre.luiz_r@hotmail.com>
- Loading branch information
Showing
11 changed files
with
274 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
@use '../../helpers'; | ||
|
||
@mixin FieldLabel() { | ||
.ods-field-label { | ||
@include helpers.font('300-regular'); | ||
|
||
color: helpers.color('content-main'); | ||
display: flex; | ||
flex-flow: column wrap; | ||
padding-top: helpers.space(0.5); | ||
|
||
+ .ods-input, | ||
+ .ods-textarea, | ||
> .ods-input, | ||
> .ods-textarea { | ||
margin-top: helpers.space(0.5); | ||
} | ||
} | ||
} |
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
90 changes: 90 additions & 0 deletions
90
packages/react/src/components/field-label/field-label.stories.tsx
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,90 @@ | ||
import { | ||
FieldLabel, | ||
FieldLabelProps, | ||
HelperText, | ||
Input, | ||
Textarea, | ||
} from '@onfido/castor-react'; | ||
import React from 'react'; | ||
import { Meta, omit, Story } from '../../../../../docs'; | ||
|
||
export default { | ||
title: 'React/FieldLabel', | ||
component: FieldLabel, | ||
argTypes: { | ||
...omit<FieldLabelProps>('className', 'style'), | ||
children: { control: 'text' }, | ||
}, | ||
args: { | ||
children: 'Label', | ||
}, | ||
} as Meta<FieldLabelProps>; | ||
|
||
export const Playground: Story<FieldLabelProps> = (props: FieldLabelProps) => ( | ||
<FieldLabel {...props} /> | ||
); | ||
|
||
interface FieldLabelWithHelperTextProps extends FieldLabelProps { | ||
label: string; | ||
helperText: string; | ||
} | ||
|
||
export const WithHelperText = ({ | ||
label, | ||
helperText, | ||
...restFieldLabelProps | ||
}: FieldLabelWithHelperTextProps) => ( | ||
<FieldLabel {...restFieldLabelProps}> | ||
{label} | ||
<HelperText>{helperText}</HelperText> | ||
</FieldLabel> | ||
); | ||
WithHelperText.argTypes = omit<FieldLabelWithHelperTextProps>('children'); | ||
WithHelperText.args = { | ||
label: 'Label', | ||
helperText: 'Helper text', | ||
}; | ||
|
||
interface FieldLabelWithInputProps extends FieldLabelProps { | ||
id: string; | ||
label: string; | ||
} | ||
|
||
export const WithInput = ({ | ||
id, | ||
label, | ||
...restFieldLabelProps | ||
}: FieldLabelWithInputProps) => ( | ||
<> | ||
<FieldLabel {...restFieldLabelProps} htmlFor={id}> | ||
{label} | ||
</FieldLabel> | ||
<Input id={id} /> | ||
</> | ||
); | ||
WithInput.args = { | ||
id: 'field-label-with-input', | ||
label: 'Label', | ||
}; | ||
|
||
interface FieldLabelWithTextareaProps extends FieldLabelProps { | ||
id: string; | ||
label: string; | ||
} | ||
|
||
export const WithTexarea = ({ | ||
id, | ||
label, | ||
...restFieldLabelProps | ||
}: FieldLabelWithTextareaProps) => ( | ||
<> | ||
<FieldLabel {...restFieldLabelProps} htmlFor={id}> | ||
{label} | ||
</FieldLabel> | ||
<Textarea id={id} /> | ||
</> | ||
); | ||
WithTexarea.args = { | ||
id: 'field-label-with-textarea', | ||
label: '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,18 @@ | ||
import { c, classy } from '@onfido/castor'; | ||
import React from 'react'; | ||
|
||
/** | ||
* Intended to be used within `Input` and `Textarea` components, providing a | ||
* text label for the field. | ||
* | ||
* Can also be used alongside these two components, but should then be connected | ||
* via the "htmlFor" prop. | ||
*/ | ||
export const FieldLabel = ({ | ||
className, | ||
...restProps | ||
}: FieldLabelProps): JSX.Element => ( | ||
<label {...restProps} className={classy(c('field-label'), className)} /> | ||
); | ||
|
||
export type FieldLabelProps = JSX.IntrinsicElements['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
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 |
---|---|---|
@@ -1,21 +1,44 @@ | ||
import { c, classy, InputProps as BaseProps, m } from '@onfido/castor'; | ||
import React from 'react'; | ||
import React, { useState } from 'react'; | ||
import { FieldLabelWrapper } from '../../internal'; | ||
import { withRef } from '../../utils'; | ||
|
||
const idPrefix = 'castor_input'; | ||
let idCount = 0; | ||
|
||
export const Input = withRef( | ||
( | ||
{ type = 'text', invalid, className, ...restProps }: InputProps, | ||
{ | ||
id: externalId, | ||
type = 'text', | ||
invalid, | ||
children, | ||
className, | ||
...restProps | ||
}: InputProps, | ||
ref: InputProps['ref'] | ||
): JSX.Element => ( | ||
<input | ||
{...restProps} | ||
ref={ref} | ||
type={type} | ||
className={classy(c('input'), m({ invalid }), className)} | ||
/> | ||
) | ||
): JSX.Element => { | ||
const [autoId] = useState(() => `${idPrefix}_${++idCount}`); | ||
const id = externalId || (children ? autoId : undefined); | ||
|
||
return ( | ||
<FieldLabelWrapper id={id}> | ||
{{ | ||
children, | ||
element: ( | ||
<input | ||
{...restProps} | ||
ref={ref} | ||
id={id} | ||
type={type} | ||
className={classy(c('input'), m({ invalid }), className)} | ||
/> | ||
), | ||
}} | ||
</FieldLabelWrapper> | ||
); | ||
} | ||
); | ||
Input.displayName = 'Input'; | ||
|
||
export type InputProps = BaseProps & | ||
Omit<JSX.IntrinsicElements['input'], 'children'>; | ||
export type InputProps = BaseProps & JSX.IntrinsicElements['input']; |
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 |
---|---|---|
@@ -1,29 +1,47 @@ | ||
import { c, classy, m, TextareaProps as BaseProps } from '@onfido/castor'; | ||
import React from 'react'; | ||
import React, { useState } from 'react'; | ||
import { FieldLabelWrapper } from '../../internal'; | ||
import { withRef } from '../../utils'; | ||
|
||
const idPrefix = 'castor_textarea'; | ||
let idCount = 0; | ||
|
||
export const Textarea = withRef( | ||
( | ||
{ | ||
id: externalId, | ||
resize = 'vertical', | ||
rows = 3, | ||
invalid, | ||
children, | ||
className, | ||
style, | ||
...restProps | ||
}: TextareaProps, | ||
ref: TextareaProps['ref'] | ||
): JSX.Element => ( | ||
<textarea | ||
{...restProps} | ||
ref={ref} | ||
rows={rows} | ||
className={classy(c('textarea'), m({ invalid }), className)} | ||
style={{ ...style, resize }} | ||
/> | ||
) | ||
): JSX.Element => { | ||
const [autoId] = useState(() => `${idPrefix}_${++idCount}`); | ||
const id = externalId || (children ? autoId : undefined); | ||
|
||
return ( | ||
<FieldLabelWrapper id={id}> | ||
{{ | ||
children, | ||
element: ( | ||
<textarea | ||
{...restProps} | ||
ref={ref} | ||
id={id} | ||
rows={rows} | ||
className={classy(c('textarea'), m({ invalid }), className)} | ||
style={{ ...style, resize }} | ||
/> | ||
), | ||
}} | ||
</FieldLabelWrapper> | ||
); | ||
} | ||
); | ||
Textarea.displayName = 'Textarea'; | ||
|
||
export type TextareaProps = BaseProps & | ||
Omit<JSX.IntrinsicElements['textarea'], 'children'>; | ||
export type TextareaProps = BaseProps & JSX.IntrinsicElements['textarea']; |
31 changes: 31 additions & 0 deletions
31
packages/react/src/internal/field-label-wrapper/field-label-wrapper.tsx
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 { FieldLabel } from '@onfido/castor-react'; | ||
import React from 'react'; | ||
|
||
/** | ||
* Wrapper for `Input` and `Textarea` components, using `FieldLabel` when | ||
* children is provided. | ||
*/ | ||
export const FieldLabelWrapper = ({ | ||
id, | ||
children: { element, children }, | ||
}: FieldLabelWrapperProps): JSX.Element => { | ||
if (!children) return element; | ||
|
||
return ( | ||
<FieldLabel htmlFor={id}> | ||
<span>{children}</span> | ||
{element} | ||
</FieldLabel> | ||
); | ||
}; | ||
|
||
export interface FieldLabelWrapperProps { | ||
id: InputElementProps['id'] | TextareaElementProps['id']; | ||
children: { | ||
children: InputElementProps['children'] | TextareaElementProps['children']; | ||
element: JSX.Element; | ||
}; | ||
} | ||
|
||
type InputElementProps = JSX.IntrinsicElements['input']; | ||
type TextareaElementProps = JSX.IntrinsicElements['textarea']; |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './field-label-wrapper/field-label-wrapper'; | ||
export * from './indicator-container/indicator-container'; |