-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement types for signature component #36
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import {InputComponentSchema} from '..'; | ||
|
||
type Validator = 'required'; | ||
type TranslatableKeys = 'label' | 'description' | 'tooltip' | 'footer'; | ||
|
||
/** | ||
* The value is base64 encoded binary (image) data, or unset and then it's a string. | ||
* | ||
* When a non-empty value is set, enforce that it is serialized as a data URI. | ||
*/ | ||
export type SignatureValue = `data:image/png;base64,${string}`; | ||
|
||
export type SignatureInputSchema = InputComponentSchema< | ||
SignatureValue | '', | ||
Validator, | ||
TranslatableKeys | ||
>; | ||
|
||
/** | ||
* The built-in Formio.js signature component type. | ||
* | ||
* Source code this is based on: | ||
* https://github.com/formio/formio.js/blob/4.13.x/src/components/signature/Signature.js | ||
* | ||
* Note that we don't offer support for many properties through our form builder, like: | ||
* | ||
* - width | ||
* - height | ||
* - penColor | ||
* - backgroundColor | ||
* - minWidth | ||
* - maxWidth | ||
* | ||
* Because of that, they are also not added to the type definitions (yet). That may | ||
* change once we implement our own renderer. | ||
* | ||
* @group Form.io components | ||
* @category Concrete types | ||
*/ | ||
export interface SignatureComponentSchema | ||
extends Omit< | ||
SignatureInputSchema, | ||
'hideLabel' | 'disabled' | 'placeholder' | 'validateOn' | 'multiple' | ||
> { | ||
type: 'signature'; | ||
/** | ||
* The footer is a text displayed below the drawing canvas which may hint the user on | ||
* what is expected of them. | ||
* | ||
* I'm not sure what the difference is with the 'description' field. | ||
*/ | ||
footer?: string; // translatable instruction | ||
|
||
/** | ||
* The value type of the component. We don't support `multiple: true` in this component. | ||
* | ||
* Note that we use the `defaultValue` property to infer the value type, we do not | ||
* support actually setting a component default value. | ||
*/ | ||
defaultValue?: SignatureValue | ''; | ||
} |
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,105 @@ | ||
import {expectAssignable, expectNotAssignable} from 'tsd'; | ||
|
||
import {SignatureComponentSchema} from '../../../lib/'; | ||
|
||
// minimal signature component schema | ||
expectAssignable<SignatureComponentSchema>({ | ||
id: 'yejak', | ||
type: 'signature', | ||
key: 'someSignature', | ||
label: 'Some signature', | ||
}); | ||
|
||
// with additional, signature-component specific properties | ||
expectAssignable<SignatureComponentSchema>({ | ||
id: 'yejak', | ||
type: 'signature', | ||
key: 'someSignature', | ||
label: 'Some signature', | ||
defaultValue: 'data:image/png;base64,dGhlIGdhbWU=', | ||
footer: 'Please do not draw inappropriate images', | ||
}); | ||
|
||
// full, correct schema | ||
expectAssignable<SignatureComponentSchema>({ | ||
id: 'yejak', | ||
type: 'signature', | ||
// basic tab in builder form | ||
label: 'Some signature', | ||
key: 'someSignature', | ||
description: 'A description', | ||
tooltip: 'A tooltip', | ||
showInSummary: true, | ||
showInEmail: false, | ||
showInPDF: true, | ||
hidden: false, | ||
clearOnHide: true, | ||
isSensitiveData: false, | ||
defaultValue: '', | ||
// advanced tab in builder form | ||
conditional: { | ||
show: undefined, | ||
when: undefined, | ||
eq: undefined, | ||
}, | ||
// validation tab in builder form | ||
validate: { | ||
required: false, | ||
plugins: undefined, | ||
}, | ||
translatedErrors: { | ||
nl: { | ||
required: 'Je moet een waarde opgeven!!!', | ||
}, | ||
}, | ||
errors: { | ||
// translatedErrors is converted into errors by the backend | ||
required: 'Je moet een waarde opgeven!!!', | ||
}, | ||
// registration tab in builder form | ||
registration: { | ||
attribute: '', | ||
}, | ||
// translations tab in builder form | ||
openForms: { | ||
translations: { | ||
nl: { | ||
label: 'foo', | ||
description: 'bar', | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// different component type | ||
expectNotAssignable<SignatureComponentSchema>({ | ||
type: 'fieldset' as const, | ||
}); | ||
|
||
// using unsupported properties | ||
expectNotAssignable<SignatureComponentSchema>({ | ||
id: 'yejak', | ||
type: 'signature' as const, | ||
key: 'someSignature', | ||
label: 'Some signature', | ||
hideLabel: true, | ||
}); | ||
|
||
// bad value format | ||
expectNotAssignable<SignatureComponentSchema>({ | ||
id: 'yejak', | ||
type: 'signature' as const, | ||
key: 'someSignature', | ||
label: 'Some signature', | ||
defaultValue: 'random string', | ||
}); | ||
|
||
// multiple is not supported | ||
expectNotAssignable<SignatureComponentSchema>({ | ||
id: 'yejak', | ||
type: 'signature' as const, | ||
key: 'someSignature', | ||
label: 'Some signature', | ||
multiple: true, | ||
defaultValue: [], | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imagine having this available in Python.. 🥹
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find it a bit scary to be honest. And there doesn't seem to be a definitive way in TS to express that something is a non-empty string, so it's clearly still lacking a lot.