Skip to content

Commit

Permalink
fix: checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
si3nloong committed Feb 24, 2021
1 parent b651c93 commit 7f66e45
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 134 deletions.
2 changes: 1 addition & 1 deletion components/accordion/src/Accordion.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
export let multiple = false;
export let style = "";
const id = `accordion-${Math.floor(Math.random() * Date.now())}`;
const id = `acd-${Math.floor(Math.random() * Date.now())}`;
let props = { type: "checkbox" };
if (!multiple) props = Object.assign(props, { type: "radio", name: id });
Expand Down
216 changes: 89 additions & 127 deletions components/checkbox/src/Checkbox.svelte
Original file line number Diff line number Diff line change
@@ -1,150 +1,112 @@
<script lang="ts">
let id = 0;
export let ref: null | HTMLInputElement = null;
export let label = "";
export let name = "";
export let value = "";
export let group = [];
export let disabled = false;
export let checked = false;
export let onChange = (_) => {};
$: updateChekbox(group);
$: updateGroup(checked);
const updateChekbox = (group) => {
checked = group.indexOf(value) >= 0;
};
const updateGroup = (checked) => {
const index = group.indexOf(value);
if (checked) {
if (index < 0) {
group.push(value);
group = group;
}
} else {
if (index >= 0) {
group.splice(index, 1);
group = group;
}
}
};
const handleOnChange = (e) => {
onChange(e.target.checked);
};
</script>

<label class="responsive-ui-checkbox" {...$$restProps}>
<input
bind:this={ref}
type="checkbox"
on:change
{name}
{disabled}
{value}
bind:checked
style="display:none"
/>
<span>
<svg viewBox="0 0 12 9" width="10px" height="9px">
<polyline points="1 5 4 8 11 1" />
</svg>
</span><span>
<slot>{label}</slot>
</span>
</label>

