Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(InputFile): multipleでファイルを追加していけるように変更 #5281

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export const AccordionPanel: React.FC<Props & ElementProps> = ({
parentRef,
}}
>
{/* eslint-disable-next-line smarthr/a11y-delegate-element-has-role-presentation */}
<div {...props} className={styles} ref={parentRef} role="presentation" />
</AccordionPanelContext.Provider>
)
Expand Down
52 changes: 34 additions & 18 deletions packages/smarthr-ui/src/components/InputFile/InputFile.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use client'

import React, {
ComponentPropsWithRef,
ReactNode,
Expand Down Expand Up @@ -86,6 +87,7 @@ export const InputFile = forwardRef<HTMLInputElement, Props & ElementProps>(
hasFileList = true,
onChange,
disabled = false,
multiple,
error,
decorators,
...props
Expand Down Expand Up @@ -129,15 +131,30 @@ export const InputFile = forwardRef<HTMLInputElement, Props & ElementProps>(
[decorators],
)

const updateFiles = useCallback(
const updateInputValue = useCallback(
(newFiles: File[]) => {
if (onChange) {
onChange(newFiles)
if (!inputRef.current) {
return
}
const buff = new DataTransfer()
newFiles.forEach((file) => {
buff.items.add(file)
})

isUpdatingFilesDirectly.current = true
inputRef.current.files = buff.files
isUpdatingFilesDirectly.current = false
},
[inputRef],
)

const updateFiles = useCallback(
(newFiles: File[]) => {
onChange?.(newFiles)
updateInputValue(newFiles)
setFiles(newFiles)
},
[setFiles, onChange],
[setFiles, onChange, updateInputValue],
)

const handleChange = useCallback(
Expand All @@ -146,9 +163,16 @@ export const InputFile = forwardRef<HTMLInputElement, Props & ElementProps>(
return
}

updateFiles(Array.from(e.target.files ?? []))
const newFiles = Array.from(e.target.files ?? [])

if (multiple) {
// multipleの場合、すでに選択済みのファイルと結合する
updateFiles([...files, ...newFiles])
Copy link
Member

@AtsushiM AtsushiM Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この仕様は議論の余地がありそう。
Chromeなどのブラウザ実装では

  • 一度目に選んだファイルは、二度目に選ばれたファイル群と結合されない

つまりイメージとしては都度渡された配列を結合するのではなく、常に上書きされます。
個人的にはmultipleを実装するならブラウザデフォルトの動きに合わせたいです。
もしユースケースとして結合させたい場合があるなら、属性名はmultiple以外にしたいです(最終的にinput[file][multiple]が出力されるとしても、デフォルトではない挙動をするなら、コンポーネントとしての属性は別の名前
にしておいたほうが事故はすくなるなるかと...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほどです!ユースケース的にはこの挙動自体は以前定例で話した通り、他のプロダクトでもある認識です〜
props名はたしかに感あるので、 multiple & appendable のときにこの挙動をする、とかだとどうでしょう?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

↑良さそう
ただ実際問題として multiple だけで設定することがないような気がする...
雑ですが multiAppendable みたいな1属性で設定してもいいかもですね...

} else {
updateFiles(newFiles)
}
},
[isUpdatingFilesDirectly, updateFiles],
[files, isUpdatingFilesDirectly, updateFiles, multiple],
)

const handleDelete = useCallback(
Expand All @@ -161,26 +185,16 @@ export const InputFile = forwardRef<HTMLInputElement, Props & ElementProps>(
const newFiles = files.filter((_, i) => index !== i)

updateFiles(newFiles)

const buff = new DataTransfer()

newFiles.forEach((file) => {
buff.items.add(file)
})

isUpdatingFilesDirectly.current = true
inputRef.current.files = buff.files
isUpdatingFilesDirectly.current = false
},
[files, isUpdatingFilesDirectly, inputRef, updateFiles],
[files, inputRef, updateFiles],
)

return (
<Stack align="flex-start" className={wrapperStyle}>
{!disabled && hasFileList && files.length > 0 && (
<BaseColumn as="ul" padding={BASE_COLUMN_PADDING} className={fileListStyle}>
{files.map((file, index) => (
<li key={index} className={fileItemStyle}>
<li key={`${file.name}-${index}`} className={fileItemStyle}>
<span className="smarthr-ui-InputFile-fileName">{file.name}</span>
<Button
variant="text"
Expand All @@ -196,8 +210,10 @@ export const InputFile = forwardRef<HTMLInputElement, Props & ElementProps>(
</BaseColumn>
)}
<span className={inputWrapperStyle}>
{}
<input
{...props}
multiple
data-smarthr-ui-input="true"
type="file"
onChange={handleChange}
Expand Down