Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dark-carrots-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solid-primitives/input-mask": patch
---

input-mask: add story illustrating function-type masks
43 changes: 43 additions & 0 deletions packages/input-mask/stories/inputMask.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,46 @@ export const ArrayMask = meta.story({
);
},
});

export const FunctionMask = meta.story({
name: "IPv4 mask (function format)",
parameters: {
docs: {
description: {
story:
"`createInputMask` accepts a function that receives value and selection and returns new value and selection, in this case to block any input that creates an invalid (partial) IPv4 address.",
},
},
},
render: () => {
const [value, setValue] = createSignal("");
let previous: [string, Selection] = ["", [0, 0]];
const mask = createInputMask((addr, sel) => {
const isValid = addr.split(/\./g).every(num => num === "" || !/\D/.test(num) && Number(num) < 256);
Comment thread
atk marked this conversation as resolved.
if (isValid) {
previous = [addr, sel];
}
return previous;
});

return (
<Container>
<h3 style={{ margin: 0 }}>IPv44 Address — "127.0.0.1"</h3>

<div style={{ display: "flex", "flex-direction": "column", gap: "0.35rem" }}>
<Label>Type an IPv4 Address (0.0.0.0)</Label>
<input
type="text"
placeholder="0.0.0.0"
onKeyup={e => setValue(mask(e))}
onInput={e => setValue(mask(e))}
onPaste={e => setValue(mask(e))}
style={inputStyle}
/>
</div>

<ValueDisplay label="value" value={value()} />
</Container>
);
},
});
Loading