Skip to content

Commit

Permalink
fix: value handling for inputs (#6110)
Browse files Browse the repository at this point in the history
  • Loading branch information
deleonio authored Feb 23, 2024
2 parents 2eeb31a + 70c8244 commit 62bbb5f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/components/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2438,7 +2438,7 @@ export namespace Components {
"collapse": () => Promise<void>;
"expand": () => Promise<void>;
"focusLink": () => Promise<void>;
"isOpen": () => Promise<boolean>;
"isOpen": () => Promise<any>;
}
interface KolTreeItemWc {
/**
Expand All @@ -2460,7 +2460,7 @@ export namespace Components {
"collapse": () => Promise<void>;
"expand": () => Promise<void>;
"focusLink": () => Promise<void>;
"isOpen": () => Promise<boolean>;
"isOpen": () => Promise<any>;
}
interface KolTreeWc {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,24 @@ type OptionalProps = {
type Props = Generic.Element.Members<RequiredProps, OptionalProps>;
type Watches = Generic.Element.Watchers<RequiredProps, OptionalProps>;

const isAssociatedTagName = (name?: string): boolean => name === 'KOL-BUTTON' || name === 'KOL-INPUT' || name === 'KOL-SELECT' || name === 'KOL-TEXTAREA';
type HTMLInputFileElement = HTMLInputElement & {
files: FileList;
};

const isAssociatedTagName = (name?: string): boolean =>
name === 'KOL-BUTTON' ||
name === 'KOL-INPUT-CHECKBOX' ||
name === 'KOL-INPUT-COLOR' ||
name === 'KOL-INPUT-DATE' ||
name === 'KOL-INPUT-EMAIL' ||
name === 'KOL-INPUT-FILE' ||
name === 'KOL-INPUT-NUMBER' ||
name === 'KOL-INPUT-PASSWORD' ||
name === 'KOL-INPUT-RADIO' ||
name === 'KOL-INPUT-RANGE' ||
name === 'KOL-INPUT-TEXT' ||
name === 'KOL-SELECT' ||
name === 'KOL-TEXTAREA';

export class AssociatedInputController implements Watches {
private readonly experimentalMode = getExperimentalMode();
Expand All @@ -27,13 +44,12 @@ export class AssociatedInputController implements Watches {
this.host = this.findHostWithShadowRoot(host);
this.type = type;

if (getExperimentalMode() && isAssociatedTagName(this.host?.tagName)) {
if (this.experimentalMode && isAssociatedTagName(this.host?.tagName)) {
this.host?.querySelectorAll('input,select,textarea').forEach((el) => {
this.host?.removeChild(el);
});
switch (this.type) {
case 'button':
case 'checkbox':
case 'color':
case 'date':
case 'email':
Expand All @@ -53,6 +69,7 @@ export class AssociatedInputController implements Watches {
case 'textarea':
this.formAssociated = document.createElement('textarea');
break;
case 'checkbox': // Checkbox use default case
default:
this.formAssociated = document.createElement('input');
this.formAssociated.setAttribute('type', 'hidden');
Expand Down Expand Up @@ -133,6 +150,9 @@ export class AssociatedInputController implements Watches {
) {
if (associatedElement) {
switch (this.type) {
case 'file':
(associatedElement as HTMLInputFileElement).files = rawValue as FileList;
break;
case 'select':
(associatedElement as HTMLSelectElement).querySelectorAll('option').forEach((el) => {
(associatedElement as HTMLSelectElement).removeChild(el);
Expand Down
13 changes: 12 additions & 1 deletion packages/samples/react/src/scenarios/static-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const StaticForm: FC = () => {
<KolInputDate _name="date" _label="Date" />
<KolInputEmail _name="email" _label="Email" />
<KolInputFile _name="file" _label="File" />
<KolInputFile _name="file" _label="Files (multiple)" _multiple />
<KolInputNumber _name="number" _label="Number" />
<KolInputPassword _name="password" _label="Password" />
<KolInputRadio
Expand All @@ -67,7 +68,17 @@ export const StaticForm: FC = () => {
{ label: 'Option B', value: 'B' },
]}
/>
<KolTextarea _name="textarea" _label="Textarea" />
<KolSelect
_name="select"
_label="Select (multiple)"
_multiple
_options={[
{ label: 'Option A', value: 'A' },
{ label: 'Option B', value: 'B' },
]}
_rows={2}
/>
<KolTextarea _name="textarea" _label="Textarea" _rows={5} />
<div className="flex flex-wrap gap-4">
<KolButton _label="Submit" _type="submit" _variant="primary" />
<KolButton _label="Reset" _type="reset" />
Expand Down

0 comments on commit 62bbb5f

Please sign in to comment.