Skip to content

Commit

Permalink
Remove form support from FSwitch (#185)
Browse files Browse the repository at this point in the history
* Complete implementation

* Commit from GitHub Actions (Forui Samples Presubmit)

* Fix issues with checkbox docs
  • Loading branch information
kawaijoe authored Sep 11, 2024
1 parent ee277f1 commit 3bd3644
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 121 deletions.
19 changes: 7 additions & 12 deletions docs/pages/docs/checkbox.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ For touch devices, a [switch](/docs/switch) is generally recommended over a chec
<Widget name='checkbox' query={{enabled: 'true'}}/>
</Tabs.Tab>
<Tabs.Tab>
```dart {12-20}
```dart {12-18}
class Page extends StatefulWidget {
const Page();
const Page({super.key});
@override
State<Page> createState() => PageState();
Expand Down Expand Up @@ -67,9 +67,9 @@ FCheckbox(
<Widget name='checkbox' query={{initialValue: 'true', enabled: 'false'}}/>
</Tabs.Tab>
<Tabs.Tab>
```dart {20}
```dart {18}
class Page extends StatefulWidget {
const Page();
const Page({super.key});
@override
State<Page> createState() => PageState();
Expand Down Expand Up @@ -101,7 +101,7 @@ FCheckbox(
<Tabs.Tab>
```dart {15}
class Page extends StatefulWidget {
const Page();
const Page({super.key});
@override
State<Page> createState() => PageState();
Expand Down Expand Up @@ -133,7 +133,7 @@ FCheckbox(
<Tabs.Tab>
```dart {12-17}
class Page extends StatefulWidget {
const Page();
const Page({super.key});
@override
State<Page> createState() => PageState();
Expand Down Expand Up @@ -161,7 +161,7 @@ FCheckbox(
<Widget name='checkbox' variant='form' query={{}} height={550}/>
</Tabs.Tab>
<Tabs.Tab>
```dart {32-47}
```dart {27-40}
class RegisterForm extends StatefulWidget {
const RegisterForm({super.key});
Expand All @@ -172,11 +172,6 @@ FCheckbox(
class _RegisterFormState extends State<RegisterForm> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) => Form(
key: _formKey,
Expand Down
99 changes: 75 additions & 24 deletions docs/pages/docs/switch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,25 @@ A switch that allows the user to toggle between checked and unchecked. It can al
<Widget name='switch' query={{enabled: 'true'}}/>
</Tabs.Tab>
<Tabs.Tab>
```dart
FSwitch(
label: const Text('Airplane Mode'),
semanticLabel: 'Airplane Mode',
onChanged: (value) {},
);
```dart {12-17}
class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => PageState();
}
class PageState extends State<Page> {
bool state = false;
@override
Widget build(BuildContext context) => FSwitch(
label: const Text('Airplane Mode'),
semanticLabel: 'Airplane Mode',
value: state,
onChange: (value) => setState(() => state = value),
);
}
```
</Tabs.Tab>
</Tabs>
Expand All @@ -32,11 +45,13 @@ A switch that allows the user to toggle between checked and unchecked. It can al
```dart
FSwitch(
label: const Text('Airplane Mode'),
description: const Text('Turn on airplane mode to disable all wireless connections.'),
error: const Text('Please turn on airplane mode.'),
semanticLabel: 'Airplane Mode',
value: true,
onChanged: (value) {},
enabled: true,
initialValue: true,
autofocus: true,
onChanged: (value) {},
);
```

Expand All @@ -49,13 +64,26 @@ FSwitch(
<Widget name='switch' query={{enabled: 'false'}}/>
</Tabs.Tab>
<Tabs.Tab>
```dart {4}
FSwitch(
label: const Text('Airplane Mode'),
semanticLabel: 'Airplane Mode',
enabled: false,
onChanged: (value) {},
);
```dart {17}
class Page extends StatefulWidget {
const Page({super.key});
@override
State<Page> createState() => PageState();
}
class PageState extends State<Page> {
bool state = false;
@override
Widget build(BuildContext context) => FSwitch(
label: const Text('Airplane Mode'),
semanticLabel: 'Airplane Mode',
value: state,
onChange: (value) => setState(() => state = value),
enabled: false,
);
}
```
</Tabs.Tab>
</Tabs>
Expand All @@ -67,7 +95,7 @@ FSwitch(
<Widget name='switch' variant='form' query={{}} height={500}/>
</Tabs.Tab>
<Tabs.Tab>
```dart {59, 90}
```dart {54-64, 95-105}
class NotificationForm extends StatefulWidget {
const NotificationForm({super.key});
Expand All @@ -78,11 +106,6 @@ FSwitch(
class _NotificationFormState extends State<NotificationForm> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
final theme = context.theme;
Expand Down Expand Up @@ -126,7 +149,17 @@ FSwitch(
],
),
),
const FSwitch(),
FormField(
initialValue: false,
onSaved: (value) {
// Save values somewhere.
},
validator: (value) => null, // No validation required.
builder: (state) => FSwitch(
value: state.value ?? false,
onChange: (value) => state.didChange(value),
),
),
],
),
),
Expand Down Expand Up @@ -157,15 +190,33 @@ FSwitch(
],
),
),
const FSwitch(initialValue: true),
FormField(
initialValue: true,
onSaved: (value) {
// Save values somewhere.
},
validator: (value) => null, // No validation required.
builder: (state) => FSwitch(
value: state.value ?? false,
onChange: (value) => state.didChange(value),
),
),
],
),
),
),
const SizedBox(height: 30),
FButton(
label: const Text('Submit'),
onPress: () => _formKey.currentState!.validate(),
onPress: () {
if (!_formKey.currentState!.validate()) {
// Handle errors here.
return;
}
_formKey.currentState!.save();
// Do something.
},
),
],
),
Expand Down
9 changes: 9 additions & 0 deletions forui/example/lib/sandbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Sandbox extends StatefulWidget {
}

