Skip to content

Conversation

@hikaru-n-cpu
Copy link
Contributor

@hikaru-n-cpu hikaru-n-cpu commented Dec 10, 2025

TASK

https://redmine.weseek.co.jp/issues/175223
https://redmine.weseek.co.jp/issues/176103

行ったこと

ファイル選択がされていない状態で更新ボタンを押そうとしたときにはボタンがdisabledになるようにしました。
ファイル選択時にファイル名が残ってしまうバグを直しました。

具体例

  1. ファイル選択ボタンを押したのに、選択されていないとき、
  2. モーダルでキャンセルボタンを押したとき
  3. ロゴを削除ボタンを押したときにボタンがdisabledになるようにしました。
  4. キャンセルボタンを押した時と、ロゴ削除ボタンを押した時にファイル名が残ってしまうバグを修正しました。

Copy link
Contributor

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

CodeQL found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

@hikaru-n-cpu hikaru-n-cpu changed the base branch from master to dev/7.4.x December 10, 2025 06:36
@hikaru-n-cpu hikaru-n-cpu changed the title Fix/disable logo update without file fix: Disable logo update button when no file is selected Dec 10, 2025
const [isImageCropModalShow, setIsImageCropModalShow] = useState<boolean>(false);
const [isDefaultLogoSelected, setIsDefaultLogoSelected] = useState<boolean>(isDefaultLogo ?? true);
const [retrieveError, setRetrieveError] = useState<any>();
const [isFileSelected, setIsFileSelected] = useState<boolean>(false);
Copy link
Member

Choose a reason for hiding this comment

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

ファイルが選択されているか否かは uploadLogoSrc を見れば良さそう

@hikaru-n-cpu
Copy link
Contributor Author

hikaru-n-cpu commented Dec 24, 2025

目的

モーダル内でキャンセルボタンを押した場合に、<input type="file >に残ったファイル名を初期化したい

やりたい動作

モーダルでキャンセルボタンを押した後には、ファイル名を消す動作が正しい

このように書いた理由

inputタグはjavascript側で任意に選択したファイル名を消す機能がなく、もし消したいなら新しいinputタグを再生成させなければなりません。なので、inputタグにkeyを設定し、resetFileInputで呼び出した場合に毎回違うkeyを生成させています。異なるkeyを受け取ったReactはinputタグを再構築し、空のinputタグを生成します。このようにしてファイル名を初期化するようにしました。setFileInputKey(Date.now().toString());とすることで呼び出した瞬間のミリ時間を文字列としてkeyに代入することにより呼び出したら毎回必ず違うkeyが生成されることにより、ファイル名の初期化という目的を達成しています。

参考文献:

<input type="file" onChange={onSelectFile} name="brandLogo" accept="image/*" />
<input
type="file"
key={fileInputKey}
Copy link
Contributor Author

@hikaru-n-cpu hikaru-n-cpu Dec 24, 2025

Choose a reason for hiding this comment

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

この部分でinputタグのkeyに呼び出した現在のミリ秒の文字列を代入しています。この処理により毎回resetFileInput()が呼び出されればinputタグが再構築されることによって空のinputタグが生成され、モーダルのキャンセルボタンを押したとしてもすでに選んでしまったfile名が残らないようにしました。

onModalClose={() => setIsImageCropModalShow(false)}
onModalClose={() => {
if (!isImageCropped) {
resetFileInput();
Copy link
Contributor Author

@hikaru-n-cpu hikaru-n-cpu Dec 24, 2025

Choose a reason for hiding this comment

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

この部分はモーダルでキャンセルボタンを押した時点で「切り抜きボタン(完了ボタン)」が押されていない場合にはファイル名を初期化するように実装してあります。
スクリーンショット 2025-12-24 154802

Copy link
Member

Choose a reason for hiding this comment

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

jsx の部分にロジックがあると見にくいので closeImageCropModalHandler みたいなメソッドを作ってそれを渡すようにしてください

}, [t, isDefaultLogoSelected]);

const resetFileInput = useCallback(() => {
setFileInputKey(Date.now().toString());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ここで呼び出した瞬間のミリ秒時間を引数として入力することによって呼び出したら必ず異なるkeyがinput タグに入力されることになります。

Copy link
Member

Choose a reason for hiding this comment

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

key をリセットするやり方がちょっとトリッキーなので useRef 使って実装してみてください。

const fileInputRef = useRef<HTMLInputElement | null>(null);

const clearFileInput = useCallback(() => {
  if (fileInputRef.current) {
    fileInputRef.current.value = '';
  }
}, []);

setUploadLogoSrc(null);
setIsImageCropped(false);
setIsImageCropModalShow(false);
resetFileInput();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ロゴを削除するというボタンが押された場合にファイル名を初期化するように実装してあります。
スクリーンショット 2025-12-24 154624

}, [t, isDefaultLogoSelected]);

const resetFileInput = useCallback(() => {
setFileInputKey(Date.now().toString());
Copy link
Member

Choose a reason for hiding this comment

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

key をリセットするやり方がちょっとトリッキーなので useRef 使って実装してみてください。

const fileInputRef = useRef<HTMLInputElement | null>(null);

const clearFileInput = useCallback(() => {
  if (fileInputRef.current) {
    fileInputRef.current.value = '';
  }
}, []);

onModalClose={() => setIsImageCropModalShow(false)}
onModalClose={() => {
if (!isImageCropped) {
resetFileInput();
Copy link
Member

Choose a reason for hiding this comment

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

jsx の部分にロジックがあると見にくいので closeImageCropModalHandler みたいなメソッドを作ってそれを渡すようにしてください

<AdminUpdateButtonRow
onClick={onClickSubmit}
disabled={retrieveError != null
|| (!isDefaultLogoSelected && uploadLogoSrc == null && !isCustomizedLogoUploaded)}
Copy link
Member

Choose a reason for hiding this comment

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

isUpdateButtonDisabled みたいな変数を作ってそれを渡すようにしてください

src={uploadLogoSrc}
onModalClose={() => setIsImageCropModalShow(false)}
onModalClose={() => {
if (!isImageCropped) {
Copy link
Member

Choose a reason for hiding this comment

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

onImageProcessCompleted というメソッドが ImageCropModal 側に提供されているみたいですが、ためしてみました?

state が増えると実装が複雑になるので増やすのは慎重にしたいです

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants