Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add text area component #276

Merged
merged 19 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
81 changes: 81 additions & 0 deletions src/components/TextArea/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Component } from 'pet-dex-utilities';
import './index.scss';

const events = [
'name:change',
'placeholder:change',
'maxLength:change',
'required:change',
'error',
];

const html = `
<div class="textarea">
<textarea class="textarea__input" data-select="textarea" rows="1" cols="1"></textarea>
<span class="textarea__trigger" data-select="resize-trigger" contenteditable></span>
</div>
`;

export default function TextArea({
name = '',
placeholder = '',
maxLength = 524288,
required = false,
}) {
Component.call(this, { html, events });
const $textarea = this.selected.get('textarea');
const $resizeTrigger = this.selected.get('resize-trigger');

this.setName(name);
this.setPlaceholder(placeholder);
this.setMaxLength(maxLength);
this.setRequired(required);

function autoResize() {
$resizeTrigger.innerText = $textarea.value;

$textarea.style.height = 'auto';
$textarea.style.height = `${$resizeTrigger.offsetHeight}px`;
}

this.listen('mount', () => {
$textarea.addEventListener('focus', () =>
$textarea.classList.remove('textarea__input--error'),
);
$textarea.addEventListener('input', autoResize);
window.addEventListener('resize', autoResize);
});

this.listen('unmount', () => {
$textarea.removeEventListener('focus', () =>
$textarea.classList.remove('textarea__input--error'),
);
$textarea.removeEventListener('input', autoResize);
window.removeEventListener('resize', autoResize);
});
}

TextArea.prototype = Object.assign(TextArea.prototype, Component.prototype, {
setName(name = '') {
this.selected.get('textarea').name = name;
this.emit('name:change', name);
},
setPlaceholder(placeholder = '') {
this.selected.get('textarea').placeholder = placeholder;
this.emit('placeholder:change', placeholder);
},
setMaxLength(maxLength = 524288) {
Mathh19 marked this conversation as resolved.
Show resolved Hide resolved
if (maxLength) {
this.selected.get('textarea').maxLength = maxLength;
}
Mathh19 marked this conversation as resolved.
Show resolved Hide resolved
this.emit('maxLength:change', maxLength);
},
setRequired(required = false) {
Mathh19 marked this conversation as resolved.
Show resolved Hide resolved
this.selected.get('textarea').required = required;
this.emit('required:change', required);
},
error() {
this.selected.get('textarea').classList.add('textarea__input--error');
this.emit('error');
},
});
56 changes: 56 additions & 0 deletions src/components/TextArea/index.scss
Mathh19 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
@use '~styles/fonts.scss' as fonts;
@use '~styles/colors.scss' as colors;

.textarea {
position: relative;

&__input,
&__trigger {
width: 100%;

font-family: fonts.$fifthFont;
font-size: fonts.$sm;
line-height: 1;
}

&__input {
overflow: hidden;

padding-bottom: 1.4rem;

border: unset;
border-bottom: 0.1rem solid colors.$gray400;

position: relative;
z-index: 1;

outline-color: transparent;

transition: 0.2s ease-in-out outline;

resize: none;

&::placeholder {
color: colors.$gray400;
}

&:focus {
outline-color: colors.$primary200;
}

&--error {
color: colors.$secondary100;

background: colors.$error100;
}
}

&__trigger {
display: block;

position: absolute;
top: 0;

visibility: hidden;
}
}
26 changes: 26 additions & 0 deletions src/stories/TextArea.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Textarea from '../components/TextArea';

export default {
title: 'Components/TextArea',
render: (args) => {
const textarea = new Textarea(args);
const $container = document.createElement('div');
textarea.mount($container);

return $container;
},
argTypes: {
name: { control: 'text', default: '' },
placeholder: { control: 'text', default: '' },
maxLength: { control: 'number', default: 524288 },
required: { control: 'boolean', default: false },
},
};

export const Default = {
args: {
name: 'textarea',
placeholder: 'Escreva o cuidado especial',
required: true,
},
};
1 change: 1 addition & 0 deletions src/styles/fonts.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ $primaryFont: 'Montserrat', sans-serif;
$secondaryFont: 'Wix Madefor Display', sans-serif;
$tertiaryFont: 'Helvetica', sans-serif;
$fourthFont: 'Noto Sans', sans-serif;
$fifthFont: 'Poppins', sans-serif;

$xs: 1.4rem;
$sm: 1.6rem;
Expand Down