mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Add support for WithDefault<string, null>
Summary: This diff adds support for: ``` propName: WithDefault<string, null>, ``` It will throw if null is used for any other type like boolean Reviewed By: TheSavior, cpojer Differential Revision: D15748556 fbshipit-source-id: 925457ca1739bfad08e4776ecb47c0beb3acacf5
This commit is contained in:
committed by
Facebook Github Bot
parent
a3b9840885
commit
0f7bf518ce
+1
-1
@@ -45,7 +45,7 @@ type PropTypeTypeAnnotation =
|
||||
|}>
|
||||
| $ReadOnly<{|
|
||||
type: 'StringTypeAnnotation',
|
||||
default: string,
|
||||
default: string | null,
|
||||
|}>
|
||||
| $ReadOnly<{|
|
||||
type: 'FloatTypeAnnotation',
|
||||
|
||||
@@ -140,6 +140,9 @@ function convertDefaultTypeToString(componentName: string, prop): string {
|
||||
case 'BooleanTypeAnnotation':
|
||||
return String(typeAnnotation.default);
|
||||
case 'StringTypeAnnotation':
|
||||
if (typeAnnotation.default == null) {
|
||||
return '';
|
||||
}
|
||||
return `"${typeAnnotation.default}"`;
|
||||
case 'Int32TypeAnnotation':
|
||||
return String(typeAnnotation.default);
|
||||
|
||||
@@ -14,9 +14,11 @@ import type {SchemaType} from '../CodegenSchema';
|
||||
const {getImports, toSafeCppString} = require('./CppHelpers');
|
||||
|
||||
type FilesOutput = Map<string, string>;
|
||||
type PropValueType = string | number | boolean;
|
||||
|
||||
type TestCase = $ReadOnly<{|
|
||||
propName: string,
|
||||
propValue: string | number | boolean,
|
||||
propValue: ?PropValueType,
|
||||
testName?: string,
|
||||
raw?: boolean,
|
||||
|}>;
|
||||
@@ -59,7 +61,10 @@ function getTestCasesForProp(propName, typeAnnotation) {
|
||||
} else if (typeAnnotation.type === 'StringTypeAnnotation') {
|
||||
cases.push({
|
||||
propName,
|
||||
propValue: typeAnnotation.default || 'foo',
|
||||
propValue:
|
||||
typeAnnotation.default != null && typeAnnotation.default !== ''
|
||||
? typeAnnotation.default
|
||||
: 'foo',
|
||||
});
|
||||
} else if (typeAnnotation.type === 'BooleanTypeAnnotation') {
|
||||
cases.push({
|
||||
|
||||
@@ -110,6 +110,14 @@ const STRING_PROP: SchemaType = {
|
||||
default: '',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'accessibilityRole',
|
||||
optional: true,
|
||||
typeAnnotation: {
|
||||
type: 'StringTypeAnnotation',
|
||||
default: null,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
|
||||
+2
-1
@@ -387,7 +387,8 @@ StringPropComponentProps::StringPropComponentProps(
|
||||
const StringPropComponentProps &sourceProps,
|
||||
const RawProps &rawProps): ViewProps(sourceProps, rawProps),
|
||||
|
||||
accessibilityHint(convertRawProp(rawProps, \\"accessibilityHint\\", sourceProps.accessibilityHint, accessibilityHint))
|
||||
accessibilityHint(convertRawProp(rawProps, \\"accessibilityHint\\", sourceProps.accessibilityHint, accessibilityHint)),
|
||||
accessibilityRole(convertRawProp(rawProps, \\"accessibilityRole\\", sourceProps.accessibilityRole, accessibilityRole))
|
||||
{}
|
||||
|
||||
} // namespace react
|
||||
|
||||
+1
@@ -452,6 +452,7 @@ class StringPropComponentProps final : public ViewProps {
|
||||
#pragma mark - Props
|
||||
|
||||
const std::string accessibilityHint{\\"\\"};
|
||||
const std::string accessibilityRole{};
|
||||
};
|
||||
|
||||
} // namespace react
|
||||
|
||||
+7
@@ -457,6 +457,13 @@ TEST(StringPropComponentProps_accessibilityHint, etc) {
|
||||
auto const &sourceProps = StringPropComponentProps();
|
||||
auto const &rawProps = RawProps(folly::dynamic::object(\\"accessibilityHint\\", \\"foo\\"));
|
||||
|
||||
StringPropComponentProps(sourceProps, rawProps);
|
||||
}
|
||||
|
||||
TEST(StringPropComponentProps_accessibilityRole, etc) {
|
||||
auto const &sourceProps = StringPropComponentProps();
|
||||
auto const &rawProps = RawProps(folly::dynamic::object(\\"accessibilityRole\\", \\"foo\\"));
|
||||
|
||||
StringPropComponentProps(sourceProps, rawProps);
|
||||
}",
|
||||
}
|
||||
|
||||
+1
@@ -478,6 +478,7 @@ const StringPropComponentViewConfig = {
|
||||
|
||||
validAttributes: {
|
||||
accessibilityHint: true,
|
||||
accessibilityRole: true,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -137,6 +137,12 @@ type ModuleProps = $ReadOnly<{|
|
||||
string_optional_value: ?WithDefault<string, ''>,
|
||||
string_optional_both?: ?WithDefault<string, ''>,
|
||||
|
||||
// String props, null default
|
||||
string_null_required: WithDefault<string, null>,
|
||||
string_null_optional_key?: WithDefault<string, null>,
|
||||
string_null_optional_value: ?WithDefault<string, null>,
|
||||
string_null_optional_both?: ?WithDefault<string, null>,
|
||||
|
||||
// Float props
|
||||
float_required: WithDefault<Float, 1.1>,
|
||||
float_optional_key?: WithDefault<Float, 1.1>,
|
||||
|
||||
+32
@@ -78,6 +78,38 @@ Object {
|
||||
"type": "StringTypeAnnotation",
|
||||
},
|
||||
},
|
||||
Object {
|
||||
"name": "string_null_required",
|
||||
"optional": false,
|
||||
"typeAnnotation": Object {
|
||||
"default": null,
|
||||
"type": "StringTypeAnnotation",
|
||||
},
|
||||
},
|
||||
Object {
|
||||
"name": "string_null_optional_key",
|
||||
"optional": true,
|
||||
"typeAnnotation": Object {
|
||||
"default": null,
|
||||
"type": "StringTypeAnnotation",
|
||||
},
|
||||
},
|
||||
Object {
|
||||
"name": "string_null_optional_value",
|
||||
"optional": true,
|
||||
"typeAnnotation": Object {
|
||||
"default": null,
|
||||
"type": "StringTypeAnnotation",
|
||||
},
|
||||
},
|
||||
Object {
|
||||
"name": "string_null_optional_both",
|
||||
"optional": true,
|
||||
"typeAnnotation": Object {
|
||||
"default": null,
|
||||
"type": "StringTypeAnnotation",
|
||||
},
|
||||
},
|
||||
Object {
|
||||
"name": "float_required",
|
||||
"optional": false,
|
||||
|
||||
+18
-6
@@ -119,7 +119,7 @@ function getTypeAnnotation(name, typeAnnotation, defaultValue) {
|
||||
name: 'PointPrimitive',
|
||||
};
|
||||
case 'Int32':
|
||||
if (defaultValue !== null) {
|
||||
if (defaultValue != null) {
|
||||
return {
|
||||
type: 'Int32TypeAnnotation',
|
||||
default: (defaultValue: number),
|
||||
@@ -127,7 +127,7 @@ function getTypeAnnotation(name, typeAnnotation, defaultValue) {
|
||||
}
|
||||
throw new Error(`A default int is required for "${name}"`);
|
||||
case 'Float':
|
||||
if (defaultValue !== null) {
|
||||
if (defaultValue != null) {
|
||||
return {
|
||||
type: 'FloatTypeAnnotation',
|
||||
default: (defaultValue: number),
|
||||
@@ -135,7 +135,7 @@ function getTypeAnnotation(name, typeAnnotation, defaultValue) {
|
||||
}
|
||||
throw new Error(`A default float is required for "${name}"`);
|
||||
case 'BooleanTypeAnnotation':
|
||||
if (defaultValue !== null) {
|
||||
if (defaultValue != null) {
|
||||
return {
|
||||
type: 'BooleanTypeAnnotation',
|
||||
default: (defaultValue: boolean),
|
||||
@@ -143,13 +143,13 @@ function getTypeAnnotation(name, typeAnnotation, defaultValue) {
|
||||
}
|
||||
throw new Error(`A default boolean is required for "${name}"`);
|
||||
case 'StringTypeAnnotation':
|
||||
if (defaultValue !== null) {
|
||||
if (typeof defaultValue !== 'undefined') {
|
||||
return {
|
||||
type: 'StringTypeAnnotation',
|
||||
default: (defaultValue: string),
|
||||
default: (defaultValue: string | null),
|
||||
};
|
||||
}
|
||||
throw new Error(`A default string is required for "${name}"`);
|
||||
throw new Error(`A default string (or null) is required for "${name}"`);
|
||||
case 'UnionTypeAnnotation':
|
||||
if (defaultValue !== null) {
|
||||
return {
|
||||
@@ -199,6 +199,18 @@ function buildPropSchema(property): ?PropTypeShape {
|
||||
}
|
||||
type = typeAnnotation.typeParameters.params[0].type;
|
||||
defaultValue = typeAnnotation.typeParameters.params[1].value;
|
||||
const defaultValueType = typeAnnotation.typeParameters.params[1].type;
|
||||
|
||||
if (defaultValueType === 'NullLiteralTypeAnnotation') {
|
||||
if (type !== 'StringTypeAnnotation') {
|
||||
throw new Error(
|
||||
`WithDefault can only provide a 'null' default value for string types (see ${name})`,
|
||||
);
|
||||
}
|
||||
|
||||
defaultValue = null;
|
||||
}
|
||||
|
||||
typeAnnotation = typeAnnotation.typeParameters.params[0];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user