-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7175e44
commit ec240ff
Showing
10 changed files
with
253 additions
and
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { FormField } from '.'; | ||
import { TextInput } from '../TextInput'; | ||
import { useState } from 'react'; | ||
import { SegmentedControl } from '../SegmentedControl'; | ||
|
||
const meta: Meta<typeof FormField> = { | ||
component: FormField, | ||
tags: ['autodocs', '!dev'], | ||
argTypes: { | ||
children: { control: false }, | ||
}, | ||
}; | ||
export default meta; | ||
|
||
type Story = StoryObj<typeof FormField>; | ||
|
||
export const TextInputExample: Story = { | ||
args: { | ||
label: "Recipient's address", | ||
asLabel: true, | ||
helperText: 'The recipient can find their address via the Receive tab.', | ||
disabled: false, | ||
}, | ||
|
||
render: function Render(props) { | ||
const [recipient, setRecipient] = useState(''); | ||
|
||
return ( | ||
<FormField {...props}> | ||
<TextInput value={recipient} onChange={setRecipient} placeholder='penumbra1abc123...' /> | ||
</FormField> | ||
); | ||
}, | ||
}; | ||
|
||
export const SegmentedControlExample: Story = { | ||
args: { | ||
label: 'Fee Tier', | ||
disabled: false, | ||
}, | ||
|
||
render: function Render(props) { | ||
const [feeTier, setFeeTier] = useState('low'); | ||
|
||
return ( | ||
<FormField {...props}> | ||
<SegmentedControl | ||
value={feeTier} | ||
options={[ | ||
{ label: 'Low', value: 'low' }, | ||
{ label: 'Medium', value: 'medium' }, | ||
{ label: 'High', value: 'high' }, | ||
]} | ||
onChange={setFeeTier} | ||
/> | ||
</FormField> | ||
); | ||
}, | ||
}; |
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,53 @@ | ||
import styled from 'styled-components'; | ||
import { small, strong } from '../utils/typography'; | ||
import { ReactNode } from 'react'; | ||
import { DisabledContext } from '../utils/DisabledContext'; | ||
|
||
const Root = styled.label` | ||
display: flex; | ||
flex-direction: column; | ||
gap: ${props => props.theme.spacing(2)}; | ||
position: relative; | ||
`; | ||
|
||
const HelperText = styled.div<{ $disabled: boolean }>` | ||
color: ${props => | ||
props.$disabled ? props.theme.color.text.muted : props.theme.color.text.secondary}; | ||
${small} | ||
`; | ||
|
||
const LabelText = styled.div<{ $disabled: boolean }>` | ||
${strong} | ||
color: ${props => | ||
props.$disabled ? props.theme.color.text.muted : props.theme.color.text.primary}; | ||
`; | ||
|
||
export interface FormFieldProps { | ||
label: string; | ||
asLabel?: boolean; | ||
disabled?: boolean; | ||
helperText?: string; | ||
/** | ||
* The form control to render for this field, whether a `<TextInput />`, | ||
* `<SegmentedControl />`, etc. | ||
*/ | ||
children: ReactNode; | ||
} | ||
|
||
export const FormField = ({ | ||
label, | ||
asLabel = false, | ||
disabled = false, | ||
helperText, | ||
children, | ||
}: FormFieldProps) => ( | ||
<Root as={asLabel ? 'label' : 'div'}> | ||
<LabelText $disabled={disabled}>{label}</LabelText> | ||
|
||
<DisabledContext.Provider value={disabled}>{children}</DisabledContext.Provider> | ||
|
||
{helperText && <HelperText $disabled={disabled}>{helperText}</HelperText>} | ||
</Root> | ||
); |
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
Oops, something went wrong.