class _SandboxState extends State<Sandbox> {
bool value = false;

@override
void initState() {
super.initState();
Expand All @@ -30,6 +32,13 @@ class _SandboxState extends State<Sandbox> {
],
),
const SizedBox(height: 20),
FSwitch(
label: const Text('Switch'),
description: const Text('Switch Description'),
value: value,
onChange: (value) => setState(() => this.value = value),
),
const SizedBox(height: 20),
FTooltip(
longPressExitDuration: const Duration(seconds: 5000),
tipBuilder: (context, style, _) => const Text('Add to library'),
Expand Down
30 changes: 15 additions & 15 deletions forui/lib/src/widgets/checkbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ class FCheckbox extends StatelessWidget {
/// The description displayed below the [label].
final Widget? description;

/// The semantic label used by accessibility frameworks.
final String? semanticLabel;

/// The error displayed below the [description].
///
/// If the value is present, the checkbox is in an error state.
final Widget? error;

/// The semantic label used by accessibility frameworks.
final String? semanticLabel;

/// The current value of the checkbox.
final bool value;

Expand All @@ -58,8 +58,8 @@ class FCheckbox extends StatelessWidget {
this.style,
this.label,
this.description,
this.semanticLabel,
this.error,
this.semanticLabel,
this.value = false,
this.onChange,
this.enabled = true,
Expand All @@ -78,18 +78,18 @@ class FCheckbox extends StatelessWidget {
(_, true) => (FLabelState.error, style.errorStyle),
};

return FocusableActionDetector(
enabled: enabled,
autofocus: autofocus,
focusNode: focusNode,
onFocusChange: onFocusChange,
mouseCursor: enabled ? SystemMouseCursors.click : MouseCursor.defer,
child: Semantics(
label: semanticLabel,
return GestureDetector(
onTap: enabled ? () => onChange?.call(!value) : null,
child: FocusableActionDetector(
enabled: enabled,
checked: value,
child: GestureDetector(
onTap: enabled ? () => onChange?.call(!value) : null,
autofocus: autofocus,
focusNode: focusNode,
onFocusChange: onFocusChange,
mouseCursor: enabled ? SystemMouseCursors.click : MouseCursor.defer,
child: Semantics(
label: semanticLabel,
enabled: enabled,
checked: value,
child: FLabel(
axis: Axis.horizontal,
state: labelState,
Expand Down
Loading

0 comments on commit 3bd3644

Please sign in to comment.