Skip to content

Commit c0dde2d

Browse files
fix(mosaic): associate the required TagInput text input with an external form (#10686)
1 parent ca14c09 commit c0dde2d

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

‎packages/mosaic/src/components/tag-input/tag-input.test.tsx‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,54 @@ describe('Mosaic TagInput', () => {
184184
expect(new FormData(form).getAll('emails')).toEqual(['preston@clerk.dev']);
185185
});
186186

187+
describe('outside the form named by form', () => {
188+
function submitInvite(props: Pick<React.ComponentProps<typeof TagInput>, 'required' | 'defaultValue'>) {
189+
const onSubmit = vi.fn((event: React.FormEvent<HTMLFormElement>) => {
190+
event.preventDefault();
191+
return new FormData(event.currentTarget).getAll('emails');
192+
});
193+
render(
194+
<>
195+
<form
196+
id='invite'
197+
data-testid='form'
198+
onSubmit={onSubmit}
199+
/>
200+
<TagInput
201+
aria-label='Email'
202+
name='emails'
203+
form='invite'
204+
{...props}
205+
/>
206+
</>,
207+
);
208+
const form = screen.getByTestId('form');
209+
if (!(form instanceof HTMLFormElement)) {
210+
throw new Error('expected a form');
211+
}
212+
form.requestSubmit();
213+
return onSubmit;
214+
}
215+
216+
it('blocks the submit while required and empty', () => {
217+
const onSubmit = submitInvite({ required: true });
218+
219+
expect(onSubmit).not.toHaveBeenCalled();
220+
});
221+
222+
it('submits a required value once it has a tag', () => {
223+
const onSubmit = submitInvite({ required: true, defaultValue: ['preston@clerk.dev'] });
224+
225+
expect(onSubmit).toHaveReturnedWith(['preston@clerk.dev']);
226+
});
227+
228+
it('submits while optional and empty', () => {
229+
const onSubmit = submitInvite({});
230+
231+
expect(onSubmit).toHaveReturnedWith([]);
232+
});
233+
});
234+
187235
it('forwards the ref and native input props to the text input', () => {
188236
const ref = React.createRef<HTMLInputElement>();
189237
render(

‎packages/mosaic/src/components/tag-input/tag-input.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ export const TagInput = React.forwardRef<HTMLInputElement, TagInputProps>(functi
168168
ref={ref}
169169
itemProps={itemProps}
170170
id={fieldProps?.id ?? id}
171+
form={form}
171172
required={required}
172173
aria-invalid={ariaInvalid}
173174
aria-labelledby={fieldProps?.['aria-labelledby'] ?? ariaLabelledBy}

0 commit comments

Comments
 (0)