-
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.
- Loading branch information
1 parent
32ce5dd
commit 024f8e0
Showing
16 changed files
with
415 additions
and
183 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 |
---|---|---|
@@ -1,6 +1,45 @@ | ||
--- | ||
title: Controlled Mode | ||
description: Controlled vs Uncontrolled mode in Hopper components | ||
order: 4 | ||
status: WIP | ||
--- | ||
When working with Hopper components, you can customize a component's behavior using controlled or uncontrolled properties, depending on your needs. This flexibility is particularly useful for implementing interactive features or modifying the default behavior of components while maintaining their visual style and structure. | ||
|
||
**Tip**: To dive deeper into the concept of controlled and uncontrolled components in React, read [React's guide here](https://react.dev/learn/sharing-state-between-components#controlled-and-uncontrolled-components). | ||
|
||
## Uncontrolled Mode | ||
|
||
**Uncontrolled mode** is suitable for scenarios where you don’t need to respond programmatically to user interactions. | ||
|
||
In uncontrolled mode, the component manages its internal state. You provide an initial value using _defaultX_ properties, and the component updates its state automatically in response to user interactions. | ||
|
||
For example, to create a TagGroup where some tags are initially selected, use the defaultSelectedKeys prop: | ||
|
||
<CodeOnlyExample src="HopperProvider/docs/controlled-mode/uncontrolled" /> | ||
|
||
In this example: | ||
- `defaultSelectedKeys`: Specifies the initially selected items. | ||
- The component handles the selection state internally. | ||
|
||
## Controlled Mode | ||
|
||
**Controlled mode** is suitable for scenarios where the component's state depends on external data or when you need to respond programmatically to user interactions | ||
|
||
In controlled mode, you manage the state of the component externally by providing the _X_ and _onXChanged_ properties. This allows for full control over the component's behavior and is ideal for complex interactions or when the component's state is derived from external logic. | ||
|
||
For example, to fully manage the selected tags: | ||
|
||
<CodeOnlyExample src="HopperProvider/docs/controlled-mode/controlled" /> | ||
|
||
In this example: | ||
- `selectedKeys`: Represents the current selection, controlled externally. | ||
- `onSelectionChange`: Callback invoked when the selection changes, allowing you to update the external state. | ||
|
||
## Choosing Between Controlled and Uncontrolled Modes | ||
- Use uncontrolled mode (defaultX) for simpler use cases where internal state management by the component suffices. | ||
- Use controlled mode (X and onXChanged) when external logic or advanced control is required. | ||
|
||
By leveraging these modes, you can tailor Hopper components to meet your application's functional requirements while maintaining consistency and reusability. | ||
|
||
|
||
|
||
|
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
24 changes: 24 additions & 0 deletions
24
packages/components/src/Form/docs/forms-concept/accessValue.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,24 @@ | ||
import { Button, ButtonGroup, Form, TextField } from "@hopper-ui/components"; | ||
import { useState, type FormEvent } from "react"; | ||
|
||
export default function Example() { | ||
let [name, setName] = useState(""); | ||
|
||
let onSubmit = (e: FormEvent) => { | ||
e.preventDefault(); | ||
|
||
// Submit data to your backend API... | ||
alert(name); | ||
}; | ||
|
||
return ( | ||
<Form onSubmit={onSubmit}> | ||
<TextField label="Name" value={name} onChange={setName} /> | ||
<div>You entered: {name}</div> | ||
<ButtonGroup> | ||
<Button type="submit" variant="primary">Submit</Button> | ||
<Button type="reset" variant="secondary">Reset</Button> | ||
</ButtonGroup> | ||
</Form> | ||
); | ||
} |
32 changes: 32 additions & 0 deletions
32
packages/components/src/Form/docs/forms-concept/customValidation.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,32 @@ | ||
import { Button, ButtonGroup, Form, TextField } from "@hopper-ui/components"; | ||
import { useState, type FormEvent } from "react"; | ||
|
||
export default function Example() { | ||
let [submitted, setSubmitted] = useState<Record<string, FormDataEntryValue> | null>(null); | ||
|
||
let onSubmit = (e: FormEvent<HTMLFormElement>) => { | ||
// Prevent default browser page refresh. | ||
e.preventDefault(); | ||
|
||
// Get form data as an object. | ||
let data = Object.fromEntries(new FormData(e.currentTarget)); | ||
|
||
// Submit to your backend API... | ||
setSubmitted(data); | ||
}; | ||
|
||
return ( | ||
<Form onSubmit={onSubmit}> | ||
<TextField name="name" label="Name" /> | ||
<ButtonGroup> | ||
<Button type="submit" variant="primary">Submit</Button> | ||
<Button type="reset" variant="secondary">Reset</Button> | ||
</ButtonGroup> | ||
{submitted && ( | ||
<div> | ||
You submitted: <code>{JSON.stringify(submitted)}</code> | ||
</div> | ||
)} | ||
</Form> | ||
); | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/components/src/Form/docs/forms-concept/errorMessage.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,20 @@ | ||
import { Button, ButtonGroup, Form, TextField } from "@hopper-ui/components"; | ||
|
||
export default function Example() { | ||
return ( | ||
<Form validationBehavior="native"> | ||
<TextField | ||
label="Name" | ||
name="name" | ||
isRequired | ||
errorMessage={({ validationDetails }) => ( | ||
validationDetails.valueMissing ? "Please enter a name." : "" | ||
)} | ||
/> | ||
<ButtonGroup> | ||
<Button type="submit" variant="primary">Submit</Button> | ||
<Button type="reset" variant="secondary">Reset</Button> | ||
</ButtonGroup> | ||
</Form> | ||
); | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/components/src/Form/docs/forms-concept/formData.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 { Button, ButtonGroup, Form, TextField } from "@hopper-ui/components"; | ||
|
||
export default function Example() { | ||
return ( | ||
<Form validationBehavior="native"> | ||
<TextField | ||
label="Username" | ||
validate={value => value === 'admin' ? 'Nice try!' : null} | ||
/> | ||
<ButtonGroup> | ||
<Button type="submit" variant="primary">Submit</Button> | ||
<Button type="reset" variant="secondary">Reset</Button> | ||
</ButtonGroup> | ||
</Form> | ||
); | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/components/src/Form/docs/forms-concept/labels.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,10 @@ | ||
import { PasswordField } from "@hopper-ui/components"; | ||
|
||
export default function Example() { | ||
return ( | ||
<PasswordField | ||
label="Password" | ||
description="Password must be at least 8 characters." | ||
/> | ||
); | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/components/src/Form/docs/forms-concept/nativeValidation.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,13 @@ | ||
import { Button, ButtonGroup, Form, TextField } from "@hopper-ui/components"; | ||
|
||
export default function Example() { | ||
return ( | ||
<Form validationBehavior="native"> | ||
<TextField label="Email" name="email" type="email" isRequired /> | ||
<ButtonGroup> | ||
<Button type="submit" variant="primary">Submit</Button> | ||
<Button type="reset" variant="secondary">Reset</Button> | ||
</ButtonGroup> | ||
</Form> | ||
); | ||
} |
Oops, something went wrong.