-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add data type to TextInputPropController
- Loading branch information
1 parent
a0ba13c
commit 3b5e36e
Showing
22 changed files
with
376 additions
and
52 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,6 @@ | ||
--- | ||
'@makeswift/prop-controllers': minor | ||
'@makeswift/runtime': minor | ||
--- | ||
|
||
Add data type to legacy `TextInput` prop controller and move it to `@makeswift/prop-controllers`. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './text-input' |
80 changes: 80 additions & 0 deletions
80
packages/prop-controllers/src/text-input/text-input.test.ts
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,80 @@ | ||
import { ControlDataTypeKey, Types } from '../prop-controllers' | ||
import { | ||
TextInputDescriptor, | ||
TextInputPropControllerDataV1, | ||
TextInputPropControllerDataV1Type, | ||
createTextInputPropControllerDataFromString, | ||
getTextInputPropControllerDataString, | ||
} from './text-input' | ||
|
||
describe('TextInputPropController', () => { | ||
describe('getTextInputPropControllerDataString', () => { | ||
test('returns value for TextInputPropControllerDataV1Type', () => { | ||
// Arrange | ||
const text = 'test text' | ||
const data: TextInputPropControllerDataV1 = { | ||
[ControlDataTypeKey]: TextInputPropControllerDataV1Type, | ||
value: text, | ||
} | ||
|
||
// Act | ||
const result = getTextInputPropControllerDataString(data) | ||
|
||
// Assert | ||
expect(result).toBe(text) | ||
}) | ||
|
||
test('returns value for TextInputPropControllerDataV0 data', () => { | ||
// Arrange | ||
const text = 'test text' | ||
|
||
// Act | ||
const result = getTextInputPropControllerDataString(text) | ||
|
||
// Assert | ||
expect(result).toBe(text) | ||
}) | ||
}) | ||
|
||
describe('createTextInputPropControllerDataFromString', () => { | ||
test('returns TextInputPropControllerDataV1 when definition version is 1', () => { | ||
// Arrange | ||
const text = 'test text' | ||
const definition: TextInputDescriptor = { | ||
type: Types.TextInput, | ||
version: 1, | ||
options: {}, | ||
} | ||
|
||
// Act | ||
const result = createTextInputPropControllerDataFromString( | ||
text, | ||
definition, | ||
) | ||
|
||
// Assert | ||
expect(result).toEqual({ | ||
[ControlDataTypeKey]: TextInputPropControllerDataV1Type, | ||
value: text, | ||
}) | ||
}) | ||
|
||
test('returns string value when definition version is not 1', () => { | ||
// Arrange | ||
const text = 'test text' | ||
const definition: TextInputDescriptor = { | ||
type: Types.TextInput, | ||
options: {}, | ||
} | ||
|
||
// Act | ||
const result = createTextInputPropControllerDataFromString( | ||
text, | ||
definition, | ||
) | ||
|
||
// Assert | ||
expect(result).toBe(text) | ||
}) | ||
}) | ||
}) |
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,94 @@ | ||
import { z } from 'zod' | ||
import { ControlDataTypeKey, Options, Types } from '../prop-controllers' | ||
import { match } from 'ts-pattern' | ||
|
||
const textInputPropControllerDataV0Schema = z.string() | ||
|
||
export type TextInputPropControllerDataV0 = z.infer< | ||
typeof textInputPropControllerDataV0Schema | ||
> | ||
|
||
export const TextInputPropControllerDataV1Type = | ||
'prop-controllers::text-input::v1' | ||
|
||
const textInputPropControllerDataV1Schema = z.object({ | ||
[ControlDataTypeKey]: z.literal(TextInputPropControllerDataV1Type), | ||
value: z.string(), | ||
}) | ||
|
||
export type TextInputPropControllerDataV1 = z.infer< | ||
typeof textInputPropControllerDataV1Schema | ||
> | ||
|
||
export const textInputPropControllerDataSchema = z.union([ | ||
textInputPropControllerDataV0Schema, | ||
textInputPropControllerDataV1Schema, | ||
]) | ||
|
||
export type TextInputPropControllerData = z.infer< | ||
typeof textInputPropControllerDataSchema | ||
> | ||
|
||
export type TextInputOptions = Options<{ | ||
label?: string | ||
placeholder?: string | ||
hidden?: boolean | ||
}> | ||
|
||
type TextInputDescriptorV0<_T = TextInputPropControllerDataV0> = { | ||
type: typeof Types.TextInput | ||
options: TextInputOptions | ||
} | ||
|
||
type TextInputDescriptorV1< | ||
_T = TextInputPropControllerData, | ||
U extends TextInputOptions = TextInputOptions, | ||
> = { | ||
type: typeof Types.TextInput | ||
version: 1 | ||
options: U | ||
} | ||
|
||
export type TextInputDescriptor<_T = TextInputPropControllerData> = | ||
| TextInputDescriptorV0 | ||
| TextInputDescriptorV1 | ||
|
||
export type ResolveTextInputPropControllerValue<T extends TextInputDescriptor> = | ||
T extends TextInputDescriptor ? string | undefined : never | ||
|
||
/** | ||
* @deprecated Imports from @makeswift/prop-controllers are deprecated. Use | ||
* @makeswift/runtime/controls instead. | ||
*/ | ||
export function TextInput( | ||
options: TextInputOptions = {}, | ||
): TextInputDescriptorV1 { | ||
return { type: Types.TextInput, version: 1, options } | ||
} | ||
|
||
export function getTextInputPropControllerDataString( | ||
data: TextInputPropControllerData | undefined, | ||
): string | undefined { | ||
return match(data) | ||
.with( | ||
{ [ControlDataTypeKey]: TextInputPropControllerDataV1Type }, | ||
(v1) => v1.value, | ||
) | ||
.otherwise((v0) => v0) | ||
} | ||
|
||
export function createTextInputPropControllerDataFromString( | ||
value: string, | ||
definition: TextInputDescriptor, | ||
): TextInputPropControllerData { | ||
return match(definition) | ||
.with( | ||
{ version: 1 }, | ||
() => | ||
({ | ||
[ControlDataTypeKey]: TextInputPropControllerDataV1Type, | ||
value, | ||
} as const), | ||
) | ||
.otherwise(() => value) | ||
} |
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
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
Oops, something went wrong.