Skip to content

Commit 8f3ad5a

Browse files
committed
feat: reset button for new color input created
1 parent 79404fa commit 8f3ad5a

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

src/pages/gradient-border.ts

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import {
88
getAllInputElements,
99
getResultPage,
1010
getCopyCodeButton,
11-
getAllFields,
1211
getResetButton,
1312
getDegreeSpanElement,
13+
getGradientPreview,
1414
} from '../lib/getElements';
1515
import {
1616
copyCodeToClipboard,
@@ -39,6 +39,7 @@ const toggleRadiusInputForGradientBorder = getCheckbox(attribute);
3939
const getOutputElement = getOutput(attribute);
4040

4141
const getDegreeElement = getRange(attribute);
42+
const resetButton = getResetButton(attribute);
4243

4344
let gradientBorderInputs = getAllInputElements('gradient-border');
4445

@@ -124,6 +125,8 @@ export function addGradientBorderListener() {
124125
function addEventListenerToTheNewColorPicker() {
125126
gradientBorderInputs = getAllInputElements(attribute);
126127
inputEventListner();
128+
if (resetButton.classList.contains('reset-show')) return;
129+
resetButton.classList.add('reset-show');
127130
}
128131

129132
function inputEventListner() {
@@ -135,39 +138,46 @@ function inputEventListner() {
135138
});
136139
}
137140

138-
139141
// reset the values of all target fields
140-
141142
function resetValues() {
142-
const { inputs } = getAllFields(attribute);
143+
const colorInput: HTMLInputElement[] = [...new Set([])];
143144

144-
getResetButton(attribute).addEventListener("click", () => {
145+
resetButton.addEventListener('click', () => {
146+
resetButton.classList.remove('reset-show');
147+
getDegreeSpanElement(attribute).innerHTML = 'deg';
145148

146-
inputs.forEach(input => {
149+
getGradientPreview(attribute).style.background = '';
150+
151+
gradientBorderInputs.forEach((input) => {
152+
input.checked = false;
147153
input.value = input.defaultValue;
148-
input.checked = false
149-
});
150154

151-
getDegreeSpanElement(attribute).innerHTML = "deg";
152-
getResetButton(attribute).classList.remove("reset-show");
153-
})
155+
if (input.id.includes('color')) {
156+
colorInput.push(input);
157+
}
158+
});
154159

160+
if (colorInput.length > 2) {
161+
for (let i = 2; i < colorInput.length; i++) {
162+
removeColorPicker(attribute);
163+
}
164+
}
165+
});
155166
}
156167

157168
// get values from all targets to get notified when values change.
158169

159170
function getValues() {
160-
161-
const { inputs } = getAllFields(attribute);
162-
163-
inputs.forEach(input => {
164-
input.addEventListener("input", () => {
165-
if (!input.checked) getResetButton(attribute).classList.remove("reset-show");
166-
if (input.value !== "" || input.checked === true) {
167-
getResetButton(attribute).classList.add("reset-show");
168-
resetValues();
171+
gradientBorderInputs.forEach((input) => {
172+
input.addEventListener('input', () => {
173+
if (input.nodeName === 'TEXTAREA') {
174+
if (input.value === '') return;
169175
}
170-
})
171-
})
176+
177+
if (resetButton.classList.contains('reset-show')) return;
178+
resetButton.classList.add('reset-show');
179+
});
180+
});
172181
}
173-
getValues();
182+
resetValues();
183+
getValues();

0 commit comments

Comments
 (0)