滑塊 組件_組件制作:如何使用鏈接的輸入創建滑塊

滑塊 組件

by Robin Sandborg

羅賓·桑德伯格(Robin Sandborg)

組件制作:如何使用鏈接的輸入創建滑塊 (Component crafting: how to create a slider with a linked input)

Here at Stacc, we’re huge fans of React and the render-props pattern. When it came time to create a slider input, we turned to one of our favorite resources — Jared Palmers awesome-react-render-props. Here we found react-compound-slider.

在Stacc ,我們是React和render-props模式的忠實擁護者。 當需要創建滑塊輸入時,我們轉向了我們最喜歡的資源之一-Jared Palmers awesome-react-render-props 。 在這里,我們找到了react-compound-slider 。

Our challenge was to combine the slider with a linked input. The user could choose whether to input their data through the keyboard input or the slider.

我們的挑戰是將滑塊與鏈接的輸入結合起來。 用戶可以選擇通過鍵盤輸入還是滑塊輸入數據。

The slider and input were like your typical siblings, constantly bickering. When the input requested a value outside the domain of the slider or one that didn’t fall exactly on one of the sliders steps, the stubborn slider not only refused to listen to the input — it would force the input to change its value. The result was a frustrating user experience.

滑塊和輸入就像您的同級兄弟一樣,不斷吵架。 當輸入請求的值超出了滑塊的范圍,或者一個值未完全落在滑塊的其中一個臺階上時,頑固的滑塊不僅拒絕監聽輸入,還會強制輸入更改其值。 結果是令人沮喪的用戶體驗。

I’ve searched but didn't find someone who’d solved this for me. So, in the spirit of sharing, here’s my solution. If you have any ideas or suggestions about how it could be even better, please share! I am, after all, more designer than developer.

我進行了搜索,但沒有找到為我解決此問題的人。 因此,本著共享的精神,這是我的解決方案。 如果您有任何更好的建議或想法,請分享! 畢竟,我是設計師,而不是開發商。

目標 (The goal)

Looks pretty simple, right? So let’s think about what we need to do here.

看起來很簡單,對吧? 因此,讓我們考慮一下我們在這里需要做什么。

  1. Put the shared value where both the slider and the input have access to it. In this case, we’ll make a component that wraps them both, and put the values there.

    將共享值放在滑塊和輸入均可訪問的位置。 在這種情況下,我們將制作一個將它們都包裝在一起的組件,并將值放在那里。
  2. Manage the values sent to both the input and the slider when something changes in either of them.

    當它們中的任何一個發生更改時,管理發送到輸入和滑塊的值。
  3. Avoid the strict rules of the slider’s domain (min and max) and steps from interfering with the users' ability to type a value into the input.

    避免嚴格限制滑塊域(最小和最大)的規則和步驟,以免干擾用戶在輸入中鍵入值的能力。

We’ll get back to the wrapping component later. First, let’s get our hands dirty with implementing the slider. Explanation below the example.

稍后我們將返回包裝組件。 首先,讓我們動手實施滑塊。 示例下方的說明。

Here I’ve implemented getDerivedStateFromProps. The slider needs to update its internal state from the values supplied from the slider’s props. I’ve also added some props for onUpdate, onChange and onSlideStart. We can handle these events in our wrapper component. Except for these details, this is pretty close to the code used in the react-compound-slider documentation.

在這里,我實現了getDerivedStateFromProps。 滑塊需要根據滑塊道具提供的值更新其內部狀態。 我還為onUpdate,onChange和onSlideStart添加了一些道具。 我們可以在包裝器組件中處理這些事件。 除了這些細節之外,這非常接近react-compound-slider文檔中使用的代碼。

The part I struggled with was handling the linking of the input and the slider. When typing, the value often goes outside of the slider’s permitted min and max values. There is no guarantee the user would type in a value that exactly matches any of the allowed steps in the slider.

我苦苦掙扎的部分是處理輸入和滑塊的鏈接。 鍵入時,該值通常超出滑塊允許的最小值和最大值。 不能保證用戶鍵入的值與滑塊中允許的任何步驟完全匹配。

If we didn’t handle these cases, the user would never be allowed to empty the input. If she typed a value outside any of the steps, it would just set the value to the closest step. Basically, any change in our input would result in the slider moving to where it thinks it should be, and thus updating our state with its value, overriding the value the user just typed.

