Skip to content

Commit 021d291

Browse files
committed
feat(components): add MoneyInput and story
1 parent 568f659 commit 021d291

File tree

4 files changed

+133
-2
lines changed

4 files changed

+133
-2
lines changed
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<!-- https://github.com/material-components/material-components-web/tree/master/packages/mdc-textfield -->
2+
<script>
3+
import { MDCTextField } from '@material/textfield'
4+
import { generateRandomID } from '../../../random'
5+
import { onMount } from 'svelte'
6+
7+
export let label = ''
8+
export let value = ''
9+
export let placeholder = ''
10+
export let maxlength = undefined
11+
export let autofocus = false
12+
export let disabled = false
13+
export let required = false
14+
15+
const labelID = generateRandomID('text-label-')
16+
17+
let element = {}
18+
let mdcTextField = {}
19+
20+
$: mdcTextField.value = value
21+
22+
onMount(() => {
23+
mdcTextField = new MDCTextField(element)
24+
return () => mdcTextField.destroy()
25+
})
26+
27+
const focus = (node) => autofocus && node.focus()
28+
</script>
29+
30+
<style>
31+
.material-icons {
32+
color: rgb(133, 140, 148);
33+
position: relative;
34+
top: 0.4rem;
35+
right: 0.6rem;
36+
}
37+
.required {
38+
color: var(--mdc-required-input, var(--mdc-theme-status-error));
39+
font-size: small;
40+
margin-left: 1rem;
41+
margin-top: 0.2rem;
42+
}
43+
.label-margin {
44+
margin-left: 1.1rem;
45+
}
46+
.mdc-text-field--label-floating .mdc-floating-label {
47+
margin-left: 0;
48+
}
49+
</style>
50+
51+
<label
52+
class="mdc-text-field mdc-text-field--outlined {$$props.class} textfield-radius"
53+
class:mdc-text-field--no-label={!label}
54+
class:mdc-text-field--disabled={disabled}
55+
bind:this={element}
56+
>
57+
<i class="material-icons" aria-hidden="true">attach_money</i>
58+
<input
59+
step="0.01"
60+
type="number"
61+
min="0"
62+
class="mdc-text-field__input"
63+
aria-labelledby={labelID}
64+
bind:value
65+
use:focus
66+
on:blur
67+
on:keydown
68+
on:keypress
69+
on:keyup
70+
{required}
71+
{maxlength}
72+
{disabled}
73+
{placeholder}
74+
/>
75+
<span class="mdc-notched-outline">
76+
<span class="mdc-notched-outline__leading" />
77+
{#if label}
78+
<span class="mdc-notched-outline__notch">
79+
<span class="mdc-floating-label label-margin" id={labelID}>
80+
{label}
81+
</span>
82+
</span>
83+
{/if}
84+
<span class="mdc-notched-outline__trailing" />
85+
</span>
86+
</label>
87+
{#if required && !value}
88+
<div class="required">*Required</div>
89+
{/if}

components/mdc/TextInput/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import './_index.scss'
22
import TextArea from './TextArea.svelte'
33
import TextField from './TextField.svelte'
4+
import MoneyInput from './MoneyInput.svelte'
45

5-
export { TextArea, TextField }
6+
export { TextArea, TextField, MoneyInput }

components/mdc/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Progress from './Progress'
2121
import Select from './Select'
2222
import Snackbar from './Snackbar'
2323
import TabBar from './TabBar'
24-
import { TextArea, TextField } from './TextInput'
24+
import { TextArea, TextField, MoneyInput } from './TextInput'
2525
import Tooltip from './Tooltip'
2626
import TopAppBar from './TopAppBar'
2727
import { setNotice } from './Snackbar/notice'
@@ -40,6 +40,7 @@ export {
4040
isAboveTablet,
4141
List,
4242
Menu,
43+
MoneyInput,
4344
Progress,
4445
TabBar,
4546
TextArea,

stories/MoneyInput.stories.svelte

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<script>
2+
import { Meta, Template, Story } from '@storybook/addon-svelte-csf'
3+
import { MoneyInput } from '../components/mdc'
4+
import { copyAndModifyArgs } from './helpers.js'
5+
6+
let title = 'MoneyInput'
7+
8+
const args = {
9+
label: title,
10+
'on:keydown': (event) => console.log('keydown', event),
11+
'on:keypress': (event) => (lastKey = event.key),
12+
'on:keyup': (event) => console.log('keyup', event),
13+
class: '', //will only work with global class
14+
}
15+
16+
let lastKey = ''
17+
</script>
18+
19+
<Meta title="Atoms/MoneyInput" component={MoneyInput} />
20+
21+
<Template let:args>
22+
<MoneyInput {...args} on:keydown={args['on:keydown']} on:keypress={args['on:keypress']} on:keyup={args['on:keyup']} />
23+
{#if lastKey}
24+
<p>Last key pressed: {lastKey}</p>
25+
{/if}
26+
</Template>
27+
28+
<Story name="Default" {args} />
29+
30+
<Story name="Required" args={copyAndModifyArgs(args, { required: true })} />
31+
32+
<Story name="Placeholder" args={copyAndModifyArgs(args, { placeholder: 'Placeholder' })} />
33+
34+
<Story name="Label" args={copyAndModifyArgs(args, { label: 'label' })} />
35+
36+
<Story name="Autofocus" args={copyAndModifyArgs(args, { autofocus: true })} />
37+
38+
<Story name="Disabled" args={copyAndModifyArgs(args, { disabled: true })} />
39+
40+
<Story name="maxlength" args={copyAndModifyArgs(args, { maxlength: 255 })} />

0 commit comments

Comments
 (0)