Skip to content

Commit c3613b3

Browse files
committed
feat(FileInput): accept prop support * 2
1 parent 738c169 commit c3613b3

File tree

2 files changed

+31
-33
lines changed

2 files changed

+31
-33
lines changed

src/components/forms/FileInput/FileInput.tsx

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
tasty,
2323
} from '../../../tasty';
2424
import { FieldBaseProps } from '../../../shared';
25+
import { useFieldProps, useFormProps } from '../Form/index';
2526
import { wrapWithField } from '../wrapper';
2627

2728
import type { AriaTextFieldProps } from '@react-types/textfield';
@@ -138,32 +139,43 @@ function extractContents(element, callback) {
138139

139140
function FileInput(props: CubeFileInputProps, ref) {
140141
props = useProviderProps(props);
142+
props = useFormProps(props);
143+
144+
const onLocalChange = useCallback(
145+
(event) => {
146+
const value = event.target.value;
147+
148+
if (type === 'file') {
149+
onChange?.(value);
150+
} else if (onChange) {
151+
extractContents(event.target, onChange);
152+
}
153+
154+
setValue(value);
155+
},
156+
[props.onChange],
157+
);
158+
159+
props = useFieldProps(
160+
{ ...props, onChange: onLocalChange },
161+
{
162+
defaultValidationTrigger: 'onChange',
163+
valuePropsMapper: ({ value, onChange }) => ({
164+
onChange,
165+
value: undefined,
166+
}),
167+
},
168+
);
141169

142170
let {
143171
id,
144172
name,
145173
qa,
146-
qaVal,
147174
onChange,
148175
placeholder,
149176
inputRef,
150-
label,
151-
extra,
152-
labelPosition,
153-
isRequired,
154-
necessityIndicator,
155-
necessityLabel,
156-
labelStyles,
157-
labelProps,
158177
isDisabled,
159-
validationState,
160-
message,
161-
description,
162-
requiredMark,
163-
tooltip,
164-
isHidden,
165178
inputStyles,
166-
labelSuffix,
167179
type = 'file',
168180
inputProps,
169181
accept,
@@ -180,21 +192,6 @@ function FileInput(props: CubeFileInputProps, ref) {
180192

181193
let styles = extractStyles(otherProps, CONTAINER_STYLES);
182194

183-
const onLocalChange = useCallback(
184-
(event) => {
185-
const value = event.target.value;
186-
187-
if (type === 'file') {
188-
onChange?.(value);
189-
} else if (onChange) {
190-
extractContents(event.target, onChange);
191-
}
192-
193-
setValue(value);
194-
},
195-
[onChange],
196-
);
197-
198195
// Expose imperative interface for ref
199196
useImperativeHandle(ref, () => ({
200197
...createFocusableRef(domRef, inputRef),
@@ -216,7 +213,7 @@ function FileInput(props: CubeFileInputProps, ref) {
216213
const fileInput = (
217214
<FileInputElement
218215
ref={domRef}
219-
qa={qa || 'FileInput'}
216+
qa="FileInput"
220217
styles={inputStyles}
221218
isDisabled={isDisabled}
222219
mods={{
@@ -237,7 +234,6 @@ function FileInput(props: CubeFileInputProps, ref) {
237234
data-element="Input"
238235
type="file"
239236
tabIndex={-1}
240-
onChange={onLocalChange}
241237
onDragEnter={() => {
242238
setDragHover(true);
243239
}}

src/components/forms/Form/ComplexForm.stories.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
TextInput,
2525
NumberInput,
2626
parseAbsoluteDate,
27+
FileInput,
2728
} from '../../../index';
2829
// import { NumberInput } from '../NumberInput/NumberInput';
2930
import { baseProps } from '../../../stories/lists/baseProps';
@@ -279,6 +280,7 @@ const Template: StoryFn<typeof Form> = (args) => {
279280
<PasswordInput label="Password field" />
280281
</Field>
281282
<DateInput name="date" label="Date field" />
283+
<FileInput name="fileContent" label="File field" />
282284
<Field
283285
name="select.one"
284286
label="Select field"

0 commit comments

Comments
 (0)