Skip to content

Commit 2617c64

Browse files
author
Amin Mahboubi
authored
feat: Input accepts type as prop (#38)
1 parent 43baa9f commit 2617c64

5 files changed

Lines changed: 96 additions & 2 deletions

File tree

src/components/Input.mdx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,17 @@ Text input element
3535
`}
3636
/>
3737
</Playground>
38+
39+
40+
## Custom Input type
41+
42+
<Playground>
43+
<MML
44+
source={`
45+
<mml>
46+
<input name="answer" value="hidden value used in form" type="hidden"></input>
47+
<button name="action" value="confirm">Confirm</button>
48+
</mml>
49+
`}
50+
/>
51+
</Playground>

src/components/Input.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ export type InputProps = {
77
label?: string;
88
/** Initial value of the input */
99
value?: string;
10+
/** The input type */
11+
type?: string;
1012
/** The placeholder of the input field */
1113
placeholder?: string;
1214
};
1315

1416
/**
1517
* Text input element. Usually you'll want to rely on regular messages
1618
*/
17-
export const Input: FC<InputProps> = ({ name, label, value = '', placeholder = '' }) => {
19+
export const Input: FC<InputProps> = ({ name, label, value = '', type = 'text', placeholder = '' }) => {
1820
const [state, setState] = useState(value);
1921
const id = `mml-${name}`;
2022

@@ -30,6 +32,7 @@ export const Input: FC<InputProps> = ({ name, label, value = '', placeholder = '
3032
className="mml-input"
3133
name={name}
3234
value={state}
35+
type={type}
3336
placeholder={placeholder}
3437
onChange={(event) => setState(event.target.value)}
3538
/>

src/parser/__snapshots__/examples.test.tsx.snap

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6591,6 +6591,23 @@ Object {
65916591
"toJSON": [Function],
65926592
"type": "text",
65936593
},
6594+
Object {
6595+
"attributes": Object {
6596+
"name": "secret",
6597+
"type": "hidden",
6598+
"value": "secret",
6599+
},
6600+
"children": Array [],
6601+
"name": "input",
6602+
"toJSON": [Function],
6603+
"type": "element",
6604+
},
6605+
Object {
6606+
"text": "
6607+
",
6608+
"toJSON": [Function],
6609+
"type": "text",
6610+
},
65946611
Object {
65956612
"attributes": Object {
65966613
"name": "send",
@@ -6677,6 +6694,27 @@ Tree {
66776694
"type": "element",
66786695
},
66796696
},
6697+
MMLTag {
6698+
"attributes": Object {
6699+
"name": "secret",
6700+
"type": "hidden",
6701+
"value": "secret",
6702+
},
6703+
"children": Array [],
6704+
"key": "tag-input-position-2",
6705+
"name": "input",
6706+
"node": Object {
6707+
"attributes": Object {
6708+
"name": "secret",
6709+
"type": "hidden",
6710+
"value": "secret",
6711+
},
6712+
"children": Array [],
6713+
"name": "input",
6714+
"toJSON": [Function],
6715+
"type": "element",
6716+
},
6717+
},
66806718
MMLTag {
66816719
"attributes": Object {
66826720
"name": "send",
@@ -6695,7 +6733,7 @@ Tree {
66956733
},
66966734
},
66976735
],
6698-
"key": "tag-button-position-2",
6736+
"key": "tag-button-position-3",
66996737
"name": "button",
67006738
"node": Object {
67016739
"attributes": Object {
@@ -6776,6 +6814,23 @@ Tree {
67766814
"toJSON": [Function],
67776815
"type": "text",
67786816
},
6817+
Object {
6818+
"attributes": Object {
6819+
"name": "secret",
6820+
"type": "hidden",
6821+
"value": "secret",
6822+
},
6823+
"children": Array [],
6824+
"name": "input",
6825+
"toJSON": [Function],
6826+
"type": "element",
6827+
},
6828+
Object {
6829+
"text": "
6830+
",
6831+
"toJSON": [Function],
6832+
"type": "text",
6833+
},
67796834
Object {
67806835
"attributes": Object {
67816836
"name": "send",
@@ -6811,6 +6866,11 @@ Tree {
68116866
name="email"
68126867
type="text"
68136868
/>,
6869+
<Input
6870+
name="secret"
6871+
type="hidden"
6872+
value="secret"
6873+
/>,
68146874
<Button
68156875
name="send"
68166876
text="Submit"
@@ -6830,6 +6890,11 @@ Array [
68306890
name="email"
68316891
type="text"
68326892
/>,
6893+
<Input
6894+
name="secret"
6895+
type="hidden"
6896+
value="secret"
6897+
/>,
68336898
<Button
68346899
name="send"
68356900
text="Submit"
@@ -6857,8 +6922,18 @@ Array [
68576922
name="email"
68586923
onChange={[Function]}
68596924
placeholder=""
6925+
type="text"
68606926
value=""
68616927
/>,
6928+
<input
6929+
className="mml-input"
6930+
id="mml-secret"
6931+
name="secret"
6932+
onChange={[Function]}
6933+
placeholder=""
6934+
type="hidden"
6935+
value="secret"
6936+
/>,
68626937
<button
68636938
className="mml-btn mml-btn--text mml-btn--grounded"
68646939
name="send"

src/parser/__snapshots__/parser.test.tsx.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,7 @@ exports[`simple input 4`] = `
954954
name="name"
955955
onChange={[Function]}
956956
placeholder=""
957+
type="text"
957958
value="John"
958959
/>
959960
`;

src/parser/examples.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ const examples = [
9292
<mml>
9393
<md>Happy to setup your new credit card application, what's your **yearly** family income?</md>
9494
<input type="text" name="email" />
95+
<input type="hidden" name="secret" value="secret" />
9596
<button name="send" value="submit">Submit</button>
9697
</mml>`,
9798
},

0 commit comments

Comments
 (0)