Skip to content

Commit d520f1d

Browse files
author
Larry Botha
committed
feat(textarea): add component and tests
closes #86
1 parent 782cc34 commit d520f1d

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

lib/components/Textarea.svelte

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script>
2+
import {getContext} from 'svelte';
3+
import {key} from './key';
4+
5+
export let name;
6+
7+
const {form, handleChange} = getContext(key);
8+
</script>
9+
10+
<textarea {name} on:change={handleChange} on:blur={handleChange} {...$$props}>
11+
{$form[name]}
12+
</textarea>

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export {createForm} from './create-form';
22
export {default as Form} from './components/Form.svelte';
3+
export {default as Textarea} from './components/Textarea.svelte';
34
export {default as Field} from './components/Field.svelte';
45
export {default as Select} from './components/Select.svelte';
56
export {default as ErrorMessage} from './components/ErrorMessage.svelte';

test/textarea.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {render as renderTl, fireEvent, waitFor} from '@testing-library/svelte';
2+
3+
import Form from '../lib/components/Form.svelte';
4+
import Textarea from '../lib/components/Textarea.svelte';
5+
6+
const defaultProps = {
7+
initialValues: {
8+
description: '',
9+
},
10+
['onSubmit']: () => {},
11+
};
12+
13+
const render = (ui, props = defaultProps) => {
14+
return renderTl(<Form {...props}>{ui}</Form>, {props});
15+
};
16+
17+
describe('Textarea', () => {
18+
test('-> updates value on change', async () => {
19+
const label = 'description';
20+
const props = {...defaultProps};
21+
const {getByLabelText} = render(
22+
<>
23+
<label for={label}>{label}</label>
24+
<Textarea id={label} name={label} />
25+
</>,
26+
props,
27+
);
28+
29+
const textarea = getByLabelText(label);
30+
expect(textarea.value.trim()).toBe(props.initialValues.description);
31+
32+
const value = 'foo';
33+
34+
await fireEvent.change(textarea, {target: {value}});
35+
36+
expect(textarea.value).toContain(value);
37+
});
38+
});

0 commit comments

Comments
 (0)