<style lang="scss">
label {
position: relative;
display: flex;
align-items: center;
$primary: #3c53c7;
$gray: #b9b8c3;
.responsive-ui-checkbox {
position: relative;
}
.responsive-ui-checkbox {
display: inline-block;
cursor: pointer;
user-select: none;
-webkit-user-select: none;
-webkit-tap-highlight-color: transparent;
caption {
margin-left: 10px;
}
span {
display: inline-block;
vertical-align: middle;
input,
svg {
width: 16px;
height: 16px;
display: block;
}
input {
position: relative;
outline: none;
background: #fff;
border: none;
margin: 0;
padding: 0;
cursor: pointer;
border-radius: 3px;
transition: box-shadow 0.3s;
appearance: none;
background: #ddd;
box-shadow: inset 0 0 0 2px #ccc;
&:nth-child(2) {
position: relative;
width: 19px;
height: 19px;
border-radius: 50%;
// transform: scale(1);
vertical-align: middle;
border: 1px solid $gray;
transition: all 0.2s ease;
&:disabled {
opacity: 0.6;
svg {
position: absolute;
z-index: 1;
top: 5px;
left: 4px;
fill: none;
stroke: white;
stroke-width: 1.3;
stroke-linecap: round;
stroke-linejoin: round;
stroke-dasharray: 16px;
stroke-dashoffset: 16px;
transition: all 0.3s ease;
transition-delay: 0.1s;
transform: translate3d(0, 0, 0);
}
}
// &:hover:not(:disabled),
// &:checked:not(:disabled) {
// box-shadow: inset 0 0 0 1px #fc4451;
// }
}
svg {
position: absolute;
top: 0;
left: 0;
width: 16px;
height: 16px;
pointer-events: none;
fill: none;
stroke-width: 2px;
stroke-linecap: round;
stroke-linejoin: round;
stroke: #ddd;
transform: scale(1) translateZ(0);
}
//&.path {
// input {
// &:checked {
// transition-delay: 0.4s;
// }
// }
// svg {
// stroke-dasharray: 86.12;
// stroke-dashoffset: 86.12;
// transition: stroke-dasharray 0.6s, stroke-dashoffset 0.6s;
// }
//}
&:last-child {
margin-left: 6px;
&.bounce {
input {
&:checked {
box-shadow: inset 0 0 0 11px #fc4451;
& + svg {
animation: bounce 0.4s linear forwards 0.2s;
}
&:after {
content: "";
position: absolute;
top: 8px;
left: 0;
height: 1px;
width: 100%;
background: $gray;
transform-origin: 0 0;
transform: scaleX(0);
}
}
}
}
@keyframes bounce {
50% {
transform: scale(1.2);
&:hover span:first-child {
border-color: $primary;
}
}
75% {
transform: scale(0.9);
input:checked + span {
&:nth-child(2) {
border-color: var(--primary-color, #fc4451);
background: var(--primary-color, #fc4451);
// animation: check 0.6s ease;
}
svg {
stroke-dashoffset: 0;
}
}
100% {
transform: scale(1);
input:disabled + span {
&:nth-child(2) {
cursor: not-allowed;
background: #dcdcdc;
}
}
}
</style>

<label class="bounce" class:disabled>
<span class="responsive-ui-checkbox">
<input
type="checkbox"
on:change
on:change={handleOnChange}
{name}
{disabled}
{value}
bind:checked />
<svg viewBox="0 0 21 21">
<polyline points="5 10.75 8.5 14.25 16 6" />
</svg>
</span>
<caption>
<slot />
</caption>
</label>
12 changes: 9 additions & 3 deletions components/checkbox/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import type { SvelteComponentTyped } from "svelte/internal";

export interface CheckboxProps {
name: string;
value?: any;
ref?: null | HTMLInputElement;
label?: string;
id?: string;
name?: string;
value?: string;
disabled?: boolean;
checked?: boolean;
style?: string;
}

export interface CheckboxEvents {}
export interface CheckboxEvents {
change?: any;
}

export interface CheckboxSlots {
default: {};
Expand Down
2 changes: 1 addition & 1 deletion components/table/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type TableColumn = {
export type TableItem = Record<string, any> | object;

export type TableProps = {
ref: null | HTMLDivElement;
ref?: null | HTMLDivElement;
key: string;
columns: Partial<TableColumn>[];
items: null | TableItem[];
Expand Down
6 changes: 4 additions & 2 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import Tab from "../components/tab/src/Tab.svelte";
import ShowMore from "../components/show-more/src/ShowMore.svelte";
import Checkbox from "../components/checkbox/src/Checkbox.svelte";
import type { SvelteComponentDev } from "svelte/internal";
import Badge from "../components/badge/src/Badge.svelte";
import Quantity from "../components/quantity/src/Quantity.svelte";
Expand Down Expand Up @@ -154,7 +153,7 @@
},
];
const uploadSuccessful = ({ detail }) => {
const uploadSuccessful = ({ detail }: CustomEvent) => {
console.log(detail.response);
console.log(detail);
};
Expand Down Expand Up @@ -418,6 +417,7 @@
<Badge count={0}>testing</Badge>
<Badge count={90}>testing</Badge>
<Badge count={100}>testing</Badge>

<!-- <Badge count={100} /><Badge count={98} /> -->
<ComponentDetail hint="@responsive-ui/header" block={true}>
<Tooltip text="Responsive UI">
Expand All @@ -436,6 +436,8 @@
<ComponentDetail hint="@responsive-ui/input">
<Input style="width: 240px;" placeholder="Enter your text..." />
</ComponentDetail>
<Checkbox>testing</Checkbox>
<Checkbox disabled label="Hello world" />
<ComponentDetail hint="@responsive-ui/input-number">
<InputNumber min={0} format={(v) => `${v}%`} />
</ComponentDetail>
Expand Down

0 comments on commit 7f66e45

Please sign in to comment.