Skip to content

Commit

Permalink
Add example
Browse files Browse the repository at this point in the history
  • Loading branch information
greglittlefield-wf committed Nov 21, 2023
1 parent 191aa2a commit 55bd5f0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions example/builder/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import './src/function_component.dart' as function;
import 'src/functional_consumed_props.dart';
import 'src/new_class_consumed_props.dart';
import 'src/nullability.dart';
import 'src/partial.dart' as partial;

class ExampleState {
final String testValue;
Expand Down Expand Up @@ -85,6 +86,7 @@ main() {
)
), querySelector('#content')
);
partial.partialExample();
}

final componentConstructorsByName = <String, Map Function()>{
Expand Down
48 changes: 48 additions & 0 deletions example/builder/src/partial.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2023 Workiva Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import 'package:over_react/over_react.dart';

part 'partial.over_react.g.dart';

UiFactory<RequiredTestProps> RequiredTest = castUiFactory(_$RequiredTest);

mixin RequiredTestProps on UiProps {
late String testRequiredProp;
}

void partialExample() {
// An empty props map
final partialProps = RequiredTest();

try {
// Throws: "Expected a value of type 'String', but got one of type 'Null'",
// because the prop is non-nullable and required, but not present in the map.
print(partialProps.testRequiredProp);
} catch (e) {
print('Threw: $e');
}

// APIs for safely accessing required props:

// 'default'
print(partialProps.getRequiredProp((p) => p.testRequiredProp, orElse: () => 'default'));
// null
print(partialProps.getRequiredPropOrNull((p) => p.testRequiredProp));

// Bonus added API: UiProps.getPropKey:

// 'NullabilityPropsMixin.testRequiredProp'
print(partialProps.getPropKey((p) => p.testRequiredProp));
}

0 comments on commit 55bd5f0

Please sign in to comment.