Skip to content

Commit

Permalink
Merge pull request #7 from bhcha/main
Browse files Browse the repository at this point in the history
add : label control
  • Loading branch information
bhcha authored Jun 29, 2024
2 parents 5df3554 + 9898160 commit a391fbd
Show file tree
Hide file tree
Showing 10 changed files with 385 additions and 69 deletions.
6 changes: 3 additions & 3 deletions src/components/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ export const Header = ({ user, onLogin, onLogout, onCreateAccount }) => html`
</div>
<div>
${user
? Button({ size: 'small', onClick: onLogout, label: 'Log out' })
? Button({ size: 'small', onClick: onLogout, feedback: 'Log out' })
: html`${Button({
size: 'small',
onClick: onLogin,
label: 'Log in',
feedback: 'Log in',
})}
${Button({
primary: true,
size: 'small',
onClick: onCreateAccount,
label: 'Sign up',
feedback: 'Sign up',
})}`}
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/components/commons/LLabelContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class LLabelContainer extends LitElement {
}

render() {
let isLabelFront = (this.labelAlign && this.labelAlign == 'front');
let isLabelLeft = (this.labelAlign && this.labelAlign == 'left');
return html`
<div class="${isLabelFront && 'container'}">
<div class="${isLabelLeft && 'container'}">
<slot></slot>
</div>
`
Expand Down
13 changes: 11 additions & 2 deletions src/components/commons/LitParents.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
// EventManager.js
import {LitElement} from "lit";
import {css, LitElement} from "lit";
import {SharedStyles} from "@/components/commons/SharedStyles.js";

class LitParents extends LitElement{

constructor(selector) {
constructor() {
super();

}

setSelector(selector) {
this.selector = selector;
}

Expand All @@ -21,6 +26,10 @@ class LitParents extends LitElement{
}
}

isValid() {
return !!getValue();
}

addEventListener(type, listener, options) {
this.shadowRoot.querySelector(this.selector).addEventListener(type, listener, options);
}
Expand Down
52 changes: 27 additions & 25 deletions src/components/commons/SharedStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,37 @@ import { css } from 'lit';
class SharedStyles {
static get styles() {
return css`
:host {
margin: 0;
font-family: var(--bs-body-font-family);
font-size: var(--bs-body-font-size);
font-weight: var(--bs-body-font-weight);
line-height: var(--bs-body-line-height);
color: var(--bs-body-color);
:host {
margin: 0;
font-family: var(--bs-body-font-family);
font-size: var(--bs-body-font-size);
font-weight: var(--bs-body-font-weight);
line-height: var(--bs-body-line-height);
color: var(--bs-body-color);
background-color: var(--bs-body-bg);
-webkit-text-size-adjust: 100%;
-webkit-tap-highlight-color: transparent;
}
background-color: var(--bs-body-bg);
-webkit-text-size-adjust: 100%;
-webkit-tap-highlight-color: transparent;
}
button, input, optgroup, select, textarea {
font-family: inherit;
font-size: inherit;
line-height: inherit;
}
button, input, optgroup, select, textarea {
font-family: inherit;
font-size: inherit;
line-height: inherit;
}
.container {
display: flex;
align-items: center;
margin-bottom: 10px;
}
.container {
display: flex;
align-items: center;
margin-bottom: 10px;
}
input:focus {
border-color: #3399ff;
box-shadow: 0 0 0 5px rgba(51, 153, 255, 0.3);
}
input:focus {
border-color: #3399ff;
box-shadow: 0 0 0 5px rgba(51, 153, 255, 0.3);
}
`;
}
}
Expand Down
100 changes: 84 additions & 16 deletions src/components/input/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import {LLabel} from '../text/Label.js';
import '../commons/common.css';
import {SharedStyles} from "../commons/SharedStyles.js";
import {LitParents} from "../commons/LitParents.js";
import {LFeedback} from "@/components/text/Feedback.js";

class LInput extends LitParents {

constructor() {
super('input');
super();

super.setSelector('input');
}

static styles =
Expand All @@ -17,10 +20,11 @@ class LInput extends LitParents {

// component css
, css`
*, ::after, ::before {
*, ::after, ::before {
box-sizing: border-box;
}
.l-input {
}
.l-input {
width: 100%;
padding: .375rem .75rem;
font-size: .875rem;
Expand All @@ -32,9 +36,9 @@ class LInput extends LitParents {
border: var(--bs-border-width) solid var(--bs-border-color);
border-radius: 8px;
outline: none;
}
}
.l-flex-input {
.l-flex-input {
flex-grow: 1;
padding: .375rem .75rem;
font-size: .875rem;
Expand All @@ -47,39 +51,103 @@ class LInput extends LitParents {
border-radius: 8px;
outline: none;
transition: all 0.3s ease-in-out;
}
`
}
.is-valid {
border-color: var(--bs-success);
padding-right: calc(1.5em + .75rem);
background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3e%3cpath fill='%231b8835' d='M2.3 6.73.6 4.53c-.4-1.04.46-1.4 1.1-.8l1.1 1.4 3.4-3.8c.6-.63 1.6-.27 1.2.7l-4 4.6c-.43.5-.8.4-1.1.1z'/%3e%3c/svg%3e");
background-repeat: no-repeat;
background-position: right calc(.375em + .1875rem) center;
background-size: calc(.75em + .375rem) calc(.75em + .375rem)
}
.is-invalid {
border-color: var(--bs-danger);
padding-right: calc(1.5em + .75rem);
background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12' width='12' height='12' fill='none' stroke='%23df1414'%3e%3ccircle cx='6' cy='6' r='4.5'/%3e%3cpath stroke-linejoin='round' d='M5.8 3.6h.4L6 6.5z'/%3e%3ccircle cx='6' cy='8.2' r='.6' fill='%23df1414' stroke='none'/%3e%3c/svg%3e");
background-repeat: no-repeat;
background-position: right calc(.375em + .1875rem) center;
background-size: calc(.75em + .375rem) calc(.75em + .375rem)
}
`
];



formatValue(value) {
// 예시: 숫자 포맷팅 (쉼표 추가)
return value.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

isValid() {
const regex = new RegExp(this.pattern);
const value = this.getValue();

if(!value && this.required) {
return false;
} else return !(regex && !regex.test(value));
}

validate() {
if(this.isValid()) {
this.shadowRoot.querySelector(this.selector).classList.remove('is-invalid');
} else {
this.shadowRoot.querySelector(this.selector).classList.add('is-invalid');
}
}


static get properties() {
return {
type: {type: String},
label: {type: String},
id: {type: String},
name: {type: String},
label: {type: String},
feedback: {type: String},
labelAlign: {type: String},
required: {type: String},
required: {type: Boolean},
disabled: {type: Boolean},
readonly: {type: Boolean},
value: {type: String},
pattern: {type: String},
placeholder: {type: String},
maxlength: {type: String},
minlength: {type: String},
};
}

render() {
let isLabelFront = (this.labelAlign && this.labelAlign == 'front');
let isLabelLeft = (this.labelAlign && this.labelAlign == 'left');

return html`
<div class="${isLabelFront ? 'container':''}">
<div class="${isLabelLeft ? 'container' : ''}">
${new LLabel({
label: `${this.label}`,
id: `${this.id}`,
labelAlign: `${this.labelAlign}`,
required: `${this.required}`
})}
<input type="${this.type}" class="${isLabelFront ? 'l-flex-input' : 'l-input'}" id="${this.id}"
required=${this.required} placeholder="${this.placeholder}">
<input type="${this.type}"
class="${isLabelLeft ? 'l-flex-input' : 'l-input'}"
id="${this.id}"
name="${this.name}"
minlength="${this.minlength}"
maxlength="${this.maxlength}"
?required=${this.required}
?disabled=${this.disabled}
?readonly=${this.readonly}
placeholder="${this.placeholder}"
pattern="${this.pattern}"
value="${this.value}"
@blur="${this.validate}"
>
</div>
${new LFeedback({
feedback: `${this.feedback}`,
})}
`;
}

}


customElements.define('l-input', LInput);
47 changes: 47 additions & 0 deletions src/components/text/Feedback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {css, html, LitElement} from 'lit';
import '../commons/common.css';

class LFeedback extends LitElement {
static styles =
[
// component css
css`
.is-valid~.valid-feedback, .is-valid~.valid-tooltip, .was-validated :valid~.valid-feedback, .was-validated :valid~.valid-tooltip {
display: block;
}
.valid-feedback {
display: block;
width: 100%;
margin-top: .25rem;
font-size: .875em;
color: var(--bs-success-text);
}
`
]
;

constructor({feedback}) {
super();

this.feedback = feedback;
}

static get properties() {
return {
feedback: {type: String},
};
}

render() {
let isLabelLeft = (this.labelAlign && this.labelAlign == 'left');
return html`
<div class="valid-feedback">
${this.feedback}
</div>
`
}
}

customElements.define('l-feedback', LFeedback);
export {LFeedback};
7 changes: 4 additions & 3 deletions src/components/text/Label.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ class LLabel extends LitElement {
}

render() {
let isLabelFront = (this.labelAlign && this.labelAlign == 'front');
let isLabelLeft = (this.labelAlign && this.labelAlign == 'left');

return html`
<label class="${(isLabelFront && this.label) ? 'l-flex-label' : 'l-label'}" for="${this.id}">
<label class="${(isLabelLeft && this.label) ? 'l-flex-label' : 'l-label'}" for="${this.id}">
${this.required == 'true'
? (isLabelFront
? (isLabelLeft
? html`<span style="color: #df1414;margin-right: 2px">*</span>${this.label}`
: html`${this.label}<span style="color: #df1414;margin-left: 2px">*</span>`)
: this.label}
Expand Down
Loading

0 comments on commit a391fbd

Please sign in to comment.