-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Input and Label primitives (#4384)
Co-authored-by: Danny Banks <djb@amazon.com> Co-authored-by: Heather Buchel <buchel@amazon.com>
- Loading branch information
1 parent
c35fe91
commit 2407ac2
Showing
47 changed files
with
1,941 additions
and
32 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,15 @@ | ||
--- | ||
"@aws-amplify/ui": patch | ||
"@aws-amplify/ui-react": patch | ||
--- | ||
|
||
feat: Input and Label primitives | ||
|
||
```jx | ||
export const InputLabelExample = () => ( | ||
<> | ||
<Label htmlFor="first_name">First Name:</Label> | ||
<Input id="first_name" name="first_name" /> | ||
</> | ||
); | ||
``` |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
89 changes: 89 additions & 0 deletions
89
docs/src/pages/[platform]/components/input/InputPropControls.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,89 @@ | ||
import { | ||
InputProps, | ||
TextField, | ||
SelectField, | ||
SwitchField, | ||
Flex, | ||
} from '@aws-amplify/ui-react'; | ||
import * as React from 'react'; | ||
|
||
export interface InputPropControlsProps extends InputProps { | ||
setVariation: (value: React.SetStateAction<InputProps['variation']>) => void; | ||
setSize: (value: React.SetStateAction<InputProps['size']>) => void; | ||
setPlaceholder: ( | ||
value: React.SetStateAction<InputProps['placeholder']> | ||
) => void; | ||
setHasError: (value: React.SetStateAction<InputProps['hasError']>) => void; | ||
setIsDisabled: ( | ||
value: React.SetStateAction<InputProps['isDisabled']> | ||
) => void; | ||
} | ||
|
||
interface InputPropControlsInterface { | ||
(props: InputPropControlsProps): JSX.Element; | ||
} | ||
|
||
export const InputPropControls: InputPropControlsInterface = ({ | ||
variation, | ||
setVariation, | ||
size, | ||
setSize, | ||
placeholder, | ||
setPlaceholder, | ||
hasError, | ||
setHasError, | ||
isDisabled, | ||
setIsDisabled, | ||
}) => { | ||
return ( | ||
<Flex direction="column"> | ||
<SelectField | ||
label="variation" | ||
value={variation} | ||
onChange={(event) => | ||
setVariation(event.target.value as InputProps['variation']) | ||
} | ||
> | ||
<option value="">default</option> | ||
<option value="quiet">quiet</option> | ||
</SelectField> | ||
|
||
<SelectField | ||
label="size" | ||
value={size} | ||
defaultValue="default" | ||
onChange={(event) => setSize(event.target.value as InputProps['size'])} | ||
> | ||
<option value="small">small</option> | ||
<option value="default">default</option> | ||
<option value="large">large</option> | ||
</SelectField> | ||
|
||
<TextField | ||
label="placeholder" | ||
value={placeholder as string} | ||
onChange={(event) => | ||
setPlaceholder(event.target.value as InputProps['placeholder']) | ||
} | ||
/> | ||
|
||
<SwitchField | ||
label="hasError" | ||
isChecked={hasError} | ||
labelPosition="end" | ||
onChange={(event) => { | ||
setHasError(event.target.checked as InputProps['hasError']); | ||
}} | ||
/> | ||
|
||
<SwitchField | ||
label="isDisabled" | ||
isChecked={isDisabled} | ||
labelPosition="end" | ||
onChange={(event) => { | ||
setIsDisabled(event.target.checked as InputProps['isDisabled']); | ||
}} | ||
/> | ||
</Flex> | ||
); | ||
}; |
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,45 @@ | ||
import * as React from 'react'; | ||
import { Input, InputProps } from '@aws-amplify/ui-react'; | ||
import { Demo } from '@/components/Demo'; | ||
import { InputPropControls } from './InputPropControls'; | ||
import { useInputProps } from './useInputProps'; | ||
import { demoState } from '@/utils/demoState'; | ||
import { getPropString } from '../utils/getPropString'; | ||
|
||
const propsToCode = (props) => { | ||
return ( | ||
`<Input` + | ||
getPropString(props.variation, 'variation') + | ||
getPropString(props.size, 'size') + | ||
getPropString(props.placeholder, 'placeholder') + | ||
(props.hasError ? '\n hasError' : '') + | ||
(props.isDisabled ? '\n isDisabled' : '') + | ||
`\n/>` | ||
); | ||
}; | ||
|
||
const defaultInputProps: InputProps = { | ||
placeholder: 'Baggins', | ||
}; | ||
|
||
export const InputDemo = () => { | ||
const InputProps = useInputProps( | ||
(demoState.get('Input') as InputProps) || defaultInputProps | ||
); | ||
|
||
return ( | ||
<Demo | ||
code={propsToCode(InputProps)} | ||
propControls={<InputPropControls {...InputProps} />} | ||
> | ||
<Input | ||
aria-label="Demo input" | ||
variation={InputProps.variation} | ||
size={InputProps.size} | ||
placeholder={InputProps.placeholder} | ||
hasError={InputProps.hasError} | ||
isDisabled={InputProps.isDisabled} | ||
/> | ||
</Demo> | ||
); | ||
}; |
8 changes: 8 additions & 0 deletions
8
docs/src/pages/[platform]/components/input/examples/DefaultInputExample.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,8 @@ | ||
import { Input, Label } from '@aws-amplify/ui-react'; | ||
|
||
export const DefaultInputExample = () => ( | ||
<> | ||
<Label htmlFor="first_name">First Name:</Label> | ||
<Input id="first_name" name="first_name" /> | ||
</> | ||
); |
11 changes: 11 additions & 0 deletions
11
docs/src/pages/[platform]/components/input/examples/DefaultRequiredInputExample.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,11 @@ | ||
import { Button, Flex, Input, Label } from '@aws-amplify/ui-react'; | ||
|
||
export const DefaultRequiredInputExample = () => { | ||
return ( | ||
<Flex as="form" direction="column" width="20rem"> | ||
<Label htmlFor="email">Email</Label> | ||
<Input id="email" type="email" isRequired /> | ||
<Button type="submit">Submit</Button> | ||
</Flex> | ||
); | ||
}; |
16 changes: 16 additions & 0 deletions
16
docs/src/pages/[platform]/components/input/examples/InputEventHandlersExample.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,16 @@ | ||
import { Input, Label } from '@aws-amplify/ui-react'; | ||
|
||
export const InputEventHandlersExample = () => ( | ||
<> | ||
<Label htmlFor="events">Event handlers</Label> | ||
<Input | ||
id="events" | ||
onSelect={(e) => console.info('onSelect fired:', e.currentTarget.value)} | ||
onInput={(e) => console.info('onInput fired:', e.currentTarget.value)} | ||
onChange={(e) => console.info('onChange fired:', e.currentTarget.value)} | ||
onCopy={(e) => console.info('onCopy fired:', e.currentTarget.value)} | ||
onPaste={(e) => console.info('onPaste fired:', e.currentTarget.value)} | ||
onCut={(e) => console.info('onCut fired:', e.currentTarget.value)} | ||
/> | ||
</> | ||
); |
14 changes: 14 additions & 0 deletions
14
docs/src/pages/[platform]/components/input/examples/InputSizeExample.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,14 @@ | ||
import { Flex, Input, Label } from '@aws-amplify/ui-react'; | ||
|
||
export const InputSizeExample = () => { | ||
return ( | ||
<Flex direction="column"> | ||
<Label htmlFor="small">Small</Label> | ||
<Input id="small" size="small" width="50%" /> | ||
<Label htmlFor="default">Default</Label> | ||
<Input id="default" width="75%" /> | ||
<Label htmlFor="large">Large</Label> | ||
<Input id="large" size="large" width="100%" /> | ||
</Flex> | ||
); | ||
}; |
12 changes: 12 additions & 0 deletions
12
docs/src/pages/[platform]/components/input/examples/InputStatesExample.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,12 @@ | ||
import { Flex, Input, Label } from '@aws-amplify/ui-react'; | ||
|
||
export const InputStatesExample = () => { | ||
return ( | ||
<Flex direction="column" gap="1rem"> | ||
<Label htmlFor="disabled">Disabled</Label> | ||
<Input id="disabled" defaultValue="Disabled" isDisabled /> | ||
<Label htmlFor="readonly">Readonly</Label> | ||
<Input id="readonly" defaultValue="You can't edit me!" isReadOnly /> | ||
</Flex> | ||
); | ||
}; |
30 changes: 30 additions & 0 deletions
30
docs/src/pages/[platform]/components/input/examples/InputStylePropsExample.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,30 @@ | ||
import { Text, Input, Label, useTheme } from '@aws-amplify/ui-react'; | ||
|
||
export const InputStylePropsExample = () => { | ||
const { tokens } = useTheme(); | ||
return ( | ||
<> | ||
<Label htmlFor="name"> | ||
<Text | ||
fontWeight={tokens.fontWeights.bold} | ||
fontSize={tokens.fontSizes.xl} | ||
> | ||
Name: | ||
</Text> | ||
</Label> | ||
<Input | ||
id="name" | ||
fontWeight={tokens.fontWeights.bold} | ||
fontSize={tokens.fontSizes.xl} | ||
padding="xl" | ||
border={`1px solid ${tokens.colors.brand.primary[60]}`} | ||
/> | ||
<Label htmlFor="special">Special Field</Label> | ||
<Input | ||
id="special" | ||
backgroundColor="brand.primary.10" | ||
border={`1px solid ${tokens.colors.brand.primary[60]}`} | ||
/> | ||
</> | ||
); | ||
}; |
22 changes: 22 additions & 0 deletions
22
docs/src/pages/[platform]/components/input/examples/InputThemeExample.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,22 @@ | ||
import { Input, Label, ThemeProvider, Theme } from '@aws-amplify/ui-react'; | ||
|
||
const theme: Theme = { | ||
name: 'input-theme', | ||
tokens: { | ||
components: { | ||
input: { | ||
color: { value: '{colors.blue.90}' }, | ||
_focus: { | ||
borderColor: { value: '{colors.blue.40}' }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export const InputThemeExample = () => ( | ||
<ThemeProvider theme={theme} colorMode="light"> | ||
<Label htmlFor="name">Name</Label> | ||
<Input id="name" /> | ||
</ThemeProvider> | ||
); |
18 changes: 18 additions & 0 deletions
18
docs/src/pages/[platform]/components/input/examples/InputValidationErrorExample.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,18 @@ | ||
import * as React from 'react'; | ||
import { Input, Label } from '@aws-amplify/ui-react'; | ||
|
||
export const InputValidationErrorExample = () => { | ||
const [hasError, setHasError] = React.useState(true); | ||
|
||
const validateUsername = (e) => { | ||
const containsDigit = /\d/.test(e.currentTarget.value); | ||
setHasError(!containsDigit); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Label htmlFor="username">Username</Label> | ||
<Input id="username" hasError={hasError} onChange={validateUsername} /> | ||
</> | ||
); | ||
}; |
12 changes: 12 additions & 0 deletions
12
docs/src/pages/[platform]/components/input/examples/InputVariationExample.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,12 @@ | ||
import { Input, Label } from '@aws-amplify/ui-react'; | ||
|
||
export const InputVariationExample = () => { | ||
return ( | ||
<> | ||
<Label htmlFor="Default">Default</Label> | ||
<Input id="default" /> | ||
<Label htmlFor="Quiet">Default</Label> | ||
<Input id="quiet" variation="quiet" /> | ||
</> | ||
); | ||
}; |
Oops, something went wrong.