Skip to content

Commit 718735a

Browse files
committed
feat(FileInput): support field
1 parent 2fad5d6 commit 718735a

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

src/components/forms/FileInput/FileInput.stories.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,26 @@ LongFileNameOverflow.play = async ({ canvasElement }) => {
5757
await userEvent.upload(input, file);
5858
});
5959
};
60+
61+
export const ExtractText = Template.bind({});
62+
ExtractText.args = {
63+
type: 'text',
64+
inputStyles: {
65+
width: '300px',
66+
},
67+
inputProps: {
68+
title: 'Test input',
69+
},
70+
};
71+
72+
ExtractText.play = async ({ canvasElement }) => {
73+
const canvas = within(canvasElement);
74+
const input = await canvas.getByTitle('Test input');
75+
const file = new File(['test'], 'file-with-a-very-long-name.txt', {
76+
type: 'text/plain',
77+
});
78+
79+
await waitFor(async () => {
80+
await userEvent.upload(input, file);
81+
});
82+
};

src/components/forms/FileInput/FileInput.tsx

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
RefObject,
44
useCallback,
55
useImperativeHandle,
6+
useMemo,
67
useRef,
78
useState,
89
} from 'react';
@@ -137,32 +138,22 @@ function extractContents(element, callback) {
137138
}
138139
}
139140

141+
function extractFileNameFromValue(value?: string) {
142+
return typeof value === 'string'
143+
? (value as string).split('\\')?.pop()
144+
: undefined;
145+
}
146+
140147
function FileInput(props: CubeFileInputProps, ref) {
141148
props = useProviderProps(props);
142149
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-
159150
props = useFieldProps(
160-
{ ...props, onChange: onLocalChange },
151+
{ ...props },
161152
{
162153
defaultValidationTrigger: 'onChange',
163154
valuePropsMapper: ({ value, onChange }) => ({
164155
onChange,
165-
value,
156+
value: type === 'file' ? value : undefined,
166157
}),
167158
},
168159
);
@@ -171,6 +162,7 @@ function FileInput(props: CubeFileInputProps, ref) {
171162
id,
172163
name,
173164
qa,
165+
value,
174166
onChange,
175167
placeholder,
176168
inputRef,
@@ -182,14 +174,37 @@ function FileInput(props: CubeFileInputProps, ref) {
182174
...otherProps
183175
} = props;
184176

185-
const [value, setValue] = useState();
186177
const [dragHover, setDragHover] = useState(false);
178+
const defaultValue = useMemo(
179+
() => (type === 'file' ? value : undefined),
180+
[type, value],
181+
);
182+
const defaultFileName = useMemo(
183+
() => extractFileNameFromValue(defaultValue),
184+
[],
185+
);
186+
const [fileName, setFileName] = useState<string | undefined>(defaultFileName);
187187

188188
let domRef = useRef(null);
189189
let defaultInputRef = useRef(null);
190190

191191
inputRef = inputRef || defaultInputRef;
192192

193+
const onLocalChange = useCallback(
194+
(event: any) => {
195+
const value = event.target.value;
196+
197+
setFileName(extractFileNameFromValue(value));
198+
199+
if (type === 'file') {
200+
onChange?.(value);
201+
} else {
202+
extractContents(event.target, onChange);
203+
}
204+
},
205+
[onChange],
206+
);
207+
193208
let styles = extractStyles(otherProps, CONTAINER_STYLES);
194209

195210
// Expose imperative interface for ref
@@ -205,11 +220,6 @@ function FileInput(props: CubeFileInputProps, ref) {
205220
},
206221
}));
207222

208-
const fileName =
209-
typeof value === 'string'
210-
? (value as string).split('\\')?.pop()
211-
: undefined;
212-
213223
const fileInput = (
214224
<FileInputElement
215225
ref={domRef}
@@ -235,6 +245,7 @@ function FileInput(props: CubeFileInputProps, ref) {
235245
type="file"
236246
multiple={false}
237247
tabIndex={-1}
248+
onChange={onLocalChange}
238249
onDragEnter={() => {
239250
setDragHover(true);
240251
}}

0 commit comments

Comments
 (0)