Skip to content

Commit 42fac00

Browse files
committed
feat(search): introduce API to control form interactions
1 parent 6f03b1a commit 42fac00

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

packages/search/src/search.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
TemplateResult,
1818
property,
1919
} from 'lit-element';
20+
import { ifDefined } from 'lit-html/directives/if-defined';
2021

2122
import { Textfield } from '@spectrum-web-components/textfield';
2223
import '@spectrum-web-components/icon';
@@ -42,15 +43,36 @@ export class Search extends Textfield {
4243
];
4344
}
4445

46+
@property()
47+
public action?: string;
48+
4549
@property()
4650
public label = 'Search';
4751

52+
@property()
53+
public method?: 'GET' | 'POST' | 'dialog';
54+
4855
@property()
4956
public placeholder = 'Search';
5057

58+
private handleSubmit(event: Event): void {
59+
const applyDefault = this.dispatchEvent(
60+
new Event('submit', {
61+
cancelable: true,
62+
})
63+
);
64+
if (!applyDefault) {
65+
event.preventDefault();
66+
}
67+
}
68+
5169
protected render(): TemplateResult {
5270
return html`
53-
<form>
71+
<form
72+
action=${ifDefined(this.action)}
73+
method=${ifDefined(this.method)}
74+
@submit=${this.handleSubmit}
75+
>
5476
<sp-icons-medium></sp-icons-medium>
5577
<sp-icon
5678
id="icon"

packages/search/stories/search.stories.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@ OF ANY KIND, either express or implied. See the License for the specific languag
1010
governing permissions and limitations under the License.
1111
*/
1212
import { storiesOf } from '@storybook/polymer';
13+
import { action } from '@storybook/addon-actions';
1314
import { html } from 'lit-html';
1415

1516
import '../';
17+
import { Search } from '../';
1618

1719
storiesOf('Search', module).add(
1820
'Default',
1921
() => html`
20-
<sp-search></sp-search>
22+
<sp-search
23+
@submit=${(e: Event) => {
24+
e.preventDefault();
25+
const search = e.target as Search;
26+
action(`Search: ${search.value}`)();
27+
}}
28+
></sp-search>
2129
<sp-search disabled></sp-search>
2230
`
2331
);

0 commit comments

Comments
 (0)