如果我們不處理這些情況,將永遠不允許用戶清空輸入。 如果她在任何步驟之外都鍵入了一個值,則只會將該值設置為最接近的步驟。 基本上,輸入的任何更改都將導致滑塊移動到它認為應該的位置,從而用其值更新狀態,從而覆蓋用戶剛剛鍵入的值。

In order to handle these situations, I needed some logic. The best solution I could come up with was this:

為了處理這些情況,我需要一些邏輯。 我能想到的最好的解決方案是:

  1. When the input has focus, set the step prop for the slider to be 1 so that it can be set to whatever the user types

    當輸入具有焦點時,將滑塊的步進屬性設置為1,以便可以將其設置為用戶鍵入的任何內容
  2. If the input’s value changes AND the new value lies in the allowed range, set the slider’s value. Otherwise, do nothing.

    如果輸入值更改并且新值在允許的范圍內,請設置滑塊的值。 否則,什么都不做。
  3. When the user starts dragging the slider, set the step prop to what it’s supposed to be again and update the value of the inputs.

    當用戶開始拖動滑塊時,將step prop設置為它應該的值,然后更新輸入的值。

You can see the entire implementation with more comments on CodeSandbox.

您可以在CodeSandbox上看到更多注釋的完整實現。

Thank you for reading!

感謝您的閱讀!

翻譯自: https://www.freecodecamp.org/news/component-crafting-how-to-create-a-slider-with-a-linked-input-600d3438a050/

滑塊 組件

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392180.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392180.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392180.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

配置靜態IPV6 NAT-PT

一.概述: IPV6 NAT-PT( Network Address Translation - Port Translation)應用與ipv4和ipv6網絡互訪的情況,根據參考鏈接配置時出現一些問題,所以記錄下來。參考鏈接:http://www.cisco.com/en/US/tech/tk648/tk361/technologies_c…

linux 線程與進程 pid,linux下線程所屬進程號問題

這一段看《unix環境高級編程》,一個關于線程的小例子。#include#include#includepthread_t ntid;void printids(const char *s){pid_t pid;pthread_t tid;pidgetpid();tidpthread_self();printf("%s pid %u tid %u (0x%x)n",s,(unsigned int)pid,(unsigne…

python3虛擬環境中解決 ModuleNotFoundError: No module named '_ssl'

前提是已經安裝了openssl 問題 當我在python3虛擬環境中導入ssl模塊時報錯,報錯如下: (py3) [rootlocalhost Python-3.6.3]# python3 Python 3.6.3 (default, Nov 19 2018, 14:18:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux Type "help…

python 使用c模塊_您可能沒有使用(但應該使用)的很棒的Python模塊

python 使用c模塊by Adam Goldschmidt亞當戈德施密特(Adam Goldschmidt) 您可能沒有使用(但應該使用)的很棒的Python模塊 (Awesome Python modules you probably aren’t using (but should be)) Python is a beautiful language, and it contains many built-in modules that…

分布與并行計算—生產者消費者模型實現(Java)

在實際的軟件開發過程中,經常會碰到如下場景:某個模塊負責產生數據,這些數據由另一個模塊來負責處理(此處的模塊是廣義的,可以是類、函數、線程、進程等)。產生數據的模塊,就形象地稱為生產者&a…

通過Xshell登錄遠程服務器實時查看log日志

主要想總結以下幾點: 1.如何使用生成密鑰的方式來登錄Xshell連接遠端服務器 2.在遠程服務器上如何上傳和下載文件(下載log文件到本地) 3.如何實時查看log,提取錯誤信息 一. 使用生成密鑰的方式來登錄Xshell連接遠端服務器 ssh登錄…

如何將Jupyter Notebook連接到遠程Spark集群并每天運行Spark作業?

As a data scientist, you are developing notebooks that process large data that does not fit in your laptop using Spark. What would you do? This is not a trivial problem.作為數據科學家,您正在開發使用Spark處理筆記本電腦無法容納的大數據的筆記本電腦…

是銀彈嗎?業務基線方法論

Fred.Brooks在1987年就提出:沒有銀彈。沒有任何一項技術或方法可以能讓軟件工程的生產力在十年內提高十倍。 我無意挑戰這個理論,只想討論一個方案,一個可能大幅提高業務系統開發效率的方案。 方案描述 我管這個方案叫做“由基線擴展…

linux core無權限,linux – 為什么編輯core_pattern受限制?

當我試圖為故意崩潰的程序生成核心文件時,最初的核心文件生成似乎被abrt-ccpp阻礙了.所以我嘗試用vim手動編輯/ proc / sys / kernel / core_pattern:> sudo vim /proc/sys/kernel/core_pattern當我試圖保存文件時,vim報告了這個錯誤:"/proc/sys…

nsa構架_我如何使用NSA的Ghidra解決了一個簡單的CrackMe挑戰

nsa構架by Denis Nu?iu丹尼斯努尤(Denis Nu?iu) 我如何使用NSA的Ghidra解決了一個簡單的CrackMe挑戰 (How I solved a simple CrackMe challenge with the NSA’s Ghidra) Hello!你好! I’ve been playing recently a bit with Ghidra, which is a reverse engi…

分布與并行計算—生產者消費者模型隊列(Java)

在生產者-消費者模型中&#xff0c;在原有代碼基礎上&#xff0c;把隊列獨立為1個類實現&#xff0c;通過公布接口&#xff0c;由生產者和消費者調用。 public class Consumer implements Runnable {int n;CountDownLatch countDownLatch;public Consumer(BlockingQueue<In…

python 日志內容提取

問題&#xff1a;如下&#xff0c;一個很大的日志文件&#xff0c;提取 start: 到 end: 標志中間的內容 日志文件a.log xxxxx yyyyy start: start: hahahaha end: start: hahahahha end: ccccccc kkkkkkk cdcdcdcd start: hahahaha end: code import reisfindFalse with open(&…

同一服務器部署多個tomcat時的端口號修改詳情

2019獨角獸企業重金招聘Python工程師標準>>> 同一服務器部署多個tomcat時&#xff0c;存在端口號沖突的問題&#xff0c;所以需要修改tomcat配置文件server.xml&#xff0c;以tomcat7為例。 首先了解下tomcat的幾個主要端口&#xff1a;<Connector port"808…

linux優盤驅動目錄,Linux U盤加載陣列卡驅動步驟(.dd或img).doc

Linux U盤加載陣列卡驅動步驟(.dd或img)如果沒有Linux的機器,可以使用安裝光盤的Linux環境&#xff1a;將?U?盤完全慢速格式化&#xff0c;將驅動拷貝到U盤&#xff0c;將U盤插在服務器上&#xff0c;用Linux安裝光盤第一張啟動到圖形安裝界面&#xff0c;按Ctrl&#xff0b;…

第一章-從雙向鏈表學習設計

鏈表學習鏈表是一種動態的數據結構使用節點作為鏈表的基本單位存儲在節點包括數據元素和節點指針一個完整的數據鏈表應包括轉載于:https://www.cnblogs.com/cjxltd/p/7125747.html

twitter 數據集處理_Twitter數據清理和數據科學預處理

twitter 數據集處理In the past decade, new forms of communication, such as microblogging and text messaging have emerged and become ubiquitous. While there is no limit to the range of information conveyed by tweets and texts, often these short messages are …

ios 動態化視圖_如何在iOS應用中使高度收集視圖動態化

ios 動態化視圖by Payal Gupta通過Payal Gupta 如何在iOS應用中使集合視圖的高度動態化 (How to make height of collection views dynamic in your iOS apps) 充滿活力&#xff0c;就像生活一樣… (Be dynamic, just like life…) Table views and collection views have alw…

新開通博客

新開通博客&#xff0c;希望兄弟們積極更新。 轉載于:https://www.cnblogs.com/ydhliphonedev/archive/2011/07/28/2119720.html

思維導圖分析http之http協議版本

1.結構總覽 在http協議這一章&#xff0c;我將先后介紹上圖六個部分&#xff0c;本文先介紹http的協議版本。 2.http協議版本 http協議的歷史并不長&#xff0c;從1991的0.9版本到現在(2017)僅僅才20多年&#xff0c;算算下來,http還是正處青年&#xff0c;正是大好發展的好時光…

分布與并行計算—生產者消費者模型RabbitMQ(Java)

連接工具 public class ConnectionUtil {public static final String QUEUE_NAME"firstQueue";private static final String RABBIT_HOST "11";private static final String RABBIT_USERNAME "";private static final String RABBIT_PASSWORD…